From 12761e1adb724791a19a5be9e00d4f56934e412d Mon Sep 17 00:00:00 2001 From: rogeecn Date: Sat, 22 Mar 2025 17:06:29 +0800 Subject: [PATCH] feat: upload --- app/http/uploads.go | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/app/http/uploads.go b/app/http/uploads.go index 7708f16..e87d9f5 100644 --- a/app/http/uploads.go +++ b/app/http/uploads.go @@ -6,7 +6,10 @@ import ( "mime/multipart" "os" "path/filepath" + "time" + "quyun/app/models" + "quyun/database/schemas/public/model" "quyun/pkg/utils" "quyun/providers/app" @@ -107,12 +110,30 @@ func (up *uploads) Complete(ctx fiber.Ctx, md5 string, body *UploadFileInfo) err } // save file to target path - targetPath := filepath.Join(up.storagePath(), body.Filename) - + targetPath := filepath.Join(up.storagePath(), md5+filepath.Ext(body.Filename)) if err := os.Rename(targetFile, targetPath); err != nil { return err } - // TODO: save file to database + + fState, err := os.Stat(targetPath) + if err != nil { + return err + } + + model := &model.Medias{ + CreatedAt: time.Now(), + Name: body.Filename, + MimeType: body.Mime, + Size: fState.Size(), // Updated to use fState.Size() + Path: targetPath, + } + + // save to db + if err := models.Medias.Create(ctx.Context(), model); err != nil { + return err + } + + log.Infof("File %s uploaded successfully", body.Filename) return nil }