github.com/0chain/gosdk@v1.17.11/zboxcore/allocationchange/copyobject.go (about)

     1  package allocationchange
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/0chain/errors"
     7  	"github.com/0chain/gosdk/core/common"
     8  	"github.com/0chain/gosdk/core/pathutil"
     9  	"github.com/0chain/gosdk/core/util"
    10  	"github.com/0chain/gosdk/zboxcore/fileref"
    11  	"github.com/google/uuid"
    12  )
    13  
    14  type CopyFileChange struct {
    15  	change
    16  	ObjectTree fileref.RefEntity
    17  	DestPath   string
    18  	Uuid       uuid.UUID
    19  }
    20  
    21  func (ch *CopyFileChange) ProcessChange(rootRef *fileref.Ref, fileIDMeta map[string]string) (err error) {
    22  
    23  	var fields []string
    24  	fields, err = common.GetPathFields(ch.DestPath)
    25  	if err != nil {
    26  		return
    27  	}
    28  	rootRef.HashToBeComputed = true
    29  	dirRef := rootRef
    30  
    31  	for i := 0; i < len(fields); i++ {
    32  		found := false
    33  		for _, child := range dirRef.Children {
    34  			if child.GetName() == fields[i] {
    35  				dirRef = child.(*fileref.Ref)
    36  				found = true
    37  				break
    38  			}
    39  		}
    40  		if !found {
    41  			newRef := &fileref.Ref{}
    42  			uid := util.GetSHA1Uuid(ch.Uuid, fields[i])
    43  			ch.Uuid = uid
    44  			newRef.FileID = uid.String()
    45  			newRef.Path = "/" + strings.Join(fields[:i+1], "/")
    46  			fileIDMeta[newRef.Path] = newRef.FileID
    47  			newRef.Type = fileref.DIRECTORY
    48  			newRef.AllocationID = dirRef.AllocationID
    49  			newRef.Name = fields[i]
    50  
    51  			dirRef.AddChild(newRef)
    52  			dirRef = newRef
    53  		}
    54  		dirRef.HashToBeComputed = true
    55  	}
    56  
    57  	if dirRef.GetPath() != ch.DestPath || dirRef.GetType() != fileref.DIRECTORY {
    58  		err = errors.New("file_not_found", "Object to copy not found in blobber")
    59  		return
    60  	}
    61  	var affectedRef *fileref.Ref
    62  	if ch.ObjectTree.GetType() == fileref.FILE {
    63  		affectedRef = &(ch.ObjectTree.(*fileref.FileRef)).Ref
    64  	} else {
    65  		affectedRef = ch.ObjectTree.(*fileref.Ref)
    66  	}
    67  
    68  	affectedRef.Path = pathutil.Join(dirRef.GetPath(), affectedRef.Name)
    69  	uid := util.GetSHA1Uuid(ch.Uuid, affectedRef.Name)
    70  	ch.Uuid = uid
    71  	affectedRef.FileID = uid.String()
    72  
    73  	affectedRef.HashToBeComputed = true
    74  	fileIDMeta[affectedRef.Path] = affectedRef.FileID
    75  
    76  	ch.processChildren(affectedRef, fileIDMeta)
    77  	dirRef.AddChild(ch.ObjectTree)
    78  
    79  	return
    80  }
    81  
    82  func (ch *CopyFileChange) processChildren(curRef *fileref.Ref, fileIDMeta map[string]string) {
    83  	for _, childRefEntity := range curRef.Children {
    84  		var childRef *fileref.Ref
    85  		if childRefEntity.GetType() == fileref.FILE {
    86  			childRef = &(childRefEntity.(*fileref.FileRef)).Ref
    87  		} else {
    88  			childRef = childRefEntity.(*fileref.Ref)
    89  		}
    90  
    91  		childRef.HashToBeComputed = true
    92  		childRef.Path = pathutil.Join(curRef.Path, childRef.Name)
    93  		uid := util.GetSHA1Uuid(ch.Uuid, childRef.Name)
    94  		ch.Uuid = uid
    95  		childRef.FileID = uid.String()
    96  		fileIDMeta[childRef.Path] = childRef.FileID
    97  
    98  		if childRefEntity.GetType() == fileref.DIRECTORY {
    99  			ch.processChildren(childRef, fileIDMeta)
   100  		}
   101  	}
   102  }
   103  
   104  func (n *CopyFileChange) GetAffectedPath() []string {
   105  	return []string{n.DestPath}
   106  }
   107  
   108  func (n *CopyFileChange) GetSize() int64 {
   109  	return n.ObjectTree.GetSize()
   110  }