@widgetic/file-browser 0.5.46 → 0.5.48
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 +104 -115
- package/dist/components/FolderTree.svelte.d.ts +2 -6
- package/dist/components/core/Dropzone.svelte +42 -2
- package/dist/components/core/FileBrowser.svelte +249 -32
- package/dist/components/core/FileUploader.svelte +134 -54
- package/dist/components/core/RecentFiles.svelte +74 -19
- package/dist/components/core/RecentFiles.svelte.d.ts +1 -0
- package/dist/components/core/SearchDropdown.svelte +6 -8
- package/dist/components/core/UploadPanel.svelte +7 -0
- package/dist/components/core/UploadPanel.svelte.d.ts +4 -0
- package/dist/components/core/UploadRow.svelte +9 -4
- package/dist/components/core/UploadRow.svelte.d.ts +2 -0
- package/dist/components/core/dropzoneClick.d.ts +28 -0
- package/dist/components/core/dropzoneClick.js +32 -0
- package/dist/components/previews/FilePreview.svelte.d.ts +2 -2
- package/dist/components/ui/DropdownMenu.svelte +1 -1
- package/dist/config.js +4 -3
- package/dist/i18n/context.js +2 -2
- package/dist/i18n/errors.js +2 -1
- package/dist/i18n/messages.d.ts +15 -1
- package/dist/i18n/messages.js +17 -3
- package/dist/i18n/text.js +2 -2
- package/dist/mediaLibrary/capabilities.js +4 -3
- package/dist/mediaLibrary/context.js +4 -3
- package/dist/mediaLibrary/errors.js +46 -35
- package/dist/mediaLibrary/normalize.js +45 -42
- package/dist/mediaLibrary/paging.js +100 -45
- package/dist/mediaLibrary/resolve.js +214 -95
- package/dist/mediaLibrary/treeOps.js +40 -20
- package/dist/mediaLibrary/urls.js +12 -10
- package/dist/mediaLibrary/widgetic.js +140 -54
- package/dist/stores/dragStore.js +1 -1
- package/dist/stores/fileStore.d.ts +1 -1
- package/dist/stores/fileStore.js +44 -37
- package/dist/stores/folderCountStore.js +12 -11
- package/dist/stores/missingFilesStore.js +3 -3
- package/dist/stores/navigationStore.d.ts +1 -1
- package/dist/stores/navigationStore.js +217 -178
- package/dist/stores/persistentSelectStore.d.ts +1 -1
- package/dist/stores/persistentSelectStore.js +117 -94
- package/dist/stores/searchStore.js +1 -1
- package/dist/stores/uploadApiStore.d.ts +10 -0
- package/dist/stores/uploadApiStore.js +7 -7
- package/dist/stores/viewportStore.js +4 -4
- package/dist/types/index.js +0 -1
- package/dist/utils/apiConfig.js +9 -9
- package/dist/utils/brandKitLibraryTree.js +57 -42
- package/dist/utils/dateUtils.js +11 -9
- package/dist/utils/deleteSelectionAction.d.ts +26 -0
- package/dist/utils/deleteSelectionAction.js +23 -0
- package/dist/utils/dragToFolder/batchMoveResult.js +114 -54
- package/dist/utils/dragToFolder/folderDropTarget.js +23 -17
- package/dist/utils/dragToFolder/springStep.js +4 -3
- package/dist/utils/fileGoneProbe.js +65 -16
- package/dist/utils/fileKinds.js +14 -13
- package/dist/utils/fileNames.js +7 -7
- package/dist/utils/fileOperations.js +440 -333
- package/dist/utils/fileTypes.js +19 -14
- package/dist/utils/fileUtils.js +85 -31
- package/dist/utils/folderUtils.js +5 -4
- package/dist/utils/gridLayout.js +7 -7
- package/dist/utils/imagePreviewCache.js +26 -15
- package/dist/utils/imagePreviewFrame.js +13 -13
- package/dist/utils/imagePreviewVisualState.js +5 -4
- package/dist/utils/imageTransform.js +27 -14
- package/dist/utils/itemMenuCapabilities.js +7 -7
- package/dist/utils/libraryTree.js +55 -39
- package/dist/utils/pendingUploadDeletes.d.ts +102 -0
- package/dist/utils/pendingUploadDeletes.js +400 -0
- package/dist/utils/previewKeys.js +1 -1
- package/dist/utils/previewListNavigation.js +13 -11
- package/dist/utils/previewNavigation.js +10 -7
- package/dist/utils/recentFiles.js +16 -6
- package/dist/utils/recentFilesLimit.js +2 -2
- package/dist/utils/removeItems.d.ts +30 -0
- package/dist/utils/removeItems.js +124 -0
- package/dist/utils/resolveFileUrl.js +6 -6
- package/dist/utils/safeText.js +14 -12
- package/dist/utils/searchDropdownPhase.d.ts +14 -3
- package/dist/utils/searchDropdownPhase.js +13 -9
- package/dist/utils/searchItems.js +30 -14
- package/dist/utils/searchNavigation.js +5 -5
- package/dist/utils/uploadAuth.d.ts +24 -0
- package/dist/utils/uploadAuth.js +140 -0
- package/dist/utils/uploadDisplayProgress.js +1 -1
- package/dist/utils/uploadLimits.d.ts +76 -0
- package/dist/utils/uploadLimits.js +242 -0
- package/dist/utils/uploadListReconcile.js +24 -11
- package/dist/utils/uploadPanelDismiss.d.ts +13 -0
- package/dist/utils/uploadPanelDismiss.js +11 -0
- package/dist/utils/uploadPanelProgress.js +4 -4
- package/dist/utils/uploadProgressMath.js +13 -11
- package/dist/utils/uploadRefusals.d.ts +42 -0
- package/dist/utils/uploadRefusals.js +86 -0
- package/dist/utils/uploadResponseMapping.js +9 -8
- package/dist/utils/uploadUiHelpers.js +17 -17
- package/dist/utils/userUploadsStorage.js +270 -133
- package/dist/utils/videoFrame.d.ts +23 -0
- package/dist/utils/videoFrame.js +26 -0
- package/dist/utils/widgeticDelete.js +103 -28
- package/dist/utils/widgeticTusUpload.js +189 -105
- package/dist/utils/widgeticUpload.js +133 -59
- package/dist/utils/xhrMultipartUpload.js +164 -116
- package/package.json +3 -3
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which clicks inside the drop zone open the file chooser.
|
|
3
|
+
*
|
|
4
|
+
* The whole zone is a target, not only the "Upload files" button: a click on the empty part, the dashed outline or the
|
|
5
|
+
* hint text opens the chooser as well. A click that lands on a control inside the zone (the button itself, the "?" help
|
|
6
|
+
* button) belongs to that control and is left alone, otherwise the chooser would open twice, or open from the help icon.
|
|
7
|
+
*/
|
|
8
|
+
/** Anything inside the zone that handles its own click. */
|
|
9
|
+
var OWN_CLICK_SELECTOR = 'button, a, input, select, textarea, [role="button"]';
|
|
10
|
+
/** Whether the click landed on a control that sits inside the zone (not on the zone itself). */
|
|
11
|
+
export function isClickOnControlInsideZone(target, zone) {
|
|
12
|
+
var _a, _b;
|
|
13
|
+
var control = (_b = (_a = target === null || target === void 0 ? void 0 : target.closest) === null || _a === void 0 ? void 0 : _a.call(target, OWN_CLICK_SELECTOR)) !== null && _b !== void 0 ? _b : null;
|
|
14
|
+
if (!control || control === zone)
|
|
15
|
+
return false;
|
|
16
|
+
return zone.contains(control);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The zone is only a click target while it is waiting for files: not while a file is dragged over it (the zone shows
|
|
20
|
+
* whether the drop is allowed) and not while the first upload is being set up.
|
|
21
|
+
*/
|
|
22
|
+
export function canBrowseFromZone(state, showInitialUploading) {
|
|
23
|
+
if (showInitialUploading)
|
|
24
|
+
return false;
|
|
25
|
+
return state !== 'drag-valid' && state !== 'drag-invalid';
|
|
26
|
+
}
|
|
27
|
+
/** Whether this click opens the file chooser. */
|
|
28
|
+
export function shouldBrowseFromZoneClick(input) {
|
|
29
|
+
if (!canBrowseFromZone(input.state, input.showInitialUploading))
|
|
30
|
+
return false;
|
|
31
|
+
return !isClickOnControlInsideZone(input.target, input.zone);
|
|
32
|
+
}
|
|
@@ -22,8 +22,8 @@ declare const FilePreview: $$__sveltets_2_IsomorphicComponent<{
|
|
|
22
22
|
currentFilter?: FileFilterType;
|
|
23
23
|
target?: string;
|
|
24
24
|
}, {
|
|
25
|
-
navigate:
|
|
26
|
-
select:
|
|
25
|
+
navigate: FileSystemItem;
|
|
26
|
+
select: FileSystemItem;
|
|
27
27
|
close: CustomEvent<void>;
|
|
28
28
|
fileUnavailable: CustomEvent<{
|
|
29
29
|
id: string;
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
<IconDots class="size-4 stroke-2" aria-hidden="true" />
|
|
55
55
|
<span class="sr-only">{$t('menu.moreOptions')}</span>
|
|
56
56
|
</DropdownMenu.Trigger>
|
|
57
|
-
<DropdownMenu.Content class="more-options-content
|
|
57
|
+
<DropdownMenu.Content class="more-options-content" align="end" sideOffset={4}>
|
|
58
58
|
{#if canRename}
|
|
59
59
|
<DropdownMenu.Item onclick={() => handleAction('rename')}>
|
|
60
60
|
<IconPencil class="stroke-2" />
|
package/dist/config.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
// File Browser Configuration
|
|
2
2
|
// Reads from environment variables with sensible defaults
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
var _a;
|
|
4
|
+
var env = process.env;
|
|
5
|
+
export var config = {
|
|
5
6
|
// Parse comma-separated origins or use defaults for development
|
|
6
|
-
ALLOWED_ORIGINS: env.PUBLIC_ALLOWED_ORIGINS
|
|
7
|
+
ALLOWED_ORIGINS: ((_a = env.PUBLIC_ALLOWED_ORIGINS) === null || _a === void 0 ? void 0 : _a.split(',')) || [
|
|
7
8
|
'http://localhost:5173', // Main site dev
|
|
8
9
|
'http://localhost:3000', // API Gateway
|
|
9
10
|
'http://localhost:5174', // Alternative dev port
|
package/dist/i18n/context.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { createI18n } from '@widgetic/design-system/i18n';
|
|
2
2
|
import { FILE_BROWSER_MESSAGES } from './messages';
|
|
3
3
|
/** The File Browser's words. Problems with the page's messages are said in the console as `[FileBrowser] …`. */
|
|
4
|
-
|
|
4
|
+
var fileBrowserI18n = createI18n({ catalog: FILE_BROWSER_MESSAGES, label: 'FileBrowser' });
|
|
5
5
|
/** The English `t`, for code that has no component around it (a helper called without a translator). */
|
|
6
|
-
export
|
|
6
|
+
export var translateEnglish = fileBrowserI18n.translateEnglish;
|
|
7
7
|
/**
|
|
8
8
|
* Called by the File Browser while it is created, so everything inside it speaks the page's language.
|
|
9
9
|
* The context belongs to one File Browser, so two File Browsers on a page can speak two languages.
|
package/dist/i18n/errors.js
CHANGED
|
@@ -7,7 +7,8 @@ import { MediaLibraryError } from '../mediaLibrary/errors';
|
|
|
7
7
|
* - Any other error keeps its message, as it always has.
|
|
8
8
|
* - With nothing to say, `fallbackKey`.
|
|
9
9
|
*/
|
|
10
|
-
export function messageForVisitor(error, t, fallbackKey
|
|
10
|
+
export function messageForVisitor(error, t, fallbackKey) {
|
|
11
|
+
if (fallbackKey === void 0) { fallbackKey = 'toast.unknownError'; }
|
|
11
12
|
if (error instanceof MediaLibraryError) {
|
|
12
13
|
if (error.messageKey !== undefined)
|
|
13
14
|
return t(error.messageKey, error.messageValues);
|
package/dist/i18n/messages.d.ts
CHANGED
|
@@ -110,6 +110,17 @@ export declare const FILE_BROWSER_MESSAGES: {
|
|
|
110
110
|
readonly 'upload.uploading': "Uploading…";
|
|
111
111
|
readonly 'upload.foldersNotAllowed': "Folders cannot be uploaded";
|
|
112
112
|
readonly 'upload.invalidType': "Invalid file type. Please use {types} files only.";
|
|
113
|
+
/** A file left out before it was sent, with the reason: {name} is the file's name, {reason} one of the "upload.refused…" reasons below. */
|
|
114
|
+
readonly 'upload.refusedOne': "\"{name}\" was not uploaded: {reason}";
|
|
115
|
+
/** Several files left out for the same reason. */
|
|
116
|
+
readonly 'upload.refusedMany': "{count, plural, one {# file was not uploaded} other {# files were not uploaded}}: {reason}";
|
|
117
|
+
/** {type} is the extensions of the files left out (".otf, .woff"), or their MIME type when a name has no extension. */
|
|
118
|
+
readonly 'upload.refusedType': "{type} files are not supported";
|
|
119
|
+
readonly 'upload.refusedTypeUnknown': "this type of file is not supported";
|
|
120
|
+
/** {limit} is the biggest size taken, in megabytes. */
|
|
121
|
+
readonly 'upload.refusedTooLarge': "larger than {limit} MB";
|
|
122
|
+
/** The session ended and could not be renewed: nothing can be sent until the visitor signs in again. */
|
|
123
|
+
readonly 'upload.sessionExpired': "Your session has expired. Sign in again to upload files.";
|
|
113
124
|
readonly 'upload.dropActive': "Drop to upload {types} files";
|
|
114
125
|
/** The four sentences of the dropzone when every kind of file is taken: they do not name a kind (Figma "Upload files"). */
|
|
115
126
|
readonly 'upload.dropActiveAny': "Drop to upload files";
|
|
@@ -183,7 +194,8 @@ export declare const FILE_BROWSER_MESSAGES: {
|
|
|
183
194
|
/** What a screen reader says for the X that closes a window. */
|
|
184
195
|
readonly 'dialog.close': "Close modal";
|
|
185
196
|
readonly 'toast.rename.started': "Renaming \"{name}\"…";
|
|
186
|
-
readonly 'toast.rename.done': "
|
|
197
|
+
readonly 'toast.rename.done': "File renamed";
|
|
198
|
+
readonly 'toast.rename.folderDone': "Folder renamed";
|
|
187
199
|
readonly 'toast.rename.doneDetail': "Successfully renamed \"{oldName}\" to \"{newName}\"";
|
|
188
200
|
readonly 'toast.rename.failed': "Rename failed";
|
|
189
201
|
readonly 'toast.rename.noChange': "No changes were made";
|
|
@@ -202,6 +214,8 @@ export declare const FILE_BROWSER_MESSAGES: {
|
|
|
202
214
|
readonly 'toast.delete.failedNames': "Failed to delete: {names}";
|
|
203
215
|
readonly 'toast.delete.failedSome': "Some items could not be deleted";
|
|
204
216
|
readonly 'toast.delete.failedAll': "Failed to delete items";
|
|
217
|
+
/** The API refused, so the files are listed again: the second line of the "Failed to delete" toast. */
|
|
218
|
+
readonly 'toast.delete.putBack': "{count, plural, one {The file is back in your library} other {The files are back in your library}}";
|
|
205
219
|
readonly 'toast.folder.created': "Folder created";
|
|
206
220
|
readonly 'toast.folder.createdDetail': "Created folder \"{name}\"";
|
|
207
221
|
readonly 'toast.folder.failed': "Failed to create folder";
|
package/dist/i18n/messages.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* Keys are grouped by where the words appear. A key that is left out of a page's `messages` keeps the English text
|
|
10
10
|
* below, so a language can be added a screen at a time.
|
|
11
11
|
*/
|
|
12
|
-
export
|
|
12
|
+
export var FILE_BROWSER_MESSAGES = {
|
|
13
13
|
// ===== Words that appear in several places =====
|
|
14
14
|
'action.cancel': 'Cancel',
|
|
15
15
|
'action.create': 'Create',
|
|
@@ -117,6 +117,17 @@ export const FILE_BROWSER_MESSAGES = {
|
|
|
117
117
|
'upload.uploading': 'Uploading…',
|
|
118
118
|
'upload.foldersNotAllowed': 'Folders cannot be uploaded',
|
|
119
119
|
'upload.invalidType': 'Invalid file type. Please use {types} files only.',
|
|
120
|
+
/** A file left out before it was sent, with the reason: {name} is the file's name, {reason} one of the "upload.refused…" reasons below. */
|
|
121
|
+
'upload.refusedOne': '"{name}" was not uploaded: {reason}',
|
|
122
|
+
/** Several files left out for the same reason. */
|
|
123
|
+
'upload.refusedMany': '{count, plural, one {# file was not uploaded} other {# files were not uploaded}}: {reason}',
|
|
124
|
+
/** {type} is the extensions of the files left out (".otf, .woff"), or their MIME type when a name has no extension. */
|
|
125
|
+
'upload.refusedType': '{type} files are not supported',
|
|
126
|
+
'upload.refusedTypeUnknown': 'this type of file is not supported',
|
|
127
|
+
/** {limit} is the biggest size taken, in megabytes. */
|
|
128
|
+
'upload.refusedTooLarge': 'larger than {limit} MB',
|
|
129
|
+
/** The session ended and could not be renewed: nothing can be sent until the visitor signs in again. */
|
|
130
|
+
'upload.sessionExpired': 'Your session has expired. Sign in again to upload files.',
|
|
120
131
|
'upload.dropActive': 'Drop to upload {types} files',
|
|
121
132
|
/** The four sentences of the dropzone when every kind of file is taken: they do not name a kind (Figma "Upload files"). */
|
|
122
133
|
'upload.dropActiveAny': 'Drop to upload files',
|
|
@@ -192,7 +203,8 @@ export const FILE_BROWSER_MESSAGES = {
|
|
|
192
203
|
'dialog.close': 'Close modal',
|
|
193
204
|
// ===== Messages that pop up after an action =====
|
|
194
205
|
'toast.rename.started': 'Renaming "{name}"…',
|
|
195
|
-
'toast.rename.done': '
|
|
206
|
+
'toast.rename.done': 'File renamed',
|
|
207
|
+
'toast.rename.folderDone': 'Folder renamed',
|
|
196
208
|
'toast.rename.doneDetail': 'Successfully renamed "{oldName}" to "{newName}"',
|
|
197
209
|
'toast.rename.failed': 'Rename failed',
|
|
198
210
|
'toast.rename.noChange': 'No changes were made',
|
|
@@ -211,6 +223,8 @@ export const FILE_BROWSER_MESSAGES = {
|
|
|
211
223
|
'toast.delete.failedNames': 'Failed to delete: {names}',
|
|
212
224
|
'toast.delete.failedSome': 'Some items could not be deleted',
|
|
213
225
|
'toast.delete.failedAll': 'Failed to delete items',
|
|
226
|
+
/** The API refused, so the files are listed again: the second line of the "Failed to delete" toast. */
|
|
227
|
+
'toast.delete.putBack': '{count, plural, one {The file is back in your library} other {The files are back in your library}}',
|
|
214
228
|
'toast.folder.created': 'Folder created',
|
|
215
229
|
'toast.folder.createdDetail': 'Created folder "{name}"',
|
|
216
230
|
'toast.folder.failed': 'Failed to create folder',
|
|
@@ -309,7 +323,7 @@ export const FILE_BROWSER_MESSAGES = {
|
|
|
309
323
|
'date.overAYearAgo': 'Over a year ago',
|
|
310
324
|
};
|
|
311
325
|
/** Catalog keys referenced from Svelte without string literals in templates (i18n audit). */
|
|
312
|
-
export
|
|
326
|
+
export var UPLOAD_ERROR_KEYS = {
|
|
313
327
|
libraryFailed: 'libraryError.uploadFailed',
|
|
314
328
|
network: 'upload.networkError'
|
|
315
329
|
};
|
package/dist/i18n/text.js
CHANGED
|
@@ -4,10 +4,10 @@
|
|
|
4
4
|
* sentence does not contain gives the sentence whole, with nothing underlined.
|
|
5
5
|
*/
|
|
6
6
|
export function splitAround(sentence, link) {
|
|
7
|
-
|
|
7
|
+
var index = link === '' ? -1 : sentence.indexOf(link);
|
|
8
8
|
if (index < 0)
|
|
9
9
|
return { before: sentence, link: '', after: '' };
|
|
10
|
-
return { before: sentence.slice(0, index), link, after: sentence.slice(index + link.length) };
|
|
10
|
+
return { before: sentence.slice(0, index), link: link, after: sentence.slice(index + link.length) };
|
|
11
11
|
}
|
|
12
12
|
/**
|
|
13
13
|
* The word for a kind of file ("image", "document", "SVG") in the page's language, for a sentence like "Upload image
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* What the File Browser offers when it was not given a media library: everything it has always offered. The Widgetic
|
|
3
3
|
* site and the editor use it this way, so they behave exactly as before.
|
|
4
4
|
*/
|
|
5
|
-
export
|
|
5
|
+
export var LEGACY_CAPABILITIES = Object.freeze({
|
|
6
6
|
upload: true,
|
|
7
7
|
rename: true,
|
|
8
8
|
delete: true,
|
|
@@ -13,7 +13,7 @@ export const LEGACY_CAPABILITIES = Object.freeze({
|
|
|
13
13
|
* What the File Browser offers when the media library it was given cannot be used: nothing, so no button leads to
|
|
14
14
|
* something that cannot work. (The screen says what is wrong.)
|
|
15
15
|
*/
|
|
16
|
-
export
|
|
16
|
+
export var NO_CAPABILITIES = Object.freeze({
|
|
17
17
|
upload: false,
|
|
18
18
|
rename: false,
|
|
19
19
|
delete: false,
|
|
@@ -31,7 +31,8 @@ export const NO_CAPABILITIES = Object.freeze({
|
|
|
31
31
|
* visitor's work. (Folders need a library that has folders of its own; the contract does not have them yet.)
|
|
32
32
|
*/
|
|
33
33
|
export function capabilitiesOf(provider) {
|
|
34
|
-
|
|
34
|
+
var _a;
|
|
35
|
+
var declared = (_a = provider.capabilities) !== null && _a !== void 0 ? _a : {};
|
|
35
36
|
return {
|
|
36
37
|
upload: typeof provider.upload === 'function' && declared.upload !== false,
|
|
37
38
|
rename: typeof provider.rename === 'function' && declared.rename !== false,
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { getContext, setContext } from 'svelte';
|
|
2
2
|
import { LEGACY_CAPABILITIES } from './capabilities';
|
|
3
|
-
|
|
3
|
+
var MEDIA_LIBRARY_CONTEXT_KEY = Symbol('widgetic-file-browser-media-library');
|
|
4
4
|
/** What a part of the File Browser sees when no library was supplied: the way it has always worked. */
|
|
5
|
-
|
|
5
|
+
var LEGACY_CONTEXT = Object.freeze({
|
|
6
6
|
library: null,
|
|
7
7
|
capabilities: LEGACY_CAPABILITIES
|
|
8
8
|
});
|
|
@@ -12,5 +12,6 @@ export function provideMediaLibraryContext(context) {
|
|
|
12
12
|
}
|
|
13
13
|
/** Called by a part of the File Browser while it is created. Never throws; outside a File Browser it reads as legacy. */
|
|
14
14
|
export function useMediaLibraryContext() {
|
|
15
|
-
|
|
15
|
+
var _a;
|
|
16
|
+
return (_a = getContext(MEDIA_LIBRARY_CONTEXT_KEY)) !== null && _a !== void 0 ? _a : LEGACY_CONTEXT;
|
|
16
17
|
}
|
|
@@ -1,32 +1,41 @@
|
|
|
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
|
+
})();
|
|
1
16
|
/** The longest message taken from a library's own error. Longer ones are cut so a toast stays readable. */
|
|
2
|
-
|
|
17
|
+
var MAX_MESSAGE_LENGTH = 300;
|
|
3
18
|
/** What Chrome, Firefox, Safari and React Native write in the TypeError of a `fetch` that could not reach the server. */
|
|
4
|
-
|
|
19
|
+
var BROWSER_NETWORK_FAILURE_TEXT = /failed to fetch|networkerror|network request failed|load failed/i;
|
|
5
20
|
/** A failed call to a media library, with a reason the File Browser can act on. */
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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;
|
|
21
|
+
var MediaLibraryError = /** @class */ (function (_super) {
|
|
22
|
+
__extends(MediaLibraryError, _super);
|
|
23
|
+
function MediaLibraryError(code, message, options) {
|
|
24
|
+
if (options === void 0) { options = {}; }
|
|
25
|
+
var _this = _super.call(this, message) || this;
|
|
26
|
+
_this.name = 'MediaLibraryError';
|
|
27
|
+
_this.code = code;
|
|
28
|
+
_this.status = options.status;
|
|
29
|
+
_this.messageKey = options.messageKey;
|
|
30
|
+
_this.messageValues = options.messageValues;
|
|
25
31
|
if (options.cause !== undefined) {
|
|
26
|
-
|
|
32
|
+
_this.cause = options.cause;
|
|
27
33
|
}
|
|
34
|
+
return _this;
|
|
28
35
|
}
|
|
29
|
-
|
|
36
|
+
return MediaLibraryError;
|
|
37
|
+
}(Error));
|
|
38
|
+
export { MediaLibraryError };
|
|
30
39
|
/** The error code that fits an HTTP status. */
|
|
31
40
|
export function codeForHttpStatus(status) {
|
|
32
41
|
if (status === 401)
|
|
@@ -52,10 +61,12 @@ export function isCancellation(error) {
|
|
|
52
61
|
}
|
|
53
62
|
/** The HTTP status a thrown value carries, if it carries one in any of the usual places. */
|
|
54
63
|
function statusOf(error) {
|
|
64
|
+
var _a;
|
|
55
65
|
if (error === null || typeof error !== 'object')
|
|
56
66
|
return undefined;
|
|
57
|
-
|
|
58
|
-
for (
|
|
67
|
+
var candidate = error;
|
|
68
|
+
for (var _i = 0, _b = [candidate.status, candidate.statusCode, (_a = candidate.response) === null || _a === void 0 ? void 0 : _a.status]; _i < _b.length; _i++) {
|
|
69
|
+
var value = _b[_i];
|
|
59
70
|
if (typeof value === 'number' && Number.isInteger(value) && value >= 100 && value <= 599)
|
|
60
71
|
return value;
|
|
61
72
|
}
|
|
@@ -65,10 +76,10 @@ function statusOf(error) {
|
|
|
65
76
|
function ownTextOf(text) {
|
|
66
77
|
if (typeof text !== 'string')
|
|
67
78
|
return null;
|
|
68
|
-
|
|
79
|
+
var trimmed = text.trim();
|
|
69
80
|
if (trimmed === '')
|
|
70
81
|
return null;
|
|
71
|
-
return trimmed.length > MAX_MESSAGE_LENGTH ?
|
|
82
|
+
return trimmed.length > MAX_MESSAGE_LENGTH ? "".concat(trimmed.slice(0, MAX_MESSAGE_LENGTH), "\u2026") : trimmed;
|
|
72
83
|
}
|
|
73
84
|
/**
|
|
74
85
|
* Turns whatever a library's function threw (or rejected with) into a {@link MediaLibraryError}.
|
|
@@ -83,30 +94,30 @@ export function toMediaLibraryError(error, fallbackMessage, fallbackKey) {
|
|
|
83
94
|
if (isAbortError(error)) {
|
|
84
95
|
return new MediaLibraryError('ABORTED', 'The request was cancelled', { cause: error });
|
|
85
96
|
}
|
|
86
|
-
|
|
87
|
-
|
|
97
|
+
var status = statusOf(error);
|
|
98
|
+
var ownText = ownTextOf(typeof error === 'string'
|
|
88
99
|
? error
|
|
89
100
|
: error !== null && typeof error === 'object'
|
|
90
101
|
? error.message
|
|
91
102
|
: undefined);
|
|
92
|
-
|
|
103
|
+
var message = ownText !== null && ownText !== void 0 ? ownText : fallbackMessage;
|
|
93
104
|
// Only the File Browser's own words are translated; what the library wrote is shown as it is.
|
|
94
|
-
|
|
105
|
+
var messageKey = ownText === null ? fallbackKey : undefined;
|
|
95
106
|
if (status !== undefined) {
|
|
96
|
-
return new MediaLibraryError(codeForHttpStatus(status), message, { status, cause: error, messageKey });
|
|
107
|
+
return new MediaLibraryError(codeForHttpStatus(status), message, { status: status, cause: error, messageKey: messageKey });
|
|
97
108
|
}
|
|
98
109
|
// `fetch` rejects with a TypeError when the server cannot be reached at all. The browser's own text for it
|
|
99
110
|
// ("Failed to fetch") is English whatever the visitor's language, so the visitor reads our sentence instead; the
|
|
100
111
|
// host's `error` event still carries what the browser said. A TypeError the library threw with words of its own
|
|
101
112
|
// is not a network failure, and its words are shown.
|
|
102
113
|
if (error instanceof TypeError) {
|
|
103
|
-
|
|
104
|
-
return new MediaLibraryError('NETWORK', ownText
|
|
114
|
+
var isBrowserNetworkText = ownText !== null && BROWSER_NETWORK_FAILURE_TEXT.test(ownText);
|
|
115
|
+
return new MediaLibraryError('NETWORK', ownText !== null && ownText !== void 0 ? ownText : 'Could not reach the media library', {
|
|
105
116
|
cause: error,
|
|
106
117
|
messageKey: ownText === null || isBrowserNetworkText ? 'libraryError.unreachable' : undefined
|
|
107
118
|
});
|
|
108
119
|
}
|
|
109
|
-
return new MediaLibraryError('FAILED', message, { cause: error, messageKey });
|
|
120
|
+
return new MediaLibraryError('FAILED', message, { cause: error, messageKey: messageKey });
|
|
110
121
|
}
|
|
111
122
|
/** True when the failure means the credentials were refused, so refreshing them and trying again may help. */
|
|
112
123
|
export function isUnauthorized(error) {
|
|
@@ -1,7 +1,18 @@
|
|
|
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
|
+
};
|
|
1
12
|
import { MediaLibraryError } from './errors';
|
|
2
13
|
import { toSafeMediaUrl } from './urls';
|
|
3
14
|
/** Limits that keep a library's data from breaking the screen. A value over its limit is cut or refused. */
|
|
4
|
-
export
|
|
15
|
+
export var LIMITS = {
|
|
5
16
|
idLength: 512,
|
|
6
17
|
nameLength: 255,
|
|
7
18
|
hashLength: 256,
|
|
@@ -9,10 +20,10 @@ export const LIMITS = {
|
|
|
9
20
|
/** More files than this in one page are ignored. */
|
|
10
21
|
itemsPerPage: 1000
|
|
11
22
|
};
|
|
12
|
-
|
|
23
|
+
var MEDIA_TYPE_PATTERN = /^[a-z0-9][a-z0-9!#$&^_.+-]*\/[a-z0-9][a-z0-9!#$&^_.+-]*$/i;
|
|
13
24
|
/** Control characters (line breaks, tabs, NUL ...) have no place in a name shown on one line. */
|
|
14
25
|
// eslint-disable-next-line no-control-regex
|
|
15
|
-
|
|
26
|
+
var CONTROL_CHARACTERS = /[\u0000-\u001f\u007f]+/g;
|
|
16
27
|
function isPlainObject(value) {
|
|
17
28
|
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
18
29
|
}
|
|
@@ -21,19 +32,19 @@ function toIdOrNull(value) {
|
|
|
21
32
|
return String(value);
|
|
22
33
|
if (typeof value !== 'string')
|
|
23
34
|
return null;
|
|
24
|
-
|
|
35
|
+
var trimmed = value.trim();
|
|
25
36
|
return trimmed !== '' && trimmed.length <= LIMITS.idLength ? trimmed : null;
|
|
26
37
|
}
|
|
27
38
|
function toNameOrNull(value) {
|
|
28
39
|
if (typeof value !== 'string')
|
|
29
40
|
return null;
|
|
30
|
-
|
|
41
|
+
var cleaned = value.replace(CONTROL_CHARACTERS, ' ').trim();
|
|
31
42
|
if (cleaned === '')
|
|
32
43
|
return null;
|
|
33
44
|
return cleaned.length > LIMITS.nameLength ? cleaned.slice(0, LIMITS.nameLength) : cleaned;
|
|
34
45
|
}
|
|
35
46
|
function toIsoDateOrUndefined(value) {
|
|
36
|
-
|
|
47
|
+
var date = null;
|
|
37
48
|
if (value instanceof Date)
|
|
38
49
|
date = value;
|
|
39
50
|
else if (typeof value === 'number' && Number.isFinite(value))
|
|
@@ -49,45 +60,36 @@ function toIsoDateOrUndefined(value) {
|
|
|
49
60
|
* no id, no name, or no address the browser may fetch. Optional fields that are wrong (a size of -1, a media type
|
|
50
61
|
* of `banana`) are dropped instead: the file is still usable without them.
|
|
51
62
|
*/
|
|
52
|
-
export function normalizeMediaLibraryFile(raw, options
|
|
63
|
+
export function normalizeMediaLibraryFile(raw, options) {
|
|
64
|
+
var _a;
|
|
65
|
+
if (options === void 0) { options = {}; }
|
|
53
66
|
if (!isPlainObject(raw)) {
|
|
54
67
|
throw new MediaLibraryError('INVALID_RESPONSE', 'A file in the media library is not an object');
|
|
55
68
|
}
|
|
56
|
-
|
|
69
|
+
var id = toIdOrNull(raw.id);
|
|
57
70
|
if (id === null) {
|
|
58
71
|
throw new MediaLibraryError('INVALID_RESPONSE', 'A file in the media library has no id (a non-empty string)');
|
|
59
72
|
}
|
|
60
|
-
|
|
73
|
+
var name = toNameOrNull(raw.name);
|
|
61
74
|
if (name === null) {
|
|
62
|
-
throw new MediaLibraryError('INVALID_RESPONSE',
|
|
75
|
+
throw new MediaLibraryError('INVALID_RESPONSE', "The file \"".concat(id, "\" in the media library has no name"));
|
|
63
76
|
}
|
|
64
|
-
|
|
77
|
+
var url = toSafeMediaUrl(raw.url, { baseHref: options.baseHref });
|
|
65
78
|
if (url === null) {
|
|
66
|
-
throw new MediaLibraryError('INVALID_RESPONSE',
|
|
79
|
+
throw new MediaLibraryError('INVALID_RESPONSE', "The file \"".concat(id, "\" in the media library has no usable url (it must be an http or https address, or a path on this site)"));
|
|
67
80
|
}
|
|
68
|
-
|
|
81
|
+
var mimeType = typeof raw.mimeType === 'string' && MEDIA_TYPE_PATTERN.test(raw.mimeType.trim())
|
|
69
82
|
? raw.mimeType.trim().toLowerCase()
|
|
70
83
|
: undefined;
|
|
71
|
-
|
|
84
|
+
var size = typeof raw.size === 'number' && Number.isFinite(raw.size) && raw.size >= 0
|
|
72
85
|
? raw.size
|
|
73
86
|
: undefined;
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
const hash = typeof raw.hash === 'string' && raw.hash !== '' && raw.hash.length <= LIMITS.hashLength
|
|
87
|
+
var thumbnail = (_a = toSafeMediaUrl(raw.thumbnail, { baseHref: options.baseHref, allowDataPicture: true })) !== null && _a !== void 0 ? _a : undefined;
|
|
88
|
+
var hash = typeof raw.hash === 'string' && raw.hash !== '' && raw.hash.length <= LIMITS.hashLength
|
|
77
89
|
? raw.hash
|
|
78
90
|
: undefined;
|
|
79
|
-
|
|
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
|
-
};
|
|
91
|
+
var modified = toIsoDateOrUndefined(raw.modified);
|
|
92
|
+
return __assign(__assign(__assign(__assign(__assign({ id: id, name: name, type: 'file', url: url }, (mimeType !== undefined && { mimeType: mimeType })), (size !== undefined && { size: size })), (modified !== undefined && { modified: modified })), (thumbnail !== undefined && { thumbnail: thumbnail })), (hash !== undefined && { hash: hash }));
|
|
91
93
|
}
|
|
92
94
|
function toCursorOrNull(value) {
|
|
93
95
|
if (typeof value === 'number' && Number.isFinite(value))
|
|
@@ -102,26 +104,27 @@ function toCursorOrNull(value) {
|
|
|
102
104
|
*
|
|
103
105
|
* Throws only when the page itself is not a page: it is not `{ items: [...] }` (a bare array is accepted too).
|
|
104
106
|
*/
|
|
105
|
-
export function normalizeMediaLibraryPage(raw, options
|
|
106
|
-
|
|
107
|
+
export function normalizeMediaLibraryPage(raw, options) {
|
|
108
|
+
if (options === void 0) { options = {}; }
|
|
109
|
+
var rawItems = Array.isArray(raw) ? raw : isPlainObject(raw) ? raw.items : undefined;
|
|
107
110
|
if (!Array.isArray(rawItems)) {
|
|
108
111
|
throw new MediaLibraryError('INVALID_RESPONSE', "The media library's list must resolve to { items: [...], nextCursor? }");
|
|
109
112
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
113
|
+
var nextCursor = isPlainObject(raw) ? toCursorOrNull(raw.nextCursor) : null;
|
|
114
|
+
var items = [];
|
|
115
|
+
var seenIds = new Set();
|
|
116
|
+
var skippedReasons = [];
|
|
117
|
+
var skipped = 0;
|
|
118
|
+
var note = function (reason) {
|
|
116
119
|
skipped += 1;
|
|
117
120
|
if (skippedReasons.length < 3)
|
|
118
121
|
skippedReasons.push(reason);
|
|
119
122
|
};
|
|
120
|
-
rawItems.slice(0, LIMITS.itemsPerPage).forEach((rawItem)
|
|
123
|
+
rawItems.slice(0, LIMITS.itemsPerPage).forEach(function (rawItem) {
|
|
121
124
|
try {
|
|
122
|
-
|
|
125
|
+
var item = normalizeMediaLibraryFile(rawItem, options);
|
|
123
126
|
if (seenIds.has(item.id)) {
|
|
124
|
-
note(
|
|
127
|
+
note("The file id \"".concat(item.id, "\" appears twice"));
|
|
125
128
|
return;
|
|
126
129
|
}
|
|
127
130
|
seenIds.add(item.id);
|
|
@@ -133,7 +136,7 @@ export function normalizeMediaLibraryPage(raw, options = {}) {
|
|
|
133
136
|
});
|
|
134
137
|
if (rawItems.length > LIMITS.itemsPerPage) {
|
|
135
138
|
skipped += rawItems.length - LIMITS.itemsPerPage;
|
|
136
|
-
skippedReasons.push(
|
|
139
|
+
skippedReasons.push("Only the first ".concat(LIMITS.itemsPerPage, " files of a page are used"));
|
|
137
140
|
}
|
|
138
|
-
return { items, nextCursor, skipped, skippedReasons };
|
|
141
|
+
return { items: items, nextCursor: nextCursor, skipped: skipped, skippedReasons: skippedReasons };
|
|
139
142
|
}
|