Skip to content

Commit 63ae024

Browse files
update jquery and backend
1 parent 0251bcc commit 63ae024

File tree

5 files changed

+137
-112
lines changed

5 files changed

+137
-112
lines changed

Amazon_Backend/AmazonS3_Backend/Controllers/AmazonS3Controller.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,18 @@ public async Task<IActionResult> UploadPart([FromForm] IFormFile part, [FromForm
117117
public async Task<IActionResult> CompleteUpload([FromBody] List<PartETag> parts, string key, string uploadId) {
118118
try {
119119
var response = await provider.CompleteUploadAsync(key, uploadId, parts);
120-
// use response if you need to pass ETag or something else when upload is finished
120+
// use `response` if you need to pass ETag or something else when upload is finished
121121
return Ok(response.ETag);
122122
} catch (Exception ex) {
123123
return StatusCode(500, ex.Message);
124124
}
125125
}
126126

127127
[HttpPost("abortUpload")]
128-
public async Task<IActionResult> AbortUpload(string uploadId) {
128+
public async Task<IActionResult> AbortUpload(string uploadId, string key) {
129129
try {
130-
var response = await provider.AbortUploadAsync(uploadId);
131-
// user response if you need to pass something to the client after aborting upload
130+
var response = await provider.AbortUploadAsync(uploadId, key);
131+
// use `response` if you need to pass something to the client after aborting upload
132132
return Ok(response.HttpStatusCode);
133133
} catch (Exception ex) {
134134
return StatusCode(500, ex.Message);

Amazon_Backend/AmazonS3_Backend/Providers/AmazonS3Provider.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ public async Task<List<FileSystemItem>> GetDirectoriesFromCommonPrefixes(List<st
223223
Key = item,
224224
Size = 0,
225225
IsDirectory = true,
226+
DateModified = DateTime.UtcNow,
226227
HasSubDirectories = await HasDirectorySubDirectoriesAsync(item),
227228
});
228229
}
@@ -313,8 +314,10 @@ public async Task<CompleteMultipartUploadResponse> CompleteUploadAsync(string ke
313314
return await Client.CompleteMultipartUploadAsync(request);
314315
}
315316

316-
public async Task<AbortMultipartUploadResponse> AbortUploadAsync(string uploadId) {
317+
public async Task<AbortMultipartUploadResponse> AbortUploadAsync(string uploadId, string key) {
317318
var request = new AbortMultipartUploadRequest() {
319+
BucketName = BucketName,
320+
Key = key,
318321
UploadId = uploadId
319322
};
320323
return await Client.AbortMultipartUploadAsync(request);

jQuery/src/amazon.filesystem.js

Lines changed: 23 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,75 +5,43 @@ class AmazonFileSystem {
55
this.gateway = new AmazonGateway(baseUrl, onRequestExecuted);
66
}
77

8-
async getItems(path) {
9-
try {
10-
return await this.gateway.getItems(path);
11-
} catch (error) {
12-
throw new DevExpress.fileManagement.FileSystemError(32767, path, error.message);
13-
}
8+
getItems(path) {
9+
return this.gateway.getItems(path);
1410
}
1511

16-
async createDirectory(key, name) {
17-
try {
18-
return await this.gateway.createDirectory(key, name);
19-
} catch (error) {
20-
throw new DevExpress.fileManagement.FileSystemError(32767, name, error.message);
21-
}
12+
createDirectory(key, name) {
13+
return this.gateway.createDirectory(key, name);
2214
}
2315

24-
async renameItem(key, parentPath, name) {
25-
try {
26-
return await this.gateway.renameItem(key, `${parentPath}/`, name);
27-
} catch (error) {
28-
throw new DevExpress.fileManagement.FileSystemError(32767, key, error.message);
29-
}
16+
renameItem(key, parentPath, name) {
17+
return this.gateway.renameItem(key, `${parentPath}/`, name);
3018
}
3119

32-
async deleteItem(key) {
33-
try {
34-
return await this.gateway.deleteItem(key);
35-
} catch (error) {
36-
throw new DevExpress.fileManagement.FileSystemError(32767, key, error.message);
37-
}
20+
deleteItem(key) {
21+
return this.gateway.deleteItem(key);
3822
}
3923

40-
async copyItem(item, destinationDir) {
41-
try {
42-
return await this.gateway.copyItem(item.key, `${destinationDir.key}${item.name}`);
43-
} catch (error) {
44-
throw new DevExpress.fileManagement.FileSystemError(32767, item.key, error.message);
45-
}
24+
copyItem(item, destinationDir) {
25+
return this.gateway.copyItem(item.key, `${destinationDir.key}${item.name}`);
4626
}
4727

48-
async moveItem(item, destinationDir) {
49-
try {
50-
return await this.gateway.moveItem(item.key, `${destinationDir.key}${item.name}`);
51-
} catch (error) {
52-
throw new DevExpress.fileManagement.FileSystemError(32767, item, error.message);
53-
}
28+
moveItem(item, destinationDir) {
29+
return this.gateway.moveItem(item.key, `${destinationDir.key}${item.name}`);
5430
}
5531

56-
async abortFileUpload(fileData, uploadInfo) {
57-
try {
58-
await this.gateway.abortFileUpload(fileData, uploadInfo);
59-
} catch (error) {
60-
throw new DevExpress.fileManagement.FileSystemError(32767, item, error.message);
61-
}
32+
async abortFileUpload(fileData, uploadInfo, destinationDirectory) {
33+
await this.gateway.abortFileUpload(fileData, uploadInfo, destinationDirectory);
6234
}
6335

6436
async uploadFileChunk(fileData, uploadInfo, destinationDirectory) {
65-
try {
66-
if (uploadInfo.chunkIndex === 0) {
67-
await this.gateway.initUpload(fileData, destinationDirectory);
68-
}
69-
70-
await this.gateway.uploadPart(fileData, uploadInfo, destinationDirectory);
37+
if (uploadInfo.chunkIndex === 0) {
38+
await this.gateway.initUpload(fileData, destinationDirectory);
39+
}
40+
// upload part even if a chunk is first or last
41+
await this.gateway.uploadPart(fileData, uploadInfo, destinationDirectory);
7142

72-
if (uploadInfo.chunkCount === uploadInfo.chunkIndex + 1) {
73-
await this.gateway.completeUpload(fileData, uploadInfo, destinationDirectory);
74-
}
75-
} catch (error) {
76-
throw new DevExpress.fileManagement.FileSystemError(32767, fileData.name, error.message);
43+
if (uploadInfo.chunkCount === uploadInfo.chunkIndex + 1) {
44+
await this.gateway.completeUpload(fileData, uploadInfo, destinationDirectory);
7745
}
7846
}
7947

@@ -94,11 +62,7 @@ class AmazonFileSystem {
9462
async downloadItems(items) {
9563
const keys = items.map((x) => x.key);
9664
const fileName = keys.length > 1 ? 'archive.zip' : this.getFileNameFromKey(keys[0]);
97-
try {
98-
const blob = await this.gateway.downloadItems(keys);
99-
saveAs(new Blob([blob], { type: 'application/octet-stream' }), fileName);
100-
} catch (error) {
101-
throw new DevExpress.fileManagement.FileSystemError(32767, fileName, error.message);
102-
}
65+
const blob = await this.gateway.downloadItems(keys);
66+
saveAs(new Blob([blob], { type: 'application/octet-stream' }), fileName);
10367
}
10468
}

jQuery/src/amazon.gateway.js

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,28 @@ class AmazonGateway {
3131
return this.uploadData[fileName].parts;
3232
}
3333

34-
async getItems(key) {
34+
getItems(key) {
3535
const params = { 'path': key };
3636
const requestParams = { method: 'GET' };
3737
return this.makeRequest('getItems', params, requestParams);
3838
}
3939

40-
async renameItem(key, parentPath, name) {
40+
renameItem(key, parentPath, name) {
4141
const params = { 'key': key, 'directory': parentPath, 'newName': name };
4242
const requestParams = { method: 'PUT' };
43-
await this.makeRequest('renameItem', params, requestParams);
43+
return this.makeRequest('renameItem', params, requestParams);
4444
}
4545

46-
async createDirectory(key, name) {
46+
createDirectory(key, name) {
4747
const params = { 'path': key, 'name': name };
4848
const requestParams = { method: 'PUT' };
49-
await this.makeRequest('createDirectory', params, requestParams);
49+
return this.makeRequest('createDirectory', params, requestParams);
5050
}
5151

5252
async deleteItem(key) {
5353
const params = { 'item': key };
5454
const requestParams = { method: 'POST' };
55-
await this.makeRequestAsync('deleteItem', params, requestParams);
55+
await this.makeRequest('deleteItem', params, requestParams);
5656
}
5757

5858
async copyItem(sourceKey, destinationKey) {
@@ -122,10 +122,12 @@ class AmazonGateway {
122122
}
123123

124124
async abortFileUpload(fileData, uploadInfo, destinationDirectory) {
125-
const params = { uploadId: this.getUploadId(fileData.name) };
125+
const key = `${destinationDirectory?.key ?? ''}${fileData.name}`;
126+
const uploadId = this.getUploadId(fileData.name);
127+
const params = { uploadId, key };
126128
const requestOptions = {
127129
method: 'POST',
128-
headers: this.defaultHeaders
130+
headers: this.defaultHeaders,
129131
};
130132
return this.makeRequest('abortUpload', params, requestOptions);
131133
}
@@ -140,36 +142,56 @@ class AmazonGateway {
140142
return this.makeRequest('getPresignedDownloadUrl', params, requestOptions);
141143
}
142144

143-
async makeRequest(method, queryParams, requestParams) {
144-
const requestUrl = this.getRequestUrl(method);
145-
const url = new URL(requestUrl);
146-
147-
Object.keys(queryParams).forEach((key) => url.searchParams.append(key, queryParams[key]));
148-
149-
if (this.onRequestExecuted) {
150-
const params = {
151-
method,
152-
urlPath: requestUrl,
153-
queryString: url.toString().replace(requestUrl, ''),
154-
};
155-
this.onRequestExecuted(params);
145+
logRequest(method, url, requestUrl) {
146+
if (!this.onRequestExecuted) {
147+
return;
156148
}
149+
const params = {
150+
method,
151+
urlPath: requestUrl,
152+
queryString: url.toString().replace(requestUrl, ''),
153+
};
154+
this.onRequestExecuted(params);
155+
}
157156

158-
const response = await fetch(url.toString(), requestParams);
159-
160-
if (!response.ok) {
161-
const errorResult = await response.text();
162-
throw new Error(errorResult.errorText);
163-
}
157+
containsAttachment(response) {
164158
const contentDisposition = response.headers.get('Content-Disposition');
165159
if (contentDisposition && contentDisposition.includes('attachment')) {
166-
// processing downloadItems request
167-
return response.blob();
160+
return true;
168161
}
162+
return false;
163+
}
164+
165+
containsPlainText(response) {
169166
const contentType = response.headers.get('Content-Type');
170167
if (!contentType || contentType.includes('text/plain')) {
171-
return response.text();
168+
return true;
169+
}
170+
return false;
171+
}
172+
173+
async makeRequest(method, queryParams, requestParams) {
174+
const requestUrl = this.getRequestUrl(method);
175+
const url = new URL(requestUrl);
176+
177+
Object.keys(queryParams).forEach((key) => url.searchParams.append(key, queryParams[key]));
178+
this.logRequest(method, url, requestUrl);
179+
180+
try {
181+
const response = await fetch(url.toString(), requestParams);
182+
if (!response.ok) {
183+
const errorMessage = await response.text();
184+
throw new Error(errorMessage);
185+
}
186+
if (this.containsAttachment(response)) {
187+
return response.blob();
188+
}
189+
if (this.containsPlainText(response)) {
190+
return response.text();
191+
}
192+
return response.json();
193+
} catch (error) {
194+
throw new Error(error.message);
172195
}
173-
return response.json();
174196
}
175197
}

jQuery/src/index.js

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,40 +33,76 @@ $(() => {
3333
});
3434
});
3535

36-
function getItems(item) {
37-
return amazon.getItems(item.key);
36+
async function getItems(item) {
37+
try {
38+
return amazon.getItems(item.key);
39+
} catch (error) {
40+
throw new DevExpress.fileManagement.FileSystemError(32767, item, error.message);
41+
}
3842
}
3943

40-
function createDirectory(parentDirectory, name) {
41-
return amazon.createDirectory(parentDirectory.key, name);
44+
async function createDirectory(parentDirectory, name) {
45+
try {
46+
await amazon.createDirectory(parentDirectory.key, name);
47+
} catch (error) {
48+
throw new DevExpress.fileManagement.FileSystemError(32767, item, error.message);
49+
}
4250
}
4351

44-
function renameItem(item, name) {
45-
return amazon.renameItem(item.key, item.parentPath, name);
52+
async function renameItem(item, name) {
53+
try {
54+
await amazon.renameItem(item.key, item.parentPath, name);
55+
} catch (error) {
56+
throw new DevExpress.fileManagement.FileSystemError(32767, item, error.message);
57+
}
4658
}
4759

48-
function deleteItem(item) {
49-
return amazon.deleteItem(item.key);
60+
async function deleteItem(item) {
61+
try {
62+
await amazon.deleteItem(item.key);
63+
} catch (error) {
64+
throw new DevExpress.fileManagement.FileSystemError(32767, item, error.message);
65+
}
5066
}
5167

52-
function copyItem(item, destinationDirectory) {
53-
return amazon.copyItem(item, destinationDirectory);
68+
async function copyItem(item, destinationDirectory) {
69+
try {
70+
await amazon.copyItem(item, destinationDirectory);
71+
} catch (error) {
72+
throw new DevExpress.fileManagement.FileSystemError(32767, item, error.message);
73+
}
5474
}
5575

56-
function moveItem(item, destinationDirectory) {
57-
return amazon.moveItem(item, destinationDirectory);
76+
async function moveItem(item, destinationDirectory) {
77+
try {
78+
await amazon.moveItem(item, destinationDirectory);
79+
} catch (error) {
80+
throw new DevExpress.fileManagement.FileSystemError(32767, item, error.message);
81+
}
5882
}
5983

60-
async function abortFileUpload(fileData, uploadInfo) {
61-
return amazon.abortFileUpload(fileData, uploadInfo);
84+
async function abortFileUpload(fileData, uploadInfo, destinationDirectory) {
85+
try {
86+
await amazon.abortFileUpload(fileData, uploadInfo, destinationDirectory);
87+
} catch (error) {
88+
throw new Error(error.message);
89+
}
6290
}
6391

6492
async function uploadFileChunk(fileData, uploadInfo, destinationDirectory) {
65-
await amazon.uploadFileChunk(fileData, uploadInfo, destinationDirectory);
93+
try {
94+
await amazon.uploadFileChunk(fileData, uploadInfo, destinationDirectory);
95+
} catch (error) {
96+
throw new Error(error.message);
97+
}
6698
}
6799

68-
function downloadItems(items) {
69-
return amazon.downloadItems(items);
100+
async function downloadItems(items) {
101+
try {
102+
await amazon.downloadItems(items);
103+
} catch (error) {
104+
throw new Error(error.message);
105+
}
70106
}
71107

72108
function onRequestExecuted(e) {

0 commit comments

Comments
 (0)