@@ -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,45 +59,64 @@ 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 ) ;
87
108
}
88
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 ) ;
119
+ }
89
120
/* eslint-disable-next-line vue/max-len */
90
121
async uploadPart ( fileData : File , uploadInfo : UploadInfo , destinationDirectory : FileSystemItem ) : Promise < any > {
91
122
const params = { } ;
@@ -103,11 +134,10 @@ export class AmazonGateway {
103
134
body : data ,
104
135
} ;
105
136
106
- const etag = await this . makeRequestAsync ( 'uploadPart' , params , requestOptions ) ;
137
+ const etag = await this . makeRequest ( 'uploadPart' , params , requestOptions ) ;
107
138
// partNumber must be > 0
108
139
this . addPartToUploadData ( key , { PartNumber : uploadInfo . chunkIndex + 1 , ETag : etag } ) ;
109
140
}
110
-
111
141
/* eslint-disable-next-line vue/max-len */
112
142
async completeUpload ( fileData : File , uploadInfo : UploadInfo , destinationDirectory : FileSystemItem ) : Promise < any > {
113
143
const key = `${ destinationDirectory . key } ${ fileData . name } ` ;
@@ -121,50 +151,63 @@ export class AmazonGateway {
121
151
body : JSON . stringify ( this . getParts ( key ) ) ,
122
152
} ;
123
153
124
- await this . makeRequestAsync ( 'completeUpload' , params , requestOptions ) ;
154
+ await this . makeRequest ( 'completeUpload' , params , requestOptions ) ;
125
155
this . removeUploadData ( key ) ;
126
156
}
127
-
128
- async initUpload ( fileData : File , destinationDirectory : FileSystemItem ) : Promise < any > {
129
- const params = { key : `${ destinationDirectory . key } ${ fileData . name } ` } ;
157
+ /* eslint-disable-next-line vue/max-len */
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 } ;
130
162
const requestOptions = {
131
163
method : 'POST' ,
132
164
headers : this . defaultHeaders ,
133
165
} ;
134
-
135
- const uploadId = await this . makeRequestAsync ( 'initUpload' , params , requestOptions ) ;
136
- this . initUploadData ( params . key , uploadId ) ;
166
+ return this . makeRequest ( 'abortUpload' , params , requestOptions ) ;
137
167
}
138
168
139
- /* eslint-disable-next-line vue/max-len */
140
- async makeRequestAsync ( method : string , queryParams : any , requestParams : RequestInit ) : Promise < any > {
169
+ async makeRequest ( method : string , queryParams : any , requestParams : RequestInit ) : Promise < any > {
141
170
const requestUrl = this . getRequestUrl ( method ) ;
142
171
const url = new URL ( requestUrl ) ;
143
172
Object . keys ( queryParams ) . forEach ( ( key ) => url . searchParams . append ( key , queryParams [ key ] ) ) ;
144
173
145
- if ( this . onRequestExecuted ) {
146
- const params = {
147
- method,
148
- urlPath : requestUrl ,
149
- queryString : url . toString ( ) . replace ( requestUrl , '' ) ,
150
- } ;
151
- 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 ) ;
152
185
}
153
- const response = await fetch ( url . toString ( ) , requestParams ) ;
186
+ }
154
187
155
- if ( ! response . ok ) {
156
- const errorMessage = await response . text ( ) ;
157
- throw new Error ( errorMessage ) ;
188
+ getResponseData ( response : Response ) : Promise < any | Blob | string > {
189
+ if ( this . containsAttachment ( response ) ) {
190
+ return response . blob ( ) ;
158
191
}
192
+ if ( this . containsPlainText ( response ) ) {
193
+ return response . text ( ) ;
194
+ }
195
+ return response . json ( ) ;
196
+ }
197
+
198
+ containsAttachment ( response : Response ) : boolean {
159
199
const contentDisposition = response . headers . get ( 'Content-Disposition' ) ;
160
200
if ( contentDisposition ?. includes ( 'attachment' ) ) {
161
- // processing downloadItems request
162
- return response . blob ( ) ;
201
+ return true ;
163
202
}
203
+ return false ;
204
+ }
205
+
206
+ containsPlainText ( response : Response ) : boolean {
164
207
const contentType = response . headers . get ( 'Content-Type' ) ;
165
208
if ( ! contentType || contentType . includes ( 'text/plain' ) ) {
166
- return response . text ( ) ;
209
+ return true ;
167
210
}
168
- return response . json ( ) ;
211
+ return false ;
169
212
}
170
213
}
0 commit comments