-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy pathutils.ts
452 lines (394 loc) · 12.6 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// This file is part of MinIO Console Server
// Copyright (c) 2021 MinIO, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
import { BucketObjectItem } from "./ListObjects/types";
import { decodeURLString, encodeURLString } from "../../../../../common/utils";
import { removeTrace } from "../../../ObjectBrowser/transferManager";
import store from "../../../../../store";
import { ContentType, PermissionResource } from "api/consoleApi";
import { api } from "../../../../../api";
import { setErrorSnackMessage } from "../../../../../systemSlice";
import { StatusCodes } from "http-status-codes";
const downloadWithLink = (href: string, downloadFileName: string) => {
const link = document.createElement("a");
link.href = href;
link.download = downloadFileName;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};
export const downloadSelectedAsZip = async (
bucketName: string,
objectList: string[],
resultFileName: string,
) => {
const state = store.getState();
const anonymousMode = state.system.anonymousMode;
try {
const resp = await api.buckets.downloadMultipleObjects(
bucketName,
objectList,
{
type: ContentType.Json,
headers: anonymousMode
? {
"X-Anonymous": "1",
}
: undefined,
},
);
const blob = await resp.blob();
const href = window.URL.createObjectURL(blob);
downloadWithLink(href, resultFileName);
} catch (err: any) {
store.dispatch(
setErrorSnackMessage({
errorMessage: `Download of multiple files failed. ${err.statusText}`,
detailedError: "",
}),
);
}
};
const isFolder = (objectPath: string) => {
return decodeURLString(objectPath).endsWith("/");
};
export const download = (
bucketName: string,
objectPath: string,
versionID: any,
fileSize: number,
overrideFileName: string | null = null,
id: string,
progressCallback: (progress: number) => void,
completeCallback: () => void,
errorCallback: (msg: string) => void,
abortCallback: () => void,
toastCallback: () => void,
) => {
let basename = document.baseURI.replace(window.location.origin, "");
const state = store.getState();
const anonymousMode = state.system.anonymousMode;
let path = `${
window.location.origin
}${basename}api/v1/buckets/${bucketName}/objects/download?prefix=${objectPath}${
overrideFileName !== null && overrideFileName.trim() !== ""
? `&override_file_name=${encodeURLString(overrideFileName || "")}`
: ""
}`;
if (versionID) {
path = path.concat(`&version_id=${versionID}`);
}
// If file is greater than 50GiB then we force browser download, if not then we use HTTP Request for Object Manager
if (fileSize > 53687091200) {
return new BrowserDownload(path, id, completeCallback, toastCallback);
}
let req = new XMLHttpRequest();
req.open("GET", path, true);
if (anonymousMode) {
req.setRequestHeader("X-Anonymous", "1");
}
req.addEventListener(
"progress",
function (evt) {
let percentComplete = Math.round((evt.loaded / fileSize) * 100);
if (progressCallback) {
progressCallback(percentComplete);
}
},
false,
);
req.responseType = "blob";
req.onreadystatechange = () => {
if (req.readyState === XMLHttpRequest.DONE) {
// Ensure object was downloaded fully, if it's a folder we don't get the fileSize
let completeDownload =
isFolder(objectPath) || req.response.size === fileSize;
if (req.status === StatusCodes.OK && completeDownload) {
const rspHeader = req.getResponseHeader("Content-Disposition");
let filename = "download";
if (rspHeader) {
let rspHeaderDecoded = decodeURIComponent(rspHeader);
filename = rspHeaderDecoded.split('"')[1];
}
if (completeCallback) {
completeCallback();
}
removeTrace(id);
downloadWithLink(window.URL.createObjectURL(req.response), filename);
} else {
if (req.getResponseHeader("Content-Type") === "application/json") {
const rspBody: { detailedMessage?: string } = JSON.parse(
req.response,
);
if (rspBody.detailedMessage) {
errorCallback(rspBody.detailedMessage);
return;
}
}
errorCallback(`Unexpected response, download incomplete.`);
}
}
};
req.onerror = () => {
if (errorCallback) {
errorCallback("A network error occurred.");
}
};
req.onabort = () => {
if (abortCallback) {
abortCallback();
}
};
return req;
};
class BrowserDownload {
path: string;
id: string;
completeCallback: () => void;
toastCallback: () => void;
constructor(
path: string,
id: string,
completeCallback: () => void,
toastCallback: () => void,
) {
this.path = path;
this.id = id;
this.completeCallback = completeCallback;
this.toastCallback = toastCallback;
}
send(): void {
this.toastCallback();
const link = document.createElement("a");
link.href = this.path;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
this.completeCallback();
removeTrace(this.id);
}
}
export type AllowedPreviews = "image" | "pdf" | "audio" | "video" | "none";
export const contentTypePreview = (contentType: string): AllowedPreviews => {
if (contentType) {
const mimeObjectType = (contentType || "").toLowerCase();
if (mimeObjectType.includes("image")) {
return "image";
}
if (mimeObjectType.includes("pdf")) {
return "pdf";
}
if (mimeObjectType.includes("audio")) {
return "audio";
}
if (mimeObjectType.includes("video")) {
return "video";
}
}
return "none";
};
// Review file extension by name & returns the type of preview browser that can be used
export const extensionPreview = (fileName: string): AllowedPreviews => {
const imageExtensions = [
"jif",
"jfif",
"apng",
"avif",
"svg",
"webp",
"bmp",
"ico",
"jpg",
"jpe",
"jpeg",
"gif",
"png",
"heic",
];
const pdfExtensions = ["pdf"];
const audioExtensions = ["wav", "mp3", "alac", "aiff", "dsd", "pcm"];
const videoExtensions = [
"mp4",
"avi",
"mpg",
"webm",
"mov",
"flv",
"mkv",
"wmv",
"avchd",
"mpeg-4",
];
let fileExtension = fileName.split(".").pop();
if (!fileExtension) {
return "none";
}
fileExtension = fileExtension.toLowerCase();
if (imageExtensions.includes(fileExtension)) {
return "image";
}
if (pdfExtensions.includes(fileExtension)) {
return "pdf";
}
if (audioExtensions.includes(fileExtension)) {
return "audio";
}
if (videoExtensions.includes(fileExtension)) {
return "video";
}
return "none";
};
export const previewObjectType = (
metaData: Record<any, any>,
objectName: string,
) => {
const metaContentType = (
(metaData && metaData["Content-Type"]) ||
""
).toString();
const extensionType = extensionPreview(objectName || "");
const contentType = contentTypePreview(metaContentType);
let objectType: AllowedPreviews = extensionType;
if (extensionType === contentType) {
objectType = extensionType;
} else if (extensionType === "none" && contentType !== "none") {
objectType = contentType;
} else if (contentType === "none" && extensionType !== "none") {
objectType = extensionType;
}
return objectType;
};
export const sortListObjects = (fieldSort: string) => {
switch (fieldSort) {
case "name":
return (a: BucketObjectItem, b: BucketObjectItem) =>
a.name.localeCompare(b.name);
case "last_modified":
return (a: BucketObjectItem, b: BucketObjectItem) =>
new Date(a.last_modified).getTime() -
new Date(b.last_modified).getTime();
case "size":
return (a: BucketObjectItem, b: BucketObjectItem) =>
(a.size || -1) - (b.size || -1);
}
};
export const permissionItems = (
bucketName: string,
currentPath: string,
permissionsArray: PermissionResource[],
): BucketObjectItem[] | null => {
if (permissionsArray.length === 0) {
return null;
}
// We get permissions applied to the current bucket
const filteredPermissionsForBucket = permissionsArray.filter(
(permissionItem) =>
permissionItem.resource?.endsWith(`:${bucketName}`) ||
permissionItem.resource?.includes(`:${bucketName}/`),
);
// No permissions for this bucket. we can throw the error message at this point
if (filteredPermissionsForBucket.length === 0) {
return null;
}
let returnElements: BucketObjectItem[] = [];
// We split current path
const splitCurrentPath = currentPath.split("/");
filteredPermissionsForBucket.forEach((permissionElement) => {
// We review paths in resource address
// We split ARN & get the last item to check the URL
const splitARN = permissionElement.resource?.split(":");
const urlARN = splitARN?.pop() || "";
// We split the paths of the URL & compare against current location to see if there are more items to include. In case current level is a wildcard or is the last one, we omit this validation
const splitURLARN = urlARN.split("/");
// splitURL has more items than bucket name, we can continue validating
if (splitURLARN.length > 1) {
splitURLARN.every((currentElementInPath, index) => {
// It is a wildcard element. We can store the verification as value should be included (?)
if (currentElementInPath === "*") {
return false;
}
// Element is not included in the path. The user is trying to browse something else.
if (
splitCurrentPath[index] &&
splitCurrentPath[index] !== currentElementInPath
) {
return false;
}
// This element is not included by index in the current paths list. We add it so user can browse into it
if (!splitCurrentPath[index]) {
returnElements.push({
name: `${currentElementInPath}/`,
size: 0,
last_modified: "",
version_id: "",
});
}
return true;
});
}
// We review prefixes in allow resources for StringEquals variant only.
if (
permissionElement.conditionOperator === "StringEquals" ||
permissionElement.conditionOperator === "StringLike"
) {
permissionElement.prefixes?.forEach((prefixItem) => {
// Prefix Item is not empty?
if (prefixItem !== "") {
const splitItems = prefixItem.split("/");
let pathToRouteElements: string[] = [];
// We verify if currentPath is contained in the path begin, if is not contained the user has no access to this subpath
const cleanCurrPath = currentPath.replace(/\/$/, "");
if (!prefixItem.startsWith(cleanCurrPath) && currentPath !== "") {
return;
}
// For every split element we iterate and check if we can construct a URL
splitItems.every((splitElement, index) => {
if (!splitElement.includes("*") && splitElement !== "") {
if (splitElement !== splitCurrentPath[index]) {
returnElements.push({
name: `${pathToRouteElements.join("/")}${
pathToRouteElements.length > 0 ? "/" : ""
}${splitElement}/`,
size: 0,
last_modified: "",
version_id: "",
});
return false;
}
if (splitElement !== "") {
pathToRouteElements.push(splitElement);
}
return true;
}
return false;
});
}
});
}
});
// We clean duplicated name entries
if (returnElements.length > 0) {
let clElements: BucketObjectItem[] = [];
let keys: string[] = [];
returnElements.forEach((itm) => {
if (!keys.includes(itm.name)) {
clElements.push(itm);
keys.push(itm.name);
}
});
returnElements = clElements;
}
return returnElements;
};