Skip to content

Commit 8f4ae4f

Browse files
committed
fix: delete files after upload
1 parent 4b2397d commit 8f4ae4f

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/node_modules
2-
/.idea
2+
/.idea
3+
/tmp
4+
.env

hooks/fetchImages.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import _ from 'lodash';
44

55

66
export default (paths: string[]) => async (context: HookContext): Promise<HookContext> => {
7-
const { service, app, result, params: { user } } = context;
7+
const {
8+
service,
9+
app,
10+
result,
11+
params: { user }
12+
} = context;
13+
814
const fileService = app.service('files');
915
const model = service.Model;
1016

services/files/files.class.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,31 @@ import { Params } from '@feathersjs/feathers';
33
import { v4 } from 'uuid';
44
import axios from 'axios';
55
import fs from 'fs';
6-
import path from 'path';
7-
import { User } from 'which-types';
86

97
// Use require to avoid bug
108
// https://stackoverflow.com/questions/62611373/heroku-crashes-when-importing-aws-sdk
9+
// TODO: use import statement
10+
// eslint-disable-next-line
1111
const S3 = require('aws-sdk/clients/s3');
1212

1313

1414
export default class Files {
15-
app!: Application;
16-
s3!: any;
17-
bucket!: string;
15+
public app!: Application;
16+
17+
private s3!: typeof S3;
18+
19+
private bucket!: string;
1820

1921
async find(params: Params): Promise<string> {
2022
const path = this.generateS3Path(params.user?.username);
2123
return this.getUploadUrl(path);
2224
}
2325

2426
public isS3url(url: string): boolean {
25-
return url.startsWith('https://${this.bucket}.s3');
27+
return url.startsWith(`https://${this.bucket}.s3`);
2628
}
2729

28-
public generateS3Path(prefix='', ext='png'): string {
30+
public generateS3Path(prefix = '', ext = 'png'): string {
2931
const key = v4();
3032
const fileName = `${key}.${ext}`;
3133
return prefix ? `${prefix}/${fileName}` : fileName;
@@ -37,45 +39,45 @@ export default class Files {
3739
Bucket: this.bucket,
3840
Key: path,
3941
ContentType: 'image/*',
40-
Expires: 300,
42+
Expires: 300
4143
});
4244
}
4345

4446
async getDownloadUrl(path: string): Promise<string> {
4547
return this.getUploadUrl(path).then((url: string) => {
4648
const queryIndex = url.indexOf('?');
4749
return url.slice(0, queryIndex);
48-
})
50+
});
4951
}
5052

5153
private createTmpDir() {
5254
if (!fs.existsSync('tmp')) fs.mkdirSync('tmp');
5355
}
5456

5557
async downloadFile(url: string): Promise<string> {
56-
return new Promise(async (resolve, reject) => {
58+
return new Promise((resolve, reject) => {
5759
this.createTmpDir();
5860
const filePath = `tmp/${v4()}`;
5961
const fileStream = fs.createWriteStream(filePath);
60-
const response = await axios.get(url, { responseType: 'stream' })
61-
response.data.pipe(fileStream)
62-
.on('error', reject)
63-
.on('close', () => resolve(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));
6469
});
6570
}
6671

67-
async uploadFileToS3(filePath: string, s3Path: string) {
72+
async uploadFileToS3(filePath: string, s3Path: string): Promise<string> {
6873
const fileStream = fs.createReadStream(filePath);
69-
const request = this.s3.upload({
74+
await this.s3.upload({
7075
Bucket: this.bucket,
7176
Key: s3Path,
7277
Body: fileStream,
7378
ContentType: 'image/png'
74-
});
75-
request.on('httpUploadProgress', progress => {
76-
console.log('progress', progress);
77-
})
78-
await request.promise();
79+
}).promise();
80+
fs.unlinkSync(filePath);
7981
return this.getDownloadUrl(s3Path);
8082
}
8183

0 commit comments

Comments
 (0)