-
Notifications
You must be signed in to change notification settings - Fork 0
/
tg.go
122 lines (102 loc) · 2.88 KB
/
tg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"cloud.google.com/go/storage"
"github.com/joho/godotenv"
"google.golang.org/api/option"
)
// models
type FileInfo struct {
ID string
Name string
}
type RequestPayload struct {
Files map[string]string `json:"files"`
}
type ResponsePayload struct {
URLs map[string]string `json:"urls"`
}
// variables
var (
bucketName string
googleAccessID string
privateKey string
client *storage.Client
)
func init() {
// Load environment variables
err := godotenv.Load()
if err != nil {
log.Fatalf("Error loading .env file")
}
// Read environment variables
credentialsFilePath := os.Getenv("GOOGLE_CREDENTIALS_FILE_PATH")
bucketName = os.Getenv("BUCKET_NAME")
googleAccessID = os.Getenv("GOOGLE_ACCESS_ID")
privateKey = os.Getenv("PRIVATE_KEY")
// Initialize GCS Client
ctx := context.Background()
client, err = storage.NewClient(ctx, option.WithCredentialsFile(credentialsFilePath))
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
}
func GenerateSignedURLs(w http.ResponseWriter, r *http.Request) {
var requestPayload RequestPayload
if err := json.NewDecoder(r.Body).Decode(&requestPayload); err != nil {
http.Error(w, "Invalid request payload", http.StatusBadRequest)
return
}
files := make([]FileInfo, 0, len(requestPayload.Files))
for id, name := range requestPayload.Files {
files = append(files, FileInfo{ID: id, Name: name})
}
ctx := context.Background()
results := make(chan FileInfo, len(files))
done := make(chan struct{})
for _, file := range files {
go func(file FileInfo) {
url, err := getSignedUrl(ctx, client, bucketName, googleAccessID, privateKey, file.Name)
if err != nil {
log.Printf("Failed to generate signed URL for %s: %v", file.Name, err)
results <- FileInfo{ID: file.ID, Name: fmt.Sprintf("Error: %v", err)}
return
}
results <- FileInfo{ID: file.ID, Name: url}
}(file)
}
go func() {
for range files {
<-results
}
close(done)
}()
responsePayload := ResponsePayload{URLs: make(map[string]string)}
for result := range results {
responsePayload.URLs[result.ID] = result.Name
}
<-done
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(responsePayload); err != nil {
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
}
}
func getSignedUrl(ctx context.Context, client *storage.Client, bucketName, googleAccessID, privateKey, objectName string) (string, error) {
url, err := storage.SignedURL(bucketName, objectName, &storage.SignedURLOptions{
GoogleAccessID: googleAccessID,
PrivateKey: []byte(strings.ReplaceAll(privateKey, "\\n", "\n")),
Method: "GET",
Expires: time.Now().Add(15 * time.Minute),
})
if err != nil {
return "", fmt.Errorf("unable to generate signed URL: %v", err)
}
return url, nil
}