Cloudreve/pkg/filesystem/local/handler.go

130 lines
3.1 KiB
Go
Raw Normal View History

2019-11-17 13:50:14 +08:00
package local
import (
"context"
2019-12-10 17:10:34 +08:00
"errors"
"fmt"
model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/auth"
"github.com/HFO4/cloudreve/pkg/conf"
2019-12-10 17:10:34 +08:00
"github.com/HFO4/cloudreve/pkg/filesystem/fsctx"
2019-12-08 22:17:36 +08:00
"github.com/HFO4/cloudreve/pkg/filesystem/response"
2019-12-10 17:10:34 +08:00
"github.com/HFO4/cloudreve/pkg/serializer"
2019-11-17 13:50:14 +08:00
"github.com/HFO4/cloudreve/pkg/util"
"io"
2019-12-10 17:10:34 +08:00
"net/url"
2019-11-17 13:50:14 +08:00
"os"
"path/filepath"
)
// Handler 本地策略适配器
2019-12-10 17:10:34 +08:00
type Handler struct {
Policy *model.Policy
}
2019-11-17 13:50:14 +08:00
// Get 获取文件内容
func (handler Handler) Get(ctx context.Context, path string) (io.ReadSeeker, error) {
// 打开文件
file, err := os.Open(path)
if err != nil {
util.Log().Debug("无法打开文件:%s", err)
return nil, err
}
// 开启一个协程用于请求结束后关闭reader
2019-12-11 18:52:41 +08:00
// go closeReader(ctx, file)
return file, nil
}
// closeReader 用于在请求结束后关闭reader
2019-12-11 18:52:41 +08:00
// TODO 让业务代码自己关闭
func closeReader(ctx context.Context, closer io.Closer) {
select {
case <-ctx.Done():
2019-11-27 13:54:25 +08:00
_ = closer.Close()
}
}
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 {
util.Log().Warning("无法创建目录,%s", err)
2019-11-17 13:50:14 +08:00
return err
}
}
// 创建目标文件
2019-11-17 13:50:14 +08:00
out, err := os.Create(dst)
if err != nil {
util.Log().Warning("无法创建文件,%s", err)
2019-11-17 13:50:14 +08:00
return err
}
defer out.Close()
// 写入文件内容
2019-11-17 13:50:14 +08:00
_, err = io.Copy(out, file)
return err
}
// Delete 删除一个或多个文件,
2019-11-30 15:09:56 +08:00
// 返回未删除的文件,及遇到的最后一个错误
func (handler Handler) Delete(ctx context.Context, files []string) ([]string, error) {
2019-11-30 15:09:56 +08:00
deleteFailed := make([]string, 0, len(files))
var retErr error
for _, value := range files {
err := os.Remove(value)
2019-11-30 15:09:56 +08:00
if err != nil {
2019-11-18 19:32:06 +08:00
util.Log().Warning("无法删除文件,%s", err)
retErr = err
2019-11-30 15:09:56 +08:00
deleteFailed = append(deleteFailed, value)
}
// 尝试删除文件的缩略图(如果有)
_ = os.Remove(value + conf.ThumbConfig.FileSuffix)
}
2019-11-30 15:09:56 +08:00
return deleteFailed, retErr
}
2019-12-08 22:17:36 +08:00
// Thumb 获取文件缩略图
func (handler Handler) Thumb(ctx context.Context, path string) (*response.ContentResponse, error) {
file, err := handler.Get(ctx, path+conf.ThumbConfig.FileSuffix)
2019-12-08 22:17:36 +08:00
if err != nil {
return nil, err
}
return &response.ContentResponse{
Redirect: false,
Content: file,
}, nil
}
2019-12-10 17:10:34 +08:00
// Source 获取外链URL
func (handler Handler) Source(ctx context.Context, path string, url url.URL, expires int64) (string, error) {
file, ok := ctx.Value(fsctx.FileModelCtx).(model.File)
if !ok {
return "", errors.New("无法获取文件记录上下文")
}
// 签名生成文件记录
signedURI, err := auth.SignURI(
fmt.Sprintf("/api/v3/file/get/%d/%s", file.ID, file.Name),
0,
)
if err != nil {
return "", serializer.NewError(serializer.CodeEncryptError, "无法对URL进行签名", err)
}
finalURL := url.ResolveReference(signedURI).String()
return finalURL, nil
}