Init V4 community edition (#2265)
* Init V4 community edition * Init V4 community edition
This commit is contained in:
75
pkg/filemanager/driver/local/entity.go
Normal file
75
pkg/filemanager/driver/local/entity.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package local
|
||||
|
||||
import (
|
||||
"github.com/cloudreve/Cloudreve/v4/ent"
|
||||
"github.com/cloudreve/Cloudreve/v4/inventory/types"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/filemanager/fs"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/util"
|
||||
"github.com/gofrs/uuid"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
// NewLocalFileEntity creates a new local file entity.
|
||||
func NewLocalFileEntity(t types.EntityType, src string) (fs.Entity, error) {
|
||||
info, err := os.Stat(util.RelativePath(src))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &localFileEntity{
|
||||
t: t,
|
||||
src: src,
|
||||
size: info.Size(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
type localFileEntity struct {
|
||||
t types.EntityType
|
||||
src string
|
||||
size int64
|
||||
}
|
||||
|
||||
func (l *localFileEntity) ID() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (l *localFileEntity) Type() types.EntityType {
|
||||
return l.t
|
||||
}
|
||||
|
||||
func (l *localFileEntity) Size() int64 {
|
||||
return l.size
|
||||
}
|
||||
|
||||
func (l *localFileEntity) UpdatedAt() time.Time {
|
||||
return time.Now()
|
||||
}
|
||||
|
||||
func (l *localFileEntity) CreatedAt() time.Time {
|
||||
return time.Now()
|
||||
}
|
||||
|
||||
func (l *localFileEntity) CreatedBy() *ent.User {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *localFileEntity) Source() string {
|
||||
return l.src
|
||||
}
|
||||
|
||||
func (l *localFileEntity) ReferenceCount() int {
|
||||
return 1
|
||||
}
|
||||
|
||||
func (l *localFileEntity) PolicyID() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (l *localFileEntity) UploadSessionID() *uuid.UUID {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *localFileEntity) Model() *ent.Entity {
|
||||
return nil
|
||||
}
|
||||
11
pkg/filemanager/driver/local/fallocate.go
Normal file
11
pkg/filemanager/driver/local/fallocate.go
Normal file
@@ -0,0 +1,11 @@
|
||||
//go:build !linux && !darwin
|
||||
// +build !linux,!darwin
|
||||
|
||||
package local
|
||||
|
||||
import "os"
|
||||
|
||||
// No-op on non-Linux/Darwin platforms.
|
||||
func Fallocate(file *os.File, offset int64, length int64) error {
|
||||
return nil
|
||||
}
|
||||
27
pkg/filemanager/driver/local/fallocate_darwin.go
Normal file
27
pkg/filemanager/driver/local/fallocate_darwin.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package local
|
||||
|
||||
import (
|
||||
"os"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func Fallocate(file *os.File, offset int64, length int64) error {
|
||||
var fst syscall.Fstore_t
|
||||
|
||||
fst.Flags = syscall.F_ALLOCATECONTIG
|
||||
fst.Posmode = syscall.F_PREALLOCATE
|
||||
fst.Offset = 0
|
||||
fst.Length = offset + length
|
||||
fst.Bytesalloc = 0
|
||||
|
||||
// Check https://lists.apple.com/archives/darwin-dev/2007/Dec/msg00040.html
|
||||
_, _, err := syscall.Syscall(syscall.SYS_FCNTL, file.Fd(), syscall.F_PREALLOCATE, uintptr(unsafe.Pointer(&fst)))
|
||||
if err != syscall.Errno(0x0) {
|
||||
fst.Flags = syscall.F_ALLOCATEALL
|
||||
// Ignore the return value
|
||||
_, _, _ = syscall.Syscall(syscall.SYS_FCNTL, file.Fd(), syscall.F_PREALLOCATE, uintptr(unsafe.Pointer(&fst)))
|
||||
}
|
||||
|
||||
return syscall.Ftruncate(int(file.Fd()), fst.Length)
|
||||
}
|
||||
14
pkg/filemanager/driver/local/fallocate_linux.go
Normal file
14
pkg/filemanager/driver/local/fallocate_linux.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package local
|
||||
|
||||
import (
|
||||
"os"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func Fallocate(file *os.File, offset int64, length int64) error {
|
||||
if length == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return syscall.Fallocate(int(file.Fd()), 0, offset, length)
|
||||
}
|
||||
301
pkg/filemanager/driver/local/local.go
Normal file
301
pkg/filemanager/driver/local/local.go
Normal file
@@ -0,0 +1,301 @@
|
||||
package local
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/cloudreve/Cloudreve/v4/ent"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/auth"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/boolset"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/conf"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/filemanager/driver"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/filemanager/fs"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/logging"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/request"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/serializer"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/util"
|
||||
)
|
||||
|
||||
const (
|
||||
Perm = 0744
|
||||
)
|
||||
|
||||
var (
|
||||
capabilities = &driver.Capabilities{
|
||||
StaticFeatures: &boolset.BooleanSet{},
|
||||
MediaMetaProxy: true,
|
||||
ThumbProxy: true,
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
boolset.Sets(map[driver.HandlerCapability]bool{
|
||||
driver.HandlerCapabilityProxyRequired: true,
|
||||
driver.HandlerCapabilityInboundGet: true,
|
||||
}, capabilities.StaticFeatures)
|
||||
}
|
||||
|
||||
// Driver 本地策略适配器
|
||||
type Driver struct {
|
||||
Policy *ent.StoragePolicy
|
||||
httpClient request.Client
|
||||
l logging.Logger
|
||||
config conf.ConfigProvider
|
||||
}
|
||||
|
||||
// New constructs a new local driver
|
||||
func New(p *ent.StoragePolicy, l logging.Logger, config conf.ConfigProvider) *Driver {
|
||||
return &Driver{
|
||||
Policy: p,
|
||||
l: l,
|
||||
httpClient: request.NewClient(config, request.WithLogger(l)),
|
||||
config: config,
|
||||
}
|
||||
}
|
||||
|
||||
//// 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("Failed to walk folder %q: %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 获取文件内容
|
||||
func (handler *Driver) Open(ctx context.Context, path string) (*os.File, error) {
|
||||
// 打开文件
|
||||
file, err := os.Open(handler.LocalPath(ctx, path))
|
||||
if err != nil {
|
||||
handler.l.Debug("Failed to open file: %s", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func (handler *Driver) LocalPath(ctx context.Context, path string) string {
|
||||
return util.RelativePath(filepath.FromSlash(path))
|
||||
}
|
||||
|
||||
// Put 将文件流保存到指定目录
|
||||
func (handler *Driver) Put(ctx context.Context, file *fs.UploadRequest) error {
|
||||
defer file.Close()
|
||||
dst := util.RelativePath(filepath.FromSlash(file.Props.SavePath))
|
||||
|
||||
// 如果非 Overwrite,则检查是否有重名冲突
|
||||
if file.Mode&fs.ModeOverwrite != fs.ModeOverwrite {
|
||||
if util.Exists(dst) {
|
||||
handler.l.Warning("File with the same name existed or unavailable: %s", dst)
|
||||
return errors.New("file with the same name existed or unavailable")
|
||||
}
|
||||
}
|
||||
|
||||
if err := handler.prepareFileDirectory(dst); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
openMode := os.O_CREATE | os.O_RDWR
|
||||
if file.Mode&fs.ModeOverwrite == fs.ModeOverwrite && file.Offset == 0 {
|
||||
openMode |= os.O_TRUNC
|
||||
}
|
||||
|
||||
out, err := os.OpenFile(dst, openMode, Perm)
|
||||
if err != nil {
|
||||
handler.l.Warning("Failed to open or create file: %s", err)
|
||||
return err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
stat, err := out.Stat()
|
||||
if err != nil {
|
||||
handler.l.Warning("Failed to read file info: %s", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if stat.Size() < file.Offset {
|
||||
return errors.New("size of unfinished uploaded chunks is not as expected")
|
||||
}
|
||||
|
||||
if _, err := out.Seek(file.Offset, io.SeekStart); err != nil {
|
||||
return fmt.Errorf("failed to seek to desired offset %d: %s", file.Offset, err)
|
||||
}
|
||||
|
||||
// 写入文件内容
|
||||
_, err = io.Copy(out, file)
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete 删除一个或多个文件,
|
||||
// 返回未删除的文件,及遇到的最后一个错误
|
||||
func (handler *Driver) Delete(ctx context.Context, files ...string) ([]string, error) {
|
||||
deleteFailed := make([]string, 0, len(files))
|
||||
var retErr error
|
||||
|
||||
for _, value := range files {
|
||||
filePath := util.RelativePath(filepath.FromSlash(value))
|
||||
if util.Exists(filePath) {
|
||||
err := os.Remove(filePath)
|
||||
if err != nil {
|
||||
handler.l.Warning("Failed to delete file: %s", err)
|
||||
retErr = err
|
||||
deleteFailed = append(deleteFailed, value)
|
||||
}
|
||||
}
|
||||
|
||||
//// 尝试删除文件的缩略图(如果有)
|
||||
//_ = os.Remove(util.RelativePath(value + model.GetSettingByNameWithDefault("thumb_file_suffix", "._thumb")))
|
||||
}
|
||||
|
||||
return deleteFailed, retErr
|
||||
}
|
||||
|
||||
// Thumb 获取文件缩略图
|
||||
func (handler *Driver) Thumb(ctx context.Context, expire *time.Time, ext string, e fs.Entity) (string, error) {
|
||||
return "", errors.New("not implemented")
|
||||
}
|
||||
|
||||
// Source 获取外链URL
|
||||
func (handler *Driver) Source(ctx context.Context, e fs.Entity, args *driver.GetSourceArgs) (string, error) {
|
||||
return "", errors.New("not implemented")
|
||||
}
|
||||
|
||||
// Token 获取上传策略和认证Token,本地策略直接返回空值
|
||||
func (handler *Driver) Token(ctx context.Context, uploadSession *fs.UploadSession, file *fs.UploadRequest) (*fs.UploadCredential, error) {
|
||||
if file.Mode&fs.ModeOverwrite != fs.ModeOverwrite && util.Exists(uploadSession.Props.SavePath) {
|
||||
return nil, errors.New("placeholder file already exist")
|
||||
}
|
||||
|
||||
dst := util.RelativePath(filepath.FromSlash(uploadSession.Props.SavePath))
|
||||
if err := handler.prepareFileDirectory(dst); err != nil {
|
||||
return nil, fmt.Errorf("failed to prepare file directory: %w", err)
|
||||
}
|
||||
|
||||
f, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, Perm)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create placeholder file: %w", err)
|
||||
}
|
||||
|
||||
// Preallocate disk space
|
||||
defer f.Close()
|
||||
if handler.Policy.Settings.PreAllocate {
|
||||
if err := Fallocate(f, 0, uploadSession.Props.Size); err != nil {
|
||||
handler.l.Warning("Failed to preallocate file: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
return &fs.UploadCredential{
|
||||
SessionID: uploadSession.Props.UploadSessionID,
|
||||
ChunkSize: handler.Policy.Settings.ChunkSize,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (h *Driver) prepareFileDirectory(dst string) error {
|
||||
basePath := filepath.Dir(dst)
|
||||
if !util.Exists(basePath) {
|
||||
err := os.MkdirAll(basePath, Perm)
|
||||
if err != nil {
|
||||
h.l.Warning("Failed to create directory: %s", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 取消上传凭证
|
||||
func (handler *Driver) CancelToken(ctx context.Context, uploadSession *fs.UploadSession) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (handler *Driver) CompleteUpload(ctx context.Context, session *fs.UploadSession) error {
|
||||
if session.Callback == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
if session.Policy.Edges.Node == nil {
|
||||
return serializer.NewError(serializer.CodeCallbackError, "Node not found", nil)
|
||||
}
|
||||
|
||||
// If callback is set, indicating this handler is used in slave node as a shadowed handler for remote policy,
|
||||
// we need to send callback request to master node.
|
||||
resp := handler.httpClient.Request(
|
||||
"POST",
|
||||
session.Callback,
|
||||
nil,
|
||||
request.WithTimeout(time.Duration(handler.config.Slave().CallbackTimeout)*time.Second),
|
||||
request.WithCredential(
|
||||
auth.HMACAuth{[]byte(session.Policy.Edges.Node.SlaveKey)},
|
||||
int64(handler.config.Slave().SignatureTTL),
|
||||
),
|
||||
request.WithContext(ctx),
|
||||
request.WithCorrelationID(),
|
||||
)
|
||||
|
||||
if resp.Err != nil {
|
||||
return serializer.NewError(serializer.CodeCallbackError, "Slave cannot send callback request", resp.Err)
|
||||
}
|
||||
|
||||
// 解析回调服务端响应
|
||||
res, err := resp.DecodeResponse()
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("Slave cannot parse callback response from master (StatusCode=%d).", resp.Response.StatusCode)
|
||||
return serializer.NewError(serializer.CodeCallbackError, msg, err)
|
||||
}
|
||||
|
||||
if res.Code != 0 {
|
||||
return serializer.NewError(res.Code, res.Msg, errors.New(res.Error))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (handler *Driver) Capabilities() *driver.Capabilities {
|
||||
return capabilities
|
||||
}
|
||||
|
||||
func (handler *Driver) MediaMeta(ctx context.Context, path, ext string) ([]driver.MediaMeta, error) {
|
||||
return nil, errors.New("not implemented")
|
||||
}
|
||||
Reference in New Issue
Block a user