feat: upload
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user