@@ -5,6 +5,7 @@ import type {
55 PersistentStorageGetBucketsCommand ,
66 PersistentStorageGetFileObjectCommand ,
77 PersistentStorageListFilesCommand ,
8+ PersistentStorageUpdateBucketCommand ,
89 PersistentStorageUploadFileCommand
910} from '../../../@types/commands.js'
1011import {
@@ -23,6 +24,21 @@ import {
2324} from '../../httpRoutes/validateCommands.js'
2425import { CommandHandler } from './handler.js'
2526
27+ const MAX_BUCKET_LABEL_LENGTH = 256
28+
29+ function validateOptionalLabel ( label : unknown ) : ValidateParams | null {
30+ if ( label === undefined || label === null ) return null
31+ if ( typeof label !== 'string' ) {
32+ return buildInvalidRequestMessage ( 'Invalid parameter: "label" must be a string' )
33+ }
34+ if ( label . length > MAX_BUCKET_LABEL_LENGTH ) {
35+ return buildInvalidRequestMessage (
36+ `Invalid parameter: "label" must be at most ${ MAX_BUCKET_LABEL_LENGTH } characters`
37+ )
38+ }
39+ return null
40+ }
41+
2642function requirePersistentStorage ( handler : CommandHandler ) : PersistentStorageFactory {
2743 const node = handler . getOceanNode ( ) as any
2844 if ( ! node . getPersistentStorage ) {
@@ -44,6 +60,8 @@ export class PersistentStorageCreateBucketHandler extends CommandHandler {
4460 'Invalid parameter: "accessLists" must be an array of objects'
4561 )
4662 }
63+ const labelError = validateOptionalLabel ( command . label )
64+ if ( labelError ) return labelError
4765 return { valid : true }
4866 }
4967
@@ -97,7 +115,11 @@ export class PersistentStorageCreateBucketHandler extends CommandHandler {
97115 }
98116 }
99117
100- const result = await storage . createNewBucket ( task . accessLists , ownerNormalized )
118+ const result = await storage . createNewBucket (
119+ task . accessLists ,
120+ ownerNormalized ,
121+ task . label
122+ )
101123 return {
102124 stream : Readable . from ( JSON . stringify ( result ) ) ,
103125 status : { httpStatus : 200 , error : null }
@@ -110,6 +132,59 @@ export class PersistentStorageCreateBucketHandler extends CommandHandler {
110132 }
111133}
112134
135+ export class PersistentStorageUpdateBucketHandler extends CommandHandler {
136+ validate ( command : PersistentStorageUpdateBucketCommand ) : ValidateParams {
137+ const base = validateCommandParameters ( command , [ 'bucketId' ] )
138+ if ( ! base . valid ) return base
139+ if ( ! command . bucketId || typeof command . bucketId !== 'string' ) {
140+ return buildInvalidRequestMessage ( 'Invalid parameter: "bucketId" must be a string' )
141+ }
142+ const labelError = validateOptionalLabel ( command . label )
143+ if ( labelError ) return labelError
144+ return { valid : true }
145+ }
146+
147+ async handle ( task : PersistentStorageUpdateBucketCommand ) : Promise < P2PCommandResponse > {
148+ const validationResponse = await this . verifyParamsAndRateLimits ( task )
149+ if ( this . shouldDenyTaskHandling ( validationResponse ) ) return validationResponse
150+
151+ const isAuthRequestValid = await this . validateTokenOrSignature (
152+ task . authorization ,
153+ task . consumerAddress ,
154+ task . nonce ,
155+ task . signature ,
156+ task . command
157+ )
158+ if ( isAuthRequestValid . status . httpStatus !== 200 ) return isAuthRequestValid
159+
160+ try {
161+ const storage = requirePersistentStorage ( this )
162+ const ownerNormalized = task . consumerAddress
163+ ? getAddress ( task . consumerAddress )
164+ : getAddress ( await this . getAddressFromToken ( task . authorization ) )
165+ const label = await storage . updateBucketLabel (
166+ task . bucketId ,
167+ task . label ,
168+ ownerNormalized
169+ )
170+ return {
171+ stream : Readable . from ( JSON . stringify ( { bucketId : task . bucketId , label } ) ) ,
172+ status : { httpStatus : 200 , error : null }
173+ }
174+ } catch ( e ) {
175+ if ( e instanceof PersistentStorageAccessDeniedError ) {
176+ return { stream : null , status : { httpStatus : 403 , error : e . message } }
177+ }
178+ const message = e instanceof Error ? e . message : String ( e )
179+ if ( message . toLowerCase ( ) . includes ( 'not found' ) ) {
180+ return { stream : null , status : { httpStatus : 404 , error : message } }
181+ }
182+ CORE_LOGGER . error ( `PersistentStorageUpdateBucketHandler error: ${ message } ` )
183+ return { stream : null , status : { httpStatus : 500 , error : message } }
184+ }
185+ }
186+ }
187+
113188export class PersistentStorageGetBucketsHandler extends CommandHandler {
114189 validate ( command : PersistentStorageGetBucketsCommand ) : ValidateParams {
115190 const base = validateCommandParameters ( command , [ 'owner' ] )
0 commit comments