Skip to content

Commit

Permalink
fix: put as task from web
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jul 1, 2022
1 parent 4d0ae6b commit 4340a48
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 7 deletions.
2 changes: 1 addition & 1 deletion internal/aria2/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (m *Monitor) Complete() error {
if err != nil {
return errors.Wrapf(err, "failed to open file %s", file.Path)
}
stream := model.FileStream{
stream := &model.FileStream{
Obj: model.Object{
Name: path.Base(file.Path),
Size: size,
Expand Down
8 changes: 8 additions & 0 deletions internal/fs/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/operations"
"github.com/alist-org/alist/v3/pkg/task"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/pkg/errors"
"sync/atomic"
)
Expand All @@ -24,6 +25,13 @@ func putAsTask(dstDirPath string, file model.FileStreamer) error {
if err != nil {
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]{
Name: fmt.Sprintf("upload %s to [%s](%s)", file.GetName(), account.GetAccount().VirtualPath, dstDirActualPath),
Func: func(task *task.Task[uint64]) error {
Expand Down
2 changes: 1 addition & 1 deletion internal/fs/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func getFileStreamFromLink(file model.Obj, link *model.Link) (model.FileStreamer
if mimetype == "" {
mimetype = "application/octet-stream"
}
stream := model.FileStream{
stream := &model.FileStream{
Obj: file,
ReadCloser: rc,
Mimetype: mimetype,
Expand Down
3 changes: 3 additions & 0 deletions internal/model/obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type FileStreamer interface {
io.ReadCloser
Obj
GetMimetype() string
SetReadCloser(io.ReadCloser)
NeedStore() bool
GetReadCloser() io.ReadCloser
}

type URL interface {
Expand Down
15 changes: 14 additions & 1 deletion internal/model/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,22 @@ import (
type FileStream struct {
Obj
io.ReadCloser
Mimetype string
Mimetype string
WebPutAsTask bool
}

func (f FileStream) GetMimetype() string {
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
}
10 changes: 10 additions & 0 deletions internal/operations/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/errs"
log "github.com/sirupsen/logrus"
"os"
stdpath "path"
"strings"
"time"
Expand Down Expand Up @@ -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 {
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() {
if err := file.Close(); err != nil {
log.Errorf("failed to close file streamer, %v", err)
Expand All @@ -241,6 +250,7 @@ func Put(ctx context.Context, account driver.Driver, dstDirPath string, file mod
up = func(p int) {}
}
err = account.Put(ctx, parentDir, file, up)
log.Debugf("put file [%s] done", file.GetName())
if err == nil {
// clear cache
key := stdpath.Join(account.GetAccount().VirtualPath, dstDirPath)
Expand Down
10 changes: 7 additions & 3 deletions server/controllers/fsmanage.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/alist-org/alist/v3/internal/sign"
"github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
stdpath "path"
"strconv"
"time"
Expand Down Expand Up @@ -205,14 +206,17 @@ func FsPut(c *gin.Context) {
common.ErrorResp(c, err, 400)
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{
Name: name,
Size: size,
Modified: time.Now(),
},
ReadCloser: c.Request.Body,
Mimetype: c.GetHeader("Content-Type"),
ReadCloser: c.Request.Body,
Mimetype: c.GetHeader("Content-Type"),
WebPutAsTask: true,
}); err != nil {
common.ErrorResp(c, err, 500)
return
Expand Down
2 changes: 1 addition & 1 deletion server/webdav/webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func (h *Handler) handlePut(w http.ResponseWriter, r *http.Request) (status int,
Size: r.ContentLength,
Modified: time.Now(),
}
stream := model.FileStream{
stream := &model.FileStream{
Obj: obj,
ReadCloser: r.Body,
Mimetype: r.Header.Get("Content-Type"),
Expand Down

0 comments on commit 4340a48

Please sign in to comment.