mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 12:17:24 +00:00
Add cancel upload functionality to file API
This commit is contained in:
@@ -230,6 +230,34 @@ func FinishUploadingFile(uid uint, fid uint) (*model.FileView, error) {
|
||||
return dbFile.ToView(), nil
|
||||
}
|
||||
|
||||
func CancelUploadingFile(uid uint, fid uint) error {
|
||||
uploadingFile, err := dao.GetUploadingFile(fid)
|
||||
if err != nil {
|
||||
log.Error("failed to get uploading file: ", err)
|
||||
return model.NewNotFoundError("file not found")
|
||||
}
|
||||
if uploadingFile.UserID != uid {
|
||||
return model.NewUnAuthorizedError("user cannot cancel uploading file")
|
||||
}
|
||||
|
||||
if err := dao.DeleteUploadingFile(fid); err != nil {
|
||||
log.Error("failed to delete uploading file: ", err)
|
||||
return model.NewInternalServerError("failed to delete uploading file")
|
||||
}
|
||||
|
||||
go func() {
|
||||
// Wait for 1 second to ensure there is no block being uploading
|
||||
time.Sleep(time.Second)
|
||||
if err := os.RemoveAll(uploadingFile.TempPath); err != nil {
|
||||
log.Error("failed to remove temp dir: ", err)
|
||||
}
|
||||
}()
|
||||
|
||||
updateUploadingSize(-uploadingFile.TotalSize)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateRedirectFile(uid uint, filename string, description string, resourceID uint, redirectUrl string) (*model.FileView, error) {
|
||||
canUpload, err := checkUserCanUpload(uid)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user