mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Feat: remote upload callback / validation
This commit is contained in:
@@ -43,6 +43,12 @@ func (fs *FileSystem) withSpeedLimit(rs response.RSCloser) response.RSCloser {
|
||||
|
||||
// AddFile 新增文件记录
|
||||
func (fs *FileSystem) AddFile(ctx context.Context, parent *model.Folder) (*model.File, error) {
|
||||
// 添加文件记录前的钩子
|
||||
err := fs.Trigger(ctx, "BeforeAddFile")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
file := ctx.Value(fsctx.FileHeaderCtx).(FileHeader)
|
||||
filePath := ctx.Value(fsctx.SavePathCtx).(string)
|
||||
|
||||
@@ -54,10 +60,13 @@ func (fs *FileSystem) AddFile(ctx context.Context, parent *model.Folder) (*model
|
||||
FolderID: parent.ID,
|
||||
PolicyID: fs.User.Policy.ID,
|
||||
}
|
||||
_, err := newFile.Create()
|
||||
_, err = newFile.Create()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if err := fs.Trigger(ctx, "AfterValidateFailed"); err != nil {
|
||||
util.Log().Debug("AfterValidateFailed 钩子执行失败,%s", err)
|
||||
}
|
||||
return nil, ErrFileExisted.WithError(err)
|
||||
}
|
||||
|
||||
return &newFile, nil
|
||||
|
||||
@@ -208,7 +208,6 @@ func GenericAfterUpdate(ctx context.Context, fs *FileSystem) error {
|
||||
}
|
||||
|
||||
// SlaveAfterUpload Slave模式下上传完成钩子
|
||||
// TODO 测试
|
||||
func SlaveAfterUpload(ctx context.Context, fs *FileSystem) error {
|
||||
fileHeader := ctx.Value(fsctx.FileHeaderCtx).(FileHeader)
|
||||
policy := ctx.Value(fsctx.UploadPolicyCtx).(serializer.UploadPolicy)
|
||||
@@ -225,6 +224,7 @@ func SlaveAfterUpload(ctx context.Context, fs *FileSystem) error {
|
||||
Name: file.Name,
|
||||
SourceName: file.SourceName,
|
||||
PicInfo: file.PicInfo,
|
||||
Size: fileHeader.GetSize(),
|
||||
}
|
||||
return request.RemoteCallback(policy.CallbackURL, callbackBody)
|
||||
}
|
||||
|
||||
@@ -44,10 +44,7 @@ func (fs *FileSystem) ValidateFileSize(ctx context.Context, size uint64) bool {
|
||||
|
||||
// ValidateCapacity 验证并扣除用户容量
|
||||
func (fs *FileSystem) ValidateCapacity(ctx context.Context, size uint64) bool {
|
||||
if fs.User.IncreaseStorage(size) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
return fs.User.IncreaseStorage(size)
|
||||
}
|
||||
|
||||
// ValidateExtension 验证文件扩展名
|
||||
|
||||
Reference in New Issue
Block a user