优化结构
This commit is contained in:
@@ -58,6 +58,17 @@ func expectedChunkSize(session *UploadSession, index int) (int64, error) {
|
||||
return last, nil
|
||||
}
|
||||
|
||||
func (uc *MediaUsecase) uploadSession(ctx context.Context, id uint) (*UploadSession, error) {
|
||||
session, err := uc.FindUploadSession(ctx, id)
|
||||
if errors.Is(err, ErrUploadSessionNotFound) {
|
||||
return nil, fmt.Errorf("上传会话不存在: %w", ErrUploadSessionNotFound)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func (uc *MediaUsecase) chunkPrefix(uploadID uint) string {
|
||||
directory := "uploads/chunks"
|
||||
if uc.settings != nil {
|
||||
@@ -117,9 +128,9 @@ func (uc *MediaUsecase) InitUpload(ctx context.Context, userID uint, name, hash
|
||||
return session, nil, indices, nil
|
||||
}
|
||||
func (uc *MediaUsecase) SaveChunk(ctx context.Context, userID, uploadID uint, index int, expected string, reader io.Reader) error {
|
||||
session, err := uc.FindUploadSession(ctx, uploadID)
|
||||
session, err := uc.uploadSession(ctx, uploadID)
|
||||
if err != nil {
|
||||
return errors.New("上传会话不存在")
|
||||
return err
|
||||
}
|
||||
if session.UserID != userID {
|
||||
return errors.New("无权操作该上传")
|
||||
@@ -173,9 +184,9 @@ func (uc *MediaUsecase) SaveChunk(ctx context.Context, userID, uploadID uint, in
|
||||
return nil
|
||||
}
|
||||
func (uc *MediaUsecase) CompleteUpload(ctx context.Context, userID, uploadID uint, mime string) (*MediaFile, error) {
|
||||
session, err := uc.FindUploadSession(ctx, uploadID)
|
||||
session, err := uc.uploadSession(ctx, uploadID)
|
||||
if err != nil {
|
||||
return nil, errors.New("上传会话不存在")
|
||||
return nil, err
|
||||
}
|
||||
if session.UserID != userID {
|
||||
return nil, errors.New("无权操作该上传")
|
||||
@@ -241,9 +252,9 @@ func (uc *MediaUsecase) CompleteUpload(ctx context.Context, userID, uploadID uin
|
||||
return media, nil
|
||||
}
|
||||
func (uc *MediaUsecase) CancelUpload(ctx context.Context, userID, uploadID uint) error {
|
||||
session, err := uc.FindUploadSession(ctx, uploadID)
|
||||
session, err := uc.uploadSession(ctx, uploadID)
|
||||
if err != nil {
|
||||
return errors.New("上传会话不存在")
|
||||
return err
|
||||
}
|
||||
if session.UserID != userID {
|
||||
return errors.New("无权操作该上传")
|
||||
|
||||
Reference in New Issue
Block a user