83 lines
1.9 KiB
Go
83 lines
1.9 KiB
Go
package medias
|
|
|
|
import (
|
|
"mime/multipart"
|
|
"time"
|
|
|
|
"backend/app/http/storages"
|
|
"backend/app/http/tenants"
|
|
"backend/database/models/qvyun_v2/public/model"
|
|
"backend/pkg/storage"
|
|
"backend/providers/jwt"
|
|
|
|
"github.com/gofiber/fiber/v3"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// @provider
|
|
type Controller struct {
|
|
tenantSvc *tenants.Service
|
|
svc *Service
|
|
storageSvc *storages.Service
|
|
log *log.Entry `inject:"false"`
|
|
}
|
|
|
|
func (ctl *Controller) Prepare() error {
|
|
ctl.log = log.WithField("module", "medias.Controller")
|
|
return nil
|
|
}
|
|
|
|
// Upload
|
|
// @Router /api/v1/medias/:tenant/upload [post]
|
|
// @Bind tenantSlug path
|
|
// @Bind req body
|
|
// @Bind file file
|
|
// @Bind claim local
|
|
func (ctl *Controller) Upload(ctx fiber.Ctx, claim *jwt.Claims, tenantSlug string, file *multipart.FileHeader, req *UploadReq) (*storage.UploadedFile, error) {
|
|
tenant, err := ctl.tenantSvc.GetTenantBySlug(ctx.Context(), tenantSlug)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
defaultStorage, err := ctl.storageSvc.GetDefault(ctx.Context())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
uploader, err := storage.NewUploader(req.FileName, req.ChunkNumber, req.TotalChunks, req.FileMD5)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
uploadedFile, err := uploader.Save(ctx, file)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if uploadedFile == nil {
|
|
return uploadedFile, nil
|
|
}
|
|
|
|
uploadedFile, err = storage.Build(defaultStorage).Save(ctx.Context(), uploadedFile)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// save to db
|
|
_, err = ctl.svc.Create(ctx.Context(), &model.Medias{
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
TenantID: tenant.ID,
|
|
UserID: claim.UserID,
|
|
StorageID: defaultStorage.ID,
|
|
Hash: uploadedFile.Hash,
|
|
Name: uploadedFile.Name,
|
|
MimeType: uploadedFile.MimeType,
|
|
Size: uploadedFile.Size,
|
|
Path: uploadedFile.Path,
|
|
})
|
|
uploadedFile.Preview = ""
|
|
|
|
return uploadedFile, err
|
|
}
|