Skip to content

Commit

Permalink
Merge pull request #1458 from 0chain/feat/user-meta
Browse files Browse the repository at this point in the history
add custom meta for dir
  • Loading branch information
dabasov authored Jul 18, 2024
2 parents e2dca5d + 9dac350 commit aac7040
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
2 changes: 2 additions & 0 deletions code/go/0chain.net/blobbercore/allocation/newdirchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type NewDir struct {
ConnectionID string `json:"connection_id" validation:"required"`
Path string `json:"filepath" validation:"required"`
AllocationID string `json:"allocation_id"`
CustomMeta string `json:"custom_meta,omitempty"`
}

func (nf *NewDir) ApplyChange(ctx context.Context, rootRef *reference.Ref, change *AllocationChange,
Expand Down Expand Up @@ -79,6 +80,7 @@ func (nf *NewDir) ApplyChange(ctx context.Context, rootRef *reference.Ref, chang
fmt.Sprintf("file path %s has no entry in fileID meta", newRef.Path))
}
newRef.FileID = fileID
newRef.CustomMeta = nf.CustomMeta
dirRef.AddChild(newRef)
dirRef = newRef
}
Expand Down
13 changes: 13 additions & 0 deletions code/go/0chain.net/blobbercore/handler/object_operation_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1245,6 +1245,8 @@ func (fsh *StorageHandler) CreateDir(ctx context.Context, r *http.Request) (*all
return nil, common.NewError("invalid_parameters", "Invalid dir path passed")
}

customMeta := r.FormValue("custom_meta")

exisitingRef, err := fsh.checkIfFileAlreadyExists(ctx, allocationID, dirPath)
if err != nil {
Logger.Error("Error file reference", zap.Error(err))
Expand All @@ -1257,6 +1259,16 @@ func (fsh *StorageHandler) CreateDir(ctx context.Context, r *http.Request) (*all
if exisitingRef != nil {
// target directory exists, return StatusOK
if exisitingRef.Type == reference.DIRECTORY {

if exisitingRef.CustomMeta != customMeta {
_ = datastore.GetStore().WithNewTransaction(func(ctx context.Context) error {
err := reference.UpdateCustomMeta(ctx, exisitingRef, customMeta)
if err != nil {
logging.Logger.Error("Error updating custom meta", zap.Error(err))
}
return err
})
}
return nil, common.NewError("directory_exists", "Directory already exists`")
}

Expand Down Expand Up @@ -1293,6 +1305,7 @@ func (fsh *StorageHandler) CreateDir(ctx context.Context, r *http.Request) (*all
newDir.ConnectionID = connectionID
newDir.Path = dirPath
newDir.AllocationID = allocationID
newDir.CustomMeta = customMeta

connectionObj.AddChange(allocationChange, &newDir)

Expand Down
6 changes: 3 additions & 3 deletions code/go/0chain.net/blobbercore/handler/storage_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (fsh *StorageHandler) GetAllocationUpdateTicket(ctx context.Context, r *htt
}

func (fsh *StorageHandler) checkIfFileAlreadyExists(ctx context.Context, allocationID, path string) (*reference.Ref, error) {
return reference.GetLimitedRefFieldsByPath(ctx, allocationID, path, []string{"id", "type"})
return reference.GetLimitedRefFieldsByPath(ctx, allocationID, path, []string{"id", "type", "custom_meta"})
}

func (fsh *StorageHandler) GetFileMeta(ctx context.Context, r *http.Request) (interface{}, error) {
Expand Down Expand Up @@ -276,7 +276,7 @@ func (fsh *StorageHandler) GetFileStats(ctx context.Context, r *http.Request) (i

// swagger:route GET /v1/file/list/{allocation} GetListFiles
// List files.
// ListHandler is the handler to respond to list requests from clients,
// ListHandler is the handler to respond to list requests from clients,
// it returns a list of files in the allocation,
// along with the metadata of the files.
//
Expand Down Expand Up @@ -344,7 +344,7 @@ func (fsh *StorageHandler) GetFileStats(ctx context.Context, r *http.Request) (i
// responses:
//
// 200: ListResult
// 400:
// 400:

func (fsh *StorageHandler) ListEntities(ctx context.Context, r *http.Request) (*blobberhttp.ListResult, error) {
clientID := ctx.Value(constants.ContextKeyClient).(string)
Expand Down
8 changes: 7 additions & 1 deletion code/go/0chain.net/blobbercore/reference/ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type Ref struct {
PathHash string `gorm:"column:path_hash;size:64;not null" dirlist:"path_hash" filelist:"path_hash"`
ParentPath string `gorm:"column:parent_path;size:999;index:idx_parent_path_alloc,priority:2"`
PathLevel int `gorm:"column:level;not null;default:0"`
CustomMeta string `gorm:"column:custom_meta;not null" filelist:"custom_meta"`
CustomMeta string `gorm:"column:custom_meta;not null" filelist:"custom_meta" dirlist:"custom_meta"`
ValidationRoot string `gorm:"column:validation_root;size:64;not null;index:idx_validation_alloc,priority:2" filelist:"validation_root"`
PrevValidationRoot string `gorm:"column:prev_validation_root" filelist:"prev_validation_root" json:"prev_validation_root"`
ValidationRootSignature string `gorm:"column:validation_root_signature;size:64" filelist:"validation_root_signature" json:"validation_root_signature,omitempty"`
Expand Down Expand Up @@ -144,6 +144,7 @@ type PaginatedRef struct { //Gorm smart select fields.
ActualThumbnailHash string `gorm:"column:actual_thumbnail_hash" json:"actual_thumbnail_hash,omitempty"`
EncryptedKey string `gorm:"column:encrypted_key" json:"encrypted_key,omitempty"`
EncryptedKeyPoint string `gorm:"column:encrypted_key_point" json:"encrypted_key_point,omitempty"`
FileMetaHash string `gorm:"column:file_meta_hash;size:64;not null" dirlist:"file_meta_hash" filelist:"file_meta_hash"`

CreatedAt common.Timestamp `gorm:"column:created_at" json:"created_at,omitempty"`
UpdatedAt common.Timestamp `gorm:"column:updated_at" json:"updated_at,omitempty"`
Expand Down Expand Up @@ -669,3 +670,8 @@ func GetListingFieldsMap(refEntity interface{}, tagName string) map[string]inter
}
return result
}

func UpdateCustomMeta(ctx context.Context, ref *Ref, customMeta string) error {
db := datastore.GetStore().GetTransaction(ctx)
return db.Exec("UPDATE reference_objects SET custom_meta = ? WHERE id = ?", customMeta, ref.ID).Error
}

0 comments on commit aac7040

Please sign in to comment.