Skip to content

Commit b52db28

Browse files
author
F0xhopper
committed
Fix the supabase image storage issue
1 parent 4115639 commit b52db28

5 files changed

Lines changed: 55 additions & 14 deletions

File tree

backend/cmd/bot/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func corsMiddleware(next http.Handler) http.Handler {
3333

3434
func main() {
3535
cfg, err := config.LoadConfig()
36+
3637
if err != nil {
3738
log.Fatalf("Error loading config: %v", err)
3839
}

backend/internal/content/service.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import (
77
"io"
88
"log"
99
"mime/multipart"
10+
"strings"
1011
"time"
1112

1213
"novissima/internal/logging"
1314

1415
"github.com/google/uuid"
16+
storage_go "github.com/supabase-community/storage-go"
1517
"github.com/supabase-community/supabase-go"
1618
)
1719

@@ -57,13 +59,39 @@ func (s *Service) AddContent(contentEnglish string, contentLatin string, file mu
5759
if file != nil && header != nil {
5860
defer file.Close()
5961

60-
filename := fmt.Sprintf("%s/%s", theme, header.Filename)
62+
fileExt := ""
63+
if idx := strings.LastIndex(header.Filename, "."); idx != -1 {
64+
fileExt = header.Filename[idx:]
65+
}
66+
filename := fmt.Sprintf("%s/%s%s", theme, uuid.New().String(), fileExt)
67+
6168
fileBytes, err := io.ReadAll(file)
6269
if err != nil {
6370
return Content{}, fmt.Errorf("failed to read file: %w", err)
64-
}
71+
}
6572

66-
_, err = s.dbClient.Storage.UploadFile(s.bucketName, filename, bytes.NewReader(fileBytes))
73+
74+
fileReader := bytes.NewReader(fileBytes)
75+
76+
77+
contentType := header.Header.Get("Content-Type")
78+
if contentType == "" {
79+
switch fileExt {
80+
case ".jpg", ".jpeg":
81+
contentType = "image/jpeg"
82+
case ".png":
83+
contentType = "image/png"
84+
case ".gif":
85+
contentType = "image/gif"
86+
default:
87+
contentType = "application/octet-stream"
88+
}
89+
}
90+
91+
fileOptions := storage_go.FileOptions{
92+
ContentType: &contentType,
93+
}
94+
_, err = s.dbClient.Storage.UploadFile(s.bucketName, filename, fileReader, fileOptions)
6795
if err != nil {
6896
return Content{}, fmt.Errorf("failed to upload to storage: %w", err)
6997
}
@@ -94,7 +122,7 @@ func (s *Service) AddContent(contentEnglish string, contentLatin string, file mu
94122
}
95123

96124
func (s *Service) GetDailyContent() (*Content, error) {
97-
themes := []string{"heaven", "hell", "judgment", "death"}
125+
themes := []string{ "death"}
98126
startDate := time.Date(2025, 7, 1, 0, 0, 0, 0, time.UTC)
99127

100128
now := time.Now()
@@ -103,13 +131,11 @@ func (s *Service) GetDailyContent() (*Content, error) {
103131

104132
currentTheme := themes[cycleDay]
105133

106-
thirtyDaysAgo := now.AddDate(0, 0, -30)
134+
// thirtyDaysAgo := now.AddDate(0, 0, -30)
107135

108136
content, _, err := s.dbClient.From("content").
109137
Select("*", "", false).
110138
Eq("theme", currentTheme).
111-
Or("last_sent.is.null", "").
112-
Or("last_sent.lt."+thirtyDaysAgo.Format("2006-01-02"), "").
113139
Limit(1, "").
114140
Execute()
115141
if err != nil {

backend/internal/scheduler/scheduler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func NewService(contentService *content.Service, twilioService *twilio.Service,
2626
func (s *Service) Start() {
2727
c := cron.New()
2828

29-
c.AddFunc("0 8 * * *", func() {
29+
c.AddFunc("*/2 * * * *", func() {
3030
log.Println("Starting daily content distribution...")
3131

3232
content, err := s.contentService.GetDailyContent()
@@ -37,7 +37,7 @@ func (s *Service) Start() {
3737

3838
err = s.twilioService.SendMessageToAllUsers(content)
3939
if err != nil {
40-
log.Printf("Failed to send daily content: %v", err)
40+
log.Printf("Failed to send daidly content: %v", err)
4141
return
4242
}
4343
log.Println("Daily content sent to all users successfully")

backend/internal/twilio/service.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"crypto/hmac"
55
"crypto/sha1"
66
"encoding/base64"
7+
"encoding/json"
78
"fmt"
89
"log"
910
"net/http"
@@ -39,10 +40,23 @@ func (s *Service) SendMessageToUser(phoneNumber, message string, mediaUrl string
3940
params := &twilioApi.CreateMessageParams{}
4041
params.SetTo("whatsapp:" + phoneNumber)
4142
params.SetFrom("whatsapp:" + s.client.phoneNumber)
42-
params.SetBody(message)
43-
params.SetMediaUrl([]string{mediaUrl})
43+
params.SetContentSid("HXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
44+
45+
contentVariables := map[string]string{
46+
"1": message,
47+
"2": mediaUrl,
48+
}
49+
50+
variablesJSON, _ := json.Marshal(contentVariables)
51+
params.SetContentVariables(string(variablesJSON))
52+
53+
twilioMessage, err := s.client.twilioClient.Api.CreateMessage(params)
54+
if err != nil {
55+
return err
56+
}
57+
jsonMsg, _ := json.MarshalIndent(twilioMessage, "", " ")
58+
log.Printf("Twilio message: %s", jsonMsg)
4459

45-
_, err := s.client.twilioClient.Api.CreateMessage(params)
4660
return err
4761
}
4862

frontend/src/app/upload/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ export default function UploadPage() {
8888
};
8989

9090
return (
91-
<div className="min-h-screen p-8">
92-
<div className="max-w-2xl mx-auto">
91+
<div className="min-h-screen flex justify-center items-center flex-col">
92+
<div className="max-w-2xl mx-auto w-full">
9393
<Card>
9494
<CardHeader>
9595
<CardTitle>Upload Content</CardTitle>

0 commit comments

Comments
 (0)