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

     1  package allocationchange
     2  
     3  import (
     4  	"fmt"
     5  	"path"
     6  	"strings"
     7  
     8  	"github.com/0chain/gosdk/core/common"
     9  	"github.com/0chain/gosdk/core/util"
    10  	"github.com/0chain/gosdk/zboxcore/fileref"
    11  	"github.com/google/uuid"
    12  )
    13  
    14  type DirCreateChange struct {
    15  	Timestamp  common.Timestamp
    16  	RemotePath string
    17  	Uuid       uuid.UUID
    18  }
    19  
    20  func (d *DirCreateChange) ProcessChange(rootRef *fileref.Ref, FileIDMeta map[string]string) (err error) {
    21  	fields, err := common.GetPathFields(d.RemotePath)
    22  	if err != nil {
    23  		return
    24  	}
    25  
    26  	dirRef := rootRef
    27  	for i := 0; i < len(fields); i++ {
    28  		found := false
    29  		for _, child := range dirRef.Children {
    30  			if child.GetName() == fields[i] {
    31  				ref, ok := child.(*fileref.Ref)
    32  				if !ok {
    33  					err = fmt.Errorf(
    34  						"invalid_ref: Expected type *fileref.Ref but got %T for"+
    35  							" file name: %s, file path: %s, file type: %s",
    36  						child, child.GetName(), child.GetPath(), child.GetType())
    37  					return
    38  				}
    39  				dirRef = ref
    40  				found = true
    41  				break
    42  			}
    43  
    44  		}
    45  		if !found {
    46  			uid := util.GetSHA1Uuid(d.Uuid, fields[i])
    47  			d.Uuid = uid
    48  
    49  			newRef := &fileref.Ref{
    50  				Type:         fileref.DIRECTORY,
    51  				AllocationID: dirRef.AllocationID,
    52  				Path:         path.Join("/", strings.Join(fields[:i+1], "/")),
    53  				Name:         fields[i],
    54  				FileID:       uid.String(),
    55  			}
    56  			FileIDMeta[newRef.Path] = newRef.FileID
    57  			newRef.HashToBeComputed = true
    58  			dirRef.AddChild(newRef)
    59  			dirRef = newRef
    60  		}
    61  	}
    62  
    63  	return
    64  }
    65  
    66  func (d *DirCreateChange) GetAffectedPath() []string {
    67  	return []string{d.RemotePath}
    68  }
    69  
    70  func (d *DirCreateChange) GetSize() int64 {
    71  	return 0
    72  }