alist/drivers/local/driver.go

226 lines
5.1 KiB
Go
Raw Normal View History

2022-06-06 14:06:33 +00:00
package local
2022-06-07 10:13:55 +00:00
import (
2022-08-11 12:32:17 +00:00
"bytes"
2022-06-07 10:13:55 +00:00
"context"
2022-08-31 12:58:57 +00:00
"errors"
"fmt"
2022-08-11 12:32:17 +00:00
"io"
2022-06-23 09:06:07 +00:00
"io/ioutil"
2022-08-11 12:32:17 +00:00
"net/http"
2022-06-23 09:06:07 +00:00
"os"
2022-08-11 12:32:17 +00:00
stdpath "path"
2022-06-23 09:06:07 +00:00
"path/filepath"
2022-08-11 12:32:17 +00:00
"strconv"
2022-06-23 09:06:07 +00:00
"strings"
2022-06-14 11:44:25 +00:00
2022-08-11 12:32:17 +00:00
"github.com/alist-org/alist/v3/internal/conf"
2022-06-07 10:13:55 +00:00
"github.com/alist-org/alist/v3/internal/driver"
2022-08-03 06:26:59 +00:00
"github.com/alist-org/alist/v3/internal/errs"
2022-06-07 10:13:55 +00:00
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/op"
2022-06-07 10:13:55 +00:00
"github.com/alist-org/alist/v3/pkg/utils"
2022-08-11 12:32:17 +00:00
"github.com/alist-org/alist/v3/server/common"
"github.com/disintegration/imaging"
2022-06-07 10:13:55 +00:00
)
2022-07-19 09:11:53 +00:00
type Local struct {
2022-07-10 06:45:39 +00:00
model.Storage
2022-06-07 10:13:55 +00:00
Addition
}
2022-07-19 09:11:53 +00:00
func (d *Local) Config() driver.Config {
2022-06-07 10:13:55 +00:00
return config
}
2022-07-19 09:11:53 +00:00
func (d *Local) Init(ctx context.Context, storage model.Storage) error {
2022-07-10 06:45:39 +00:00
d.Storage = storage
err := utils.Json.UnmarshalFromString(d.Storage.Addition, &d.Addition)
2022-06-07 10:13:55 +00:00
if err != nil {
2022-08-31 12:58:57 +00:00
return err
2022-06-07 10:13:55 +00:00
}
if !utils.Exists(d.RootFolder) {
2022-08-31 12:58:57 +00:00
err = fmt.Errorf("root folder %s not exists", d.RootFolder)
} else {
2022-06-27 12:37:05 +00:00
if !filepath.IsAbs(d.RootFolder) {
d.RootFolder, err = filepath.Abs(d.RootFolder)
if err != nil {
2022-08-31 12:58:57 +00:00
return err
2022-06-27 12:37:05 +00:00
}
}
}
op.MustSaveDriverStorage(d)
return err
2022-06-07 10:13:55 +00:00
}
2022-07-19 09:11:53 +00:00
func (d *Local) Drop(ctx context.Context) error {
2022-06-07 14:02:41 +00:00
return nil
2022-06-07 10:13:55 +00:00
}
2022-07-19 09:11:53 +00:00
func (d *Local) GetAddition() driver.Additional {
2022-06-08 08:20:58 +00:00
return d.Addition
2022-06-07 10:13:55 +00:00
}
2022-08-11 12:32:17 +00:00
func (d *Local) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
2022-09-02 14:46:31 +00:00
fullPath := dir.GetPath()
2022-06-23 09:06:07 +00:00
rawFiles, err := ioutil.ReadDir(fullPath)
if err != nil {
2022-08-31 12:58:57 +00:00
return nil, err
2022-06-23 09:06:07 +00:00
}
var files []model.Obj
for _, f := range rawFiles {
if strings.HasPrefix(f.Name(), ".") {
continue
}
2022-08-11 12:32:17 +00:00
thumb := ""
if d.Thumbnail && utils.GetFileType(f.Name()) == conf.IMAGE {
thumb = common.GetApiUrl(nil) + stdpath.Join("/d", args.ReqPath, f.Name())
thumb = utils.EncodePath(thumb, true)
thumb += "?type=thumb"
}
2022-09-02 10:24:14 +00:00
file := model.ObjThumb{
2022-08-11 12:32:17 +00:00
Object: model.Object{
Name: f.Name(),
Modified: f.ModTime(),
Size: f.Size(),
IsFolder: f.IsDir(),
},
Thumbnail: model.Thumbnail{
Thumbnail: thumb,
},
2022-06-23 09:06:07 +00:00
}
files = append(files, &file)
}
return files, nil
2022-06-07 10:13:55 +00:00
}
2022-07-19 09:11:53 +00:00
func (d *Local) Get(ctx context.Context, path string) (model.Obj, error) {
2022-06-28 14:13:47 +00:00
f, err := os.Stat(path)
if err != nil {
if strings.Contains(err.Error(), "cannot find the file") {
2022-08-31 12:58:57 +00:00
return nil, errs.ObjectNotFound
}
2022-08-31 12:58:57 +00:00
return nil, err
2022-06-28 14:13:47 +00:00
}
file := model.Object{
2022-09-02 14:46:31 +00:00
Path: path,
2022-06-28 14:13:47 +00:00
Name: f.Name(),
Modified: f.ModTime(),
Size: f.Size(),
IsFolder: f.IsDir(),
}
return &file, nil
}
2022-07-19 09:11:53 +00:00
func (d *Local) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
2022-09-02 14:46:31 +00:00
fullPath := file.GetPath()
2022-08-11 12:32:17 +00:00
var link model.Link
if args.Type == "thumb" && utils.Ext(file.GetName()) != "svg" {
imgData, err := ioutil.ReadFile(fullPath)
if err != nil {
return nil, err
}
srcBuf := bytes.NewBuffer(imgData)
image, err := imaging.Decode(srcBuf)
if err != nil {
return nil, err
}
thumbImg := imaging.Resize(image, 144, 0, imaging.Lanczos)
var buf bytes.Buffer
err = imaging.Encode(&buf, thumbImg, imaging.PNG)
if err != nil {
return nil, err
}
size := buf.Len()
link.Data = io.NopCloser(&buf)
link.Header = http.Header{
"Content-Length": []string{strconv.Itoa(size)},
}
} else {
link.FilePath = &fullPath
2022-06-23 09:06:07 +00:00
}
return &link, nil
2022-06-07 10:13:55 +00:00
}
2022-07-19 09:11:53 +00:00
func (d *Local) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
2022-09-02 14:46:31 +00:00
fullPath := filepath.Join(parentDir.GetPath(), dirName)
2022-06-23 09:06:07 +00:00
err := os.MkdirAll(fullPath, 0700)
if err != nil {
2022-08-31 12:58:57 +00:00
return err
2022-06-23 09:06:07 +00:00
}
return nil
2022-06-07 14:02:41 +00:00
}
2022-07-19 09:11:53 +00:00
func (d *Local) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
2022-09-02 14:46:31 +00:00
srcPath := srcObj.GetPath()
dstPath := filepath.Join(dstDir.GetPath(), srcObj.GetName())
2022-06-23 09:06:07 +00:00
err := os.Rename(srcPath, dstPath)
if err != nil {
2022-08-31 12:58:57 +00:00
return err
2022-06-23 09:06:07 +00:00
}
return nil
2022-06-07 10:13:55 +00:00
}
2022-07-19 09:11:53 +00:00
func (d *Local) Rename(ctx context.Context, srcObj model.Obj, newName string) error {
2022-09-02 14:46:31 +00:00
srcPath := srcObj.GetPath()
2022-06-23 09:06:07 +00:00
dstPath := filepath.Join(filepath.Dir(srcPath), newName)
err := os.Rename(srcPath, dstPath)
if err != nil {
2022-08-31 12:58:57 +00:00
return err
2022-06-23 09:06:07 +00:00
}
return nil
2022-06-07 10:13:55 +00:00
}
2022-07-19 09:11:53 +00:00
func (d *Local) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
2022-09-02 14:46:31 +00:00
srcPath := srcObj.GetPath()
dstPath := filepath.Join(dstDir.GetPath(), srcObj.GetName())
2022-06-23 09:06:07 +00:00
var err error
if srcObj.IsDir() {
err = copyDir(srcPath, dstPath)
} else {
err = copyFile(srcPath, dstPath)
}
if err != nil {
2022-08-31 12:58:57 +00:00
return err
2022-06-23 09:06:07 +00:00
}
return nil
2022-06-07 10:13:55 +00:00
}
2022-07-19 09:11:53 +00:00
func (d *Local) Remove(ctx context.Context, obj model.Obj) error {
2022-06-23 09:06:07 +00:00
var err error
if obj.IsDir() {
2022-09-02 14:46:31 +00:00
err = os.RemoveAll(obj.GetPath())
2022-06-23 09:06:07 +00:00
} else {
2022-09-02 14:46:31 +00:00
err = os.Remove(obj.GetPath())
2022-06-23 09:06:07 +00:00
}
if err != nil {
2022-08-31 12:58:57 +00:00
return err
2022-06-23 09:06:07 +00:00
}
return nil
2022-06-07 10:13:55 +00:00
}
2022-07-19 09:11:53 +00:00
func (d *Local) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
2022-09-02 14:46:31 +00:00
fullPath := filepath.Join(dstDir.GetPath(), stream.GetName())
2022-06-23 09:06:07 +00:00
out, err := os.Create(fullPath)
if err != nil {
2022-08-31 12:58:57 +00:00
return err
2022-06-23 09:06:07 +00:00
}
defer func() {
_ = out.Close()
2022-06-25 07:14:03 +00:00
if errors.Is(err, context.Canceled) {
_ = os.Remove(fullPath)
}
2022-06-23 09:06:07 +00:00
}()
2022-08-31 14:41:27 +00:00
err = utils.CopyWithCtx(ctx, out, stream, stream.GetSize(), up)
2022-06-23 09:06:07 +00:00
if err != nil {
2022-08-31 12:58:57 +00:00
return err
2022-06-23 09:06:07 +00:00
}
return nil
2022-06-07 10:13:55 +00:00
}
2022-08-03 06:14:37 +00:00
func (d *Local) Other(ctx context.Context, args model.OtherArgs) (interface{}, error) {
2022-06-23 09:06:07 +00:00
return nil, errs.NotSupport
2022-06-07 10:13:55 +00:00
}
2022-07-19 09:11:53 +00:00
var _ driver.Driver = (*Local)(nil)