Skip to content

Commit 4d980e0

Browse files
committed
add /lifeliners/private/verification-photo return photo from gcs
1 parent 7d694ac commit 4d980e0

3 files changed

Lines changed: 27 additions & 5 deletions

File tree

src/lifeliners/lifeliners.controller.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import {
77
Patch,
88
Post,
99
Query,
10+
Res,
1011
UploadedFile,
1112
UseGuards,
1213
UseInterceptors,
1314
} from '@nestjs/common';
15+
import type { Response } from 'express';
1416
import { FileInterceptor } from '@nestjs/platform-express';
1517
import { Role } from '@prisma/client';
1618
import { CurrentUser } from '../auth/decorators/current-user.decorator';
@@ -116,7 +118,13 @@ export class LifelinersController {
116118
@Get('private/verification-photo')
117119
@UseGuards(JwtAuthGuard, RolesGuard)
118120
@Roles(Role.LIFELINER)
119-
async getVerificationPhoto(@CurrentUser() user: AuthUser) {
120-
return this.lifelinersService.getVerificationPhotoUrl(user.id);
121+
async getVerificationPhoto(
122+
@CurrentUser() user: AuthUser,
123+
@Res({ passthrough: true }) res: Response,
124+
) {
125+
const { stream, contentType } =
126+
await this.lifelinersService.getVerificationPhoto(user.id);
127+
res.setHeader('Content-Type', contentType);
128+
stream.pipe(res);
121129
}
122130
}

src/lifeliners/lifeliners.service.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,10 @@ export class LifelinersService {
179179
});
180180
}
181181

182-
async getVerificationPhotoUrl(userId: string): Promise<{ url: string }> {
182+
async getVerificationPhoto(userId: string): Promise<{
183+
stream: NodeJS.ReadableStream;
184+
contentType: string;
185+
}> {
183186
const lifeliner = await this.prisma.lifeliner.findUnique({
184187
where: { user_id: userId },
185188
select: { private_picture: true },
@@ -188,8 +191,7 @@ export class LifelinersService {
188191
if (!lifeliner.private_picture)
189192
throw new NotFoundException('No verification photo uploaded');
190193

191-
const url = await this.storage.getSignedUrl(lifeliner.private_picture);
192-
return { url };
194+
return this.storage.download(lifeliner.private_picture);
193195
}
194196

195197
private extractGcsPath(url: string): string | null {

src/storage/storage.service.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,16 @@ export class StorageService {
5151
});
5252
return url;
5353
}
54+
55+
async download(filePath: string): Promise<{
56+
stream: NodeJS.ReadableStream;
57+
contentType: string;
58+
}> {
59+
const file = this.bucket.file(this.prefix(filePath));
60+
const [metadata] = await file.getMetadata();
61+
return {
62+
stream: file.createReadStream(),
63+
contentType: metadata.contentType ?? 'application/octet-stream',
64+
};
65+
}
5466
}

0 commit comments

Comments
 (0)