Skip to content

Commit 54e6e44

Browse files
update react
1 parent 6bd9c15 commit 54e6e44

File tree

4 files changed

+96
-33
lines changed

4 files changed

+96
-33
lines changed

React/src/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useCallback, useState, useMemo } from 'react';
22
import './App.css';
33
import 'devextreme/dist/css/dx.material.blue.light.compact.css';
44

5-
import FileManager, { Permissions } from 'devextreme-react/file-manager';
5+
import FileManager, { Permissions, Upload } from 'devextreme-react/file-manager';
66

77
import { getAmazonFileSystemProvider } from './api/amazon.custom.provider';
88

@@ -39,8 +39,8 @@ export default function App(): JSX.Element {
3939
fileSystemProvider={fileSystemProvider}
4040
allowedFileExtensions={allowedFileExtensions}
4141
>
42+
<Upload chunkSize={5242880}></Upload>
4243
<Permissions download={true}></Permissions>
43-
{/* uncomment the code below to enable file/directory management */}
4444
<Permissions
4545
create={true}
4646
copy={true}

React/src/api/amazon.custom.provider.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export class AmazonCustomProvider {
2626
moveItem: this.moveItem,
2727
uploadFileChunk: this.uploadFileChunk,
2828
downloadItems: this.downloadItems,
29+
abortFileUpload: this.abortFileUpload,
2930
};
3031
this.fileSystemProvider = new CustomFileSystemProvider(options);
3132
}
@@ -94,6 +95,14 @@ export class AmazonCustomProvider {
9495
throw new FileSystemError(32767, item, error.message);
9596
}
9697
};
98+
99+
abortFileUpload = async (fileData: File, uploadInfo: UploadInfo, destinationDirectory: FileSystemItem): Promise<void> => {
100+
try {
101+
await this.amazon.abortFileUpload(fileData, uploadInfo, destinationDirectory);
102+
} catch (error: any) {
103+
throw new Error(error.message);
104+
}
105+
};
97106
}
98107

99108
export function getAmazonFileSystemProvider(endpointUrl: string, onRequestExecuted?: Function): CustomFileSystemProvider {

React/src/api/amazon.filesystem.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export class AmazonFileSystem {
1212
}
1313

1414
async getItems(key: string): Promise<FileSystemItem[]> {
15-
return await this.gateway.getItems(key) as Promise<FileSystemItem[]>;
15+
return this.gateway.getItems(key) as Promise<FileSystemItem[]>;
1616
}
1717

1818
async createDirectory(key: string, name: string): Promise<any> {
@@ -48,6 +48,10 @@ export class AmazonFileSystem {
4848
}
4949
}
5050

51+
async getPresignedDownloadUrl(fileName: string): Promise<any> {
52+
return this.gateway.getPresignedDownloadUrl(fileName);
53+
}
54+
5155
getFileNameFromKey(key: string): string {
5256
const index = key.lastIndexOf('/');
5357
if (index === -1) {
@@ -56,6 +60,10 @@ export class AmazonFileSystem {
5660
return key.substring(index + 1);
5761
}
5862

63+
async abortFileUpload(fileData: File, uploadInfo: UploadInfo, destinationDirectory: FileSystemItem): Promise<any> {
64+
await this.gateway.abortFileUpload(fileData, uploadInfo, destinationDirectory);
65+
}
66+
5967
async uploadFileChunk(fileData: File, uploadInfo: UploadInfo, destinationDirectory: FileSystemItem): Promise<any> {
6068
try {
6169
if (uploadInfo.chunkIndex === 0) {

React/src/api/amazon.gateway.ts

Lines changed: 76 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ export class AmazonGateway {
2121
return `${this.endpointUrl}/${methodName}`;
2222
}
2323

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+
2436
removeUploadData(fileName: string): void {
2537
const index = this.uploadData.findIndex((data) => data.key === fileName);
2638
if (index !== -1) {
@@ -47,43 +59,63 @@ export class AmazonGateway {
4759
async getItems(path: string): Promise<any> {
4860
const params = { path };
4961
const requestParams = { method: 'GET' };
50-
return this.makeRequestAsync('getItems', params, requestParams);
62+
return this.makeRequest('getItems', params, requestParams);
5163
}
5264

5365
async createDirectory(path: string, name: string): Promise<void> {
5466
const params = { path, name };
5567
const requestParams = { method: 'PUT' };
56-
await this.makeRequestAsync('createDirectory', params, requestParams);
68+
await this.makeRequest('createDirectory', params, requestParams);
5769
}
5870

5971
async renameItem(key: string, parentPath: string, name: string): Promise<any> {
6072
const params = { key, directory: parentPath, newName: name };
6173
const requestParams = { method: 'PUT' };
62-
await this.makeRequestAsync('renameItem', params, requestParams);
74+
await this.makeRequest('renameItem', params, requestParams);
6375
}
6476

6577
async deleteItem(key: string): Promise<any> {
6678
const params = { item: key };
6779
const requestParams = { method: 'POST' };
68-
await this.makeRequestAsync('deleteItem', params, requestParams);
80+
await this.makeRequest('deleteItem', params, requestParams);
6981
}
7082

7183
async copyItem(sourceKey: string, destinationKey: string): Promise<any> {
7284
const params = { sourceKey, destinationKey };
7385
const requestParams = { method: 'PUT' };
74-
await this.makeRequestAsync('copyItem', params, requestParams);
86+
await this.makeRequest('copyItem', params, requestParams);
7587
}
7688

7789
async moveItem(sourceKey: string, destinationKey: string): Promise<any> {
7890
const params = { sourceKey, destinationKey };
7991
const requestParams = { method: 'POST' };
80-
await this.makeRequestAsync('moveItem', params, requestParams);
92+
await this.makeRequest('moveItem', params, requestParams);
8193
}
8294

8395
async downloadItems(keys: string[]): Promise<any> {
8496
const params = {};
8597
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);
87119
}
88120

89121
async uploadPart(fileData: File, uploadInfo: UploadInfo, destinationDirectory: FileSystemItem): Promise<any> {
@@ -102,7 +134,7 @@ export class AmazonGateway {
102134
body: data,
103135
};
104136

105-
const etag = await this.makeRequestAsync('uploadPart', params, requestOptions);
137+
const etag = await this.makeRequest('uploadPart', params, requestOptions);
106138
// partNumber must be > 0
107139
this.addPartToUploadData(key, { PartNumber: uploadInfo.chunkIndex + 1, ETag: etag });
108140
}
@@ -119,49 +151,63 @@ export class AmazonGateway {
119151
body: JSON.stringify(this.getParts(key)),
120152
};
121153

122-
await this.makeRequestAsync('completeUpload', params, requestOptions);
154+
await this.makeRequest('completeUpload', params, requestOptions);
123155
this.removeUploadData(key);
124156
}
125157

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 };
128162
const requestOptions = {
129163
method: 'POST',
130164
headers: this.defaultHeaders,
131165
};
132-
133-
const uploadId = await this.makeRequestAsync('initUpload', params, requestOptions);
134-
this.initUploadData(params.key, uploadId);
166+
return this.makeRequest('abortUpload', params, requestOptions);
135167
}
136168

137-
async makeRequestAsync(method: string, queryParams: any, requestParams: RequestInit): Promise<any> {
169+
async makeRequest(method: string, queryParams: any, requestParams: RequestInit): Promise<any> {
138170
const requestUrl = this.getRequestUrl(method);
139171
const url = new URL(requestUrl);
140172
Object.keys(queryParams).forEach((key) => url.searchParams.append(key, queryParams[key]));
141173

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);
149185
}
150-
const response = await fetch(url.toString(), requestParams);
186+
}
151187

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();
155194
}
195+
return response.json();
196+
}
197+
198+
containsAttachment(response: Response): boolean {
156199
const contentDisposition = response.headers.get('Content-Disposition');
157200
if (contentDisposition?.includes('attachment')) {
158-
// processing downloadItems request
159-
return response.blob();
201+
return true;
160202
}
203+
return false;
204+
}
205+
206+
containsPlainText(response: Response): boolean {
161207
const contentType = response.headers.get('Content-Type');
162208
if (!contentType || contentType.includes('text/plain')) {
163-
return response.text();
209+
return true;
164210
}
165-
return response.json();
211+
return false;
166212
}
167213
}

0 commit comments

Comments
 (0)