2019-11-17 13:50:14 +08:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/HFO4/cloudreve/pkg/util"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
2019-11-19 16:42:36 +08:00
|
|
|
// Handler 本地策略适配器
|
|
|
|
type Handler struct{}
|
2019-11-17 13:50:14 +08:00
|
|
|
|
|
|
|
// Put 将文件流保存到指定目录
|
2019-11-26 11:42:26 +08:00
|
|
|
func (handler Handler) Put(ctx context.Context, file io.ReadCloser, dst string, size uint64) error {
|
2019-11-17 13:50:14 +08:00
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
// 如果目标目录不存在,创建
|
|
|
|
basePath := filepath.Dir(dst)
|
|
|
|
if !util.Exists(basePath) {
|
2019-11-17 20:30:25 +08:00
|
|
|
err := os.MkdirAll(basePath, 0700)
|
2019-11-17 13:50:14 +08:00
|
|
|
if err != nil {
|
2019-11-18 14:06:15 +08:00
|
|
|
util.Log().Warning("无法创建目录,%s", err)
|
2019-11-17 13:50:14 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 13:26:32 +08:00
|
|
|
// 创建目标文件
|
2019-11-17 13:50:14 +08:00
|
|
|
out, err := os.Create(dst)
|
|
|
|
if err != nil {
|
2019-11-18 14:06:15 +08:00
|
|
|
util.Log().Warning("无法创建文件,%s", err)
|
2019-11-17 13:50:14 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
|
2019-11-18 13:26:32 +08:00
|
|
|
// 写入文件内容
|
2019-11-17 13:50:14 +08:00
|
|
|
_, err = io.Copy(out, file)
|
|
|
|
return err
|
|
|
|
}
|
2019-11-18 14:06:15 +08:00
|
|
|
|
|
|
|
// Delete 删除一个或多个文件,
|
|
|
|
// 返回已删除的文件,及遇到的最后一个错误
|
|
|
|
func (handler Handler) Delete(ctx context.Context, files []string) ([]string, error) {
|
|
|
|
deleted := make([]string, 0, len(files))
|
|
|
|
var retErr error
|
|
|
|
|
|
|
|
for _, value := range files {
|
|
|
|
err := os.Remove(value)
|
|
|
|
if err == nil {
|
|
|
|
deleted = append(deleted, value)
|
|
|
|
} else {
|
2019-11-18 19:32:06 +08:00
|
|
|
util.Log().Warning("无法删除文件,%s", err)
|
2019-11-18 14:06:15 +08:00
|
|
|
retErr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return deleted, retErr
|
|
|
|
}
|