Skip to content

Commit 1cfb7e0

Browse files
upload vue
1 parent 54e6e44 commit 1cfb7e0

File tree

4 files changed

+96
-36
lines changed

4 files changed

+96
-36
lines changed

Vue/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+
/* eslint-disable-next-line vue/max-len */
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
/* eslint-disable-next-line vue/max-len */

Vue/src/api/amazon.filesystem.ts

Lines changed: 9 additions & 2 deletions
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,14 +48,21 @@ 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) {
5458
return key;
5559
}
5660
return key.substring(index + 1);
5761
}
58-
62+
/* eslint-disable-next-line vue/max-len */
63+
async abortFileUpload(fileData: File, uploadInfo: UploadInfo, destinationDirectory: FileSystemItem): Promise<any> {
64+
await this.gateway.abortFileUpload(fileData, uploadInfo, destinationDirectory);
65+
}
5966
/* eslint-disable-next-line vue/max-len */
6067
async uploadFileChunk(fileData: File, uploadInfo: UploadInfo, destinationDirectory: FileSystemItem): Promise<any> {
6168
try {

Vue/src/api/amazon.gateway.ts

Lines changed: 76 additions & 33 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,45 +59,64 @@ 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);
87108
}
88109

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+
}
89120
/* eslint-disable-next-line vue/max-len */
90121
async uploadPart(fileData: File, uploadInfo: UploadInfo, destinationDirectory: FileSystemItem): Promise<any> {
91122
const params = {};
@@ -103,11 +134,10 @@ export class AmazonGateway {
103134
body: data,
104135
};
105136

106-
const etag = await this.makeRequestAsync('uploadPart', params, requestOptions);
137+
const etag = await this.makeRequest('uploadPart', params, requestOptions);
107138
// partNumber must be > 0
108139
this.addPartToUploadData(key, { PartNumber: uploadInfo.chunkIndex + 1, ETag: etag });
109140
}
110-
111141
/* eslint-disable-next-line vue/max-len */
112142
async completeUpload(fileData: File, uploadInfo: UploadInfo, destinationDirectory: FileSystemItem): Promise<any> {
113143
const key = `${destinationDirectory.key}${fileData.name}`;
@@ -121,50 +151,63 @@ export class AmazonGateway {
121151
body: JSON.stringify(this.getParts(key)),
122152
};
123153

124-
await this.makeRequestAsync('completeUpload', params, requestOptions);
154+
await this.makeRequest('completeUpload', params, requestOptions);
125155
this.removeUploadData(key);
126156
}
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 };
130162
const requestOptions = {
131163
method: 'POST',
132164
headers: this.defaultHeaders,
133165
};
134-
135-
const uploadId = await this.makeRequestAsync('initUpload', params, requestOptions);
136-
this.initUploadData(params.key, uploadId);
166+
return this.makeRequest('abortUpload', params, requestOptions);
137167
}
138168

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> {
141170
const requestUrl = this.getRequestUrl(method);
142171
const url = new URL(requestUrl);
143172
Object.keys(queryParams).forEach((key) => url.searchParams.append(key, queryParams[key]));
144173

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

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();
158191
}
192+
if (this.containsPlainText(response)) {
193+
return response.text();
194+
}
195+
return response.json();
196+
}
197+
198+
containsAttachment(response: Response): boolean {
159199
const contentDisposition = response.headers.get('Content-Disposition');
160200
if (contentDisposition?.includes('attachment')) {
161-
// processing downloadItems request
162-
return response.blob();
201+
return true;
163202
}
203+
return false;
204+
}
205+
206+
containsPlainText(response: Response): boolean {
164207
const contentType = response.headers.get('Content-Type');
165208
if (!contentType || contentType.includes('text/plain')) {
166-
return response.text();
209+
return true;
167210
}
168-
return response.json();
211+
return false;
169212
}
170213
}

Vue/src/components/HomeContent.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ref } from 'vue';
33
import type { Ref } from 'vue';
44
55
import {
6-
DxFileManager, DxPermissions
6+
DxFileManager, DxPermissions, DxUpload
77
} from 'devextreme-vue/file-manager';
88
99
import 'devextreme/dist/css/dx.material.blue.light.compact.css';
@@ -29,6 +29,7 @@ const fileSystemProvider = getAmazonFileSystemProvider(endpointUrl, onRequestExe
2929
:file-system-provider="fileSystemProvider"
3030
:allowed-file-extensions="allowedFileExtensions"
3131
>
32+
<DxUpload :chunk-size="5242880"/>
3233
<DxPermissions/>
3334
<DxPermissions
3435
:create="true"

0 commit comments

Comments
 (0)