Skip to content

Commit b59b17a

Browse files
authored
Merge pull request #147 from import-ai/refactor/attachments
refactor(attachments): update API
2 parents 5c01f86 + 53dd650 commit b59b17a

13 files changed

+337
-220
lines changed

src/app/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { AttachmentsModule } from 'omniboxd/attachments/attachments.module';
3737
import { Shares1753866547335 } from 'omniboxd/migrations/1753866547335-shares';
3838
import { SharesModule } from 'omniboxd/shares/shares.module';
3939
import { ApiKeys1754550165406 } from 'omniboxd/migrations/1754550165406-api-keys';
40+
import { ResourceAttachments1755059371000 } from 'omniboxd/migrations/1755059371000-resource-attachments';
4041

4142
@Module({})
4243
export class AppModule implements NestModule {
@@ -104,6 +105,7 @@ export class AppModule implements NestModule {
104105
NullUserEmail1752814358259,
105106
Shares1753866547335,
106107
ApiKeys1754550165406,
108+
ResourceAttachments1755059371000,
107109
...extraMigrations,
108110
],
109111
migrationsRun: true,

src/attachments/attachments.controller.ts

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
HttpStatus,
66
Param,
77
Post,
8-
Query,
98
Req,
109
Res,
1110
UploadedFiles,
@@ -15,20 +14,21 @@ import { FilesInterceptor } from '@nestjs/platform-express';
1514
import { AttachmentsService } from 'omniboxd/attachments/attachments.service';
1615
import { Request, Response } from 'express';
1716
import { UserId } from 'omniboxd/decorators/user-id.decorator';
18-
import { CookieAuth } from 'omniboxd/auth';
17+
import { UploadAttachmentsResponseDto } from './dto/upload-attachments-response.dto';
18+
import { CookieAuth } from 'omniboxd/auth/decorators';
1919

20-
@Controller('api/v1/attachments')
20+
@Controller('api/v1/namespaces/:namespaceId/resources/:resourceId/attachments')
2121
export class AttachmentsController {
2222
constructor(private readonly attachmentsService: AttachmentsService) {}
2323

2424
@Post()
2525
@UseInterceptors(FilesInterceptor('file[]'))
2626
async uploadAttachments(
2727
@UserId() userId: string,
28+
@Param('namespaceId') namespaceId: string,
29+
@Param('resourceId') resourceId: string,
2830
@UploadedFiles() files: Express.Multer.File[],
29-
@Query('namespaceId') namespaceId: string,
30-
@Query('resourceId') resourceId: string,
31-
) {
31+
): Promise<UploadAttachmentsResponseDto> {
3232
return await this.attachmentsService.uploadAttachments(
3333
namespaceId,
3434
resourceId,
@@ -38,13 +38,19 @@ export class AttachmentsController {
3838
}
3939

4040
@Get(':attachmentId')
41+
@CookieAuth({ onAuthFail: 'continue' })
4142
async downloadAttachment(
42-
@UserId() userId: string,
43-
@Res() res: Response,
44-
@Query('namespaceId') namespaceId: string,
45-
@Query('resourceId') resourceId: string,
43+
@Req() req: Request,
44+
@UserId({ optional: true }) userId: string | undefined,
45+
@Param('namespaceId') namespaceId: string,
46+
@Param('resourceId') resourceId: string,
4647
@Param('attachmentId') attachmentId: string,
48+
@Res() res: Response,
4749
) {
50+
if (!userId) {
51+
this.setRedirect(req, res);
52+
return;
53+
}
4854
return await this.attachmentsService.downloadAttachment(
4955
namespaceId,
5056
resourceId,
@@ -61,47 +67,11 @@ export class AttachmentsController {
6167
.redirect(`/user/login?redirect=${encodeURIComponent(req.url)}`);
6268
}
6369

64-
@CookieAuth({ onAuthFail: 'continue' })
65-
@Get('images/:attachmentId')
66-
async displayImage(
67-
@UserId({ optional: true }) userId: string | undefined,
68-
@Req() req: Request,
69-
@Res() res: Response,
70-
@Param('attachmentId') attachmentId: string,
71-
) {
72-
if (userId) {
73-
return await this.attachmentsService.displayMedia(
74-
attachmentId,
75-
userId,
76-
res,
77-
);
78-
}
79-
this.setRedirect(req, res);
80-
}
81-
82-
@CookieAuth({ onAuthFail: 'continue' })
83-
@Get('media/:attachmentId')
84-
async displayMedia(
85-
@UserId({ optional: true }) userId: string | undefined,
86-
@Req() req: Request,
87-
@Res() res: Response,
88-
@Param('attachmentId') attachmentId: string,
89-
) {
90-
if (userId) {
91-
return await this.attachmentsService.displayMedia(
92-
attachmentId,
93-
userId,
94-
res,
95-
);
96-
}
97-
this.setRedirect(req, res);
98-
}
99-
10070
@Delete(':attachmentId')
10171
async deleteAttachment(
10272
@UserId() userId: string,
103-
@Query('namespaceId') namespaceId: string,
104-
@Query('resourceId') resourceId: string,
73+
@Param('namespaceId') namespaceId: string,
74+
@Param('resourceId') resourceId: string,
10575
@Param('attachmentId') attachmentId: string,
10676
) {
10777
return await this.attachmentsService.deleteAttachment(

0 commit comments

Comments
 (0)