feature/clean-public-urls #1

Merged
admin merged 6 commits from feature/clean-public-urls into main 2025-11-21 12:14:34 +00:00
Showing only changes of commit d5b78d7449 - Show all commits

View File

@@ -3,6 +3,7 @@ package services
import ( import (
"context" "context"
"fmt" "fmt"
"net/url"
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
@@ -17,10 +18,12 @@ import (
var ( var (
FirebaseApp *firebase.App FirebaseApp *firebase.App
FirebaseClient *storage.Client FirebaseClient *storage.Client
FirebaseBucket string // Store bucket name for URL construction
) )
// InitFirebase initializes Firebase Admin SDK and Storage client // InitFirebase initializes Firebase Admin SDK and Storage client
func InitFirebase(cfg *config.Config) (*storage.Client, error) { func InitFirebase(cfg *config.Config) (*storage.Client, error) {
// Note: Returns Firebase Storage client, not GCS client
ctx := context.Background() ctx := context.Background()
// Determine credentials file path // Determine credentials file path
@@ -72,6 +75,7 @@ func InitFirebase(cfg *config.Config) (*storage.Client, error) {
FirebaseApp = app FirebaseApp = app
FirebaseClient = client FirebaseClient = client
FirebaseBucket = cfg.FirebaseStorageBucket
return client, nil return client, nil
} }
@@ -128,14 +132,23 @@ func UploadImage(ctx context.Context, imageData []byte, folderPath string, filen
return "", fmt.Errorf("failed to close writer: %w", err) return "", fmt.Errorf("failed to close writer: %w", err)
} }
// Get public URL from Firebase // Make the object publicly accessible
attrs, err := obj.Attrs(ctx) // Firebase Storage v4 uses string literals for ACL
if err != nil { acl := obj.ACL()
return "", fmt.Errorf("failed to get object attributes: %w", err) if err := acl.Set(ctx, "allUsers", "READER"); err != nil {
// Log warning but don't fail - file might still be accessible
// In some cases, bucket-level policies might already make it public
fmt.Printf("Warning: Failed to set public ACL for %s: %v\n", objectPath, err)
} }
// Use Firebase's original download link (MediaLink) // Construct clean GCS public URL
publicURL := attrs.MediaLink // Format: https://storage.googleapis.com/<bucket>/<path>
encodedPath := url.PathEscape(objectPath)
publicURL := fmt.Sprintf(
"https://storage.googleapis.com/%s/%s",
FirebaseBucket,
encodedPath,
)
return publicURL, nil return publicURL, nil
} }