@widgetic/file-browser 0.5.47 → 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/components/core/Dropzone.svelte +42 -2
- package/dist/components/core/FileBrowser.svelte +221 -13
- package/dist/components/core/FileUploader.svelte +124 -41
- package/dist/components/core/RecentFiles.svelte +54 -16
- 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 +31 -0
- package/dist/components/ui/DropdownMenu.svelte +1 -1
- package/dist/i18n/messages.d.ts +13 -0
- package/dist/i18n/messages.js +13 -0
- package/dist/stores/uploadApiStore.d.ts +10 -0
- package/dist/utils/deleteSelectionAction.d.ts +26 -0
- package/dist/utils/deleteSelectionAction.js +23 -0
- package/dist/utils/pendingUploadDeletes.d.ts +102 -0
- package/dist/utils/pendingUploadDeletes.js +224 -0
- package/dist/utils/removeItems.d.ts +30 -0
- package/dist/utils/removeItems.js +92 -0
- package/dist/utils/searchDropdownPhase.d.ts +14 -3
- package/dist/utils/searchDropdownPhase.js +12 -8
- package/dist/utils/uploadAuth.d.ts +24 -0
- package/dist/utils/uploadAuth.js +65 -0
- package/dist/utils/uploadLimits.d.ts +76 -0
- package/dist/utils/uploadLimits.js +170 -0
- package/dist/utils/uploadRefusals.d.ts +42 -0
- package/dist/utils/uploadRefusals.js +70 -0
- package/dist/utils/videoFrame.d.ts +23 -0
- package/dist/utils/videoFrame.js +26 -0
- package/dist/utils/widgeticDelete.js +5 -3
- package/dist/utils/widgeticUpload.js +10 -4
- package/package.json +3 -3
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { withTokenRetriedOnce } from './uploadAuth';
|
|
2
|
+
import { WIDGETIC_MULTIPART_MAX_BYTES } from './widgeticTusUpload';
|
|
3
|
+
/**
|
|
4
|
+
* How long the answer of the API is kept before it is asked again. The limits are configuration that rarely changes; the
|
|
5
|
+
* minute is short so that the request that doubles as the session check still runs before each batch of files.
|
|
6
|
+
*/
|
|
7
|
+
const LIMITS_CACHE_MILLISECONDS = 60 * 1000;
|
|
8
|
+
/** The extensions that go with each MIME type the API can list (what the file chooser and the allowed types name). */
|
|
9
|
+
const EXTENSIONS_BY_MIME_TYPE = {
|
|
10
|
+
'image/png': ['.png'],
|
|
11
|
+
'image/jpeg': ['.jpg', '.jpeg'],
|
|
12
|
+
'image/webp': ['.webp'],
|
|
13
|
+
'image/gif': ['.gif'],
|
|
14
|
+
'image/svg+xml': ['.svg'],
|
|
15
|
+
'image/heic': ['.heic'],
|
|
16
|
+
'image/heif': ['.heif'],
|
|
17
|
+
'video/mp4': ['.mp4'],
|
|
18
|
+
'video/webm': ['.webm'],
|
|
19
|
+
'video/quicktime': ['.mov'],
|
|
20
|
+
'application/pdf': ['.pdf'],
|
|
21
|
+
'text/csv': ['.csv'],
|
|
22
|
+
'application/json': ['.json'],
|
|
23
|
+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': ['.xlsx'],
|
|
24
|
+
};
|
|
25
|
+
function stringList(value) {
|
|
26
|
+
if (!Array.isArray(value))
|
|
27
|
+
return null;
|
|
28
|
+
return value.filter((entry) => typeof entry === 'string').map((entry) => entry.trim().toLowerCase());
|
|
29
|
+
}
|
|
30
|
+
function positiveNumber(value) {
|
|
31
|
+
return typeof value === 'number' && Number.isFinite(value) && value > 0 ? value : null;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Reads the answer of `GET /v1/uploads/limits` (the payload is the `data` of the API's envelope; a bare payload is read
|
|
35
|
+
* too). `null` when it does not list the accepted types, so a changed answer never blocks an upload on a guess.
|
|
36
|
+
*/
|
|
37
|
+
export function parseUploadLimits(json) {
|
|
38
|
+
if (json === null || typeof json !== 'object')
|
|
39
|
+
return null;
|
|
40
|
+
const body = json;
|
|
41
|
+
const payload = body.data !== null && typeof body.data === 'object' ? body.data : body;
|
|
42
|
+
const multipartMimeTypes = stringList(payload.multipart_mime_types);
|
|
43
|
+
if (multipartMimeTypes === null || multipartMimeTypes.length === 0)
|
|
44
|
+
return null;
|
|
45
|
+
const tusMimeTypes = stringList(payload.tus_mime_types);
|
|
46
|
+
return {
|
|
47
|
+
multipartMaxBytes: positiveNumber(payload.multipart_max_bytes),
|
|
48
|
+
tusMaxBytes: positiveNumber(payload.tus_max_bytes),
|
|
49
|
+
tusEnabled: payload.tus_enabled !== false,
|
|
50
|
+
multipartMimeTypes: new Set(multipartMimeTypes),
|
|
51
|
+
tusMimeTypes: new Set(tusMimeTypes !== null && tusMimeTypes.length > 0 ? tusMimeTypes : multipartMimeTypes),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The reason the API would refuse a file, or `null` when it takes it (or when the limits are not known: the API then
|
|
56
|
+
* decides, as it always has). The type is judged first with the list that fits the way the file is sent, because a file
|
|
57
|
+
* of a kind the API never takes is refused for that whatever its size.
|
|
58
|
+
*/
|
|
59
|
+
export function refusalForFile(file, limits, isResumable = file.size > WIDGETIC_MULTIPART_MAX_BYTES) {
|
|
60
|
+
if (limits === null)
|
|
61
|
+
return null;
|
|
62
|
+
const mimeType = file.type.trim().toLowerCase();
|
|
63
|
+
const takenTypes = isResumable && limits.tusEnabled ? limits.tusMimeTypes : limits.multipartMimeTypes;
|
|
64
|
+
if (mimeType === '' || !takenTypes.has(mimeType))
|
|
65
|
+
return { reason: 'unsupportedType', mimeType };
|
|
66
|
+
const largestSize = limits.tusEnabled ? (limits.tusMaxBytes ?? limits.multipartMaxBytes) : limits.multipartMaxBytes;
|
|
67
|
+
if (largestSize !== null && file.size > largestSize)
|
|
68
|
+
return { reason: 'tooLarge', maxBytes: largestSize };
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
/** The extension of a file name with its dot and in lower case (".otf"); an empty string when the name has none. */
|
|
72
|
+
export function extensionOfFileName(fileName) {
|
|
73
|
+
const dotIndex = fileName.lastIndexOf('.');
|
|
74
|
+
if (dotIndex <= 0 || dotIndex === fileName.length - 1)
|
|
75
|
+
return '';
|
|
76
|
+
return fileName.slice(dotIndex).toLowerCase();
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The extensions the API takes: the ones that go with every MIME type it lists, in the order the File Browser lists its
|
|
80
|
+
* own kinds. `null` when the limits are not known.
|
|
81
|
+
*/
|
|
82
|
+
export function extensionsTakenBy(limits) {
|
|
83
|
+
if (limits === null)
|
|
84
|
+
return null;
|
|
85
|
+
const extensions = new Set();
|
|
86
|
+
for (const mimeType of [...limits.multipartMimeTypes, ...limits.tusMimeTypes]) {
|
|
87
|
+
for (const extension of EXTENSIONS_BY_MIME_TYPE[mimeType] ?? [])
|
|
88
|
+
extensions.add(extension);
|
|
89
|
+
}
|
|
90
|
+
return extensions;
|
|
91
|
+
}
|
|
92
|
+
/** The extensions of a list that the API takes. The whole list when the limits are not known. */
|
|
93
|
+
export function extensionsTakenFrom(extensions, limits) {
|
|
94
|
+
const taken = extensionsTakenBy(limits);
|
|
95
|
+
if (taken === null)
|
|
96
|
+
return [...extensions];
|
|
97
|
+
return extensions.filter((extension) => taken.has(extension.toLowerCase()));
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* True when the API takes a file of this MIME type, as far as the limits say. An empty type is "not known" (a browser
|
|
101
|
+
* reports none for some files while they are only dragged), so it is not refused here.
|
|
102
|
+
*/
|
|
103
|
+
export function limitsTakeMimeType(mimeType, limits) {
|
|
104
|
+
if (limits === null)
|
|
105
|
+
return true;
|
|
106
|
+
const normalized = mimeType.trim().toLowerCase();
|
|
107
|
+
if (normalized === '')
|
|
108
|
+
return true;
|
|
109
|
+
return limits.multipartMimeTypes.has(normalized) || limits.tusMimeTypes.has(normalized);
|
|
110
|
+
}
|
|
111
|
+
const limitsCacheByApiBase = new Map();
|
|
112
|
+
const limitsRequestByApiBase = new Map();
|
|
113
|
+
/** Forgets what was read (a test, or a page that changed its API address). */
|
|
114
|
+
export function resetUploadLimitsCache() {
|
|
115
|
+
limitsCacheByApiBase.clear();
|
|
116
|
+
limitsRequestByApiBase.clear();
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* The limits of the API behind `config`: from memory when they were read in the last minute, else asked for
|
|
120
|
+
* (`GET {apiBasePath}/uploads/limits`, with the token the page gives and a fresh one when the API refuses it). The same
|
|
121
|
+
* request is the check of the session that comes before the first file of a batch. One request is shared by every caller
|
|
122
|
+
* that asks while it is under way.
|
|
123
|
+
*/
|
|
124
|
+
export async function loadUploadLimits(config, options = {}) {
|
|
125
|
+
const apiBasePath = config.apiBasePath.replace(/\/+$/, '');
|
|
126
|
+
const now = options.now ?? Date.now;
|
|
127
|
+
const cached = limitsCacheByApiBase.get(apiBasePath);
|
|
128
|
+
if (!options.forceReload && cached && now() - cached.fetchedAt < LIMITS_CACHE_MILLISECONDS) {
|
|
129
|
+
return { limits: cached.limits, sessionRefused: false };
|
|
130
|
+
}
|
|
131
|
+
const requestInFlight = limitsRequestByApiBase.get(apiBasePath);
|
|
132
|
+
if (requestInFlight && !options.forceReload)
|
|
133
|
+
return requestInFlight;
|
|
134
|
+
const request = (async () => {
|
|
135
|
+
try {
|
|
136
|
+
const response = await withTokenRetriedOnce(config, (accessToken) => fetch(`${apiBasePath}/uploads/limits`, {
|
|
137
|
+
headers: { Authorization: `Bearer ${accessToken}` },
|
|
138
|
+
signal: options.signal,
|
|
139
|
+
}), { signal: options.signal });
|
|
140
|
+
if (response.status === 401) {
|
|
141
|
+
console.warn('[FileBrowser] The API refused the session when asking for the upload limits');
|
|
142
|
+
return { limits: null, sessionRefused: true };
|
|
143
|
+
}
|
|
144
|
+
if (!response.ok) {
|
|
145
|
+
console.warn(`[FileBrowser] Could not read the upload limits (${response.status}); the API will judge each file`);
|
|
146
|
+
return { limits: null, sessionRefused: false };
|
|
147
|
+
}
|
|
148
|
+
const limits = parseUploadLimits(await response.json());
|
|
149
|
+
if (limits === null) {
|
|
150
|
+
console.warn('[FileBrowser] The upload limits had no list of accepted types; the API will judge each file');
|
|
151
|
+
return { limits: null, sessionRefused: false };
|
|
152
|
+
}
|
|
153
|
+
limitsCacheByApiBase.set(apiBasePath, { limits, fetchedAt: now() });
|
|
154
|
+
return { limits, sessionRefused: false };
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
if (!(error instanceof DOMException && error.name === 'AbortError')) {
|
|
158
|
+
console.warn('[FileBrowser] Could not read the upload limits; the API will judge each file', error);
|
|
159
|
+
}
|
|
160
|
+
return { limits: null, sessionRefused: false };
|
|
161
|
+
}
|
|
162
|
+
})();
|
|
163
|
+
limitsRequestByApiBase.set(apiBasePath, request);
|
|
164
|
+
// The request is shared only while it is under way. (A newer one, started with `forceReload`, is not forgotten.)
|
|
165
|
+
void request.finally(() => {
|
|
166
|
+
if (limitsRequestByApiBase.get(apiBasePath) === request)
|
|
167
|
+
limitsRequestByApiBase.delete(apiBasePath);
|
|
168
|
+
});
|
|
169
|
+
return request;
|
|
170
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Translate } from '../i18n/context';
|
|
2
|
+
import { type UploadLimits, type UploadRefusal } from './uploadLimits';
|
|
3
|
+
/**
|
|
4
|
+
* Which of the files a visitor picked or dropped are sent, and which are left out with a reason, before anything is
|
|
5
|
+
* queued: a file the API would refuse (415 "Unsupported file type", 413 "too large") never becomes a row of the upload
|
|
6
|
+
* panel that fails after the wait and offers "Retry failed uploads?" for a file that can never go through.
|
|
7
|
+
*/
|
|
8
|
+
/** What is wrong with a file: the API's refusal, or the filter of this File Browser (it was set to take only some kinds). */
|
|
9
|
+
export type FileRefusal = UploadRefusal | {
|
|
10
|
+
reason: 'filter';
|
|
11
|
+
};
|
|
12
|
+
export type RefusedFile = {
|
|
13
|
+
file: File;
|
|
14
|
+
refusal: FileRefusal;
|
|
15
|
+
};
|
|
16
|
+
export type UploadPlan = {
|
|
17
|
+
accepted: File[];
|
|
18
|
+
refused: RefusedFile[];
|
|
19
|
+
};
|
|
20
|
+
/** How a file's type is named to the visitor: its extension (".otf"), else its MIME type, else nothing. */
|
|
21
|
+
export declare function typeLabelOfFile(file: Pick<File, 'name' | 'type'>): string;
|
|
22
|
+
/**
|
|
23
|
+
* Splits the files into those that go on to be uploaded and those that are refused. A file is refused when its
|
|
24
|
+
* extension is not one this File Browser takes (`takesExtension`, the visitor's filter), and then when the API says it
|
|
25
|
+
* would not take it (`limits`; without them the API judges each file, as it always has).
|
|
26
|
+
*/
|
|
27
|
+
export declare function planUploads(files: readonly File[], rules: {
|
|
28
|
+
takesExtension: (extension: string) => boolean;
|
|
29
|
+
limits: UploadLimits | null;
|
|
30
|
+
}): UploadPlan;
|
|
31
|
+
/** The sentence that says a kind of file is not supported: ".otf, .woff files are not supported". */
|
|
32
|
+
export declare function unsupportedTypeReason(labels: readonly string[], translate: Translate): string;
|
|
33
|
+
/**
|
|
34
|
+
* The sentences that tell the visitor which files were left out and why: one per reason (a pick of fonts and a huge
|
|
35
|
+
* video is two sentences), each naming the file when it is one file ("a.otf" was not uploaded: .otf files are not
|
|
36
|
+
* supported) or counting them when there are several (3 files were not uploaded: .otf, .woff files are not supported).
|
|
37
|
+
*
|
|
38
|
+
* `filterReason` is the sentence for the files the filter does not take, which the File Browser words from the kinds it
|
|
39
|
+
* was set to ("Invalid file type. Please use image files only."). `null` when it takes every kind: a file it does not list
|
|
40
|
+
* is then worded like any other unsupported type.
|
|
41
|
+
*/
|
|
42
|
+
export declare function refusalMessages(refused: readonly RefusedFile[], translate: Translate, filterReason: string | null): string[];
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { extensionOfFileName, refusalForFile } from './uploadLimits';
|
|
2
|
+
/** The most kinds of file named in one message (".a, .b, .c, .d, …"): more would only make the toast too long to read. */
|
|
3
|
+
const MOST_TYPES_NAMED = 4;
|
|
4
|
+
const BYTES_PER_MEGABYTE = 1024 * 1024;
|
|
5
|
+
/** How a file's type is named to the visitor: its extension (".otf"), else its MIME type, else nothing. */
|
|
6
|
+
export function typeLabelOfFile(file) {
|
|
7
|
+
return extensionOfFileName(file.name) || file.type.trim().toLowerCase();
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Splits the files into those that go on to be uploaded and those that are refused. A file is refused when its
|
|
11
|
+
* extension is not one this File Browser takes (`takesExtension`, the visitor's filter), and then when the API says it
|
|
12
|
+
* would not take it (`limits`; without them the API judges each file, as it always has).
|
|
13
|
+
*/
|
|
14
|
+
export function planUploads(files, rules) {
|
|
15
|
+
const accepted = [];
|
|
16
|
+
const refused = [];
|
|
17
|
+
for (const file of files) {
|
|
18
|
+
if (!rules.takesExtension(extensionOfFileName(file.name))) {
|
|
19
|
+
refused.push({ file, refusal: { reason: 'filter' } });
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
const refusal = refusalForFile(file, rules.limits);
|
|
23
|
+
if (refusal) {
|
|
24
|
+
refused.push({ file, refusal });
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
accepted.push(file);
|
|
28
|
+
}
|
|
29
|
+
return { accepted, refused };
|
|
30
|
+
}
|
|
31
|
+
/** The sentence that says a kind of file is not supported: ".otf, .woff files are not supported". */
|
|
32
|
+
export function unsupportedTypeReason(labels, translate) {
|
|
33
|
+
const named = [...new Set(labels.filter((label) => label !== ''))];
|
|
34
|
+
if (named.length === 0)
|
|
35
|
+
return translate('upload.refusedTypeUnknown');
|
|
36
|
+
const separator = translate('list.separator');
|
|
37
|
+
const shown = named.slice(0, MOST_TYPES_NAMED).join(separator);
|
|
38
|
+
return translate('upload.refusedType', { type: named.length > MOST_TYPES_NAMED ? `${shown}${separator}…` : shown });
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The sentences that tell the visitor which files were left out and why: one per reason (a pick of fonts and a huge
|
|
42
|
+
* video is two sentences), each naming the file when it is one file ("a.otf" was not uploaded: .otf files are not
|
|
43
|
+
* supported) or counting them when there are several (3 files were not uploaded: .otf, .woff files are not supported).
|
|
44
|
+
*
|
|
45
|
+
* `filterReason` is the sentence for the files the filter does not take, which the File Browser words from the kinds it
|
|
46
|
+
* was set to ("Invalid file type. Please use image files only."). `null` when it takes every kind: a file it does not list
|
|
47
|
+
* is then worded like any other unsupported type.
|
|
48
|
+
*/
|
|
49
|
+
export function refusalMessages(refused, translate, filterReason) {
|
|
50
|
+
const groups = new Map();
|
|
51
|
+
for (const entry of refused) {
|
|
52
|
+
const groupKey = entry.refusal.reason === 'tooLarge'
|
|
53
|
+
? `tooLarge:${entry.refusal.maxBytes}`
|
|
54
|
+
: entry.refusal.reason === 'filter' && filterReason !== null
|
|
55
|
+
? 'filter'
|
|
56
|
+
: 'unsupportedType';
|
|
57
|
+
groups.set(groupKey, [...(groups.get(groupKey) ?? []), entry]);
|
|
58
|
+
}
|
|
59
|
+
return [...groups.entries()].map(([groupKey, group]) => {
|
|
60
|
+
const firstRefusal = group[0].refusal;
|
|
61
|
+
const reason = groupKey === 'filter'
|
|
62
|
+
? filterReason
|
|
63
|
+
: firstRefusal.reason === 'tooLarge'
|
|
64
|
+
? translate('upload.refusedTooLarge', { limit: Math.round(firstRefusal.maxBytes / BYTES_PER_MEGABYTE) })
|
|
65
|
+
: unsupportedTypeReason(group.map(({ file }) => typeLabelOfFile(file)), translate);
|
|
66
|
+
return group.length === 1
|
|
67
|
+
? translate('upload.refusedOne', { name: group[0].file.name, reason })
|
|
68
|
+
: translate('upload.refusedMany', { count: group.length, reason });
|
|
69
|
+
});
|
|
70
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The cover of a video that has no thumbnail of its own is its first frame.
|
|
3
|
+
*
|
|
4
|
+
* A video cannot be drawn by `<img>` (the browser says "broken image"), but a `<video>` that loads only the metadata of the
|
|
5
|
+
* file shows its first frame, and nothing has to be generated or stored for it. The media fragment `#t=0.1` asks the
|
|
6
|
+
* browser to seek 0.1 s into the file: Safari and iOS draw no frame at all without it, and the very first frame of many
|
|
7
|
+
* videos is black.
|
|
8
|
+
*/
|
|
9
|
+
/** Where in the video the cover is taken, in seconds. */
|
|
10
|
+
export declare const VIDEO_FRAME_SECONDS = 0.1;
|
|
11
|
+
/**
|
|
12
|
+
* The address to give a `<video>` so that it shows a frame without playing. An address that already carries a media
|
|
13
|
+
* fragment (`#t=…`) or any other fragment is left alone: the one who wrote it knows better.
|
|
14
|
+
*/
|
|
15
|
+
export declare function videoFrameSrc(url: string): string;
|
|
16
|
+
/**
|
|
17
|
+
* True when a card draws the video itself as its cover: the file has an address and no thumbnail. A video that comes with a
|
|
18
|
+
* thumbnail (a media library gave one) keeps that picture, as before.
|
|
19
|
+
*/
|
|
20
|
+
export declare function drawsVideoFrame(file: {
|
|
21
|
+
url?: string;
|
|
22
|
+
thumbnail?: string;
|
|
23
|
+
}): boolean;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The cover of a video that has no thumbnail of its own is its first frame.
|
|
3
|
+
*
|
|
4
|
+
* A video cannot be drawn by `<img>` (the browser says "broken image"), but a `<video>` that loads only the metadata of the
|
|
5
|
+
* file shows its first frame, and nothing has to be generated or stored for it. The media fragment `#t=0.1` asks the
|
|
6
|
+
* browser to seek 0.1 s into the file: Safari and iOS draw no frame at all without it, and the very first frame of many
|
|
7
|
+
* videos is black.
|
|
8
|
+
*/
|
|
9
|
+
/** Where in the video the cover is taken, in seconds. */
|
|
10
|
+
export const VIDEO_FRAME_SECONDS = 0.1;
|
|
11
|
+
/**
|
|
12
|
+
* The address to give a `<video>` so that it shows a frame without playing. An address that already carries a media
|
|
13
|
+
* fragment (`#t=…`) or any other fragment is left alone: the one who wrote it knows better.
|
|
14
|
+
*/
|
|
15
|
+
export function videoFrameSrc(url) {
|
|
16
|
+
if (!url || url.includes('#'))
|
|
17
|
+
return url;
|
|
18
|
+
return `${url}#t=${VIDEO_FRAME_SECONDS}`;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* True when a card draws the video itself as its cover: the file has an address and no thumbnail. A video that comes with a
|
|
22
|
+
* thumbnail (a media library gave one) keeps that picture, as before.
|
|
23
|
+
*/
|
|
24
|
+
export function drawsVideoFrame(file) {
|
|
25
|
+
return Boolean(file.url) && !file.thumbnail;
|
|
26
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { MediaLibraryError, codeForHttpStatus } from '../mediaLibrary/errors';
|
|
2
|
+
import { withTokenRetriedOnce } from './uploadAuth';
|
|
2
3
|
function authHeaders(token) {
|
|
3
4
|
return { Authorization: `Bearer ${token.replace(/^Bearer\s+/i, '')}` };
|
|
4
5
|
}
|
|
@@ -7,11 +8,12 @@ function authHeaders(token) {
|
|
|
7
8
|
*/
|
|
8
9
|
export async function deleteUploadFromWidgeticApi(config, uploadId, options = {}) {
|
|
9
10
|
const basePath = config.apiBasePath.replace(/\/+$/, '');
|
|
10
|
-
|
|
11
|
+
// With the token the page gives now, and once more with a fresh one when the API refuses it (see `uploadAuth.ts`).
|
|
12
|
+
const response = await withTokenRetriedOnce(config, (accessToken) => fetch(`${basePath}/uploads/${encodeURIComponent(uploadId)}`, {
|
|
11
13
|
method: 'DELETE',
|
|
12
|
-
headers: authHeaders(
|
|
14
|
+
headers: authHeaders(accessToken),
|
|
13
15
|
signal: options.signal,
|
|
14
|
-
});
|
|
16
|
+
}), { signal: options.signal });
|
|
15
17
|
if (response.status === 404)
|
|
16
18
|
return;
|
|
17
19
|
if (!response.ok) {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { MediaLibraryError, codeForHttpStatus } from '../mediaLibrary/errors';
|
|
2
2
|
import { shouldUseTusUpload, uploadToWidgeticApiTus, } from './widgeticTusUpload';
|
|
3
3
|
import { inferImageMime } from './uploadResponseMapping';
|
|
4
|
+
import { withTokenRetriedOnce } from './uploadAuth';
|
|
4
5
|
import { xhrMultipartUpload } from './xhrMultipartUpload';
|
|
5
6
|
/**
|
|
6
7
|
* Upload a file to Widgetic API Gateway.
|
|
@@ -8,6 +9,12 @@ import { xhrMultipartUpload } from './xhrMultipartUpload';
|
|
|
8
9
|
* Parent app must set upload config via setFileBrowserUploadConfig().
|
|
9
10
|
*/
|
|
10
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 });
|
|
15
|
+
}
|
|
16
|
+
/** One try of the upload, with the token in `config.accessToken`. */
|
|
17
|
+
async function uploadWithAccessToken(file, config, options) {
|
|
11
18
|
if (shouldUseTusUpload(file)) {
|
|
12
19
|
return uploadToWidgeticApiTus(file, config, options);
|
|
13
20
|
}
|
|
@@ -30,14 +37,13 @@ export async function uploadToWidgeticApi(file, config, options = {}) {
|
|
|
30
37
|
*/
|
|
31
38
|
export async function fetchUserUploadsFromApi(config, options = {}) {
|
|
32
39
|
const basePath = config.apiBasePath.replace(/\/+$/, '');
|
|
33
|
-
const token = config.accessToken.replace(/^Bearer\s+/i, '');
|
|
34
40
|
const params = new URLSearchParams();
|
|
35
41
|
// List all user uploads in the library — do not filter by widget_id (uploads remain reusable).
|
|
36
42
|
params.set('limit', String(options.limit ?? 100));
|
|
37
|
-
const response = await fetch(`${basePath}/uploads?${params.toString()}`, {
|
|
38
|
-
headers: { Authorization: `Bearer ${
|
|
43
|
+
const response = await withTokenRetriedOnce(config, (accessToken) => fetch(`${basePath}/uploads?${params.toString()}`, {
|
|
44
|
+
headers: { Authorization: `Bearer ${accessToken}` },
|
|
39
45
|
signal: options.signal,
|
|
40
|
-
});
|
|
46
|
+
}), { signal: options.signal });
|
|
41
47
|
if (!response.ok) {
|
|
42
48
|
let detail = `List uploads failed (${response.status})`;
|
|
43
49
|
try {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@widgetic/file-browser",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.49",
|
|
4
4
|
"license": "SEE LICENSE IN LICENSE",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -96,7 +96,7 @@
|
|
|
96
96
|
"@sveltejs/vite-plugin-svelte": "^5.1.1",
|
|
97
97
|
"@tailwindcss/postcss": "^4.3.0",
|
|
98
98
|
"@types/node": "^22.13.4",
|
|
99
|
-
"@widgetic/cache-layer": "^0.1.
|
|
99
|
+
"@widgetic/cache-layer": "^0.1.7",
|
|
100
100
|
"@widgetic/design-system": "^0.5.28",
|
|
101
101
|
"@widgetic/dev-tools": "^0.3.0",
|
|
102
102
|
"concurrently": "^9.1.2",
|
|
@@ -117,7 +117,7 @@
|
|
|
117
117
|
},
|
|
118
118
|
"peerDependencies": {
|
|
119
119
|
"@sveltejs/kit": "^2.20.7",
|
|
120
|
-
"@widgetic/cache-layer": "^0.1.
|
|
120
|
+
"@widgetic/cache-layer": "^0.1.7",
|
|
121
121
|
"@widgetic/design-system": ">=0.5.28",
|
|
122
122
|
"pdfjs-dist": "^4.10.38",
|
|
123
123
|
"svelte": "^5.0.0"
|