Feat: Put file in oss handler
This commit is contained in:
parent
7eda63f089
commit
83a17645a1
3 changed files with 41 additions and 15 deletions
|
@ -162,6 +162,11 @@ func (policy *Policy) IsPathGenerateNeeded() bool {
|
|||
return policy.Type != "remote"
|
||||
}
|
||||
|
||||
// IsThumbGenerateNeeded 返回此策略是否需要在上传后生成缩略图
|
||||
func (policy *Policy) IsThumbGenerateNeeded() bool {
|
||||
return policy.Type == "local"
|
||||
}
|
||||
|
||||
// GetUploadURL 获取文件上传服务API地址
|
||||
func (policy *Policy) GetUploadURL() string {
|
||||
server, err := url.Parse(policy.Server)
|
||||
|
|
|
@ -139,12 +139,9 @@ func HookChangeCapacity(ctx context.Context, fs *FileSystem) error {
|
|||
func HookDeleteTempFile(ctx context.Context, fs *FileSystem) error {
|
||||
filePath := ctx.Value(fsctx.SavePathCtx).(string)
|
||||
// 删除临时文件
|
||||
// TODO 其他策略。Exists?
|
||||
if util.Exists(filePath) {
|
||||
_, err := fs.Handler.Delete(ctx, []string{filePath})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := fs.Handler.Delete(ctx, []string{filePath})
|
||||
if err != nil {
|
||||
util.Log().Warning("无法清理上传临时文件,%s", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -207,12 +204,14 @@ func GenericAfterUpdate(ctx context.Context, fs *FileSystem) error {
|
|||
}
|
||||
|
||||
// 尝试清空原有缩略图并重新生成
|
||||
go func() {
|
||||
if originFile.PicInfo != "" {
|
||||
_, _ = fs.Handler.Delete(ctx, []string{originFile.SourceName + conf.ThumbConfig.FileSuffix})
|
||||
fs.GenerateThumbnail(ctx, &originFile)
|
||||
}
|
||||
}()
|
||||
if originFile.GetPolicy().IsThumbGenerateNeeded() {
|
||||
go func() {
|
||||
if originFile.PicInfo != "" {
|
||||
_, _ = fs.Handler.Delete(ctx, []string{originFile.SourceName + conf.ThumbConfig.FileSuffix})
|
||||
fs.GenerateThumbnail(ctx, &originFile)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -270,7 +269,9 @@ func GenericAfterUpload(ctx context.Context, fs *FileSystem) error {
|
|||
fs.SetTargetFile(&[]model.File{*file})
|
||||
|
||||
// 异步尝试生成缩略图
|
||||
go fs.GenerateThumbnail(ctx, file)
|
||||
if fs.User.Policy.IsThumbGenerateNeeded() {
|
||||
go fs.GenerateThumbnail(ctx, file)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -116,7 +116,27 @@ func (handler Handler) Get(ctx context.Context, path string) (response.RSCloser,
|
|||
|
||||
// Put 将文件流保存到指定目录
|
||||
func (handler Handler) Put(ctx context.Context, file io.ReadCloser, dst string, size uint64) error {
|
||||
return errors.New("未实现")
|
||||
defer file.Close()
|
||||
|
||||
// 初始化客户端
|
||||
if err := handler.InitOSSClient(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 凭证有效期
|
||||
credentialTTL := model.GetIntSetting("upload_credential_timeout", 3600)
|
||||
|
||||
options := []oss.Option{
|
||||
oss.Expires(time.Now().Add(time.Duration(credentialTTL) * time.Second)),
|
||||
}
|
||||
|
||||
// 上传文件
|
||||
err := handler.bucket.PutObject(dst, file, options...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete 删除一个或多个文件,
|
||||
|
@ -217,7 +237,7 @@ func (handler Handler) Source(
|
|||
|
||||
func (handler Handler) signSourceURL(ctx context.Context, path string, ttl int64, options []oss.Option) (string, error) {
|
||||
// 是否带有 Version ID
|
||||
if versionID, ok := ctx.Value(VersionID).(int64); ok {
|
||||
if _, ok := ctx.Value(VersionID).(int64); ok {
|
||||
|
||||
}
|
||||
signedURL, err := handler.bucket.SignURL(path, oss.HTTPGet, ttl, options...)
|
||||
|
|
Loading…
Add table
Reference in a new issue