@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,50 +1,3 @@
|
|
|
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
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
13
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
14
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
15
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
16
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
17
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
18
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
22
|
-
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);
|
|
23
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
24
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
25
|
-
function step(op) {
|
|
26
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
27
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
28
|
-
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;
|
|
29
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
30
|
-
switch (op[0]) {
|
|
31
|
-
case 0: case 1: t = op; break;
|
|
32
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
33
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
34
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
35
|
-
default:
|
|
36
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
37
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
38
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
39
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
40
|
-
if (t[2]) _.ops.pop();
|
|
41
|
-
_.trys.pop(); continue;
|
|
42
|
-
}
|
|
43
|
-
op = body.call(thisArg, _);
|
|
44
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
45
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
1
|
import { MediaLibraryError, codeForHttpStatus } from '../mediaLibrary/errors';
|
|
49
2
|
import { shouldUseTusUpload, uploadToWidgeticApiTus, } from './widgeticTusUpload';
|
|
50
3
|
import { inferImageMime } from './uploadResponseMapping';
|
|
@@ -55,94 +8,73 @@ import { xhrMultipartUpload } from './xhrMultipartUpload';
|
|
|
55
8
|
* Files ≤10MB use POST /v1/uploads; larger files use TUS (/v1/uploads/tus).
|
|
56
9
|
* Parent app must set upload config via setFileBrowserUploadConfig().
|
|
57
10
|
*/
|
|
58
|
-
export function uploadToWidgeticApi(
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
// The token is asked for when the upload starts, and once more when the API refuses it (a page that renewed its session
|
|
63
|
-
// while the File Browser stayed open): the visitor does not have to reload the page to upload again.
|
|
64
|
-
return [2 /*return*/, withTokenRetriedOnce(config, function (accessToken) { return uploadWithAccessToken(file, __assign(__assign({}, config), { accessToken: accessToken }), options); }, { signal: options.abortSignal })];
|
|
65
|
-
});
|
|
66
|
-
});
|
|
11
|
+
export async function uploadToWidgeticApi(file, config, options = {}) {
|
|
12
|
+
// The token is asked for when the upload starts, and once more when the API refuses it (a page that renewed its session
|
|
13
|
+
// while the File Browser stayed open): the visitor does not have to reload the page to upload again.
|
|
14
|
+
return withTokenRetriedOnce(config, (accessToken) => uploadWithAccessToken(file, { ...config, accessToken }, options), { signal: options.abortSignal });
|
|
67
15
|
}
|
|
68
16
|
/** One try of the upload, with the token in `config.accessToken`. */
|
|
69
|
-
function uploadWithAccessToken(file, config, options) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
token = config.accessToken.replace(/^Bearer\s+/i, '');
|
|
88
|
-
return [2 /*return*/, xhrMultipartUpload("".concat(basePath, "/uploads"), token, formData, file, options)];
|
|
89
|
-
});
|
|
90
|
-
});
|
|
17
|
+
async function uploadWithAccessToken(file, config, options) {
|
|
18
|
+
if (shouldUseTusUpload(file)) {
|
|
19
|
+
return uploadToWidgeticApiTus(file, config, options);
|
|
20
|
+
}
|
|
21
|
+
const basePath = config.apiBasePath.replace(/\/+$/, '');
|
|
22
|
+
const formData = new FormData();
|
|
23
|
+
formData.append('file', file);
|
|
24
|
+
formData.append('context_type', config.contextType || 'content-property');
|
|
25
|
+
formData.append('source', config.source || 'file-browser-upload');
|
|
26
|
+
if (config.widgetId) {
|
|
27
|
+
formData.append('widget_id', config.widgetId);
|
|
28
|
+
}
|
|
29
|
+
if (config.conversationId) {
|
|
30
|
+
formData.append('conversation_id', config.conversationId);
|
|
31
|
+
}
|
|
32
|
+
const token = config.accessToken.replace(/^Bearer\s+/i, '');
|
|
33
|
+
return xhrMultipartUpload(`${basePath}/uploads`, token, formData, file, options);
|
|
91
34
|
}
|
|
92
35
|
/**
|
|
93
36
|
* Hydrate FileBrowser "My uploads" from GET /v1/uploads.
|
|
94
37
|
*/
|
|
95
|
-
export function fetchUserUploadsFromApi(
|
|
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
|
-
.filter(function (row) { return row.id && row.url; })
|
|
137
|
-
.map(function (row) {
|
|
138
|
-
var _a, _b, _c, _d;
|
|
139
|
-
var fileName = (row.file_name || row.fileName || 'upload');
|
|
140
|
-
var url = row.url;
|
|
141
|
-
var mime = inferImageMime((row.mime_type || row.mimeType || ''), fileName, url);
|
|
142
|
-
var libraryFolderPath = ((_b = (_a = row.library_folder_path) !== null && _a !== void 0 ? _a : row.libraryFolderPath) !== null && _b !== void 0 ? _b : null);
|
|
143
|
-
return __assign({ id: row.id, name: fileName, type: 'file', size: ((_d = (_c = row.file_size) !== null && _c !== void 0 ? _c : row.fileSize) !== null && _d !== void 0 ? _d : 0), modified: new Date().toISOString(), url: url, thumbnail: mime.startsWith('image/') ? url : undefined, mimeType: mime || undefined }, (libraryFolderPath ? { libraryFolderPath: libraryFolderPath } : {}));
|
|
144
|
-
})];
|
|
145
|
-
}
|
|
146
|
-
});
|
|
38
|
+
export async function fetchUserUploadsFromApi(config, options = {}) {
|
|
39
|
+
const basePath = config.apiBasePath.replace(/\/+$/, '');
|
|
40
|
+
const params = new URLSearchParams();
|
|
41
|
+
// List all user uploads in the library — do not filter by widget_id (uploads remain reusable).
|
|
42
|
+
params.set('limit', String(options.limit ?? 100));
|
|
43
|
+
const response = await withTokenRetriedOnce(config, (accessToken) => fetch(`${basePath}/uploads?${params.toString()}`, {
|
|
44
|
+
headers: { Authorization: `Bearer ${accessToken}` },
|
|
45
|
+
signal: options.signal,
|
|
46
|
+
}), { signal: options.signal });
|
|
47
|
+
if (!response.ok) {
|
|
48
|
+
let detail = `List uploads failed (${response.status})`;
|
|
49
|
+
try {
|
|
50
|
+
const errJson = await response.json();
|
|
51
|
+
detail = errJson?.message || errJson?.error?.message || detail;
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
/* ignore */
|
|
55
|
+
}
|
|
56
|
+
// A MediaLibraryError is an Error too; it also says whether the token was refused (401).
|
|
57
|
+
throw new MediaLibraryError(codeForHttpStatus(response.status), detail, { status: response.status });
|
|
58
|
+
}
|
|
59
|
+
const json = await response.json();
|
|
60
|
+
const rows = (json?.data ?? []);
|
|
61
|
+
return rows
|
|
62
|
+
.filter((row) => row.id && row.url)
|
|
63
|
+
.map((row) => {
|
|
64
|
+
const fileName = (row.file_name || row.fileName || 'upload');
|
|
65
|
+
const url = row.url;
|
|
66
|
+
const mime = inferImageMime((row.mime_type || row.mimeType || ''), fileName, url);
|
|
67
|
+
const libraryFolderPath = (row.library_folder_path ?? row.libraryFolderPath ?? null);
|
|
68
|
+
return {
|
|
69
|
+
id: row.id,
|
|
70
|
+
name: fileName,
|
|
71
|
+
type: 'file',
|
|
72
|
+
size: (row.file_size ?? row.fileSize ?? 0),
|
|
73
|
+
modified: new Date().toISOString(),
|
|
74
|
+
url,
|
|
75
|
+
thumbnail: mime.startsWith('image/') ? url : undefined,
|
|
76
|
+
mimeType: mime || undefined,
|
|
77
|
+
...(libraryFolderPath ? { libraryFolderPath } : {}),
|
|
78
|
+
};
|
|
147
79
|
});
|
|
148
80
|
}
|
|
@@ -1,173 +1,125 @@
|
|
|
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 { mapWidgeticUploadSuccessJson } from './uploadResponseMapping';
|
|
38
2
|
/**
|
|
39
3
|
* Multipart POST with upload progress via XMLHttpRequest (fetch cannot observe upload progress).
|
|
40
4
|
* Behavior matches the previous fetch-based upload: URL, headers, FormData fields, JSON parse, error mapping.
|
|
41
5
|
*/
|
|
42
|
-
export function xhrMultipartUpload(
|
|
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
|
-
|
|
6
|
+
export async function xhrMultipartUpload(url, token, formData, file, options = {}, createXhr = () => new XMLHttpRequest()) {
|
|
7
|
+
const authToken = token.replace(/^Bearer\s+/i, '');
|
|
8
|
+
const xhr = createXhr();
|
|
9
|
+
const xhrAny = xhr;
|
|
10
|
+
if (options.abortSignal)
|
|
11
|
+
xhrAny.__abortSignal = options.abortSignal;
|
|
12
|
+
let aborted = false;
|
|
13
|
+
const onAbort = () => {
|
|
14
|
+
aborted = true;
|
|
15
|
+
try {
|
|
16
|
+
xhr.abort();
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
/* ignore */
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
if (options.abortSignal) {
|
|
23
|
+
if (options.abortSignal.aborted) {
|
|
24
|
+
throw typeof DOMException !== 'undefined' ? new DOMException('The operation was aborted', 'AbortError') : new Error('AbortError');
|
|
25
|
+
}
|
|
26
|
+
options.abortSignal.addEventListener('abort', onAbort, { once: true });
|
|
27
|
+
}
|
|
28
|
+
const cleanup = () => {
|
|
29
|
+
options.abortSignal?.removeEventListener('abort', onAbort);
|
|
30
|
+
};
|
|
31
|
+
return new Promise((resolve, reject) => {
|
|
32
|
+
let settled = false;
|
|
33
|
+
const finish = (result) => {
|
|
34
|
+
if (settled)
|
|
35
|
+
return;
|
|
36
|
+
settled = true;
|
|
37
|
+
cleanup();
|
|
38
|
+
resolve(result);
|
|
39
|
+
};
|
|
40
|
+
const rejectAbort = () => {
|
|
41
|
+
if (settled)
|
|
42
|
+
return;
|
|
43
|
+
settled = true;
|
|
44
|
+
cleanup();
|
|
45
|
+
reject(typeof DOMException !== 'undefined' ? new DOMException('The operation was aborted', 'AbortError') : new Error('AbortError'));
|
|
46
|
+
};
|
|
47
|
+
/** Match `fetch` when the transport cannot reach the server so `toMediaLibraryError` maps to `libraryError.unreachable`. */
|
|
48
|
+
const rejectNetworkFailure = () => {
|
|
49
|
+
if (settled)
|
|
50
|
+
return;
|
|
51
|
+
settled = true;
|
|
52
|
+
cleanup();
|
|
53
|
+
reject(new TypeError('Failed to fetch'));
|
|
54
|
+
};
|
|
55
|
+
const tryParseJsonError = (text, status) => {
|
|
56
|
+
const fallback = `Upload failed (${status})`;
|
|
57
|
+
try {
|
|
58
|
+
const errJson = JSON.parse(text);
|
|
59
|
+
const fromTop = typeof errJson?.message === 'string' ? errJson.message : '';
|
|
60
|
+
const fromNested = typeof errJson?.error?.message === 'string' ? errJson.error.message : '';
|
|
61
|
+
return fromTop || fromNested || fallback;
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return fallback;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
xhr.addEventListener('load', () => {
|
|
68
|
+
if (aborted) {
|
|
69
|
+
rejectAbort();
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const status = xhr.status;
|
|
73
|
+
const text = xhr.responseText || '';
|
|
74
|
+
if (!status || status < 200 || status >= 300) {
|
|
75
|
+
finish({ success: false, error: tryParseJsonError(text, status || 0), status });
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
try {
|
|
79
|
+
const json = JSON.parse(text);
|
|
80
|
+
const data = (json?.data ?? json);
|
|
81
|
+
finish(mapWidgeticUploadSuccessJson(data, file));
|
|
68
82
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
var rejectAbort = function () {
|
|
83
|
-
if (settled)
|
|
84
|
-
return;
|
|
85
|
-
settled = true;
|
|
86
|
-
cleanup();
|
|
87
|
-
reject(typeof DOMException !== 'undefined' ? new DOMException('The operation was aborted', 'AbortError') : new Error('AbortError'));
|
|
88
|
-
};
|
|
89
|
-
/** Match `fetch` when the transport cannot reach the server so `toMediaLibraryError` maps to `libraryError.unreachable`. */
|
|
90
|
-
var rejectNetworkFailure = function () {
|
|
91
|
-
if (settled)
|
|
92
|
-
return;
|
|
93
|
-
settled = true;
|
|
94
|
-
cleanup();
|
|
95
|
-
reject(new TypeError('Failed to fetch'));
|
|
96
|
-
};
|
|
97
|
-
var tryParseJsonError = function (text, status) {
|
|
98
|
-
var _a;
|
|
99
|
-
var fallback = "Upload failed (".concat(status, ")");
|
|
100
|
-
try {
|
|
101
|
-
var errJson = JSON.parse(text);
|
|
102
|
-
var fromTop = typeof (errJson === null || errJson === void 0 ? void 0 : errJson.message) === 'string' ? errJson.message : '';
|
|
103
|
-
var fromNested = typeof ((_a = errJson === null || errJson === void 0 ? void 0 : errJson.error) === null || _a === void 0 ? void 0 : _a.message) === 'string' ? errJson.error.message : '';
|
|
104
|
-
return fromTop || fromNested || fallback;
|
|
105
|
-
}
|
|
106
|
-
catch (_b) {
|
|
107
|
-
return fallback;
|
|
108
|
-
}
|
|
109
|
-
};
|
|
110
|
-
xhr.addEventListener('load', function () {
|
|
111
|
-
var _a;
|
|
112
|
-
if (aborted) {
|
|
113
|
-
rejectAbort();
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
var status = xhr.status;
|
|
117
|
-
var text = xhr.responseText || '';
|
|
118
|
-
if (!status || status < 200 || status >= 300) {
|
|
119
|
-
finish({ success: false, error: tryParseJsonError(text, status || 0), status: status });
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
try {
|
|
123
|
-
var json = JSON.parse(text);
|
|
124
|
-
var data = ((_a = json === null || json === void 0 ? void 0 : json.data) !== null && _a !== void 0 ? _a : json);
|
|
125
|
-
finish(mapWidgeticUploadSuccessJson(data, file));
|
|
126
|
-
}
|
|
127
|
-
catch (_b) {
|
|
128
|
-
finish({ success: false, error: 'Upload failed (invalid JSON)' });
|
|
129
|
-
}
|
|
130
|
-
});
|
|
131
|
-
xhr.addEventListener('error', function () {
|
|
132
|
-
if (aborted) {
|
|
133
|
-
rejectAbort();
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
rejectNetworkFailure();
|
|
137
|
-
});
|
|
138
|
-
xhr.addEventListener('abort', function () {
|
|
139
|
-
rejectAbort();
|
|
140
|
-
});
|
|
141
|
-
xhr.upload.addEventListener('progress', function (evt) {
|
|
142
|
-
var _a;
|
|
143
|
-
if (aborted)
|
|
144
|
-
return;
|
|
145
|
-
var total = evt.lengthComputable ? evt.total : file.size;
|
|
146
|
-
var loaded = evt.loaded;
|
|
147
|
-
var safeTotal = total > 0 ? total : file.size || 1;
|
|
148
|
-
var percentage = safeTotal > 0 ? Math.round(Math.min(100, Math.max(0, (loaded / safeTotal) * 100))) : 0;
|
|
149
|
-
(_a = options.onProgress) === null || _a === void 0 ? void 0 : _a.call(options, { bytesUploaded: loaded, bytesTotal: safeTotal, percentage: percentage });
|
|
150
|
-
});
|
|
151
|
-
xhr.upload.addEventListener('load', function () {
|
|
152
|
-
var _a;
|
|
153
|
-
if (aborted)
|
|
154
|
-
return;
|
|
155
|
-
// Bytes are on the wire; waiting for HTTP response body (can be slow for large images).
|
|
156
|
-
(_a = options.onAwaitingResponse) === null || _a === void 0 ? void 0 : _a.call(options);
|
|
157
|
-
});
|
|
158
|
-
try {
|
|
159
|
-
xhr.open('POST', url);
|
|
160
|
-
xhr.setRequestHeader('Authorization', "Bearer ".concat(authToken));
|
|
161
|
-
xhr.send(formData);
|
|
162
|
-
}
|
|
163
|
-
catch (_a) {
|
|
164
|
-
if (aborted) {
|
|
165
|
-
rejectAbort();
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
rejectNetworkFailure();
|
|
169
|
-
}
|
|
170
|
-
})];
|
|
83
|
+
catch {
|
|
84
|
+
finish({ success: false, error: 'Upload failed (invalid JSON)' });
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
xhr.addEventListener('error', () => {
|
|
88
|
+
if (aborted) {
|
|
89
|
+
rejectAbort();
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
rejectNetworkFailure();
|
|
93
|
+
});
|
|
94
|
+
xhr.addEventListener('abort', () => {
|
|
95
|
+
rejectAbort();
|
|
171
96
|
});
|
|
97
|
+
xhr.upload.addEventListener('progress', (evt) => {
|
|
98
|
+
if (aborted)
|
|
99
|
+
return;
|
|
100
|
+
const total = evt.lengthComputable ? evt.total : file.size;
|
|
101
|
+
const loaded = evt.loaded;
|
|
102
|
+
const safeTotal = total > 0 ? total : file.size || 1;
|
|
103
|
+
const percentage = safeTotal > 0 ? Math.round(Math.min(100, Math.max(0, (loaded / safeTotal) * 100))) : 0;
|
|
104
|
+
options.onProgress?.({ bytesUploaded: loaded, bytesTotal: safeTotal, percentage });
|
|
105
|
+
});
|
|
106
|
+
xhr.upload.addEventListener('load', () => {
|
|
107
|
+
if (aborted)
|
|
108
|
+
return;
|
|
109
|
+
// Bytes are on the wire; waiting for HTTP response body (can be slow for large images).
|
|
110
|
+
options.onAwaitingResponse?.();
|
|
111
|
+
});
|
|
112
|
+
try {
|
|
113
|
+
xhr.open('POST', url);
|
|
114
|
+
xhr.setRequestHeader('Authorization', `Bearer ${authToken}`);
|
|
115
|
+
xhr.send(formData);
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
if (aborted) {
|
|
119
|
+
rejectAbort();
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
rejectNetworkFailure();
|
|
123
|
+
}
|
|
172
124
|
});
|
|
173
125
|
}
|