feat: file proxy handle

This commit is contained in:
Noah Hsu
2022-06-28 21:58:46 +08:00
parent d1efec4539
commit 96380a50da
11 changed files with 167 additions and 90 deletions

View File

@ -85,7 +85,7 @@ func copyFileBetween2Accounts(tsk *task.Task[uint64], srcAccount, dstAccount dri
if err != nil {
return errors.WithMessagef(err, "failed get src [%s] file", srcFilePath)
}
link, err := operations.Link(tsk.Ctx, srcAccount, srcFilePath, model.LinkArgs{})
link, _, err := operations.Link(tsk.Ctx, srcAccount, srcFilePath, model.LinkArgs{})
if err != nil {
return errors.WithMessagef(err, "failed get [%s] link", srcFilePath)
}

View File

@ -30,13 +30,13 @@ func Get(ctx context.Context, path string) (model.Obj, error) {
return res, nil
}
func Link(ctx context.Context, path string, args model.LinkArgs) (*model.Link, error) {
res, err := link(ctx, path, args)
func Link(ctx context.Context, path string, args model.LinkArgs) (*model.Link, model.Obj, error) {
res, file, err := link(ctx, path, args)
if err != nil {
log.Errorf("failed link %s: %+v", path, err)
return nil, err
return nil, nil, err
}
return res, nil
return res, file, nil
}
func MakeDir(ctx context.Context, path string) error {

View File

@ -7,10 +7,10 @@ import (
"github.com/pkg/errors"
)
func link(ctx context.Context, path string, args model.LinkArgs) (*model.Link, error) {
func link(ctx context.Context, path string, args model.LinkArgs) (*model.Link, model.Obj, error) {
account, actualPath, err := operations.GetAccountAndActualPath(path)
if err != nil {
return nil, errors.WithMessage(err, "failed get account")
return nil, nil, errors.WithMessage(err, "failed get account")
}
return operations.Link(ctx, account, actualPath, args)
}

View File

@ -115,19 +115,19 @@ var linkCache = cache.NewMemCache(cache.WithShards[*model.Link](16))
var linkG singleflight.Group[*model.Link]
// Link get link, if is an url. should have an expiry time
func Link(ctx context.Context, account driver.Driver, path string, args model.LinkArgs) (*model.Link, error) {
func Link(ctx context.Context, account driver.Driver, path string, args model.LinkArgs) (*model.Link, model.Obj, error) {
file, err := Get(ctx, account, path)
if err != nil {
return nil, nil, errors.WithMessage(err, "failed to get file")
}
if file.IsDir() {
return nil, nil, errors.WithStack(errs.NotFile)
}
key := stdpath.Join(account.GetAccount().VirtualPath, path)
if link, ok := linkCache.Get(key); ok {
return link, nil
return link, file, nil
}
fn := func() (*model.Link, error) {
file, err := Get(ctx, account, path)
if err != nil {
return nil, errors.WithMessage(err, "failed to get file")
}
if file.IsDir() {
return nil, errors.WithStack(errs.NotFile)
}
link, err := account.Link(ctx, file, args)
if err != nil {
return nil, errors.WithMessage(err, "failed get link")
@ -138,7 +138,7 @@ func Link(ctx context.Context, account driver.Driver, path string, args model.Li
return link, nil
}
link, err, _ := linkG.Do(key, fn)
return link, err
return link, file, err
}
func MakeDir(ctx context.Context, account driver.Driver, path string) error {

View File

@ -10,6 +10,15 @@ import (
var once sync.Once
var instance sign.Sign
func Sign(data string) string {
expire := setting.GetIntSetting("link_expiration", 0)
if expire == 0 {
return NotExpired(data)
} else {
return WithDuration(data, time.Duration(expire)*time.Hour)
}
}
func WithDuration(data string, d time.Duration) string {
once.Do(Instance)
return instance.Sign(data, time.Now().Add(d).Unix())