diff options
author | eug-vs <eug-vs@keemail.me> | 2020-08-12 19:02:38 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2020-08-12 19:02:38 +0300 |
commit | 8f4ae4fd89cab5ba4f9e8d2750bc8589ce997ff1 (patch) | |
tree | 1bbe4629b0a0ee537a21be15c91fd31b243df97d | |
parent | 4b2397d68b62d9f5682d1fd9f4fb2082ac4aa260 (diff) | |
download | which-api-8f4ae4fd89cab5ba4f9e8d2750bc8589ce997ff1.tar.gz |
fix: delete files after upload
-rw-r--r-- | .gitignore | 4 | ||||
-rw-r--r-- | hooks/fetchImages.ts | 8 | ||||
-rw-r--r-- | services/files/files.class.ts | 44 |
3 files changed, 33 insertions, 23 deletions
@@ -1,2 +1,4 @@ /node_modules -/.idea
\ No newline at end of file +/.idea +/tmp +.env diff --git a/hooks/fetchImages.ts b/hooks/fetchImages.ts index d6e7d27..44aac6c 100644 --- a/hooks/fetchImages.ts +++ b/hooks/fetchImages.ts @@ -4,7 +4,13 @@ import _ from 'lodash'; export default (paths: string[]) => async (context: HookContext): Promise<HookContext> => { - const { service, app, result, params: { user } } = context; + const { + service, + app, + result, + params: { user } + } = context; + const fileService = app.service('files'); const model = service.Model; diff --git a/services/files/files.class.ts b/services/files/files.class.ts index 7e4bcbf..f2a0960 100644 --- a/services/files/files.class.ts +++ b/services/files/files.class.ts @@ -3,18 +3,20 @@ import { Params } from '@feathersjs/feathers'; import { v4 } from 'uuid'; import axios from 'axios'; import fs from 'fs'; -import path from 'path'; -import { User } from 'which-types'; // Use require to avoid bug // https://stackoverflow.com/questions/62611373/heroku-crashes-when-importing-aws-sdk +// TODO: use import statement +// eslint-disable-next-line const S3 = require('aws-sdk/clients/s3'); export default class Files { - app!: Application; - s3!: any; - bucket!: string; + public app!: Application; + + private s3!: typeof S3; + + private bucket!: string; async find(params: Params): Promise<string> { const path = this.generateS3Path(params.user?.username); @@ -22,10 +24,10 @@ export default class Files { } public isS3url(url: string): boolean { - return url.startsWith('https://${this.bucket}.s3'); + return url.startsWith(`https://${this.bucket}.s3`); } - public generateS3Path(prefix='', ext='png'): string { + public generateS3Path(prefix = '', ext = 'png'): string { const key = v4(); const fileName = `${key}.${ext}`; return prefix ? `${prefix}/${fileName}` : fileName; @@ -37,7 +39,7 @@ export default class Files { Bucket: this.bucket, Key: path, ContentType: 'image/*', - Expires: 300, + Expires: 300 }); } @@ -45,7 +47,7 @@ export default class Files { return this.getUploadUrl(path).then((url: string) => { const queryIndex = url.indexOf('?'); return url.slice(0, queryIndex); - }) + }); } private createTmpDir() { @@ -53,29 +55,29 @@ export default class Files { } async downloadFile(url: string): Promise<string> { - return new Promise(async (resolve, reject) => { + return new Promise((resolve, reject) => { this.createTmpDir(); const filePath = `tmp/${v4()}`; const fileStream = fs.createWriteStream(filePath); - const response = await axios.get(url, { responseType: 'stream' }) - response.data.pipe(fileStream) - .on('error', reject) - .on('close', () => resolve(filePath)); + axios.get(url, { responseType: 'stream' }) + .then(response => { + response.data.pipe(fileStream) + .on('error', reject) + .on('close', () => resolve(filePath)); + }) + .catch(error => reject(error)); }); } - async uploadFileToS3(filePath: string, s3Path: string) { + async uploadFileToS3(filePath: string, s3Path: string): Promise<string> { const fileStream = fs.createReadStream(filePath); - const request = this.s3.upload({ + await this.s3.upload({ Bucket: this.bucket, Key: s3Path, Body: fileStream, ContentType: 'image/png' - }); - request.on('httpUploadProgress', progress => { - console.log('progress', progress); - }) - await request.promise(); + }).promise(); + fs.unlinkSync(filePath); return this.getDownloadUrl(s3Path); } |