mirror of https://github.com/Xhofe/alist
				
				
				
			feat: copy files between 2 accounts
							parent
							
								
									4fa7846f00
								
							
						
					
					
						commit
						2d60dab13c
					
				| 
						 | 
					@ -0,0 +1,41 @@
 | 
				
			||||||
 | 
					package fs
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"context"
 | 
				
			||||||
 | 
						stdpath "path"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/alist-org/alist/v3/internal/driver"
 | 
				
			||||||
 | 
						"github.com/alist-org/alist/v3/internal/model"
 | 
				
			||||||
 | 
						"github.com/alist-org/alist/v3/internal/operations"
 | 
				
			||||||
 | 
						"github.com/pkg/errors"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func CopyBetween2Accounts(ctx context.Context, srcAccount, dstAccount driver.Driver, srcPath, dstPath string) error {
 | 
				
			||||||
 | 
						srcFile, err := operations.Get(ctx, srcAccount, srcPath)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return errors.WithMessagef(err, "failed get src [%s] file", srcPath)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if srcFile.IsDir() {
 | 
				
			||||||
 | 
							files, err := operations.List(ctx, srcAccount, srcPath)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return errors.WithMessagef(err, "failed list src [%s] files", srcPath)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							for _, file := range files {
 | 
				
			||||||
 | 
								srcFilePath := stdpath.Join(srcPath, file.GetName())
 | 
				
			||||||
 | 
								dstFilePath := stdpath.Join(dstPath, file.GetName())
 | 
				
			||||||
 | 
								if err := CopyBetween2Accounts(ctx, srcAccount, dstAccount, srcFilePath, dstFilePath); err != nil {
 | 
				
			||||||
 | 
									return errors.WithMessagef(err, "failed copy file [%s] to [%s]", srcFilePath, dstFilePath)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						link, err := operations.Link(ctx, srcAccount, srcPath, model.LinkArgs{})
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return errors.WithMessagef(err, "failed get [%s] link", srcPath)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						stream, err := getFileStreamFromLink(srcFile, link)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return errors.WithMessagef(err, "failed get [%s] stream", srcPath)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						// TODO add as task
 | 
				
			||||||
 | 
						return operations.Put(ctx, dstAccount, dstPath, stream)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -55,18 +55,19 @@ func Copy(ctx context.Context, account driver.Driver, srcPath, dstPath string) e
 | 
				
			||||||
	if srcAccount.GetAccount() == dstAccount.GetAccount() {
 | 
						if srcAccount.GetAccount() == dstAccount.GetAccount() {
 | 
				
			||||||
		return operations.Copy(ctx, account, srcActualPath, dstActualPath)
 | 
							return operations.Copy(ctx, account, srcActualPath, dstActualPath)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	// not in an account, add copy task
 | 
						// not in an account
 | 
				
			||||||
	srcFile, err := operations.Get(ctx, srcAccount, srcActualPath)
 | 
						return CopyBetween2Accounts(ctx, srcAccount, dstAccount, srcActualPath, dstActualPath)
 | 
				
			||||||
	if srcFile.IsDir() {
 | 
						// srcFile, err := operations.Get(ctx, srcAccount, srcActualPath)
 | 
				
			||||||
		// TODO: recursive copy
 | 
						// if srcFile.IsDir() {
 | 
				
			||||||
		return nil
 | 
						// 	// TODO: recursive copy
 | 
				
			||||||
	}
 | 
						// 	return nil
 | 
				
			||||||
	// TODO: add copy task, maybe like this:
 | 
						// }
 | 
				
			||||||
	// operations.Link(ctx,srcAccount,srcActualPath,args)
 | 
						// // TODO: add copy task, maybe like this:
 | 
				
			||||||
	// get a Reader from link
 | 
						// // operations.Link(ctx,srcAccount,srcActualPath,args)
 | 
				
			||||||
	// boxing the Reader to a driver.FileStream
 | 
						// // get a Reader from link
 | 
				
			||||||
	// operations.Put(ctx,dstParentPath, stream)
 | 
						// // boxing the Reader to a driver.FileStream
 | 
				
			||||||
	panic("TODO")
 | 
						// // operations.Put(ctx,dstParentPath, stream)
 | 
				
			||||||
 | 
						// panic("TODO")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func Remove(ctx context.Context, account driver.Driver, path string) error {
 | 
					func Remove(ctx context.Context, account driver.Driver, path string) error {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue