-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImageStore.ts
63 lines (51 loc) · 1.8 KB
/
ImageStore.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
61
62
63
import AWS from 'aws-sdk';
import { Readable } from 'stream';
import Cfg from './Cfg';
export default class ImageStore {
static getFileUrl(gameName: string, frameId: string) {
const fileName = this.getFileName(gameName, frameId);
return `https://${Cfg.imageStoreAwsBucket}.s3-us-west-2.amazonaws.com/${fileName}`;
}
static getFileName(gameName: string, frameId: string) {
return `${gameName}/${frameId}.png`;
}
static getAvatarFileUrl(id: string) {
const fileName = this.getAvatarFileName(id);
return `https://${Cfg.imageStoreAwsBucket}.s3-us-west-2.amazonaws.com/${fileName}`;
}
static getAvatarFileName(id: string) {
return `avatars/${id}.png`;
}
static async uploadAvatar(id: string, data: Readable): Promise<{ fileName: string; fileUrl: string }> {
const s3 = new AWS.S3();
const fileName = this.getAvatarFileName(id);
const fileUrl = this.getAvatarFileUrl(id);
await s3
.upload({
Bucket: Cfg.imageStoreAwsBucket,
Key: fileName,
Body: data,
ContentType: 'image/png',
})
.promise();
return { fileName, fileUrl };
}
static async uploadImage(
gameName: string,
frameId: string,
data: Readable
): Promise<{ fileName: string; fileUrl: string }> {
const s3 = new AWS.S3();
const fileName = this.getFileName(gameName, frameId);
const fileUrl = this.getFileUrl(gameName, frameId);
await s3
.upload({
Bucket: Cfg.imageStoreAwsBucket,
Key: fileName,
Body: data,
ContentType: 'image/png',
})
.promise();
return { fileName, fileUrl };
}
}