generated from idea2app/NodeTS-LeanCloud
-
Notifications
You must be signed in to change notification settings - Fork 12
/
File.ts
60 lines (51 loc) · 1.41 KB
/
File.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import {
JsonController,
Post,
Authorized,
Ctx,
UploadedFile,
Get,
QueryParam,
Delete,
Param,
OnUndefined,
} from 'routing-controllers';
import { File } from '@koa/multer';
import { File as LCFile, ACL, Object as LCObject } from 'leanengine';
import { LCContext, queryPage } from '../utility';
import { RoleController } from './Role';
@JsonController('/file')
export class FileController {
@Post()
@Authorized()
async create(
@Ctx() { currentUser }: LCContext,
@UploadedFile('file') { buffer, originalname }: File,
) {
const acl = new ACL();
acl.setPublicReadAccess(true);
acl.setPublicWriteAccess(false);
acl.setWriteAccess(currentUser, true);
const admin = await RoleController.getAdmin();
if (admin) acl.setRoleWriteAccess(admin, true);
const file = await new LCFile(originalname, buffer).setACL(acl).save();
return file.toJSON() as LCFile;
}
@Get()
@Authorized()
getList(
@QueryParam('pageSize') size: number,
@QueryParam('pageIndex') index: number,
) {
return queryPage(LCFile, { size, index });
}
@Delete('/:id')
@Authorized()
@OnUndefined(204)
async delete(
@Ctx() { currentUser: user }: LCContext,
@Param('id') id: string,
) {
await LCObject.createWithoutData('_File', id).destroy({ user });
}
}