2022-06-06 14:06:33 +00:00
|
|
|
package local
|
2022-06-07 10:13:55 +00:00
|
|
|
|
|
|
|
import (
|
feat: Crypt driver, improve http/webdav handling (#4884)
this PR has several enhancements, fixes, and features:
- [x] Crypt: a transparent encryption driver. Anyone can easily, and safely store encrypted data on the remote storage provider. Consider your data is safely stored in the safe, and the storage provider can only see the safe, but not your data.
- [x] Optional: compatible with [Rclone Crypt](https://rclone.org/crypt/). More ways to manipulate the encrypted data.
- [x] directory and filename encryption
- [x] server-side encryption mode (server encrypts & decrypts all data, all data flows thru the server)
- [x] obfuscate sensitive information internally
- [x] introduced a server memory-cached multi-thread downloader.
- [x] Driver: **Quark** enabled this feature, faster load in any single thread scenario. e.g. media player directly playing from the link, now it's faster.
- [x] general improvement on HTTP/WebDAV stream processing & header handling & response handling
- [x] Driver: **Mega** driver support ranged http header
- [x] Driver: **Quark** fix bug of not closing HTTP request to Quark server while user end has closed connection to alist
## Crypt, a transparent Encrypt/Decrypt Driver. (Rclone Crypt compatible)
e.g.
Crypt mount path -> /vault
Crypt remote path -> /ali/encrypted
Aliyun mount paht -> /ali
when the user uploads a.jpg to /vault, the data will be encrypted and saved to /ali/encrypted/xxxxx. And when the user wants to access a.jpg, it's automatically decrypted, and the user can do anything with it.
Since it's Rclone Crypt compatible, users can download /ali/encrypted/xxxxx and decrypt it with rclone crypt tool. Or the user can mount this folder using rclone, then mount the decrypted folder in Linux...
NB. Some breaking changes is made to make it follow global standard, e.g. processing the HTTP header properly.
close #4679
close #4827
Co-authored-by: Sean He <866155+seanhe26@users.noreply.github.com>
Co-authored-by: Andy Hsu <i@nn.ci>
2023-08-02 06:40:36 +00:00
|
|
|
"bytes"
|
2022-06-07 10:13:55 +00:00
|
|
|
"context"
|
2022-08-31 12:58:57 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-08-27 15:05:13 +00:00
|
|
|
"io/fs"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
stdpath "path"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-08-29 05:31:24 +00:00
|
|
|
"time"
|
2023-08-27 15:05:13 +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"
|
2023-01-16 11:55:43 +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"
|
2022-12-09 02:07:17 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/sign"
|
2022-06-07 10:13:55 +00:00
|
|
|
"github.com/alist-org/alist/v3/pkg/utils"
|
2023-02-26 13:25:32 +00:00
|
|
|
"github.com/alist-org/alist/v3/server/common"
|
2024-07-21 12:48:48 +00:00
|
|
|
"github.com/alist-org/times"
|
2024-11-01 15:31:33 +00:00
|
|
|
cp "github.com/otiai10/copy"
|
2023-08-27 15:05:13 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-11-29 01:47:40 +00:00
|
|
|
_ "golang.org/x/image/webp"
|
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
|
2023-02-26 13:25:32 +00:00
|
|
|
mkdirPerm int32
|
2024-09-03 12:03:30 +00:00
|
|
|
|
|
|
|
// zero means no limit
|
|
|
|
thumbConcurrency int
|
|
|
|
thumbTokenBucket TokenBucket
|
2022-06-07 10:13:55 +00:00
|
|
|
}
|
|
|
|
|
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-12-17 11:49:05 +00:00
|
|
|
func (d *Local) Init(ctx context.Context) error {
|
2023-02-26 13:25:32 +00:00
|
|
|
if d.MkdirPerm == "" {
|
|
|
|
d.mkdirPerm = 0777
|
|
|
|
} else {
|
|
|
|
v, err := strconv.ParseUint(d.MkdirPerm, 8, 32)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.mkdirPerm = int32(v)
|
2023-02-21 09:45:15 +00:00
|
|
|
}
|
2022-09-04 05:07:53 +00:00
|
|
|
if !utils.Exists(d.GetRootPath()) {
|
2022-12-17 11:49:05 +00:00
|
|
|
return fmt.Errorf("root folder %s not exists", d.GetRootPath())
|
|
|
|
}
|
|
|
|
if !filepath.IsAbs(d.GetRootPath()) {
|
|
|
|
abs, err := filepath.Abs(d.GetRootPath())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-06-27 12:37:05 +00:00
|
|
|
}
|
2022-12-17 11:49:05 +00:00
|
|
|
d.Addition.RootFolderPath = abs
|
2022-06-15 10:48:30 +00:00
|
|
|
}
|
2023-05-11 11:57:24 +00:00
|
|
|
if d.ThumbCacheFolder != "" && !utils.Exists(d.ThumbCacheFolder) {
|
|
|
|
err := os.MkdirAll(d.ThumbCacheFolder, os.FileMode(d.mkdirPerm))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-09-03 12:03:30 +00:00
|
|
|
if d.ThumbConcurrency != "" {
|
|
|
|
v, err := strconv.ParseUint(d.ThumbConcurrency, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.thumbConcurrency = int(v)
|
|
|
|
}
|
|
|
|
if d.thumbConcurrency == 0 {
|
|
|
|
d.thumbTokenBucket = NewNopTokenBucket()
|
|
|
|
} else {
|
2024-11-01 12:58:53 +00:00
|
|
|
d.thumbTokenBucket = NewStaticTokenBucketWithMigration(d.thumbTokenBucket, d.thumbConcurrency)
|
2024-09-03 12:03:30 +00:00
|
|
|
}
|
2025-01-10 12:48:45 +00:00
|
|
|
// Check the VideoThumbPos value
|
|
|
|
if d.VideoThumbPos == "" {
|
|
|
|
d.VideoThumbPos = "20%"
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(d.VideoThumbPos, "%") {
|
|
|
|
percentage := strings.TrimSuffix(d.VideoThumbPos, "%")
|
|
|
|
val, err := strconv.ParseFloat(percentage, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid video_thumb_pos value: %s, err: %s", d.VideoThumbPos, err)
|
|
|
|
}
|
|
|
|
if val < 0 || val > 100 {
|
|
|
|
return fmt.Errorf("invalid video_thumb_pos value: %s, the precentage must be a number between 0 and 100", d.VideoThumbPos)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
val, err := strconv.ParseFloat(d.VideoThumbPos, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid video_thumb_pos value: %s, err: %s", d.VideoThumbPos, err)
|
|
|
|
}
|
|
|
|
if val < 0 {
|
|
|
|
return fmt.Errorf("invalid video_thumb_pos value: %s, the time must be a positive number", d.VideoThumbPos)
|
|
|
|
}
|
|
|
|
}
|
2022-12-17 11:49:05 +00:00
|
|
|
return nil
|
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-12-13 10:03:30 +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()
|
2023-03-23 07:18:37 +00:00
|
|
|
rawFiles, err := readDir(fullPath)
|
2022-06-23 09:06:07 +00:00
|
|
|
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 {
|
2022-10-23 06:53:07 +00:00
|
|
|
if !d.ShowHidden && strings.HasPrefix(f.Name(), ".") {
|
2022-06-23 09:06:07 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-12-25 13:13:54 +00:00
|
|
|
file := d.FileInfoToObj(ctx, f, args.ReqPath, fullPath)
|
feat: Crypt driver, improve http/webdav handling (#4884)
this PR has several enhancements, fixes, and features:
- [x] Crypt: a transparent encryption driver. Anyone can easily, and safely store encrypted data on the remote storage provider. Consider your data is safely stored in the safe, and the storage provider can only see the safe, but not your data.
- [x] Optional: compatible with [Rclone Crypt](https://rclone.org/crypt/). More ways to manipulate the encrypted data.
- [x] directory and filename encryption
- [x] server-side encryption mode (server encrypts & decrypts all data, all data flows thru the server)
- [x] obfuscate sensitive information internally
- [x] introduced a server memory-cached multi-thread downloader.
- [x] Driver: **Quark** enabled this feature, faster load in any single thread scenario. e.g. media player directly playing from the link, now it's faster.
- [x] general improvement on HTTP/WebDAV stream processing & header handling & response handling
- [x] Driver: **Mega** driver support ranged http header
- [x] Driver: **Quark** fix bug of not closing HTTP request to Quark server while user end has closed connection to alist
## Crypt, a transparent Encrypt/Decrypt Driver. (Rclone Crypt compatible)
e.g.
Crypt mount path -> /vault
Crypt remote path -> /ali/encrypted
Aliyun mount paht -> /ali
when the user uploads a.jpg to /vault, the data will be encrypted and saved to /ali/encrypted/xxxxx. And when the user wants to access a.jpg, it's automatically decrypted, and the user can do anything with it.
Since it's Rclone Crypt compatible, users can download /ali/encrypted/xxxxx and decrypt it with rclone crypt tool. Or the user can mount this folder using rclone, then mount the decrypted folder in Linux...
NB. Some breaking changes is made to make it follow global standard, e.g. processing the HTTP header properly.
close #4679
close #4827
Co-authored-by: Sean He <866155+seanhe26@users.noreply.github.com>
Co-authored-by: Andy Hsu <i@nn.ci>
2023-08-02 06:40:36 +00:00
|
|
|
files = append(files, file)
|
2022-06-23 09:06:07 +00:00
|
|
|
}
|
|
|
|
return files, nil
|
2022-06-07 10:13:55 +00:00
|
|
|
}
|
2024-12-25 13:13:54 +00:00
|
|
|
func (d *Local) FileInfoToObj(ctx context.Context, f fs.FileInfo, reqPath string, fullPath string) model.Obj {
|
feat: Crypt driver, improve http/webdav handling (#4884)
this PR has several enhancements, fixes, and features:
- [x] Crypt: a transparent encryption driver. Anyone can easily, and safely store encrypted data on the remote storage provider. Consider your data is safely stored in the safe, and the storage provider can only see the safe, but not your data.
- [x] Optional: compatible with [Rclone Crypt](https://rclone.org/crypt/). More ways to manipulate the encrypted data.
- [x] directory and filename encryption
- [x] server-side encryption mode (server encrypts & decrypts all data, all data flows thru the server)
- [x] obfuscate sensitive information internally
- [x] introduced a server memory-cached multi-thread downloader.
- [x] Driver: **Quark** enabled this feature, faster load in any single thread scenario. e.g. media player directly playing from the link, now it's faster.
- [x] general improvement on HTTP/WebDAV stream processing & header handling & response handling
- [x] Driver: **Mega** driver support ranged http header
- [x] Driver: **Quark** fix bug of not closing HTTP request to Quark server while user end has closed connection to alist
## Crypt, a transparent Encrypt/Decrypt Driver. (Rclone Crypt compatible)
e.g.
Crypt mount path -> /vault
Crypt remote path -> /ali/encrypted
Aliyun mount paht -> /ali
when the user uploads a.jpg to /vault, the data will be encrypted and saved to /ali/encrypted/xxxxx. And when the user wants to access a.jpg, it's automatically decrypted, and the user can do anything with it.
Since it's Rclone Crypt compatible, users can download /ali/encrypted/xxxxx and decrypt it with rclone crypt tool. Or the user can mount this folder using rclone, then mount the decrypted folder in Linux...
NB. Some breaking changes is made to make it follow global standard, e.g. processing the HTTP header properly.
close #4679
close #4827
Co-authored-by: Sean He <866155+seanhe26@users.noreply.github.com>
Co-authored-by: Andy Hsu <i@nn.ci>
2023-08-02 06:40:36 +00:00
|
|
|
thumb := ""
|
|
|
|
if d.Thumbnail {
|
|
|
|
typeName := utils.GetFileType(f.Name())
|
|
|
|
if typeName == conf.IMAGE || typeName == conf.VIDEO {
|
2024-12-25 13:13:54 +00:00
|
|
|
thumb = common.GetApiUrl(common.GetHttpReq(ctx)) + stdpath.Join("/d", reqPath, f.Name())
|
feat: Crypt driver, improve http/webdav handling (#4884)
this PR has several enhancements, fixes, and features:
- [x] Crypt: a transparent encryption driver. Anyone can easily, and safely store encrypted data on the remote storage provider. Consider your data is safely stored in the safe, and the storage provider can only see the safe, but not your data.
- [x] Optional: compatible with [Rclone Crypt](https://rclone.org/crypt/). More ways to manipulate the encrypted data.
- [x] directory and filename encryption
- [x] server-side encryption mode (server encrypts & decrypts all data, all data flows thru the server)
- [x] obfuscate sensitive information internally
- [x] introduced a server memory-cached multi-thread downloader.
- [x] Driver: **Quark** enabled this feature, faster load in any single thread scenario. e.g. media player directly playing from the link, now it's faster.
- [x] general improvement on HTTP/WebDAV stream processing & header handling & response handling
- [x] Driver: **Mega** driver support ranged http header
- [x] Driver: **Quark** fix bug of not closing HTTP request to Quark server while user end has closed connection to alist
## Crypt, a transparent Encrypt/Decrypt Driver. (Rclone Crypt compatible)
e.g.
Crypt mount path -> /vault
Crypt remote path -> /ali/encrypted
Aliyun mount paht -> /ali
when the user uploads a.jpg to /vault, the data will be encrypted and saved to /ali/encrypted/xxxxx. And when the user wants to access a.jpg, it's automatically decrypted, and the user can do anything with it.
Since it's Rclone Crypt compatible, users can download /ali/encrypted/xxxxx and decrypt it with rclone crypt tool. Or the user can mount this folder using rclone, then mount the decrypted folder in Linux...
NB. Some breaking changes is made to make it follow global standard, e.g. processing the HTTP header properly.
close #4679
close #4827
Co-authored-by: Sean He <866155+seanhe26@users.noreply.github.com>
Co-authored-by: Andy Hsu <i@nn.ci>
2023-08-02 06:40:36 +00:00
|
|
|
thumb = utils.EncodePath(thumb, true)
|
|
|
|
thumb += "?type=thumb&sign=" + sign.Sign(stdpath.Join(reqPath, f.Name()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
isFolder := f.IsDir() || isSymlinkDir(f, fullPath)
|
|
|
|
var size int64
|
|
|
|
if !isFolder {
|
|
|
|
size = f.Size()
|
|
|
|
}
|
2023-08-29 05:31:24 +00:00
|
|
|
var ctime time.Time
|
2023-08-27 13:14:23 +00:00
|
|
|
t, err := times.Stat(stdpath.Join(fullPath, f.Name()))
|
|
|
|
if err == nil {
|
|
|
|
if t.HasBirthTime() {
|
|
|
|
ctime = t.BirthTime()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
feat: Crypt driver, improve http/webdav handling (#4884)
this PR has several enhancements, fixes, and features:
- [x] Crypt: a transparent encryption driver. Anyone can easily, and safely store encrypted data on the remote storage provider. Consider your data is safely stored in the safe, and the storage provider can only see the safe, but not your data.
- [x] Optional: compatible with [Rclone Crypt](https://rclone.org/crypt/). More ways to manipulate the encrypted data.
- [x] directory and filename encryption
- [x] server-side encryption mode (server encrypts & decrypts all data, all data flows thru the server)
- [x] obfuscate sensitive information internally
- [x] introduced a server memory-cached multi-thread downloader.
- [x] Driver: **Quark** enabled this feature, faster load in any single thread scenario. e.g. media player directly playing from the link, now it's faster.
- [x] general improvement on HTTP/WebDAV stream processing & header handling & response handling
- [x] Driver: **Mega** driver support ranged http header
- [x] Driver: **Quark** fix bug of not closing HTTP request to Quark server while user end has closed connection to alist
## Crypt, a transparent Encrypt/Decrypt Driver. (Rclone Crypt compatible)
e.g.
Crypt mount path -> /vault
Crypt remote path -> /ali/encrypted
Aliyun mount paht -> /ali
when the user uploads a.jpg to /vault, the data will be encrypted and saved to /ali/encrypted/xxxxx. And when the user wants to access a.jpg, it's automatically decrypted, and the user can do anything with it.
Since it's Rclone Crypt compatible, users can download /ali/encrypted/xxxxx and decrypt it with rclone crypt tool. Or the user can mount this folder using rclone, then mount the decrypted folder in Linux...
NB. Some breaking changes is made to make it follow global standard, e.g. processing the HTTP header properly.
close #4679
close #4827
Co-authored-by: Sean He <866155+seanhe26@users.noreply.github.com>
Co-authored-by: Andy Hsu <i@nn.ci>
2023-08-02 06:40:36 +00:00
|
|
|
file := model.ObjThumb{
|
|
|
|
Object: model.Object{
|
|
|
|
Path: filepath.Join(fullPath, f.Name()),
|
|
|
|
Name: f.Name(),
|
|
|
|
Modified: f.ModTime(),
|
|
|
|
Size: size,
|
|
|
|
IsFolder: isFolder,
|
2023-08-27 13:14:23 +00:00
|
|
|
Ctime: ctime,
|
feat: Crypt driver, improve http/webdav handling (#4884)
this PR has several enhancements, fixes, and features:
- [x] Crypt: a transparent encryption driver. Anyone can easily, and safely store encrypted data on the remote storage provider. Consider your data is safely stored in the safe, and the storage provider can only see the safe, but not your data.
- [x] Optional: compatible with [Rclone Crypt](https://rclone.org/crypt/). More ways to manipulate the encrypted data.
- [x] directory and filename encryption
- [x] server-side encryption mode (server encrypts & decrypts all data, all data flows thru the server)
- [x] obfuscate sensitive information internally
- [x] introduced a server memory-cached multi-thread downloader.
- [x] Driver: **Quark** enabled this feature, faster load in any single thread scenario. e.g. media player directly playing from the link, now it's faster.
- [x] general improvement on HTTP/WebDAV stream processing & header handling & response handling
- [x] Driver: **Mega** driver support ranged http header
- [x] Driver: **Quark** fix bug of not closing HTTP request to Quark server while user end has closed connection to alist
## Crypt, a transparent Encrypt/Decrypt Driver. (Rclone Crypt compatible)
e.g.
Crypt mount path -> /vault
Crypt remote path -> /ali/encrypted
Aliyun mount paht -> /ali
when the user uploads a.jpg to /vault, the data will be encrypted and saved to /ali/encrypted/xxxxx. And when the user wants to access a.jpg, it's automatically decrypted, and the user can do anything with it.
Since it's Rclone Crypt compatible, users can download /ali/encrypted/xxxxx and decrypt it with rclone crypt tool. Or the user can mount this folder using rclone, then mount the decrypted folder in Linux...
NB. Some breaking changes is made to make it follow global standard, e.g. processing the HTTP header properly.
close #4679
close #4827
Co-authored-by: Sean He <866155+seanhe26@users.noreply.github.com>
Co-authored-by: Andy Hsu <i@nn.ci>
2023-08-02 06:40:36 +00:00
|
|
|
},
|
|
|
|
Thumbnail: model.Thumbnail{
|
|
|
|
Thumbnail: thumb,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return &file
|
|
|
|
}
|
|
|
|
func (d *Local) GetMeta(ctx context.Context, path string) (model.Obj, error) {
|
|
|
|
f, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-12-25 13:13:54 +00:00
|
|
|
file := d.FileInfoToObj(ctx, f, path, path)
|
feat: Crypt driver, improve http/webdav handling (#4884)
this PR has several enhancements, fixes, and features:
- [x] Crypt: a transparent encryption driver. Anyone can easily, and safely store encrypted data on the remote storage provider. Consider your data is safely stored in the safe, and the storage provider can only see the safe, but not your data.
- [x] Optional: compatible with [Rclone Crypt](https://rclone.org/crypt/). More ways to manipulate the encrypted data.
- [x] directory and filename encryption
- [x] server-side encryption mode (server encrypts & decrypts all data, all data flows thru the server)
- [x] obfuscate sensitive information internally
- [x] introduced a server memory-cached multi-thread downloader.
- [x] Driver: **Quark** enabled this feature, faster load in any single thread scenario. e.g. media player directly playing from the link, now it's faster.
- [x] general improvement on HTTP/WebDAV stream processing & header handling & response handling
- [x] Driver: **Mega** driver support ranged http header
- [x] Driver: **Quark** fix bug of not closing HTTP request to Quark server while user end has closed connection to alist
## Crypt, a transparent Encrypt/Decrypt Driver. (Rclone Crypt compatible)
e.g.
Crypt mount path -> /vault
Crypt remote path -> /ali/encrypted
Aliyun mount paht -> /ali
when the user uploads a.jpg to /vault, the data will be encrypted and saved to /ali/encrypted/xxxxx. And when the user wants to access a.jpg, it's automatically decrypted, and the user can do anything with it.
Since it's Rclone Crypt compatible, users can download /ali/encrypted/xxxxx and decrypt it with rclone crypt tool. Or the user can mount this folder using rclone, then mount the decrypted folder in Linux...
NB. Some breaking changes is made to make it follow global standard, e.g. processing the HTTP header properly.
close #4679
close #4827
Co-authored-by: Sean He <866155+seanhe26@users.noreply.github.com>
Co-authored-by: Andy Hsu <i@nn.ci>
2023-08-02 06:40:36 +00:00
|
|
|
//h := "123123"
|
|
|
|
//if s, ok := f.(model.SetHash); ok && file.GetHash() == ("","") {
|
|
|
|
// s.SetHash(h,"SHA1")
|
|
|
|
//}
|
|
|
|
return file, nil
|
|
|
|
|
|
|
|
}
|
2022-06-07 10:13:55 +00:00
|
|
|
|
2023-01-16 11:55:43 +00:00
|
|
|
func (d *Local) Get(ctx context.Context, path string) (model.Obj, error) {
|
|
|
|
path = filepath.Join(d.GetRootPath(), path)
|
|
|
|
f, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), "cannot find the file") {
|
|
|
|
return nil, errs.ObjectNotFound
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
isFolder := f.IsDir() || isSymlinkDir(f, path)
|
|
|
|
size := f.Size()
|
|
|
|
if isFolder {
|
|
|
|
size = 0
|
|
|
|
}
|
2023-08-29 05:31:24 +00:00
|
|
|
var ctime time.Time
|
|
|
|
t, err := times.Stat(path)
|
|
|
|
if err == nil {
|
|
|
|
if t.HasBirthTime() {
|
|
|
|
ctime = t.BirthTime()
|
|
|
|
}
|
|
|
|
}
|
2023-01-16 11:55:43 +00:00
|
|
|
file := model.Object{
|
|
|
|
Path: path,
|
|
|
|
Name: f.Name(),
|
|
|
|
Modified: f.ModTime(),
|
2023-08-29 05:31:24 +00:00
|
|
|
Ctime: ctime,
|
2023-01-16 11:55:43 +00:00
|
|
|
Size: size,
|
|
|
|
IsFolder: isFolder,
|
|
|
|
}
|
|
|
|
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" {
|
2024-09-03 12:03:30 +00:00
|
|
|
var buf *bytes.Buffer
|
|
|
|
var thumbPath *string
|
|
|
|
err := d.thumbTokenBucket.Do(ctx, func() error {
|
|
|
|
var err error
|
|
|
|
buf, thumbPath, err = d.getThumb(file)
|
|
|
|
return err
|
|
|
|
})
|
2022-08-11 12:32:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
link.Header = http.Header{
|
2023-05-11 11:57:24 +00:00
|
|
|
"Content-Type": []string{"image/png"},
|
|
|
|
}
|
|
|
|
if thumbPath != nil {
|
feat: Crypt driver, improve http/webdav handling (#4884)
this PR has several enhancements, fixes, and features:
- [x] Crypt: a transparent encryption driver. Anyone can easily, and safely store encrypted data on the remote storage provider. Consider your data is safely stored in the safe, and the storage provider can only see the safe, but not your data.
- [x] Optional: compatible with [Rclone Crypt](https://rclone.org/crypt/). More ways to manipulate the encrypted data.
- [x] directory and filename encryption
- [x] server-side encryption mode (server encrypts & decrypts all data, all data flows thru the server)
- [x] obfuscate sensitive information internally
- [x] introduced a server memory-cached multi-thread downloader.
- [x] Driver: **Quark** enabled this feature, faster load in any single thread scenario. e.g. media player directly playing from the link, now it's faster.
- [x] general improvement on HTTP/WebDAV stream processing & header handling & response handling
- [x] Driver: **Mega** driver support ranged http header
- [x] Driver: **Quark** fix bug of not closing HTTP request to Quark server while user end has closed connection to alist
## Crypt, a transparent Encrypt/Decrypt Driver. (Rclone Crypt compatible)
e.g.
Crypt mount path -> /vault
Crypt remote path -> /ali/encrypted
Aliyun mount paht -> /ali
when the user uploads a.jpg to /vault, the data will be encrypted and saved to /ali/encrypted/xxxxx. And when the user wants to access a.jpg, it's automatically decrypted, and the user can do anything with it.
Since it's Rclone Crypt compatible, users can download /ali/encrypted/xxxxx and decrypt it with rclone crypt tool. Or the user can mount this folder using rclone, then mount the decrypted folder in Linux...
NB. Some breaking changes is made to make it follow global standard, e.g. processing the HTTP header properly.
close #4679
close #4827
Co-authored-by: Sean He <866155+seanhe26@users.noreply.github.com>
Co-authored-by: Andy Hsu <i@nn.ci>
2023-08-02 06:40:36 +00:00
|
|
|
open, err := os.Open(*thumbPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-27 13:14:23 +00:00
|
|
|
link.MFile = open
|
2023-05-11 11:57:24 +00:00
|
|
|
} else {
|
2023-08-27 13:14:23 +00:00
|
|
|
link.MFile = model.NewNopMFile(bytes.NewReader(buf.Bytes()))
|
feat: Crypt driver, improve http/webdav handling (#4884)
this PR has several enhancements, fixes, and features:
- [x] Crypt: a transparent encryption driver. Anyone can easily, and safely store encrypted data on the remote storage provider. Consider your data is safely stored in the safe, and the storage provider can only see the safe, but not your data.
- [x] Optional: compatible with [Rclone Crypt](https://rclone.org/crypt/). More ways to manipulate the encrypted data.
- [x] directory and filename encryption
- [x] server-side encryption mode (server encrypts & decrypts all data, all data flows thru the server)
- [x] obfuscate sensitive information internally
- [x] introduced a server memory-cached multi-thread downloader.
- [x] Driver: **Quark** enabled this feature, faster load in any single thread scenario. e.g. media player directly playing from the link, now it's faster.
- [x] general improvement on HTTP/WebDAV stream processing & header handling & response handling
- [x] Driver: **Mega** driver support ranged http header
- [x] Driver: **Quark** fix bug of not closing HTTP request to Quark server while user end has closed connection to alist
## Crypt, a transparent Encrypt/Decrypt Driver. (Rclone Crypt compatible)
e.g.
Crypt mount path -> /vault
Crypt remote path -> /ali/encrypted
Aliyun mount paht -> /ali
when the user uploads a.jpg to /vault, the data will be encrypted and saved to /ali/encrypted/xxxxx. And when the user wants to access a.jpg, it's automatically decrypted, and the user can do anything with it.
Since it's Rclone Crypt compatible, users can download /ali/encrypted/xxxxx and decrypt it with rclone crypt tool. Or the user can mount this folder using rclone, then mount the decrypted folder in Linux...
NB. Some breaking changes is made to make it follow global standard, e.g. processing the HTTP header properly.
close #4679
close #4827
Co-authored-by: Sean He <866155+seanhe26@users.noreply.github.com>
Co-authored-by: Andy Hsu <i@nn.ci>
2023-08-02 06:40:36 +00:00
|
|
|
//link.Header.Set("Content-Length", strconv.Itoa(buf.Len()))
|
2022-08-11 12:32:17 +00:00
|
|
|
}
|
|
|
|
} else {
|
feat: Crypt driver, improve http/webdav handling (#4884)
this PR has several enhancements, fixes, and features:
- [x] Crypt: a transparent encryption driver. Anyone can easily, and safely store encrypted data on the remote storage provider. Consider your data is safely stored in the safe, and the storage provider can only see the safe, but not your data.
- [x] Optional: compatible with [Rclone Crypt](https://rclone.org/crypt/). More ways to manipulate the encrypted data.
- [x] directory and filename encryption
- [x] server-side encryption mode (server encrypts & decrypts all data, all data flows thru the server)
- [x] obfuscate sensitive information internally
- [x] introduced a server memory-cached multi-thread downloader.
- [x] Driver: **Quark** enabled this feature, faster load in any single thread scenario. e.g. media player directly playing from the link, now it's faster.
- [x] general improvement on HTTP/WebDAV stream processing & header handling & response handling
- [x] Driver: **Mega** driver support ranged http header
- [x] Driver: **Quark** fix bug of not closing HTTP request to Quark server while user end has closed connection to alist
## Crypt, a transparent Encrypt/Decrypt Driver. (Rclone Crypt compatible)
e.g.
Crypt mount path -> /vault
Crypt remote path -> /ali/encrypted
Aliyun mount paht -> /ali
when the user uploads a.jpg to /vault, the data will be encrypted and saved to /ali/encrypted/xxxxx. And when the user wants to access a.jpg, it's automatically decrypted, and the user can do anything with it.
Since it's Rclone Crypt compatible, users can download /ali/encrypted/xxxxx and decrypt it with rclone crypt tool. Or the user can mount this folder using rclone, then mount the decrypted folder in Linux...
NB. Some breaking changes is made to make it follow global standard, e.g. processing the HTTP header properly.
close #4679
close #4827
Co-authored-by: Sean He <866155+seanhe26@users.noreply.github.com>
Co-authored-by: Andy Hsu <i@nn.ci>
2023-08-02 06:40:36 +00:00
|
|
|
open, err := os.Open(fullPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-27 13:14:23 +00:00
|
|
|
link.MFile = open
|
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)
|
2023-02-26 13:25:32 +00:00
|
|
|
err := os.MkdirAll(fullPath, os.FileMode(d.mkdirPerm))
|
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 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-12-20 08:27:04 +00:00
|
|
|
if utils.IsSubPath(srcPath, dstPath) {
|
|
|
|
return fmt.Errorf("the destination folder is a subfolder of the source folder")
|
|
|
|
}
|
2024-11-01 15:31:33 +00:00
|
|
|
if err := os.Rename(srcPath, dstPath); err != nil && strings.Contains(err.Error(), "invalid cross-device link") {
|
|
|
|
// Handle cross-device file move in local driver
|
|
|
|
if err = d.Copy(ctx, srcObj, dstDir); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
// Directly remove file without check recycle bin if successfully copied
|
|
|
|
if srcObj.IsDir() {
|
|
|
|
err = os.RemoveAll(srcObj.GetPath())
|
|
|
|
} else {
|
|
|
|
err = os.Remove(srcObj.GetPath())
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2022-08-31 12:58:57 +00:00
|
|
|
return err
|
2022-06-23 09:06:07 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2024-11-01 15:31:33 +00:00
|
|
|
func (d *Local) Copy(_ 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-12-20 08:27:04 +00:00
|
|
|
if utils.IsSubPath(srcPath, dstPath) {
|
|
|
|
return fmt.Errorf("the destination folder is a subfolder of the source folder")
|
|
|
|
}
|
2024-11-01 15:31:33 +00:00
|
|
|
// Copy using otiai10/copy to perform more secure & efficient copy
|
|
|
|
return cp.Copy(srcPath, dstPath, cp.Options{
|
|
|
|
Sync: true, // Sync file to disk after copy, may have performance penalty in filesystem such as ZFS
|
|
|
|
PreserveTimes: true,
|
2024-11-21 14:38:41 +00:00
|
|
|
PreserveOwner: true,
|
2024-11-01 15:31:33 +00:00
|
|
|
})
|
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
|
2024-01-09 10:51:21 +00:00
|
|
|
if utils.SliceContains([]string{"", "delete permanently"}, d.RecycleBinPath) {
|
|
|
|
if obj.IsDir() {
|
|
|
|
err = os.RemoveAll(obj.GetPath())
|
|
|
|
} else {
|
|
|
|
err = os.Remove(obj.GetPath())
|
|
|
|
}
|
2022-06-23 09:06:07 +00:00
|
|
|
} else {
|
2024-01-09 10:51:21 +00:00
|
|
|
dstPath := filepath.Join(d.RecycleBinPath, obj.GetName())
|
|
|
|
if utils.Exists(dstPath) {
|
|
|
|
dstPath = filepath.Join(d.RecycleBinPath, obj.GetName()+"_"+time.Now().Format("20060102150405"))
|
|
|
|
}
|
|
|
|
err = os.Rename(obj.GetPath(), dstPath)
|
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
|
|
|
}
|
2023-08-27 15:05:13 +00:00
|
|
|
err = os.Chtimes(fullPath, stream.ModTime(), stream.ModTime())
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("[local] failed to change time of %s: %s", fullPath, 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
|
|
|
var _ driver.Driver = (*Local)(nil)
|