|
1 | 1 | import { Application } from '@feathersjs/express';
|
2 | 2 | import { Params } from '@feathersjs/feathers';
|
3 | 3 | import { v4 } from 'uuid';
|
| 4 | +import axios from 'axios'; |
| 5 | +import fs from 'fs'; |
4 | 6 |
|
5 | 7 | // Use require to avoid bug
|
6 | 8 | // https://stackoverflow.com/questions/62611373/heroku-crashes-when-importing-aws-sdk
|
| 9 | +// TODO: use import statement |
| 10 | +// eslint-disable-next-line |
7 | 11 | const S3 = require('aws-sdk/clients/s3');
|
8 | 12 |
|
9 | 13 |
|
10 | 14 | export default class Files {
|
11 |
| - app!: Application; |
12 |
| - s3!: any; |
13 |
| - bucket!: string; |
| 15 | + public app!: Application; |
| 16 | + |
| 17 | + private s3!: typeof S3; |
| 18 | + |
| 19 | + private bucket!: string; |
14 | 20 |
|
15 | 21 | async find(params: Params): Promise<string> {
|
| 22 | + const path = this.generateS3Path(params.user?.username); |
| 23 | + return this.getUploadUrl(path); |
| 24 | + } |
| 25 | + |
| 26 | + public isS3url(url: string): boolean { |
| 27 | + return url.startsWith(`https://${this.bucket}.s3`); |
| 28 | + } |
| 29 | + |
| 30 | + public generateS3Path(prefix = '', ext = 'png'): string { |
| 31 | + const key = v4(); |
| 32 | + const fileName = `${key}.${ext}`; |
| 33 | + return prefix ? `${prefix}/${fileName}` : fileName; |
| 34 | + } |
| 35 | + |
| 36 | + async getUploadUrl(path: string): Promise<string> { |
16 | 37 | // Return signed upload URL
|
17 | 38 | return this.s3.getSignedUrl('putObject', {
|
18 | 39 | Bucket: this.bucket,
|
19 |
| - Key: `${params.user?.username}/${v4()}.png`, |
| 40 | + Key: path, |
20 | 41 | ContentType: 'image/*',
|
21 |
| - Expires: 300, |
| 42 | + Expires: 300 |
22 | 43 | });
|
23 | 44 | }
|
24 | 45 |
|
| 46 | + async getDownloadUrl(path: string): Promise<string> { |
| 47 | + return this.getUploadUrl(path).then((url: string) => { |
| 48 | + const queryIndex = url.indexOf('?'); |
| 49 | + return url.slice(0, queryIndex); |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + private createTmpDir() { |
| 54 | + if (!fs.existsSync('tmp')) fs.mkdirSync('tmp'); |
| 55 | + } |
| 56 | + |
| 57 | + async downloadFile(url: string): Promise<string> { |
| 58 | + return new Promise((resolve, reject) => { |
| 59 | + this.createTmpDir(); |
| 60 | + const filePath = `tmp/${v4()}`; |
| 61 | + const fileStream = fs.createWriteStream(filePath); |
| 62 | + axios.get(url, { responseType: 'stream' }) |
| 63 | + .then(response => { |
| 64 | + response.data.pipe(fileStream) |
| 65 | + .on('error', reject) |
| 66 | + .on('close', () => resolve(filePath)); |
| 67 | + }) |
| 68 | + .catch(error => reject(error)); |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + async uploadFileToS3(filePath: string, s3Path: string): Promise<string> { |
| 73 | + const fileStream = fs.createReadStream(filePath); |
| 74 | + await this.s3.upload({ |
| 75 | + Bucket: this.bucket, |
| 76 | + Key: s3Path, |
| 77 | + Body: fileStream, |
| 78 | + ContentType: 'image/png' |
| 79 | + }).promise(); |
| 80 | + fs.unlinkSync(filePath); |
| 81 | + return this.getDownloadUrl(s3Path); |
| 82 | + } |
| 83 | + |
25 | 84 | setup(app: Application): void {
|
26 | 85 | this.app = app;
|
27 | 86 | this.s3 = new S3({
|
|
0 commit comments