1- import {
2- existsSync ,
3- fstatSync ,
4- readFileSync ,
5- statSync ,
6- writeFileSync
7- } from "fs"
1+ import { existsSync , readFileSync , statSync , writeFileSync } from "fs"
82import path from "path"
3+ import { ObjectInfo } from "@bnb-chain/greenfield-cosmos-types/greenfield/storage/types"
94import {
105 bytesFromBase64 ,
116 Long ,
127 RedundancyType ,
138 VisibilityType
149} from "@bnb-chain/greenfield-js-sdk"
15- import { ObjectInfo } from "@bnb-chain/greenfield-js-sdk/dist/esm/types/sp/Common"
1610import { NodeAdapterReedSolomon } from "@bnb-chain/reed-solomon/node.adapter"
1711import type { Hex } from "viem"
1812
@@ -54,11 +48,13 @@ export const createFile = async (
5448 {
5549 privateKey,
5650 filePath,
57- bucketName
51+ bucketName,
52+ visibility
5853 } : {
5954 privateKey : Hex
6055 filePath : string
6156 bucketName ?: string
57+ visibility ?: "public" | "private"
6258 }
6359) : Promise < ApiResponse < FileData > > => {
6460 try {
@@ -101,7 +97,10 @@ export const createFile = async (
10197 bucketName : _bucketName ,
10298 objectName : objectName ,
10399 creator : account . address ,
104- visibility : VisibilityType . VISIBILITY_TYPE_PRIVATE ,
100+ visibility :
101+ visibility === "public"
102+ ? VisibilityType . VISIBILITY_TYPE_PUBLIC_READ
103+ : VisibilityType . VISIBILITY_TYPE_PRIVATE ,
105104 contentType : fileObj . type as string ,
106105 redundancyType : RedundancyType . REDUNDANCY_EC_TYPE ,
107106 payloadSize : Long . fromInt ( fileObj . content . byteLength ) ,
@@ -161,11 +160,13 @@ export const createFolder = async (
161160 {
162161 privateKey,
163162 folderName,
164- bucketName
163+ bucketName,
164+ visibility
165165 } : {
166166 privateKey : Hex
167167 folderName ?: string
168168 bucketName ?: string
169+ visibility ?: "public" | "private"
169170 }
170171) : Promise < ApiResponse < { bucketName : string ; folderName : string } > > => {
171172 try {
@@ -197,7 +198,10 @@ export const createFolder = async (
197198 bucketName : _bucketName ,
198199 objectName : formattedFolderName ,
199200 creator : account . address ,
200- visibility : VisibilityType . VISIBILITY_TYPE_PRIVATE ,
201+ visibility :
202+ visibility === "public"
203+ ? VisibilityType . VISIBILITY_TYPE_PUBLIC_READ
204+ : VisibilityType . VISIBILITY_TYPE_PRIVATE ,
201205 redundancyType : RedundancyType . REDUNDANCY_EC_TYPE
202206 } )
203207
@@ -240,7 +244,7 @@ export const getObjectInfo = async (
240244 const client = getClient ( network )
241245 const res = await client . object . headObject ( bucketName , objectName )
242246
243- return response . success ( res . objectInfo as { } as ObjectInfo )
247+ return response . success ( res . objectInfo as ObjectInfo )
244248 } catch ( error ) {
245249 Logger . error ( `Get object info operation failed: ${ error } ` )
246250 return response . fail ( `Get object info operation failed: ${ error } ` )
@@ -311,7 +315,9 @@ export const listObjects = async (
311315 network : "testnet" | "mainnet" ,
312316 bucketName : string
313317) : Promise <
314- ApiResponse < { objects : Array < { objectName : string ; createAt : number } > } >
318+ ApiResponse < {
319+ objects : Array < { objectName : string ; createAt : number ; visibility : string } >
320+ } >
315321> => {
316322 try {
317323 const client = getClient ( network )
@@ -339,7 +345,11 @@ export const listObjects = async (
339345 objectsRes . body ?. GfSpListObjectsByBucketNameResponse ?. Objects || [ ]
340346 const objects = res . map ( ( it ) => ( {
341347 objectName : it . ObjectInfo . ObjectName ,
342- createAt : it . ObjectInfo . CreateAt
348+ createAt : it . ObjectInfo . CreateAt ,
349+ visibility :
350+ it . ObjectInfo . Visibility === VisibilityType . VISIBILITY_TYPE_PUBLIC_READ
351+ ? "public"
352+ : "private"
343353 } ) )
344354
345355 return response . success ( { objects } )
@@ -352,18 +362,46 @@ export const listObjects = async (
352362export const downloadObject = async (
353363 network : "testnet" | "mainnet" ,
354364 {
355- privateKey,
356365 bucketName,
357366 objectName,
367+ privateKey,
358368 targetPath
359369 } : {
360- privateKey : Hex
361370 bucketName : string
362371 objectName : string
372+ privateKey ?: Hex
363373 targetPath ?: string
364374 }
365375) : Promise < ApiResponse < { file : string } > > => {
366376 try {
377+ const objectInfo = await getObjectInfo ( network , {
378+ bucketName,
379+ objectName
380+ } )
381+ if ( objectInfo . status === "error" ) {
382+ return response . fail ( objectInfo . message || "Object does not exist" )
383+ }
384+
385+ const objectInfoData = objectInfo . data as ObjectInfo
386+ Logger . debug ( `Object info: ${ JSON . stringify ( objectInfoData ) } ` )
387+ // for public object, use sp endpoint to download
388+ if (
389+ objectInfoData . visibility === VisibilityType . VISIBILITY_TYPE_PUBLIC_READ
390+ ) {
391+ const sp = await selectSp ( network )
392+ return response . success ( {
393+ file : sp . endpoint + "/view/" + bucketName + "/" + objectName
394+ } )
395+ }
396+
397+ // for private object, private key is required
398+ if (
399+ objectInfoData . visibility === VisibilityType . VISIBILITY_TYPE_PRIVATE &&
400+ ! privateKey
401+ ) {
402+ return response . fail ( "Object is private and private key is not provided" )
403+ }
404+
367405 let filePath = ""
368406 if ( ! targetPath || ! existsSync ( targetPath ) ) {
369407 Logger . debug (
@@ -383,7 +421,7 @@ export const downloadObject = async (
383421 } ,
384422 {
385423 type : "ECDSA" ,
386- privateKey : privateKey
424+ privateKey : privateKey || ""
387425 }
388426 )
389427 if ( res . code !== 0 ) {
0 commit comments