Cloudreve/pkg/filesystem/driver/local/handler.go

222 lines
5.4 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"
2019-11-17 13:50:14 +08:00
"io"
2019-12-10 17:10:34 +08:00
"net/url"
2019-11-17 13:50:14 +08:00
"os"
"path/filepath"
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/auth"
"github.com/cloudreve/Cloudreve/v3/pkg/cache"
"github.com/cloudreve/Cloudreve/v3/pkg/conf"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/response"
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
2019-11-17 13:50:14 +08:00
)
2020-01-17 13:04:14 +08:00
// Driver 本地策略适配器
type Driver struct {
2019-12-10 17:10:34 +08:00
Policy *model.Policy
}
2019-11-17 13:50:14 +08:00
// List 递归列取给定物理路径下所有文件
func (handler Driver) List(ctx context.Context, path string, recursive bool) ([]response.Object, error) {
var res []response.Object
// 取得起始路径
root := util.RelativePath(filepath.FromSlash(path))
// 开始遍历路径下的文件、目录
err := filepath.Walk(root,
func(path string, info os.FileInfo, err error) error {
// 跳过根目录
if path == root {
return nil
}
if err != nil {
util.Log().Warning("无法遍历目录 %s, %s", path, err)
return filepath.SkipDir
}
// 将遍历对象的绝对路径转换为相对路径
rel, err := filepath.Rel(root, path)
if err != nil {
return err
}
res = append(res, response.Object{
Name: info.Name(),
RelativePath: filepath.ToSlash(rel),
Source: path,
Size: uint64(info.Size()),
IsDir: info.IsDir(),
LastModify: info.ModTime(),
})
// 如果非递归,则不步入目录
if !recursive && info.IsDir() {
return filepath.SkipDir
}
return nil
})
return res, err
}
// Get 获取文件内容
2020-01-17 13:04:14 +08:00
func (handler Driver) Get(ctx context.Context, path string) (response.RSCloser, error) {
// 打开文件
2020-03-11 10:32:35 +08:00
file, err := os.Open(util.RelativePath(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 将文件流保存到指定目录
2020-01-17 13:04:14 +08:00
func (handler Driver) Put(ctx context.Context, file io.ReadCloser, dst string, size uint64) error {
2019-11-17 13:50:14 +08:00
defer file.Close()
2020-03-11 10:32:35 +08:00
dst = util.RelativePath(filepath.FromSlash(dst))
2019-11-17 13:50:14 +08:00
// 如果目标目录不存在,创建
basePath := filepath.Dir(dst)
if !util.Exists(basePath) {
err := os.MkdirAll(basePath, 0744)
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
// 写入文件内容
2019-11-17 13:50:14 +08:00
_, err = io.Copy(out, file)
return err
}
// Delete 删除一个或多个文件,
2019-11-30 15:09:56 +08:00
// 返回未删除的文件,及遇到的最后一个错误
2020-01-17 13:04:14 +08:00
func (handler Driver) 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 {
2020-03-11 10:32:35 +08:00
err := os.Remove(util.RelativePath(filepath.FromSlash(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(util.RelativePath(value + conf.ThumbConfig.FileSuffix))
}
2019-11-30 15:09:56 +08:00
return deleteFailed, retErr
}
2019-12-08 22:17:36 +08:00
// Thumb 获取文件缩略图
2020-01-17 13:04:14 +08:00
func (handler Driver) 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
2020-01-17 13:04:14 +08:00
func (handler Driver) Source(
ctx context.Context,
path string,
baseURL url.URL,
ttl int64,
isDownload bool,
speed int,
) (string, error) {
2019-12-10 17:10:34 +08:00
file, ok := ctx.Value(fsctx.FileModelCtx).(model.File)
if !ok {
return "", errors.New("无法获取文件记录上下文")
}
2020-02-26 15:11:06 +08:00
// 是否启用了CDN
if handler.Policy.BaseURL != "" {
cdnURL, err := url.Parse(handler.Policy.BaseURL)
if err != nil {
return "", err
}
baseURL = *cdnURL
}
var (
signedURI *url.URL
err error
)
if isDownload {
// 创建下载会话,将文件信息写入缓存
downloadSessionID := util.RandStringRunes(16)
err = cache.Set("download_"+downloadSessionID, file, int(ttl))
if err != nil {
return "", serializer.NewError(serializer.CodeCacheOperation, "无法创建下载会话", err)
}
// 签名生成文件记录
signedURI, err = auth.SignURI(
auth.General,
fmt.Sprintf("/api/v3/file/download/%s", downloadSessionID),
ttl,
)
} else {
// 签名生成文件记录
signedURI, err = auth.SignURI(
auth.General,
fmt.Sprintf("/api/v3/file/get/%d/%s", file.ID, file.Name),
ttl,
)
}
if err != nil {
return "", serializer.NewError(serializer.CodeEncryptError, "无法对URL进行签名", err)
}
finalURL := baseURL.ResolveReference(signedURI).String()
return finalURL, nil
}
2019-12-28 15:50:56 +08:00
// Token 获取上传策略和认证Token本地策略直接返回空值
2020-01-17 13:04:14 +08:00
func (handler Driver) Token(ctx context.Context, ttl int64, key string) (serializer.UploadCredential, error) {
2019-12-28 15:50:56 +08:00
return serializer.UploadCredential{}, nil
}