Skip to content

Commit

Permalink
base64 upload image
Browse files Browse the repository at this point in the history
  • Loading branch information
zcpua committed Jul 16, 2023
1 parent 5e70f63 commit 080b85f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import {
} from '@nestjs/common';
import { MidjourneyService } from './midjourney.service';
import { FastifyReply } from 'fastify';
import { AvatarBody, CustomBody, PromptMessageBody } from './interfaces';
import {
AvatarBody,
Base64Body,
CustomBody,
PromptMessageBody,
} from './interfaces';
@Controller('midjourney')
export class AppController {
constructor(private readonly MjService: MidjourneyService) {}
Expand All @@ -27,6 +32,23 @@ export class AppController {
res.raw.end();
}

@Post('base64')
@UseInterceptors()
async Base64(@Body() data: Base64Body, @Res() res: FastifyReply) {
const uri = await this.MjService.UploadImg(data.img);
res.send({ uri });
// res.raw.setHeader('Content-Type', 'text/html; charset=utf-8');
// res.raw.setHeader('Transfer-Encoding', 'chunked');
// res.raw.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
// res.raw.setHeader('Pragma', 'no-cache');
// res.raw.setHeader('Expires', '0');
// const msg = await this.MjService.Avatar(data.img, (uri, progress) => {
// res.raw.write(JSON.stringify({ uri, progress }));
// });
// res.raw.write(JSON.stringify(msg));
// res.raw.end();
}

@Post('avatar')
@UseInterceptors()
async Avatar(@Body() data: AvatarBody, @Res() res: FastifyReply) {
Expand Down
3 changes: 3 additions & 0 deletions src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export type OptionMessageBody = {
export type AvatarBody = {
img: string;
};
export type Base64Body = {
img: string;
};

export type CustomBody = {
flags: number;
Expand Down
5 changes: 5 additions & 0 deletions src/midjourney.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export class MidjourneyService implements OnModuleInit {
});
return msg;
}
async UploadImg(img: string) {
const blob = await base64ToBlob(img);
const desc = await this.Midjourney.DescribeByBlob(blob);
return desc.uri;
}

async Custom(
msgId: string,
Expand Down

0 comments on commit 080b85f

Please sign in to comment.