feat: upload
This commit is contained in:
@@ -6,7 +6,10 @@ import (
|
|||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"quyun/app/models"
|
||||||
|
"quyun/database/schemas/public/model"
|
||||||
"quyun/pkg/utils"
|
"quyun/pkg/utils"
|
||||||
"quyun/providers/app"
|
"quyun/providers/app"
|
||||||
|
|
||||||
@@ -107,12 +110,30 @@ func (up *uploads) Complete(ctx fiber.Ctx, md5 string, body *UploadFileInfo) err
|
|||||||
}
|
}
|
||||||
|
|
||||||
// save file to target path
|
// 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 {
|
if err := os.Rename(targetFile, targetPath); err != nil {
|
||||||
return err
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user