working file sharing

This commit is contained in:
2026-04-16 20:38:16 +02:00
parent f0fdaedd8c
commit 84e3f852fe
14 changed files with 190 additions and 81 deletions
+13 -25
View File
@@ -5,42 +5,27 @@ import (
"io"
"mime"
"net/url"
"strconv"
"time"
"go-socket/packages/globals"
"github.com/google/uuid"
"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
func GetKey(connectionId uuid.UUID, mimeType string) string {
extensions, err := mime.ExtensionsByType(mimeType)
if err != nil || len(extensions) == 0 {
extensions = []string{".unknown"}
}
exts, err := mime.ExtensionsByType(ct)
if err != nil || len(exts) == 0 {
return ".unk"
}
return exts[0]
return connectionId.String() + "/" + strconv.FormatInt(time.Now().UnixMilli(), 10) + extensions[0]
}
func GetKey(hash string, contentType string) string {
return hash + extensionFromContentType(contentType)
}
func Init() {
ctx := context.Background()
func Init(ctx context.Context) {
var err error
minClient, err = minio.New("localhost:9000", &minio.Options{
Creds: credentials.NewStaticV4("root", "change_to_env", ""),
@@ -58,7 +43,10 @@ func Init() {
if !exists {
err = minClient.MakeBucket(ctx, globals.FileStorageBucketName, minio.MakeBucketOptions{})
if err != nil {
return
exists, checkErr := minClient.BucketExists(ctx, globals.FileStorageBucketName)
if checkErr != nil || !exists {
panic(err)
}
}
}