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

@ -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 {