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