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)
This commit is contained in:
+45
-11
@@ -2,20 +2,47 @@ package minio
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go-socket/packages/globals"
|
||||
"io"
|
||||
"mime"
|
||||
"net/url"
|
||||
|
||||
"go-socket/packages/globals"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
var dbConn *minio.Client
|
||||
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
|
||||
dbConn, err = minio.New("localhost:9000", &minio.Options{
|
||||
minClient, err = minio.New("localhost:9000", &minio.Options{
|
||||
Creds: credentials.NewStaticV4("root", "change_to_env", ""),
|
||||
Secure: false,
|
||||
}) // TODO change in production
|
||||
@@ -23,22 +50,29 @@ func MinInit() {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
exists, err := dbConn.BucketExists(ctx, "main")
|
||||
exists, err := minClient.BucketExists(ctx, globals.FileStorageBucketName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if !exists {
|
||||
err = dbConn.MakeBucket(ctx, globals.FileStorageBucketName, minio.MakeBucketOptions{})
|
||||
err = minClient.MakeBucket(ctx, globals.FileStorageBucketName, minio.MakeBucketOptions{})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func putFile(ctx context.Context, key string, reader io.Reader, size uint32, contentType string) error {
|
||||
dbConn.PutObject(ctx, globals.FileStorageBucketName, key, reader, int64(size), minio.PutObjectOptions{
|
||||
ContentType: contentType,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user