mirror of https://github.com/Xhofe/alist
				
				
				
			
		
			
				
	
	
		
			84 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Go
		
	
	
			
		
		
	
	
			84 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Go
		
	
	
| package tool
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"path/filepath"
 | |
| 
 | |
| 	"github.com/alist-org/alist/v3/internal/conf"
 | |
| 	"github.com/alist-org/alist/v3/internal/errs"
 | |
| 	"github.com/alist-org/alist/v3/internal/op"
 | |
| 	"github.com/google/uuid"
 | |
| 	"github.com/pkg/errors"
 | |
| 	"github.com/xhofe/tache"
 | |
| )
 | |
| 
 | |
| type DeletePolicy string
 | |
| 
 | |
| const (
 | |
| 	DeleteOnUploadSucceed DeletePolicy = "delete_on_upload_succeed"
 | |
| 	DeleteOnUploadFailed  DeletePolicy = "delete_on_upload_failed"
 | |
| 	DeleteNever           DeletePolicy = "delete_never"
 | |
| 	DeleteAlways          DeletePolicy = "delete_always"
 | |
| )
 | |
| 
 | |
| type AddURLArgs struct {
 | |
| 	URL          string
 | |
| 	DstDirPath   string
 | |
| 	Tool         string
 | |
| 	DeletePolicy DeletePolicy
 | |
| }
 | |
| 
 | |
| func AddURL(ctx context.Context, args *AddURLArgs) (tache.TaskWithInfo, error) {
 | |
| 	// get tool
 | |
| 	tool, err := Tools.Get(args.Tool)
 | |
| 	if err != nil {
 | |
| 		return nil, errors.Wrapf(err, "failed get tool")
 | |
| 	}
 | |
| 	// check tool is ready
 | |
| 	if !tool.IsReady() {
 | |
| 		// try to init tool
 | |
| 		if _, err := tool.Init(); err != nil {
 | |
| 			return nil, errors.Wrapf(err, "failed init tool %s", args.Tool)
 | |
| 		}
 | |
| 	}
 | |
| 	// check storage
 | |
| 	storage, dstDirActualPath, err := op.GetStorageAndActualPath(args.DstDirPath)
 | |
| 	if err != nil {
 | |
| 		return nil, errors.WithMessage(err, "failed get storage")
 | |
| 	}
 | |
| 	// check is it could upload
 | |
| 	if storage.Config().NoUpload {
 | |
| 		return nil, errors.WithStack(errs.UploadNotSupported)
 | |
| 	}
 | |
| 	// check path is valid
 | |
| 	obj, err := op.Get(ctx, storage, dstDirActualPath)
 | |
| 	if err != nil {
 | |
| 		if !errs.IsObjectNotFound(err) {
 | |
| 			return nil, errors.WithMessage(err, "failed get object")
 | |
| 		}
 | |
| 	} else {
 | |
| 		if !obj.IsDir() {
 | |
| 			// can't add to a file
 | |
| 			return nil, errors.WithStack(errs.NotFolder)
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	uid := uuid.NewString()
 | |
| 	tempDir := filepath.Join(conf.Conf.TempDir, args.Tool, uid)
 | |
| 	deletePolicy := args.DeletePolicy
 | |
| 	if args.Tool == "pikpak" {
 | |
| 		tempDir = args.DstDirPath
 | |
| 		// 防止将下载好的文件删除
 | |
| 		deletePolicy = DeleteNever
 | |
| 	}
 | |
| 	t := &DownloadTask{
 | |
| 		Url:          args.URL,
 | |
| 		DstDirPath:   args.DstDirPath,
 | |
| 		TempDir:      tempDir,
 | |
| 		DeletePolicy: deletePolicy,
 | |
| 		tool:         tool,
 | |
| 	}
 | |
| 	DownloadTaskManager.Add(t)
 | |
| 	return t, nil
 | |
| }
 |