|
- package local
-
- import (
- "context"
- "io"
- "os"
- "path/filepath"
-
- stgtypes "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/storage/types"
- jcstypes "gitlink.org.cn/cloudream/jcs-pub/common/types"
- )
-
- type S2STransfer struct {
- feat *jcstypes.S2STransferFeature
- detail *jcstypes.UserSpaceDetail
- localStg *jcstypes.LocalCred
- dstPath jcstypes.JPath
- }
-
- // 只有同一个机器的存储之间才可以进行数据直传
- func (*S2STransfer) CanTransfer(src, dst *jcstypes.UserSpaceDetail) bool {
- if stgtypes.FindFeature[*jcstypes.S2STransferFeature](dst) == nil {
- return false
- }
-
- _, ok := src.UserSpace.Storage.(*jcstypes.LocalType)
- if !ok {
- return false
- }
-
- if src.RecommendHub.HubID != dst.RecommendHub.HubID {
- return false
- }
-
- return true
- }
-
- // 执行数据直传
- func (s *S2STransfer) Transfer(ctx context.Context, src *jcstypes.UserSpaceDetail, srcPath jcstypes.JPath, dstPath jcstypes.JPath) (stgtypes.FileInfo, error) {
- s.dstPath = dstPath
-
- copy, err := os.OpenFile(filepath.Join(s.localStg.RootDir, s.dstPath.JoinOSPath()), os.O_WRONLY|os.O_CREATE, 0644)
- if err != nil {
- return stgtypes.FileInfo{}, err
- }
- defer copy.Close()
-
- srcFile, err := os.Open(filepath.Join(s.localStg.RootDir, srcPath.JoinOSPath()))
- if err != nil {
- return stgtypes.FileInfo{}, err
- }
- defer srcFile.Close()
-
- n, err := io.Copy(copy, srcFile)
- if err != nil {
- return stgtypes.FileInfo{}, err
- }
-
- return stgtypes.FileInfo{
- Path: dstPath,
- Size: n,
- Hash: "",
- }, nil
- }
-
- func (s *S2STransfer) Close() {
-
- }
|