Skip to content

Commit aae224e

Browse files
committed
[add] File controller
[optimize] replace PATCH method with PUT to reuse Model classes [fix] some detail bugs Signed-off-by: TechQuery <shiy2008@gmail.com>
1 parent adc5196 commit aae224e

27 files changed

+1427
-160
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@kaiyuanshe/openhackathon-service",
3-
"version": "0.21.1",
3+
"version": "0.22.0",
44
"license": "LGPL-3.0",
55
"author": "shiy2008@gmail.com",
66
"description": "RESTful API service scaffold based on Node.js & TypeScript",
@@ -10,6 +10,8 @@
1010
"node": ">=22"
1111
},
1212
"dependencies": {
13+
"@aws-sdk/client-s3": "^3.821.0",
14+
"@aws-sdk/s3-request-presigner": "^3.821.0",
1315
"@koa/cors": "^5.0.0",
1416
"@koa/multer": "^4.0.0",
1517
"@koa/router": "^13.1.0",

pnpm-lock.yaml

Lines changed: 1205 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/controller/Announcement.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import {
1010
OnNull,
1111
OnUndefined,
1212
Param,
13-
Patch,
1413
Post,
14+
Put,
1515
QueryParams
1616
} from 'routing-controllers';
1717
import { ResponseSchema } from 'routing-controllers-openapi';
@@ -61,7 +61,7 @@ export class AnnouncementController {
6161
return saved;
6262
}
6363

64-
@Patch('/:id')
64+
@Put('/:id')
6565
@Authorized()
6666
@ResponseSchema(Announcement)
6767
async updateOne(

source/controller/Enrollment.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {
88
JsonController,
99
NotFoundError,
1010
Param,
11-
Patch,
1211
Post,
12+
Put,
1313
QueryParams
1414
} from 'routing-controllers';
1515
import { ResponseSchema } from 'routing-controllers-openapi';
@@ -47,7 +47,7 @@ export class EnrollmentController {
4747
return store.findOneBy({ createdBy });
4848
}
4949

50-
@Patch('/:id')
50+
@Put('/:id')
5151
@Authorized()
5252
@ResponseSchema(Enrollment)
5353
async updateOne(

source/controller/File.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { DeleteObjectCommand, PutObjectCommand } from '@aws-sdk/client-s3';
2+
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
3+
import {
4+
Authorized,
5+
Controller,
6+
CurrentUser,
7+
Delete,
8+
HttpCode,
9+
OnUndefined,
10+
Param,
11+
Post
12+
} from 'routing-controllers';
13+
import { ResponseSchema } from 'routing-controllers-openapi';
14+
15+
import { SignedLink, User } from '../model';
16+
import { AWS_S3_BUCKET, AWS_S3_PUBLIC_HOST, s3Client } from '../utility';
17+
18+
@Controller('/file')
19+
export class FileController {
20+
@Post('/signed-link/:path(.*)')
21+
@Authorized()
22+
@HttpCode(201)
23+
@ResponseSchema(SignedLink)
24+
async createSignedLink(
25+
@CurrentUser() { id }: User,
26+
@Param('path') path: string
27+
) {
28+
const Key = `user/${id}/${path}`;
29+
30+
const command = new PutObjectCommand({ Bucket: AWS_S3_BUCKET, Key });
31+
32+
const putLink = await getSignedUrl(s3Client, command);
33+
34+
return { putLink, getLink: `${AWS_S3_PUBLIC_HOST}/${Key}` };
35+
}
36+
37+
@Delete('/:path(.*)')
38+
@Authorized()
39+
@OnUndefined(204)
40+
async deleteFile(@CurrentUser() { id }: User, @Param('path') path: string) {
41+
const Key = `user/${id}/${path}`;
42+
43+
const command = new DeleteObjectCommand({ Bucket: AWS_S3_BUCKET, Key });
44+
45+
await s3Client.send(command);
46+
}
47+
}

source/controller/Hackathon.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {
2121
dataSource,
2222
Hackathon,
2323
HackathonFilter,
24-
HackathonInput,
2524
HackathonListChunk,
2625
StaffType,
2726
User
@@ -60,7 +59,7 @@ export class HackathonController {
6059
async updateOne(
6160
@CurrentUser() updatedBy: User,
6261
@Param('name') name: string,
63-
@Body() newData: HackathonInput
62+
@Body() newData: Hackathon
6463
) {
6564
const old = await store.findOne({
6665
where: { name },
@@ -124,7 +123,7 @@ export class HackathonController {
124123
@ResponseSchema(Hackathon)
125124
async createOne(
126125
@CurrentUser() createdBy: User,
127-
@Body() hackathon: HackathonInput
126+
@Body() hackathon: Hackathon
128127
) {
129128
const saved = await store.save({ ...hackathon, createdBy });
130129

source/controller/OAuth.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'core-js/full/array/from-async';
2-
31
import { githubClient, User as GitHubUser } from 'mobx-github';
42
import { Body, HttpCode, JsonController, Post } from 'routing-controllers';
53
import { ResponseSchema } from 'routing-controllers-openapi';

source/controller/Organizer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99
NotFoundError,
1010
OnUndefined,
1111
Param,
12-
Patch,
1312
Post,
13+
Put,
1414
QueryParams
1515
} from 'routing-controllers';
1616
import { ResponseSchema } from 'routing-controllers-openapi';
@@ -59,7 +59,7 @@ export class OrganizerController {
5959
return saved;
6060
}
6161

62-
@Patch('/:id')
62+
@Put('/:id')
6363
@Authorized()
6464
@ResponseSchema(Organizer)
6565
async updateOne(

source/controller/Staff.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
NotFoundError,
1111
OnUndefined,
1212
Param,
13-
Patch,
1413
Put,
1514
QueryParams
1615
} from 'routing-controllers';
@@ -96,7 +95,7 @@ export class StaffController {
9695
});
9796
}
9897

99-
@Patch('/:uid')
98+
@Put('/:uid')
10099
@Authorized()
101100
@ResponseSchema(Staff)
102101
async updateOne(

source/controller/Team.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import {
1111
OnNull,
1212
OnUndefined,
1313
Param,
14-
Patch,
1514
Post,
15+
Put,
1616
QueryParams
1717
} from 'routing-controllers';
1818
import { ResponseSchema } from 'routing-controllers-openapi';
@@ -92,7 +92,7 @@ export class TeamController {
9292
return saved;
9393
}
9494

95-
@Patch('/:id')
95+
@Put('/:id')
9696
@Authorized()
9797
@ResponseSchema(Team)
9898
async updateOne(

0 commit comments

Comments
 (0)