2019-11-17 13:50:14 +08:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/HFO4/cloudreve/pkg/util"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Handler struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put 将文件流保存到指定目录
|
|
|
|
func (handler Handler) Put(ctx context.Context, file io.ReadCloser, dst string) error {
|
|
|
|
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 {
|
|
|
|
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 {
|
|
|
|
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
|
|
|
|
}
|