package fileurls import ( "context" "fmt" "net/url" "strings" "time" ) const defaultPresignedExpiry = time.Hour // URLProvider generates presigned download URLs for MinIO object keys. type URLProvider interface { GetURL(ctx context.Context, objectKey string, expiry time.Duration) (string, error) BucketName() string } // Resolver converts stored file references to presigned download URLs. type Resolver struct { provider URLProvider } func New(provider URLProvider) *Resolver { return &Resolver{provider: provider} } // Resolve converts minio:// references, object keys, and stale presigned URLs to fresh presigned URLs. // Static paths and unrelated HTTPS URLs are returned unchanged. func (r *Resolver) Resolve(ctx context.Context, reference string) string { if r == nil || r.provider == nil { return reference } ref := strings.TrimSpace(reference) if ref == "" || strings.HasPrefix(ref, "/static/") { return reference } if !NeedsResolution(ref) { return reference } key, err := ExtractObjectKey(ref, r.provider.BucketName()) if err != nil { return reference } freshURL, err := r.provider.GetURL(ctx, key, defaultPresignedExpiry) if err != nil { return reference } return freshURL } // NeedsResolution reports whether a stored value should be presigned on outbound API responses. func NeedsResolution(reference string) bool { ref := strings.TrimSpace(reference) if ref == "" || strings.HasPrefix(ref, "/static/") { return false } if strings.HasPrefix(ref, "minio://") { return true } if strings.HasPrefix(ref, "http://") || strings.HasPrefix(ref, "https://") { return isPresignedS3URL(ref) } return isKnownObjectKey(ref) } // ExtractObjectKey parses minio:// URIs, presigned URLs, and raw object keys. func ExtractObjectKey(reference, bucketName string) (string, error) { ref := strings.TrimSpace(reference) if ref == "" { return "", fmt.Errorf("reference is required") } if strings.HasPrefix(ref, "minio://") { key := strings.TrimPrefix(ref, "minio://") if key == "" { return "", fmt.Errorf("invalid minio reference") } return key, nil } if strings.HasPrefix(ref, "http://") || strings.HasPrefix(ref, "https://") { u, err := url.Parse(ref) if err != nil || u.Host == "" { return "", fmt.Errorf("invalid file URL") } objectPath := strings.TrimPrefix(u.Path, "/") if objectPath == "" { return "", fmt.Errorf("invalid file URL") } bucket := strings.TrimSpace(bucketName) if bucket != "" { prefix := bucket + "/" if strings.HasPrefix(objectPath, prefix) { objectPath = strings.TrimPrefix(objectPath, prefix) } } if objectPath == "" { return "", fmt.Errorf("invalid file URL") } return objectPath, nil } if isKnownObjectKey(ref) { return ref, nil } if !strings.Contains(ref, "://") && !strings.HasPrefix(ref, "/") { return ref, nil } return "", fmt.Errorf("unsupported file reference") } func isPresignedS3URL(raw string) bool { u, err := url.Parse(raw) if err != nil || u.Host == "" { return false } q := u.Query() return q.Has("X-Amz-Algorithm") || q.Has("X-Amz-Signature") } func isKnownObjectKey(key string) bool { knownPrefixes := []string{ "image/", "audio/", "pdf/", "profile_pictures/", "notification_images/", "audio_answers/", } for _, prefix := range knownPrefixes { if strings.HasPrefix(key, prefix) { return true } } return false }