47 lines
1.3 KiB
Go
Raw Normal View History

2022-06-20 20:34:58 +08:00
package aria2
import (
"context"
2022-06-20 22:29:52 +08:00
"github.com/alist-org/alist/v3/conf"
2022-06-20 20:34:58 +08:00
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/fs"
"github.com/alist-org/alist/v3/internal/operations"
2022-06-20 22:29:52 +08:00
"github.com/google/uuid"
2022-06-20 20:34:58 +08:00
"github.com/pkg/errors"
2022-06-20 22:29:52 +08:00
"path/filepath"
2022-06-20 20:34:58 +08:00
)
2022-06-20 22:29:52 +08:00
func AddURI(ctx context.Context, uri string, dstPath string, parentPath string) error {
2022-06-20 20:34:58 +08:00
// check account
account, actualParentPath, err := operations.GetAccountAndActualPath(parentPath)
if err != nil {
return errors.WithMessage(err, "failed get account")
}
// check is it could upload
if account.Config().NoUpload {
return errors.WithStack(fs.ErrUploadNotSupported)
}
// check path is valid
obj, err := operations.Get(ctx, account, actualParentPath)
if err != nil {
if !errors.Is(errors.Cause(err), driver.ErrorObjectNotFound) {
return errors.WithMessage(err, "failed get object")
}
} else {
if !obj.IsDir() {
// can't add to a file
return errors.WithStack(fs.ErrNotFolder)
}
}
2022-06-20 22:29:52 +08:00
// call aria2 rpc
options := map[string]interface{}{
"dir": filepath.Join(conf.Conf.TempDir, "aria2", uuid.NewString()),
}
gid, err := client.AddURI([]string{uri}, options)
if err != nil {
return errors.Wrapf(err, "failed to add uri %s", uri)
}
// TODO add to task manager
2022-06-20 20:34:58 +08:00
return nil
}