Skip to content

Commit

Permalink
feat(be): add upload nft image api
Browse files Browse the repository at this point in the history
  • Loading branch information
harisato committed Oct 11, 2023
1 parent 5ef5b8f commit 7778a51
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ScheduleModule } from '@nestjs/schedule';
import { TasksModule } from './task/task.module';
import { CreatorModule } from './creator/creator.module';
import { APP_PIPE } from '@nestjs/core';
import { QuestModule } from './quest/quest.module';

@Module({
imports: [
Expand All @@ -27,6 +28,7 @@ import { APP_PIPE } from '@nestjs/core';
FilesModule,
GraphqlModule,
UserModule,
QuestModule,
],
controllers: [],
providers: [
Expand Down
15 changes: 3 additions & 12 deletions src/files/files.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,20 @@ export class FilesService {
if (!f.mimetype.includes('image')) {
throw Error('file type is not valid');
}
// const keyName = `manga-${mangaId}/${f.fieldname}.${f.originalname
// .split('.')
// .pop()}`;

const s3SubFolder =
this.configService.get<string>('aws.s3SubFolder') || 'images';
const keyName = `${s3SubFolder}/${key}/${f.originalname}`;

const extension = f.originalname.split('.').pop();
await this.uploadToS3(keyName, f.buffer, f.mimetype, extension);
await this.uploadToS3(keyName, f.buffer, f.mimetype);
return new URL(keyName, this.configService.get<string>('aws.queryEndpoint'))
.href;
}

async uploadToS3(
keyName: string,
filePath: string | Buffer,
mimetype: string,
extension?: string
mimetype: string
) {
const file =
typeof filePath === 'string' ? readFileSync(filePath) : filePath;
Expand All @@ -118,11 +114,6 @@ export class FilesService {
ContentType: mimetype,
};

if (extension && extension === 'svg')
input.Metadata = {
'Content-Type': 'image/svg+xml',
};

// Create a promise on S3 service object
const command = new PutObjectCommand(input);
return client.send(command);
Expand Down
6 changes: 6 additions & 0 deletions src/quest/dto/upload-nft-image.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';

export class UploadNftImageRequestDto {
@ApiProperty({ type: 'string', format: 'binary' })
file: Express.Multer.File;
}
36 changes: 36 additions & 0 deletions src/quest/quest.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {
Body,
Controller,
Post,
UploadedFiles,
UseGuards,
UseInterceptors,
} from '@nestjs/common';
import { AuthGuard } from '../auth/auth.guard';
import { ApiBearerAuth, ApiConsumes, ApiTags } from '@nestjs/swagger';
import { AuthUserInterceptor } from '../interceptors/auth-user.interceptor';
import { AnyFilesInterceptor } from '@nestjs/platform-express';
import { Roles } from '../auth/roles.decorator';
import { Role } from '../auth/role.enum';
import { RolesGuard } from '../auth/role.guard';
import { UploadNftImageRequestDto } from './dto/upload-nft-image.dto';
import { QuestService } from './quest.service';

@Controller('quest')
@ApiTags('quest')
export class QuestController {
constructor(private readonly questSvc: QuestService) {}

@UseGuards(AuthGuard, RolesGuard)
@ApiBearerAuth()
@Roles(Role.Admin)
@Post('upload')
@ApiConsumes('multipart/form-data')
@UseInterceptors(AuthUserInterceptor, AnyFilesInterceptor())
upload(
@Body() data: UploadNftImageRequestDto,
@UploadedFiles() files: Array<Express.Multer.File>
) {
return this.questSvc.upload(files.filter((f) => f.fieldname === 'file')[0]);
}
}
13 changes: 13 additions & 0 deletions src/quest/quest.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Module } from '@nestjs/common';
import { QuestService } from './quest.service';
import { FilesModule } from '../files/files.module';
import { QuestController } from './quest.controller';
import { JwtModule } from '@nestjs/jwt';

@Module({
imports: [FilesModule, JwtModule],
providers: [QuestService],
controllers: [QuestController],
exports: [],
})
export class QuestModule {}
25 changes: 25 additions & 0 deletions src/quest/quest.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Injectable, Logger } from '@nestjs/common';

import { FilesService } from '../files/files.service';

@Injectable()
export class QuestService {
private readonly logger = new Logger(QuestService.name);

constructor(private filesService: FilesService) {}

async upload(file: Express.Multer.File) {
try {
const url = await this.filesService.uploadImageToS3(`nft`, file);

this.logger.debug(`uploading nft image ${file.originalname} success`);
return {
url,
};
} catch (errors) {
return {
errors,
};
}
}
}

0 comments on commit 7778a51

Please sign in to comment.