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