Files
go-socket/packages/minio/minio.go
T
admin 48d3c6f857 add file storage: metadata schema, minio upload/download, content-type extension mapping
- add files_metadata and files_metadata_connections tables with CRUD helpers
- add FileMetadata type and Sha256Hash typedef; replace Media struct
- add minio upload, presigned download URL, and key generation
- fix bucket existence check to use FileStorageBucketName instead of hardcoded "main"
- fix files_metadata_connections table name and trailing comma in DDL
- fix column name original -> name in files_metadata schema
- add canonical MIME-to-extension map with .unk fallback
- add FileDownloadLinkTtl constant (24h)
2026-04-14 18:55:22 +02:00

79 lines
1.7 KiB
Go

package minio
import (
"context"
"io"
"mime"
"net/url"
"go-socket/packages/globals"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
var minClient *minio.Client
var canonicalExt = map[string]string{
"image/jpeg": ".jpg",
"image/png": ".png",
"image/gif": ".gif",
"image/webp": ".webp",
"video/mp4": ".mp4",
"application/pdf": ".pdf",
}
func extensionFromContentType(ct string) string {
if ext, ok := canonicalExt[ct]; ok {
return ext
}
exts, err := mime.ExtensionsByType(ct)
if err != nil || len(exts) == 0 {
return ".unk"
}
return exts[0]
}
func getKey(hash string, contentType string) string {
return hash + extensionFromContentType(contentType)
}
func MinInit() {
ctx := context.Background()
var err error
minClient, err = minio.New("localhost:9000", &minio.Options{
Creds: credentials.NewStaticV4("root", "change_to_env", ""),
Secure: false,
}) // TODO change in production
if err != nil {
panic(err)
}
exists, err := minClient.BucketExists(ctx, globals.FileStorageBucketName)
if err != nil {
panic(err)
}
if !exists {
err = minClient.MakeBucket(ctx, globals.FileStorageBucketName, minio.MakeBucketOptions{})
if err != nil {
return
}
}
}
func upload(ctx context.Context, key string, body io.Reader, size int64, contentType string) error {
_, err := minClient.PutObject(ctx, globals.FileStorageBucketName, key, body, size,
minio.PutObjectOptions{
ContentType: contentType,
})
return err
}
func getDownloadUrl(ctx context.Context, key string) (*url.URL, error) {
u, err := minClient.PresignedGetObject(ctx, globals.FileStorageBucketName, key, globals.FileDownloadLinkTtl, nil)
return u, err
}