-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththumbnails.ts
More file actions
50 lines (45 loc) · 1.19 KB
/
thumbnails.ts
File metadata and controls
50 lines (45 loc) · 1.19 KB
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
import { Express } from 'express';
import { BucketPolicy, BucketPolicyError, createApp, ObjectMeta, S3Bucket } from '../src';
import { SharpTransformer } from '../src/transformers/sharp';
import { s3client } from './s3';
export const imagesOnlyPolicy: BucketPolicy = {
async head(_path: string) {
},
async get(_path: string) {
},
async put(meta: ObjectMeta) {
if (!meta.contentType.startsWith('image/')) {
throw new BucketPolicyError('Only images are allowed in this bucket.');
}
},
async delete(_path: string) {
},
async list(_prefix?: string) {
},
async mkdir(_prefix?: string) {
},
};
/**
* This example combines a custom policy that only allows image files and
* a transformer that resize the images to a thumbnail-like size.
*
* (bucket based on an s3 bucket)
*/
export default (expressApp: Express, _seshatRootDir: string) => {
const imageResizer = new SharpTransformer({
output: {
format: 'png',
},
resize: {
width: 400,
},
});
expressApp.use('/thumbnails', createApp({
bucket: new S3Bucket({
s3client,
bucket: 'my-s3-bucket',
policies: [imagesOnlyPolicy],
transformers: [imageResizer],
}),
}));
};