fix: put as task from web
This commit is contained in:
parent
4d0ae6b1ef
commit
4340a48633
@ -147,7 +147,7 @@ func (m *Monitor) Complete() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "failed to open file %s", file.Path)
|
return errors.Wrapf(err, "failed to open file %s", file.Path)
|
||||||
}
|
}
|
||||||
stream := model.FileStream{
|
stream := &model.FileStream{
|
||||||
Obj: model.Object{
|
Obj: model.Object{
|
||||||
Name: path.Base(file.Path),
|
Name: path.Base(file.Path),
|
||||||
Size: size,
|
Size: size,
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/alist-org/alist/v3/internal/model"
|
"github.com/alist-org/alist/v3/internal/model"
|
||||||
"github.com/alist-org/alist/v3/internal/operations"
|
"github.com/alist-org/alist/v3/internal/operations"
|
||||||
"github.com/alist-org/alist/v3/pkg/task"
|
"github.com/alist-org/alist/v3/pkg/task"
|
||||||
|
"github.com/alist-org/alist/v3/pkg/utils"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
)
|
)
|
||||||
@ -24,6 +25,13 @@ func putAsTask(dstDirPath string, file model.FileStreamer) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.WithMessage(err, "failed get account")
|
return errors.WithMessage(err, "failed get account")
|
||||||
}
|
}
|
||||||
|
if file.NeedStore() {
|
||||||
|
tempFile, err := utils.CreateTempFile(file)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "failed to create temp file")
|
||||||
|
}
|
||||||
|
file.SetReadCloser(tempFile)
|
||||||
|
}
|
||||||
UploadTaskManager.Submit(task.WithCancelCtx(&task.Task[uint64]{
|
UploadTaskManager.Submit(task.WithCancelCtx(&task.Task[uint64]{
|
||||||
Name: fmt.Sprintf("upload %s to [%s](%s)", file.GetName(), account.GetAccount().VirtualPath, dstDirActualPath),
|
Name: fmt.Sprintf("upload %s to [%s](%s)", file.GetName(), account.GetAccount().VirtualPath, dstDirActualPath),
|
||||||
Func: func(task *task.Task[uint64]) error {
|
Func: func(task *task.Task[uint64]) error {
|
||||||
|
@ -65,7 +65,7 @@ func getFileStreamFromLink(file model.Obj, link *model.Link) (model.FileStreamer
|
|||||||
if mimetype == "" {
|
if mimetype == "" {
|
||||||
mimetype = "application/octet-stream"
|
mimetype = "application/octet-stream"
|
||||||
}
|
}
|
||||||
stream := model.FileStream{
|
stream := &model.FileStream{
|
||||||
Obj: file,
|
Obj: file,
|
||||||
ReadCloser: rc,
|
ReadCloser: rc,
|
||||||
Mimetype: mimetype,
|
Mimetype: mimetype,
|
||||||
|
@ -19,6 +19,9 @@ type FileStreamer interface {
|
|||||||
io.ReadCloser
|
io.ReadCloser
|
||||||
Obj
|
Obj
|
||||||
GetMimetype() string
|
GetMimetype() string
|
||||||
|
SetReadCloser(io.ReadCloser)
|
||||||
|
NeedStore() bool
|
||||||
|
GetReadCloser() io.ReadCloser
|
||||||
}
|
}
|
||||||
|
|
||||||
type URL interface {
|
type URL interface {
|
||||||
|
@ -7,9 +7,22 @@ import (
|
|||||||
type FileStream struct {
|
type FileStream struct {
|
||||||
Obj
|
Obj
|
||||||
io.ReadCloser
|
io.ReadCloser
|
||||||
Mimetype string
|
Mimetype string
|
||||||
|
WebPutAsTask bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f FileStream) GetMimetype() string {
|
func (f FileStream) GetMimetype() string {
|
||||||
return f.Mimetype
|
return f.Mimetype
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f FileStream) NeedStore() bool {
|
||||||
|
return f.WebPutAsTask
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *FileStream) GetReadCloser() io.ReadCloser {
|
||||||
|
return f.ReadCloser
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *FileStream) SetReadCloser(rc io.ReadCloser) {
|
||||||
|
f.ReadCloser = rc
|
||||||
|
}
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"github.com/alist-org/alist/v3/internal/conf"
|
"github.com/alist-org/alist/v3/internal/conf"
|
||||||
"github.com/alist-org/alist/v3/internal/errs"
|
"github.com/alist-org/alist/v3/internal/errs"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
"os"
|
||||||
stdpath "path"
|
stdpath "path"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -222,6 +223,14 @@ func Remove(ctx context.Context, account driver.Driver, path string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Put(ctx context.Context, account driver.Driver, dstDirPath string, file model.FileStreamer, up driver.UpdateProgress) error {
|
func Put(ctx context.Context, account driver.Driver, dstDirPath string, file model.FileStreamer, up driver.UpdateProgress) error {
|
||||||
|
defer func() {
|
||||||
|
if f, ok := file.GetReadCloser().(*os.File); ok {
|
||||||
|
err := os.RemoveAll(f.Name())
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("failed to remove file [%s]", f.Name())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := file.Close(); err != nil {
|
if err := file.Close(); err != nil {
|
||||||
log.Errorf("failed to close file streamer, %v", err)
|
log.Errorf("failed to close file streamer, %v", err)
|
||||||
@ -241,6 +250,7 @@ func Put(ctx context.Context, account driver.Driver, dstDirPath string, file mod
|
|||||||
up = func(p int) {}
|
up = func(p int) {}
|
||||||
}
|
}
|
||||||
err = account.Put(ctx, parentDir, file, up)
|
err = account.Put(ctx, parentDir, file, up)
|
||||||
|
log.Debugf("put file [%s] done", file.GetName())
|
||||||
if err == nil {
|
if err == nil {
|
||||||
// clear cache
|
// clear cache
|
||||||
key := stdpath.Join(account.GetAccount().VirtualPath, dstDirPath)
|
key := stdpath.Join(account.GetAccount().VirtualPath, dstDirPath)
|
||||||
|
@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/alist-org/alist/v3/internal/sign"
|
"github.com/alist-org/alist/v3/internal/sign"
|
||||||
"github.com/alist-org/alist/v3/server/common"
|
"github.com/alist-org/alist/v3/server/common"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
stdpath "path"
|
stdpath "path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
@ -205,14 +206,17 @@ func FsPut(c *gin.Context) {
|
|||||||
common.ErrorResp(c, err, 400)
|
common.ErrorResp(c, err, 400)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := fs.PutAsTask(dir, model.FileStream{
|
log.Debugf("c.Request.Close, %v", c.Request.Close)
|
||||||
|
c.Request.Close = false
|
||||||
|
if err := fs.PutAsTask(dir, &model.FileStream{
|
||||||
Obj: model.Object{
|
Obj: model.Object{
|
||||||
Name: name,
|
Name: name,
|
||||||
Size: size,
|
Size: size,
|
||||||
Modified: time.Now(),
|
Modified: time.Now(),
|
||||||
},
|
},
|
||||||
ReadCloser: c.Request.Body,
|
ReadCloser: c.Request.Body,
|
||||||
Mimetype: c.GetHeader("Content-Type"),
|
Mimetype: c.GetHeader("Content-Type"),
|
||||||
|
WebPutAsTask: true,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
common.ErrorResp(c, err, 500)
|
common.ErrorResp(c, err, 500)
|
||||||
return
|
return
|
||||||
|
@ -291,7 +291,7 @@ func (h *Handler) handlePut(w http.ResponseWriter, r *http.Request) (status int,
|
|||||||
Size: r.ContentLength,
|
Size: r.ContentLength,
|
||||||
Modified: time.Now(),
|
Modified: time.Now(),
|
||||||
}
|
}
|
||||||
stream := model.FileStream{
|
stream := &model.FileStream{
|
||||||
Obj: obj,
|
Obj: obj,
|
||||||
ReadCloser: r.Body,
|
ReadCloser: r.Body,
|
||||||
Mimetype: r.Header.Get("Content-Type"),
|
Mimetype: r.Header.Get("Content-Type"),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user