feat: upload

This commit is contained in:
rogeecn
2025-03-22 17:06:29 +08:00
parent 271b1a2230
commit 12761e1adb

View File

@@ -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
}