@@ -21,6 +21,18 @@ export class AmazonGateway {
21
21
return `${ this . endpointUrl } /${ methodName } ` ;
22
22
}
23
23
24
+ logRequest ( method : string , url : URL , requestUrl : string ) : void {
25
+ if ( ! this . onRequestExecuted ) {
26
+ return ;
27
+ }
28
+ const params = {
29
+ method,
30
+ urlPath : requestUrl ,
31
+ queryString : url . toString ( ) . replace ( requestUrl , '' ) ,
32
+ } ;
33
+ this . onRequestExecuted ( params ) ;
34
+ }
35
+
24
36
removeUploadData ( fileName : string ) : void {
25
37
const index = this . uploadData . findIndex ( ( data ) => data . key === fileName ) ;
26
38
if ( index !== - 1 ) {
@@ -47,43 +59,63 @@ export class AmazonGateway {
47
59
async getItems ( path : string ) : Promise < any > {
48
60
const params = { path } ;
49
61
const requestParams = { method : 'GET' } ;
50
- return this . makeRequestAsync ( 'getItems' , params , requestParams ) ;
62
+ return this . makeRequest ( 'getItems' , params , requestParams ) ;
51
63
}
52
64
53
65
async createDirectory ( path : string , name : string ) : Promise < void > {
54
66
const params = { path, name } ;
55
67
const requestParams = { method : 'PUT' } ;
56
- await this . makeRequestAsync ( 'createDirectory' , params , requestParams ) ;
68
+ await this . makeRequest ( 'createDirectory' , params , requestParams ) ;
57
69
}
58
70
59
71
async renameItem ( key : string , parentPath : string , name : string ) : Promise < any > {
60
72
const params = { key, directory : parentPath , newName : name } ;
61
73
const requestParams = { method : 'PUT' } ;
62
- await this . makeRequestAsync ( 'renameItem' , params , requestParams ) ;
74
+ await this . makeRequest ( 'renameItem' , params , requestParams ) ;
63
75
}
64
76
65
77
async deleteItem ( key : string ) : Promise < any > {
66
78
const params = { item : key } ;
67
79
const requestParams = { method : 'POST' } ;
68
- await this . makeRequestAsync ( 'deleteItem' , params , requestParams ) ;
80
+ await this . makeRequest ( 'deleteItem' , params , requestParams ) ;
69
81
}
70
82
71
83
async copyItem ( sourceKey : string , destinationKey : string ) : Promise < any > {
72
84
const params = { sourceKey, destinationKey } ;
73
85
const requestParams = { method : 'PUT' } ;
74
- await this . makeRequestAsync ( 'copyItem' , params , requestParams ) ;
86
+ await this . makeRequest ( 'copyItem' , params , requestParams ) ;
75
87
}
76
88
77
89
async moveItem ( sourceKey : string , destinationKey : string ) : Promise < any > {
78
90
const params = { sourceKey, destinationKey } ;
79
91
const requestParams = { method : 'POST' } ;
80
- await this . makeRequestAsync ( 'moveItem' , params , requestParams ) ;
92
+ await this . makeRequest ( 'moveItem' , params , requestParams ) ;
81
93
}
82
94
83
95
async downloadItems ( keys : string [ ] ) : Promise < any > {
84
96
const params = { } ;
85
97
const requestParams = { method : 'POST' , body : JSON . stringify ( keys ) , headers : this . defaultHeaders } ;
86
- return this . makeRequestAsync ( 'downloadItems' , params , requestParams ) ;
98
+ return this . makeRequest ( 'downloadItems' , params , requestParams ) ;
99
+ }
100
+
101
+ async getPresignedDownloadUrl ( fileName : string ) : Promise < any > {
102
+ const params = { key : fileName } ;
103
+ const requestOptions = {
104
+ method : 'POST' ,
105
+ headers : this . defaultHeaders ,
106
+ } ;
107
+ return this . makeRequest ( 'getPresignedDownloadUrl' , params , requestOptions ) ;
108
+ }
109
+
110
+ async initUpload ( fileData : File , destinationDirectory : FileSystemItem ) : Promise < any > {
111
+ const params = { key : `${ destinationDirectory . key } ${ fileData . name } ` } ;
112
+ const requestOptions = {
113
+ method : 'POST' ,
114
+ headers : this . defaultHeaders ,
115
+ } ;
116
+
117
+ const uploadId = await this . makeRequest ( 'initUpload' , params , requestOptions ) ;
118
+ this . initUploadData ( params . key , uploadId ) ;
87
119
}
88
120
89
121
async uploadPart ( fileData : File , uploadInfo : UploadInfo , destinationDirectory : FileSystemItem ) : Promise < any > {
@@ -102,7 +134,7 @@ export class AmazonGateway {
102
134
body : data ,
103
135
} ;
104
136
105
- const etag = await this . makeRequestAsync ( 'uploadPart' , params , requestOptions ) ;
137
+ const etag = await this . makeRequest ( 'uploadPart' , params , requestOptions ) ;
106
138
// partNumber must be > 0
107
139
this . addPartToUploadData ( key , { PartNumber : uploadInfo . chunkIndex + 1 , ETag : etag } ) ;
108
140
}
@@ -119,49 +151,63 @@ export class AmazonGateway {
119
151
body : JSON . stringify ( this . getParts ( key ) ) ,
120
152
} ;
121
153
122
- await this . makeRequestAsync ( 'completeUpload' , params , requestOptions ) ;
154
+ await this . makeRequest ( 'completeUpload' , params , requestOptions ) ;
123
155
this . removeUploadData ( key ) ;
124
156
}
125
157
126
- async initUpload ( fileData : File , destinationDirectory : FileSystemItem ) : Promise < any > {
127
- const params = { key : `${ destinationDirectory . key } ${ fileData . name } ` } ;
158
+ async abortFileUpload ( fileData : File , uploadInfo : UploadInfo , destinationDirectory : FileSystemItem ) : Promise < any > {
159
+ const key = `${ destinationDirectory ?. key ?? '' } ${ fileData . name } ` ;
160
+ const uploadId = this . getUploadId ( fileData . name ) ;
161
+ const params = { uploadId, key } ;
128
162
const requestOptions = {
129
163
method : 'POST' ,
130
164
headers : this . defaultHeaders ,
131
165
} ;
132
-
133
- const uploadId = await this . makeRequestAsync ( 'initUpload' , params , requestOptions ) ;
134
- this . initUploadData ( params . key , uploadId ) ;
166
+ return this . makeRequest ( 'abortUpload' , params , requestOptions ) ;
135
167
}
136
168
137
- async makeRequestAsync ( method : string , queryParams : any , requestParams : RequestInit ) : Promise < any > {
169
+ async makeRequest ( method : string , queryParams : any , requestParams : RequestInit ) : Promise < any > {
138
170
const requestUrl = this . getRequestUrl ( method ) ;
139
171
const url = new URL ( requestUrl ) ;
140
172
Object . keys ( queryParams ) . forEach ( ( key ) => url . searchParams . append ( key , queryParams [ key ] ) ) ;
141
173
142
- if ( this . onRequestExecuted ) {
143
- const params = {
144
- method,
145
- urlPath : requestUrl ,
146
- queryString : url . toString ( ) . replace ( requestUrl , '' ) ,
147
- } ;
148
- this . onRequestExecuted ( params ) ;
174
+ this . logRequest ( method , url , requestUrl ) ;
175
+ try {
176
+ const response = await fetch ( url . toString ( ) , requestParams ) ;
177
+ if ( ! response . ok ) {
178
+ const errorMessage = await response . text ( ) ;
179
+ throw new Error ( errorMessage ) ;
180
+ }
181
+ /* eslint-disable-next-line @typescript-eslint/no-unsafe-return */
182
+ return await this . getResponseData ( response ) ;
183
+ } catch ( error : any ) {
184
+ throw new Error ( error ) ;
149
185
}
150
- const response = await fetch ( url . toString ( ) , requestParams ) ;
186
+ }
151
187
152
- if ( ! response . ok ) {
153
- const errorMessage = await response . text ( ) ;
154
- throw new Error ( errorMessage ) ;
188
+ getResponseData ( response : Response ) : Promise < any | Blob | string > {
189
+ if ( this . containsAttachment ( response ) ) {
190
+ return response . blob ( ) ;
191
+ }
192
+ if ( this . containsPlainText ( response ) ) {
193
+ return response . text ( ) ;
155
194
}
195
+ return response . json ( ) ;
196
+ }
197
+
198
+ containsAttachment ( response : Response ) : boolean {
156
199
const contentDisposition = response . headers . get ( 'Content-Disposition' ) ;
157
200
if ( contentDisposition ?. includes ( 'attachment' ) ) {
158
- // processing downloadItems request
159
- return response . blob ( ) ;
201
+ return true ;
160
202
}
203
+ return false ;
204
+ }
205
+
206
+ containsPlainText ( response : Response ) : boolean {
161
207
const contentType = response . headers . get ( 'Content-Type' ) ;
162
208
if ( ! contentType || contentType . includes ( 'text/plain' ) ) {
163
- return response . text ( ) ;
209
+ return true ;
164
210
}
165
- return response . json ( ) ;
211
+ return false ;
166
212
}
167
213
}
0 commit comments