github.com/0chain/gosdk@v1.17.11/zboxcore/allocationchange/newfile.go (about) 1 package allocationchange 2 3 import ( 4 "errors" 5 "fmt" 6 "path" 7 "strings" 8 9 zError "github.com/0chain/errors" 10 "github.com/0chain/gosdk/core/common" 11 "github.com/0chain/gosdk/core/pathutil" 12 "github.com/0chain/gosdk/core/util" 13 "github.com/0chain/gosdk/zboxcore/client" 14 "github.com/0chain/gosdk/zboxcore/fileref" 15 "github.com/0chain/gosdk/zboxcore/logger" 16 "github.com/google/uuid" 17 ) 18 19 type NewFileChange struct { 20 change 21 File *fileref.FileRef 22 Uuid uuid.UUID 23 } 24 25 func (ch *NewFileChange) ProcessChange(rootRef *fileref.Ref, fileIDMeta map[string]string) (err error) { 26 27 fields, err := common.GetPathFields(pathutil.Dir(ch.File.Path)) 28 if err != nil { 29 return 30 } 31 32 if ch.File.ActualFileHash == "" { 33 logger.Logger.Error("emptyFileHash: ", ch.File.Path) 34 err = errors.New("empty actual file hash field") 35 return 36 } 37 38 if ch.File.ValidationRoot == "" { 39 err = errors.New("empty validation root field") 40 return 41 } 42 43 fileHashSign, err := client.Sign(ch.File.ActualFileHash) 44 if err != nil { 45 return 46 } 47 48 validationRootSign, err := client.Sign(fileHashSign + ch.File.ValidationRoot) 49 if err != nil { 50 return 51 } 52 53 ch.File.ActualFileHashSignature = fileHashSign 54 ch.File.ValidationRootSignature = validationRootSign 55 56 rootRef.HashToBeComputed = true 57 dirRef := rootRef 58 for i := 0; i < len(fields); i++ { 59 found := false 60 for _, child := range dirRef.Children { 61 if child.GetName() == fields[i] { 62 if child.GetType() == fileref.DIRECTORY { 63 dirRef = child.(*fileref.Ref) 64 found = true 65 break 66 } 67 err = zError.New("invalid_file_path", 68 fmt.Sprintf("type of %s is required to be directory", child.GetPath())) 69 return 70 } 71 } 72 73 if !found { 74 uid := util.GetSHA1Uuid(ch.Uuid, fields[i]) 75 ch.Uuid = uid 76 newRef := &fileref.Ref{ 77 Type: fileref.DIRECTORY, 78 AllocationID: dirRef.AllocationID, 79 Path: path.Join("/", strings.Join(fields[:i+1], "/")), 80 Name: fields[i], 81 FileID: uid.String(), 82 } 83 fileIDMeta[newRef.Path] = newRef.FileID 84 dirRef.AddChild(newRef) 85 dirRef = newRef 86 } 87 dirRef.HashToBeComputed = true 88 } 89 uid := util.GetSHA1Uuid(ch.Uuid, ch.File.Name) 90 ch.Uuid = uid 91 92 ch.File.FileID = uid.String() 93 ch.File.HashToBeComputed = true 94 fileIDMeta[ch.File.GetPath()] = ch.File.FileID 95 96 dirRef.AddChild(ch.File) 97 return 98 } 99 100 func (n *NewFileChange) GetAffectedPath() []string { 101 if n.File != nil { 102 return []string{n.File.Path} 103 } 104 return nil 105 } 106 107 func (n *NewFileChange) GetSize() int64 { 108 if n.File != nil { 109 return n.File.Size 110 } 111 return int64(0) 112 }