@widgetic/file-browser 0.5.48 → 0.5.49
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/actions/dragSelectedToFolder.js +115 -104
- package/dist/components/FolderTree.svelte.d.ts +7 -4
- package/dist/components/core/FileBrowser.svelte +1 -3
- package/dist/components/core/dropzoneClick.js +2 -3
- package/dist/components/previews/FilePreview.svelte.d.ts +2 -2
- package/dist/config.js +3 -4
- package/dist/i18n/context.js +2 -2
- package/dist/i18n/errors.js +1 -2
- package/dist/i18n/messages.js +2 -2
- package/dist/i18n/text.js +2 -2
- package/dist/mediaLibrary/capabilities.js +3 -4
- package/dist/mediaLibrary/context.js +3 -4
- package/dist/mediaLibrary/errors.js +35 -46
- package/dist/mediaLibrary/normalize.js +42 -45
- package/dist/mediaLibrary/paging.js +45 -100
- package/dist/mediaLibrary/resolve.js +95 -214
- package/dist/mediaLibrary/treeOps.js +20 -40
- package/dist/mediaLibrary/urls.js +10 -12
- package/dist/mediaLibrary/widgetic.js +54 -140
- package/dist/stores/dragStore.js +1 -1
- package/dist/stores/fileStore.d.ts +3 -3
- package/dist/stores/fileStore.js +37 -44
- package/dist/stores/folderCountStore.js +11 -12
- package/dist/stores/missingFilesStore.js +3 -3
- package/dist/stores/navigationStore.d.ts +1 -1
- package/dist/stores/navigationStore.js +178 -217
- package/dist/stores/persistentSelectStore.d.ts +1 -1
- package/dist/stores/persistentSelectStore.js +94 -117
- package/dist/stores/searchStore.js +1 -1
- package/dist/stores/uploadApiStore.js +7 -7
- package/dist/stores/viewportStore.js +4 -4
- package/dist/types/index.js +1 -0
- package/dist/utils/apiConfig.js +9 -9
- package/dist/utils/brandKitLibraryTree.js +42 -57
- package/dist/utils/dateUtils.js +9 -11
- package/dist/utils/dragToFolder/batchMoveResult.js +54 -114
- package/dist/utils/dragToFolder/folderDropTarget.js +17 -23
- package/dist/utils/dragToFolder/springStep.js +3 -4
- package/dist/utils/fileGoneProbe.js +16 -65
- package/dist/utils/fileKinds.js +13 -14
- package/dist/utils/fileNames.js +7 -7
- package/dist/utils/fileOperations.js +332 -440
- package/dist/utils/fileTypes.js +14 -19
- package/dist/utils/fileUtils.js +31 -85
- package/dist/utils/folderUtils.js +4 -5
- package/dist/utils/gridLayout.js +7 -7
- package/dist/utils/imagePreviewCache.js +15 -26
- package/dist/utils/imagePreviewFrame.js +13 -13
- package/dist/utils/imagePreviewVisualState.js +4 -5
- package/dist/utils/imageTransform.js +14 -27
- package/dist/utils/itemMenuCapabilities.js +7 -7
- package/dist/utils/libraryTree.js +39 -55
- package/dist/utils/pendingUploadDeletes.js +113 -289
- package/dist/utils/previewKeys.js +1 -1
- package/dist/utils/previewListNavigation.js +11 -13
- package/dist/utils/previewNavigation.js +7 -10
- package/dist/utils/recentFiles.js +6 -16
- package/dist/utils/recentFilesLimit.js +2 -2
- package/dist/utils/removeItems.js +29 -61
- package/dist/utils/resolveFileUrl.js +6 -6
- package/dist/utils/safeText.js +12 -14
- package/dist/utils/searchDropdownPhase.js +2 -2
- package/dist/utils/searchItems.js +14 -30
- package/dist/utils/searchNavigation.js +5 -5
- package/dist/utils/uploadAuth.js +36 -111
- package/dist/utils/uploadDisplayProgress.js +1 -1
- package/dist/utils/uploadLimits.js +66 -138
- package/dist/utils/uploadListReconcile.js +11 -24
- package/dist/utils/uploadPanelDismiss.js +1 -1
- package/dist/utils/uploadPanelProgress.js +4 -4
- package/dist/utils/uploadProgressMath.js +11 -13
- package/dist/utils/uploadRefusals.js +24 -40
- package/dist/utils/uploadResponseMapping.js +8 -9
- package/dist/utils/uploadUiHelpers.js +17 -17
- package/dist/utils/userUploadsStorage.js +133 -270
- package/dist/utils/videoFrame.js +2 -2
- package/dist/utils/widgeticDelete.js +30 -103
- package/dist/utils/widgeticTusUpload.js +105 -189
- package/dist/utils/widgeticUpload.js +62 -130
- package/dist/utils/xhrMultipartUpload.js +116 -164
- package/package.json +1 -1
|
@@ -1,41 +1,32 @@
|
|
|
1
|
-
var __extends = (this && this.__extends) || (function () {
|
|
2
|
-
var extendStatics = function (d, b) {
|
|
3
|
-
extendStatics = Object.setPrototypeOf ||
|
|
4
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
5
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
6
|
-
return extendStatics(d, b);
|
|
7
|
-
};
|
|
8
|
-
return function (d, b) {
|
|
9
|
-
if (typeof b !== "function" && b !== null)
|
|
10
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
11
|
-
extendStatics(d, b);
|
|
12
|
-
function __() { this.constructor = d; }
|
|
13
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
14
|
-
};
|
|
15
|
-
})();
|
|
16
1
|
/** The longest message taken from a library's own error. Longer ones are cut so a toast stays readable. */
|
|
17
|
-
|
|
2
|
+
const MAX_MESSAGE_LENGTH = 300;
|
|
18
3
|
/** What Chrome, Firefox, Safari and React Native write in the TypeError of a `fetch` that could not reach the server. */
|
|
19
|
-
|
|
4
|
+
const BROWSER_NETWORK_FAILURE_TEXT = /failed to fetch|networkerror|network request failed|load failed/i;
|
|
20
5
|
/** A failed call to a media library, with a reason the File Browser can act on. */
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
6
|
+
export class MediaLibraryError extends Error {
|
|
7
|
+
code;
|
|
8
|
+
/** The HTTP status behind the failure, when there was one. */
|
|
9
|
+
status;
|
|
10
|
+
/**
|
|
11
|
+
* The catalog message that says the same thing in the visitor's language. Set when `message` is the File Browser's
|
|
12
|
+
* own English text (not one the library wrote): the visitor then reads the translation, while the `error` event and
|
|
13
|
+
* the console keep the English `message`. Absent when the library supplied the text, which is shown as it is.
|
|
14
|
+
*/
|
|
15
|
+
messageKey;
|
|
16
|
+
/** The values the message at `messageKey` asks for (a size limit, a count), when it asks for any. */
|
|
17
|
+
messageValues;
|
|
18
|
+
constructor(code, message, options = {}) {
|
|
19
|
+
super(message);
|
|
20
|
+
this.name = 'MediaLibraryError';
|
|
21
|
+
this.code = code;
|
|
22
|
+
this.status = options.status;
|
|
23
|
+
this.messageKey = options.messageKey;
|
|
24
|
+
this.messageValues = options.messageValues;
|
|
31
25
|
if (options.cause !== undefined) {
|
|
32
|
-
|
|
26
|
+
this.cause = options.cause;
|
|
33
27
|
}
|
|
34
|
-
return _this;
|
|
35
28
|
}
|
|
36
|
-
|
|
37
|
-
}(Error));
|
|
38
|
-
export { MediaLibraryError };
|
|
29
|
+
}
|
|
39
30
|
/** The error code that fits an HTTP status. */
|
|
40
31
|
export function codeForHttpStatus(status) {
|
|
41
32
|
if (status === 401)
|
|
@@ -61,12 +52,10 @@ export function isCancellation(error) {
|
|
|
61
52
|
}
|
|
62
53
|
/** The HTTP status a thrown value carries, if it carries one in any of the usual places. */
|
|
63
54
|
function statusOf(error) {
|
|
64
|
-
var _a;
|
|
65
55
|
if (error === null || typeof error !== 'object')
|
|
66
56
|
return undefined;
|
|
67
|
-
|
|
68
|
-
for (
|
|
69
|
-
var value = _b[_i];
|
|
57
|
+
const candidate = error;
|
|
58
|
+
for (const value of [candidate.status, candidate.statusCode, candidate.response?.status]) {
|
|
70
59
|
if (typeof value === 'number' && Number.isInteger(value) && value >= 100 && value <= 599)
|
|
71
60
|
return value;
|
|
72
61
|
}
|
|
@@ -76,10 +65,10 @@ function statusOf(error) {
|
|
|
76
65
|
function ownTextOf(text) {
|
|
77
66
|
if (typeof text !== 'string')
|
|
78
67
|
return null;
|
|
79
|
-
|
|
68
|
+
const trimmed = text.trim();
|
|
80
69
|
if (trimmed === '')
|
|
81
70
|
return null;
|
|
82
|
-
return trimmed.length > MAX_MESSAGE_LENGTH ?
|
|
71
|
+
return trimmed.length > MAX_MESSAGE_LENGTH ? `${trimmed.slice(0, MAX_MESSAGE_LENGTH)}…` : trimmed;
|
|
83
72
|
}
|
|
84
73
|
/**
|
|
85
74
|
* Turns whatever a library's function threw (or rejected with) into a {@link MediaLibraryError}.
|
|
@@ -94,30 +83,30 @@ export function toMediaLibraryError(error, fallbackMessage, fallbackKey) {
|
|
|
94
83
|
if (isAbortError(error)) {
|
|
95
84
|
return new MediaLibraryError('ABORTED', 'The request was cancelled', { cause: error });
|
|
96
85
|
}
|
|
97
|
-
|
|
98
|
-
|
|
86
|
+
const status = statusOf(error);
|
|
87
|
+
const ownText = ownTextOf(typeof error === 'string'
|
|
99
88
|
? error
|
|
100
89
|
: error !== null && typeof error === 'object'
|
|
101
90
|
? error.message
|
|
102
91
|
: undefined);
|
|
103
|
-
|
|
92
|
+
const message = ownText ?? fallbackMessage;
|
|
104
93
|
// Only the File Browser's own words are translated; what the library wrote is shown as it is.
|
|
105
|
-
|
|
94
|
+
const messageKey = ownText === null ? fallbackKey : undefined;
|
|
106
95
|
if (status !== undefined) {
|
|
107
|
-
return new MediaLibraryError(codeForHttpStatus(status), message, { status
|
|
96
|
+
return new MediaLibraryError(codeForHttpStatus(status), message, { status, cause: error, messageKey });
|
|
108
97
|
}
|
|
109
98
|
// `fetch` rejects with a TypeError when the server cannot be reached at all. The browser's own text for it
|
|
110
99
|
// ("Failed to fetch") is English whatever the visitor's language, so the visitor reads our sentence instead; the
|
|
111
100
|
// host's `error` event still carries what the browser said. A TypeError the library threw with words of its own
|
|
112
101
|
// is not a network failure, and its words are shown.
|
|
113
102
|
if (error instanceof TypeError) {
|
|
114
|
-
|
|
115
|
-
return new MediaLibraryError('NETWORK', ownText
|
|
103
|
+
const isBrowserNetworkText = ownText !== null && BROWSER_NETWORK_FAILURE_TEXT.test(ownText);
|
|
104
|
+
return new MediaLibraryError('NETWORK', ownText ?? 'Could not reach the media library', {
|
|
116
105
|
cause: error,
|
|
117
106
|
messageKey: ownText === null || isBrowserNetworkText ? 'libraryError.unreachable' : undefined
|
|
118
107
|
});
|
|
119
108
|
}
|
|
120
|
-
return new MediaLibraryError('FAILED', message, { cause: error, messageKey
|
|
109
|
+
return new MediaLibraryError('FAILED', message, { cause: error, messageKey });
|
|
121
110
|
}
|
|
122
111
|
/** True when the failure means the credentials were refused, so refreshing them and trying again may help. */
|
|
123
112
|
export function isUnauthorized(error) {
|
|
@@ -1,18 +1,7 @@
|
|
|
1
|
-
var __assign = (this && this.__assign) || function () {
|
|
2
|
-
__assign = Object.assign || function(t) {
|
|
3
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
-
s = arguments[i];
|
|
5
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
-
t[p] = s[p];
|
|
7
|
-
}
|
|
8
|
-
return t;
|
|
9
|
-
};
|
|
10
|
-
return __assign.apply(this, arguments);
|
|
11
|
-
};
|
|
12
1
|
import { MediaLibraryError } from './errors';
|
|
13
2
|
import { toSafeMediaUrl } from './urls';
|
|
14
3
|
/** Limits that keep a library's data from breaking the screen. A value over its limit is cut or refused. */
|
|
15
|
-
export
|
|
4
|
+
export const LIMITS = {
|
|
16
5
|
idLength: 512,
|
|
17
6
|
nameLength: 255,
|
|
18
7
|
hashLength: 256,
|
|
@@ -20,10 +9,10 @@ export var LIMITS = {
|
|
|
20
9
|
/** More files than this in one page are ignored. */
|
|
21
10
|
itemsPerPage: 1000
|
|
22
11
|
};
|
|
23
|
-
|
|
12
|
+
const MEDIA_TYPE_PATTERN = /^[a-z0-9][a-z0-9!#$&^_.+-]*\/[a-z0-9][a-z0-9!#$&^_.+-]*$/i;
|
|
24
13
|
/** Control characters (line breaks, tabs, NUL ...) have no place in a name shown on one line. */
|
|
25
14
|
// eslint-disable-next-line no-control-regex
|
|
26
|
-
|
|
15
|
+
const CONTROL_CHARACTERS = /[\u0000-\u001f\u007f]+/g;
|
|
27
16
|
function isPlainObject(value) {
|
|
28
17
|
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
29
18
|
}
|
|
@@ -32,19 +21,19 @@ function toIdOrNull(value) {
|
|
|
32
21
|
return String(value);
|
|
33
22
|
if (typeof value !== 'string')
|
|
34
23
|
return null;
|
|
35
|
-
|
|
24
|
+
const trimmed = value.trim();
|
|
36
25
|
return trimmed !== '' && trimmed.length <= LIMITS.idLength ? trimmed : null;
|
|
37
26
|
}
|
|
38
27
|
function toNameOrNull(value) {
|
|
39
28
|
if (typeof value !== 'string')
|
|
40
29
|
return null;
|
|
41
|
-
|
|
30
|
+
const cleaned = value.replace(CONTROL_CHARACTERS, ' ').trim();
|
|
42
31
|
if (cleaned === '')
|
|
43
32
|
return null;
|
|
44
33
|
return cleaned.length > LIMITS.nameLength ? cleaned.slice(0, LIMITS.nameLength) : cleaned;
|
|
45
34
|
}
|
|
46
35
|
function toIsoDateOrUndefined(value) {
|
|
47
|
-
|
|
36
|
+
let date = null;
|
|
48
37
|
if (value instanceof Date)
|
|
49
38
|
date = value;
|
|
50
39
|
else if (typeof value === 'number' && Number.isFinite(value))
|
|
@@ -60,36 +49,45 @@ function toIsoDateOrUndefined(value) {
|
|
|
60
49
|
* no id, no name, or no address the browser may fetch. Optional fields that are wrong (a size of -1, a media type
|
|
61
50
|
* of `banana`) are dropped instead: the file is still usable without them.
|
|
62
51
|
*/
|
|
63
|
-
export function normalizeMediaLibraryFile(raw, options) {
|
|
64
|
-
var _a;
|
|
65
|
-
if (options === void 0) { options = {}; }
|
|
52
|
+
export function normalizeMediaLibraryFile(raw, options = {}) {
|
|
66
53
|
if (!isPlainObject(raw)) {
|
|
67
54
|
throw new MediaLibraryError('INVALID_RESPONSE', 'A file in the media library is not an object');
|
|
68
55
|
}
|
|
69
|
-
|
|
56
|
+
const id = toIdOrNull(raw.id);
|
|
70
57
|
if (id === null) {
|
|
71
58
|
throw new MediaLibraryError('INVALID_RESPONSE', 'A file in the media library has no id (a non-empty string)');
|
|
72
59
|
}
|
|
73
|
-
|
|
60
|
+
const name = toNameOrNull(raw.name);
|
|
74
61
|
if (name === null) {
|
|
75
|
-
throw new MediaLibraryError('INVALID_RESPONSE',
|
|
62
|
+
throw new MediaLibraryError('INVALID_RESPONSE', `The file "${id}" in the media library has no name`);
|
|
76
63
|
}
|
|
77
|
-
|
|
64
|
+
const url = toSafeMediaUrl(raw.url, { baseHref: options.baseHref });
|
|
78
65
|
if (url === null) {
|
|
79
|
-
throw new MediaLibraryError('INVALID_RESPONSE',
|
|
66
|
+
throw new MediaLibraryError('INVALID_RESPONSE', `The file "${id}" in the media library has no usable url (it must be an http or https address, or a path on this site)`);
|
|
80
67
|
}
|
|
81
|
-
|
|
68
|
+
const mimeType = typeof raw.mimeType === 'string' && MEDIA_TYPE_PATTERN.test(raw.mimeType.trim())
|
|
82
69
|
? raw.mimeType.trim().toLowerCase()
|
|
83
70
|
: undefined;
|
|
84
|
-
|
|
71
|
+
const size = typeof raw.size === 'number' && Number.isFinite(raw.size) && raw.size >= 0
|
|
85
72
|
? raw.size
|
|
86
73
|
: undefined;
|
|
87
|
-
|
|
88
|
-
|
|
74
|
+
const thumbnail = toSafeMediaUrl(raw.thumbnail, { baseHref: options.baseHref, allowDataPicture: true }) ??
|
|
75
|
+
undefined;
|
|
76
|
+
const hash = typeof raw.hash === 'string' && raw.hash !== '' && raw.hash.length <= LIMITS.hashLength
|
|
89
77
|
? raw.hash
|
|
90
78
|
: undefined;
|
|
91
|
-
|
|
92
|
-
return
|
|
79
|
+
const modified = toIsoDateOrUndefined(raw.modified);
|
|
80
|
+
return {
|
|
81
|
+
id,
|
|
82
|
+
name,
|
|
83
|
+
type: 'file',
|
|
84
|
+
url,
|
|
85
|
+
...(mimeType !== undefined && { mimeType }),
|
|
86
|
+
...(size !== undefined && { size }),
|
|
87
|
+
...(modified !== undefined && { modified }),
|
|
88
|
+
...(thumbnail !== undefined && { thumbnail }),
|
|
89
|
+
...(hash !== undefined && { hash })
|
|
90
|
+
};
|
|
93
91
|
}
|
|
94
92
|
function toCursorOrNull(value) {
|
|
95
93
|
if (typeof value === 'number' && Number.isFinite(value))
|
|
@@ -104,27 +102,26 @@ function toCursorOrNull(value) {
|
|
|
104
102
|
*
|
|
105
103
|
* Throws only when the page itself is not a page: it is not `{ items: [...] }` (a bare array is accepted too).
|
|
106
104
|
*/
|
|
107
|
-
export function normalizeMediaLibraryPage(raw, options) {
|
|
108
|
-
|
|
109
|
-
var rawItems = Array.isArray(raw) ? raw : isPlainObject(raw) ? raw.items : undefined;
|
|
105
|
+
export function normalizeMediaLibraryPage(raw, options = {}) {
|
|
106
|
+
const rawItems = Array.isArray(raw) ? raw : isPlainObject(raw) ? raw.items : undefined;
|
|
110
107
|
if (!Array.isArray(rawItems)) {
|
|
111
108
|
throw new MediaLibraryError('INVALID_RESPONSE', "The media library's list must resolve to { items: [...], nextCursor? }");
|
|
112
109
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
110
|
+
const nextCursor = isPlainObject(raw) ? toCursorOrNull(raw.nextCursor) : null;
|
|
111
|
+
const items = [];
|
|
112
|
+
const seenIds = new Set();
|
|
113
|
+
const skippedReasons = [];
|
|
114
|
+
let skipped = 0;
|
|
115
|
+
const note = (reason) => {
|
|
119
116
|
skipped += 1;
|
|
120
117
|
if (skippedReasons.length < 3)
|
|
121
118
|
skippedReasons.push(reason);
|
|
122
119
|
};
|
|
123
|
-
rawItems.slice(0, LIMITS.itemsPerPage).forEach(
|
|
120
|
+
rawItems.slice(0, LIMITS.itemsPerPage).forEach((rawItem) => {
|
|
124
121
|
try {
|
|
125
|
-
|
|
122
|
+
const item = normalizeMediaLibraryFile(rawItem, options);
|
|
126
123
|
if (seenIds.has(item.id)) {
|
|
127
|
-
note(
|
|
124
|
+
note(`The file id "${item.id}" appears twice`);
|
|
128
125
|
return;
|
|
129
126
|
}
|
|
130
127
|
seenIds.add(item.id);
|
|
@@ -136,7 +133,7 @@ export function normalizeMediaLibraryPage(raw, options) {
|
|
|
136
133
|
});
|
|
137
134
|
if (rawItems.length > LIMITS.itemsPerPage) {
|
|
138
135
|
skipped += rawItems.length - LIMITS.itemsPerPage;
|
|
139
|
-
skippedReasons.push(
|
|
136
|
+
skippedReasons.push(`Only the first ${LIMITS.itemsPerPage} files of a page are used`);
|
|
140
137
|
}
|
|
141
|
-
return { items
|
|
138
|
+
return { items, nextCursor, skipped, skippedReasons };
|
|
142
139
|
}
|
|
@@ -1,49 +1,13 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
12
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
-
function step(op) {
|
|
15
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
17
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
-
switch (op[0]) {
|
|
20
|
-
case 0: case 1: t = op; break;
|
|
21
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
-
default:
|
|
25
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
-
if (t[2]) _.ops.pop();
|
|
30
|
-
_.trys.pop(); continue;
|
|
31
|
-
}
|
|
32
|
-
op = body.call(thisArg, _);
|
|
33
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
1
|
import { MediaLibraryError, toMediaLibraryError } from './errors';
|
|
38
2
|
/** How many files the File Browser asks for per page. */
|
|
39
|
-
export
|
|
3
|
+
export const DEFAULT_PAGE_SIZE = 100;
|
|
40
4
|
/**
|
|
41
5
|
* The most files the File Browser loads. Files beyond it are not shown (search and filters only look at the files
|
|
42
6
|
* that were loaded), so a library with more files should list the ones a visitor is most likely to want first.
|
|
43
7
|
*/
|
|
44
|
-
export
|
|
8
|
+
export const DEFAULT_MAX_ITEMS = 1000;
|
|
45
9
|
/** A safety stop: no library is read for more pages than this, whatever it says about having more. */
|
|
46
|
-
export
|
|
10
|
+
export const DEFAULT_MAX_PAGES = 50;
|
|
47
11
|
/**
|
|
48
12
|
* Reads a library page by page, newest first, until it runs out or a limit is reached.
|
|
49
13
|
*
|
|
@@ -53,65 +17,46 @@ export var DEFAULT_MAX_PAGES = 50;
|
|
|
53
17
|
* The first page failing throws (there is nothing to show). A later page failing returns what was loaded with the
|
|
54
18
|
* failure attached, because most of the library is better than none of it.
|
|
55
19
|
*/
|
|
56
|
-
export function loadLibraryPages(library, options) {
|
|
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
|
-
return [2 /*return*/, { items: items, truncated: false }];
|
|
99
|
-
// The library says there is more. Make sure it is not going in circles before asking again.
|
|
100
|
-
if (newItems.length === 0 || usedCursors.has(page.nextCursor) || page.nextCursor === cursor) {
|
|
101
|
-
failure = new MediaLibraryError('INVALID_RESPONSE', 'The media library keeps returning the same files, so the rest was not loaded', { messageKey: 'libraryError.repeatedPage' });
|
|
102
|
-
console.warn("[FileBrowser] The media library \"".concat(library.id, "\" returned a page with nothing new or a repeated cursor; stopping at ").concat(items.length, " files"));
|
|
103
|
-
return [2 /*return*/, { items: items, truncated: true, failure: failure }];
|
|
104
|
-
}
|
|
105
|
-
if (items.length >= maxItems)
|
|
106
|
-
return [2 /*return*/, { items: items, truncated: true }];
|
|
107
|
-
usedCursors.add(page.nextCursor);
|
|
108
|
-
cursor = page.nextCursor;
|
|
109
|
-
_e.label = 6;
|
|
110
|
-
case 6:
|
|
111
|
-
pageNumber += 1;
|
|
112
|
-
return [3 /*break*/, 1];
|
|
113
|
-
case 7: return [2 /*return*/, { items: items, truncated: true }];
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
});
|
|
20
|
+
export async function loadLibraryPages(library, options) {
|
|
21
|
+
const pageSize = options.pageSize ?? DEFAULT_PAGE_SIZE;
|
|
22
|
+
const maxItems = options.maxItems ?? DEFAULT_MAX_ITEMS;
|
|
23
|
+
const maxPages = options.maxPages ?? DEFAULT_MAX_PAGES;
|
|
24
|
+
const items = [];
|
|
25
|
+
const seenIds = new Set();
|
|
26
|
+
const usedCursors = new Set();
|
|
27
|
+
let cursor;
|
|
28
|
+
for (let pageNumber = 1; pageNumber <= maxPages; pageNumber += 1) {
|
|
29
|
+
let page;
|
|
30
|
+
try {
|
|
31
|
+
page = await library.listPage({ cursor, limit: pageSize, signal: options.signal });
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
const failure = toMediaLibraryError(error, 'Could not load the files', 'libraryError.loadFailed');
|
|
35
|
+
if (pageNumber === 1 || failure.code === 'ABORTED')
|
|
36
|
+
throw failure;
|
|
37
|
+
console.warn(`[FileBrowser] Page ${pageNumber} of the media library "${library.id}" failed; showing the ${items.length} files already loaded:`, failure.message);
|
|
38
|
+
return { items, truncated: true, failure };
|
|
39
|
+
}
|
|
40
|
+
if (page.skipped > 0) {
|
|
41
|
+
console.warn(`[FileBrowser] The media library "${library.id}" gave ${page.skipped} file(s) on page ${pageNumber} that cannot be shown:`, page.skippedReasons);
|
|
42
|
+
}
|
|
43
|
+
const newItems = page.items.filter((item) => !seenIds.has(item.id));
|
|
44
|
+
newItems.forEach((item) => seenIds.add(item.id));
|
|
45
|
+
items.push(...newItems);
|
|
46
|
+
if (newItems.length > 0)
|
|
47
|
+
options.onPage?.(newItems, pageNumber);
|
|
48
|
+
if (page.nextCursor === null)
|
|
49
|
+
return { items, truncated: false };
|
|
50
|
+
// The library says there is more. Make sure it is not going in circles before asking again.
|
|
51
|
+
if (newItems.length === 0 || usedCursors.has(page.nextCursor) || page.nextCursor === cursor) {
|
|
52
|
+
const failure = new MediaLibraryError('INVALID_RESPONSE', 'The media library keeps returning the same files, so the rest was not loaded', { messageKey: 'libraryError.repeatedPage' });
|
|
53
|
+
console.warn(`[FileBrowser] The media library "${library.id}" returned a page with nothing new or a repeated cursor; stopping at ${items.length} files`);
|
|
54
|
+
return { items, truncated: true, failure };
|
|
55
|
+
}
|
|
56
|
+
if (items.length >= maxItems)
|
|
57
|
+
return { items, truncated: true };
|
|
58
|
+
usedCursors.add(page.nextCursor);
|
|
59
|
+
cursor = page.nextCursor;
|
|
60
|
+
}
|
|
61
|
+
return { items, truncated: true };
|
|
117
62
|
}
|