fix(copy): copy from driver that return writer (close #4291)

This commit is contained in:
Andy Hsu
2023-05-26 21:54:57 +08:00
parent 6b97b4eb20
commit b2f5757f8d
7 changed files with 91 additions and 77 deletions

View File

@ -3,6 +3,7 @@ package fs
import (
"context"
"fmt"
"net/http"
stdpath "path"
"sync/atomic"
@ -87,7 +88,9 @@ func copyFileBetween2Storages(tsk *task.Task[uint64], srcStorage, dstStorage dri
if err != nil {
return errors.WithMessagef(err, "failed get src [%s] file", srcFilePath)
}
link, _, err := op.Link(tsk.Ctx, srcStorage, srcFilePath, model.LinkArgs{})
link, _, err := op.Link(tsk.Ctx, srcStorage, srcFilePath, model.LinkArgs{
Header: http.Header{},
})
if err != nil {
return errors.WithMessagef(err, "failed get [%s] link", srcFilePath)
}

View File

@ -10,30 +10,13 @@ import (
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/op"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/alist-org/alist/v3/server/common"
"github.com/google/uuid"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
func ClearCache(path string) {
storage, actualPath, err := op.GetStorageAndActualPath(path)
if err != nil {
return
}
op.ClearCache(storage, actualPath)
}
func containsByName(files []model.Obj, file model.Obj) bool {
for _, f := range files {
if f.GetName() == file.GetName() {
return true
}
}
return false
}
func getFileStreamFromLink(file model.Obj, link *model.Link) (*model.FileStream, error) {
var rc io.ReadCloser
mimetype := utils.GetMimeType(file.GetName())
@ -51,6 +34,16 @@ func getFileStreamFromLink(file model.Obj, link *model.Link) (*model.FileStream,
return nil, errors.Wrapf(err, "failed to open file %s", *link.FilePath)
}
rc = f
} else if link.Writer != nil {
r, w := io.Pipe()
go func() {
err := link.Writer(w)
err = w.CloseWithError(err)
if err != nil {
log.Errorf("[getFileStreamFromLink] failed to write: %v", err)
}
}()
rc = r
} else {
req, err := http.NewRequest(http.MethodGet, link.URL, nil)
if err != nil {

View File

@ -18,13 +18,14 @@ type LinkArgs struct {
}
type Link struct {
URL string `json:"url"`
Header http.Header `json:"header"` // needed header
Data io.ReadCloser // return file reader directly
Status int // status maybe 200 or 206, etc
FilePath *string // local file, return the filepath
Expiration *time.Duration // url expiration time
Handle func(w http.ResponseWriter, r *http.Request) error `json:"-"` // custom handler
URL string `json:"url"`
Header http.Header `json:"header"` // needed header
Data io.ReadCloser // return file reader directly
Status int // status maybe 200 or 206, etc
FilePath *string // local file, return the filepath
Expiration *time.Duration // url expiration time
//Handle func(w http.ResponseWriter, r *http.Request) error `json:"-"` // custom handler
Writer WriterFunc `json:"-"` // custom writer
}
type OtherArgs struct {
@ -38,3 +39,5 @@ type FsOtherArgs struct {
Method string `json:"method" form:"method"`
Data interface{} `json:"data" form:"data"`
}
type WriterFunc func(w io.Writer) error