Skip to content

Commit aac7040

Browse files
authored
Merge pull request #1458 from 0chain/feat/user-meta
add custom meta for dir
2 parents e2dca5d + 9dac350 commit aac7040

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

code/go/0chain.net/blobbercore/allocation/newdirchange.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type NewDir struct {
1919
ConnectionID string `json:"connection_id" validation:"required"`
2020
Path string `json:"filepath" validation:"required"`
2121
AllocationID string `json:"allocation_id"`
22+
CustomMeta string `json:"custom_meta,omitempty"`
2223
}
2324

2425
func (nf *NewDir) ApplyChange(ctx context.Context, rootRef *reference.Ref, change *AllocationChange,
@@ -79,6 +80,7 @@ func (nf *NewDir) ApplyChange(ctx context.Context, rootRef *reference.Ref, chang
7980
fmt.Sprintf("file path %s has no entry in fileID meta", newRef.Path))
8081
}
8182
newRef.FileID = fileID
83+
newRef.CustomMeta = nf.CustomMeta
8284
dirRef.AddChild(newRef)
8385
dirRef = newRef
8486
}

code/go/0chain.net/blobbercore/handler/object_operation_handler.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,8 @@ func (fsh *StorageHandler) CreateDir(ctx context.Context, r *http.Request) (*all
12451245
return nil, common.NewError("invalid_parameters", "Invalid dir path passed")
12461246
}
12471247

1248+
customMeta := r.FormValue("custom_meta")
1249+
12481250
exisitingRef, err := fsh.checkIfFileAlreadyExists(ctx, allocationID, dirPath)
12491251
if err != nil {
12501252
Logger.Error("Error file reference", zap.Error(err))
@@ -1257,6 +1259,16 @@ func (fsh *StorageHandler) CreateDir(ctx context.Context, r *http.Request) (*all
12571259
if exisitingRef != nil {
12581260
// target directory exists, return StatusOK
12591261
if exisitingRef.Type == reference.DIRECTORY {
1262+
1263+
if exisitingRef.CustomMeta != customMeta {
1264+
_ = datastore.GetStore().WithNewTransaction(func(ctx context.Context) error {
1265+
err := reference.UpdateCustomMeta(ctx, exisitingRef, customMeta)
1266+
if err != nil {
1267+
logging.Logger.Error("Error updating custom meta", zap.Error(err))
1268+
}
1269+
return err
1270+
})
1271+
}
12601272
return nil, common.NewError("directory_exists", "Directory already exists`")
12611273
}
12621274

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

12971310
connectionObj.AddChange(allocationChange, &newDir)
12981311

code/go/0chain.net/blobbercore/handler/storage_handler.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (fsh *StorageHandler) GetAllocationUpdateTicket(ctx context.Context, r *htt
103103
}
104104

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

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

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

349349
func (fsh *StorageHandler) ListEntities(ctx context.Context, r *http.Request) (*blobberhttp.ListResult, error) {
350350
clientID := ctx.Value(constants.ContextKeyClient).(string)

code/go/0chain.net/blobbercore/reference/ref.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ type Ref struct {
6060
PathHash string `gorm:"column:path_hash;size:64;not null" dirlist:"path_hash" filelist:"path_hash"`
6161
ParentPath string `gorm:"column:parent_path;size:999;index:idx_parent_path_alloc,priority:2"`
6262
PathLevel int `gorm:"column:level;not null;default:0"`
63-
CustomMeta string `gorm:"column:custom_meta;not null" filelist:"custom_meta"`
63+
CustomMeta string `gorm:"column:custom_meta;not null" filelist:"custom_meta" dirlist:"custom_meta"`
6464
ValidationRoot string `gorm:"column:validation_root;size:64;not null;index:idx_validation_alloc,priority:2" filelist:"validation_root"`
6565
PrevValidationRoot string `gorm:"column:prev_validation_root" filelist:"prev_validation_root" json:"prev_validation_root"`
6666
ValidationRootSignature string `gorm:"column:validation_root_signature;size:64" filelist:"validation_root_signature" json:"validation_root_signature,omitempty"`
@@ -144,6 +144,7 @@ type PaginatedRef struct { //Gorm smart select fields.
144144
ActualThumbnailHash string `gorm:"column:actual_thumbnail_hash" json:"actual_thumbnail_hash,omitempty"`
145145
EncryptedKey string `gorm:"column:encrypted_key" json:"encrypted_key,omitempty"`
146146
EncryptedKeyPoint string `gorm:"column:encrypted_key_point" json:"encrypted_key_point,omitempty"`
147+
FileMetaHash string `gorm:"column:file_meta_hash;size:64;not null" dirlist:"file_meta_hash" filelist:"file_meta_hash"`
147148

148149
CreatedAt common.Timestamp `gorm:"column:created_at" json:"created_at,omitempty"`
149150
UpdatedAt common.Timestamp `gorm:"column:updated_at" json:"updated_at,omitempty"`
@@ -669,3 +670,8 @@ func GetListingFieldsMap(refEntity interface{}, tagName string) map[string]inter
669670
}
670671
return result
671672
}
673+
674+
func UpdateCustomMeta(ctx context.Context, ref *Ref, customMeta string) error {
675+
db := datastore.GetStore().GetTransaction(ctx)
676+
return db.Exec("UPDATE reference_objects SET custom_meta = ? WHERE id = ?", customMeta, ref.ID).Error
677+
}

0 commit comments

Comments
 (0)