@@ -20,10 +20,11 @@ import {GetTradesDto} from "./dto/trade.dto";
2020import { AddUserDto , GetUsersDto } from "./dto/user.dto" ;
2121import { UserService } from "./user/user.service" ;
2222import { FileInterceptor } from "@nestjs/platform-express" ;
23- import { Storage } from '@google-cloud/storage'
2423import * as path from "node:path" ;
2524import { GcloudService } from "./gcloud/gcloud.service" ;
2625const serviceKey = path . join ( __dirname , './keys.json' )
26+ import { v4 as uuidv4 } from 'uuid' ;
27+ import { AddTokenMetadataDto } from "./dto/metadata.dto" ;
2728
2829@SkipThrottle ( )
2930@ApiTags ( 'app' )
@@ -97,10 +98,35 @@ export class AppController {
9798
9899 @Post ( '/uploadImage' )
99100 @UseInterceptors ( FileInterceptor ( 'file' ) )
100- async uploadFile ( @UploadedFile ( ) uploadedFile : Express . Multer . File , @Headers ( ) headers ) {
101+ async uploadImage ( @UploadedFile ( ) uploadedFile : Express . Multer . File , @Headers ( ) headers ) {
101102 const userAddress = headers [ 'meta_user_address' ]
102- const publicImageUrl = await this . gCloudService . uploadImage ( userAddress , uploadedFile )
103- this . logger . log ( `Image uploaded, publicUrl=${ publicImageUrl } , userAddress=${ userAddress } ` )
104- return publicImageUrl
103+ const uuid = uuidv4 ( )
104+ const imageUrl = await this . gCloudService . uploadImage ( uploadedFile , uuid )
105+ this . logger . log ( `Image uploaded, imageUrl=${ imageUrl } , userAddress=${ userAddress } ` )
106+ return imageUrl
107+ }
108+
109+ @Post ( '/metadata' )
110+ async addMetadata ( @Body ( ) dto : AddTokenMetadataDto ) {
111+ let uuid = ''
112+
113+ if ( ! dto . image ) {
114+ throw new BadRequestException ( 'Image property is missing' )
115+ }
116+
117+ const imageItems = dto . image . split ( '/' )
118+ if ( imageItems . length > 0 ) {
119+ uuid = imageItems [ imageItems . length - 1 ] . split ( '.' ) [ 0 ]
120+ } else {
121+ throw new BadRequestException ( 'Invalid image url' )
122+ }
123+
124+ if ( ! uuid ) {
125+ throw new BadRequestException ( 'Failed to get uuid' )
126+ }
127+
128+ const metadataUrl = await this . gCloudService . uploadMetadata ( dto , uuid )
129+ this . logger . log ( `Metadata uploaded, url=${ metadataUrl } , content: ${ JSON . stringify ( dto ) } ` )
130+ return metadataUrl
105131 }
106132}
0 commit comments