This commit is contained in:
2026-08-16 15:11:38 +08:00
parent f7f6db8695
commit a85477be80
40 changed files with 761 additions and 144 deletions
+5 -2
View File
@@ -17,6 +17,8 @@ import (
"github.com/google/uuid"
)
var ErrUploadSessionNotFound = errors.New("upload session not found")
func (uc *MediaUsecase) chunkPrefix(uploadID uint) string {
directory := "uploads/chunks"
if uc.settings != nil {
@@ -44,11 +46,13 @@ func (uc *MediaUsecase) InitUpload(ctx context.Context, userID uint, name, hash
}
}
session, err := uc.FindUploadingSession(ctx, userID, hash)
if err != nil {
if errors.Is(err, ErrUploadSessionNotFound) {
session = &UploadSession{UserID: userID, FileName: name, FileHash: hash, FileSize: size, ChunkSize: chunkSize, ChunkTotal: total, Status: "uploading"}
if err = uc.CreateUploadSession(ctx, session); err != nil {
return nil, nil, nil, err
}
} else if err != nil {
return nil, nil, nil, err
}
chunks, err := uc.ListChunks(ctx, session.ID)
if err != nil {
@@ -146,7 +150,6 @@ func (uc *MediaUsecase) CompleteUpload(ctx context.Context, userID, uploadID uin
// byte count.
media := &MediaFile{Name: session.FileName, URL: stored.URL, Tag: strings.TrimPrefix(filepath.Ext(session.FileName), "."), Key: key, Size: session.FileSize, Mime: mime, MD5: hash, UserID: userID}
if err = uc.CreateMedia(ctx, media); err != nil {
_ = uc.files.Delete(ctx, key)
return fail(err)
}
_ = uc.CompleteUploadSession(ctx, uploadID, key, media.ID)