blob: a7eb30c50f5e37366d6f28ef7f458f50761ff393 (
plain)
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
|
import { HookContext } from '@feathersjs/feathers';
import Bluebird from 'bluebird';
import _ from 'lodash';
import Debug from 'debug';
const debug = Debug('s3-reuploads');
export default (paths: string[]) => async (context: HookContext): Promise<HookContext> => {
const {
service,
app,
result,
params: { user }
} = context;
const fileService = app.service('files');
const model = service.Model;
Bluebird.map(paths, async (path: string) => {
const url = _.get(result, path);
// If image is not from our s3, fetch it!
if (!fileService.isS3url(url)) {
debug('Found non-s3 url!');
const filePath = await fileService.downloadFile(url);
const s3Path = fileService.generateS3Path(user?.username);
const s3Url = await fileService.uploadFileToS3(filePath, s3Path);
await model.findOneAndUpdate({ _id: result._id }, { $set: { [path]: s3Url } });
debug(`Fetched and updated: from ${url} to ${s3Url}`);
return s3Url;
}
return url;
});
return context;
};
|