@antelopejs/dms-media 0.6.2 → 0.7.0
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/README.md +3 -3
- package/frontend-vue/app/build/imageEditor/useEditorSaving.ts +3 -2
- package/frontend-vue/app/build/library/libraryCore.ts +2 -1
- package/frontend-vue/app/components/FirstRun.vue +2 -1
- package/frontend-vue/app/components/dialogs/DestinationDialog.vue +3 -3
- package/frontend-vue/app/components/field/FieldAltPopover.vue +2 -1
- package/frontend-vue/app/composables/useAssetFieldUploads.ts +9 -1
- package/frontend-vue/app/composables/useAssetLink.ts +33 -13
- package/frontend-vue/app/composables/useFileDetails.ts +6 -5
- package/frontend-vue/app/composables/useUploadFailureText.ts +24 -5
- package/frontend-vue/app/composables/useUploadQueue.ts +178 -44
- package/frontend-vue/app/composables/useUploadToasts.ts +81 -19
- package/frontend-vue/package.json +2 -2
- package/frontend-vue/tests/api-error-presentation.test.ts +183 -0
- package/frontend-vue/tests/fixtures/serverRender.ts +1 -1
- package/package.json +3 -3
- package/skills/dms-media/SKILL.md +2 -2
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
<a href="https://www.npmjs.com/package/@antelopejs/dms-media"><img alt="npm" src="https://img.shields.io/npm/v/@antelopejs/dms-media?style=for-the-badge&labelColor=000000&color=000000"></a>
|
|
6
6
|
<a href="./LICENSE"><img alt="License" src="https://img.shields.io/npm/l/@antelopejs/dms-media?style=for-the-badge&labelColor=000000&color=000000"></a>
|
|
7
|
-
<a href="https://discord.gg/
|
|
7
|
+
<a href="https://discord.gg/g4pFt6eXqu"><img alt="Discord" src="https://img.shields.io/badge/Discord-18181B?logo=discord&style=for-the-badge&color=000000"></a>
|
|
8
8
|
<a href="https://dms.antelopejs.com/docs/modules/media"><img alt="Documentation" src="https://img.shields.io/badge/Docs-18181B?style=for-the-badge&color=000000"></a>
|
|
9
9
|
|
|
10
10
|
A media library for the AntelopeJS DMS: uploads, folders, image sizes and light editing.
|
|
@@ -79,9 +79,9 @@ new AssetType({
|
|
|
79
79
|
|
|
80
80
|
| Package | Version |
|
|
81
81
|
| --- | --- |
|
|
82
|
-
| `@antelopejs/dms` | `>=0.
|
|
82
|
+
| `@antelopejs/dms` | `>=0.13.1 <1.0.0` |
|
|
83
83
|
| `@antelopejs/interface-dms` | `>=0.9.0 <1.0.0` |
|
|
84
|
-
| `@antelopejs/dms-frontend` (renderer) | `>=0.9.
|
|
84
|
+
| `@antelopejs/dms-frontend` (renderer) | `>=0.9.2 <0.10.0` |
|
|
85
85
|
| `@antelopejs/interface-dms-media` | `>=0.3.0 <0.4.0` |
|
|
86
86
|
| File storage | an `@antelopejs/interface-file-storage` implementation, such as `@antelopejs/file-storage-local` |
|
|
87
87
|
|
|
@@ -20,6 +20,7 @@ export interface EditorSaving {
|
|
|
20
20
|
*/
|
|
21
21
|
export function useEditorSaving({ asset, isSaving, hasChanges, operations, api, toast, t, confirm }: EditorSaving) {
|
|
22
22
|
const isLeavingSaved = ref(false)
|
|
23
|
+
const { presentError } = useApiErrorPresenter()
|
|
23
24
|
|
|
24
25
|
useUnsavedChanges({ dirty: () => hasChanges() && !isLeavingSaved.value })
|
|
25
26
|
|
|
@@ -34,7 +35,7 @@ export function useEditorSaving({ asset, isSaving, hasChanges, operations, api,
|
|
|
34
35
|
try {
|
|
35
36
|
await run()
|
|
36
37
|
} catch (error) {
|
|
37
|
-
|
|
38
|
+
presentError(error, { fallbackKey: 'dms_media.editor.save_failed' })
|
|
38
39
|
} finally {
|
|
39
40
|
isSaving.value = false
|
|
40
41
|
}
|
|
@@ -71,7 +72,7 @@ export function useEditorSaving({ asset, isSaving, hasChanges, operations, api,
|
|
|
71
72
|
toast.add({ title: t('dms_media.editor.reverted'), color: 'success', icon: 'i-ph-arrow-counter-clockwise' })
|
|
72
73
|
await leaveTo(fileLink(current.id))
|
|
73
74
|
}
|
|
74
|
-
if (skipConfirm) return run().catch((error) =>
|
|
75
|
+
if (skipConfirm) return run().catch((error) => presentError(error, { fallbackKey: 'dms_media.editor.revert_failed' }))
|
|
75
76
|
await confirm({
|
|
76
77
|
title: t('dms_media.editor.revert_title'),
|
|
77
78
|
description: t('dms_media.editor.revert_description'),
|
|
@@ -7,6 +7,7 @@ export function createMediaLibrary(options: MediaLibraryOptions) {
|
|
|
7
7
|
const api = useMediaApi()
|
|
8
8
|
const { t } = useI18n()
|
|
9
9
|
const toast = useToast()
|
|
10
|
+
const { presentError } = useApiErrorPresenter()
|
|
10
11
|
const previews = useMediaPreviews()
|
|
11
12
|
const uploads = useUploadQueue()
|
|
12
13
|
const tree = useLibraryTree({ scopeFolderId: options.scopeFolderId })
|
|
@@ -51,7 +52,7 @@ export function createMediaLibrary(options: MediaLibraryOptions) {
|
|
|
51
52
|
}
|
|
52
53
|
|
|
53
54
|
function notifyFailure(error: unknown, titleKey: string): void {
|
|
54
|
-
|
|
55
|
+
presentError(error, { fallbackKey: titleKey })
|
|
55
56
|
}
|
|
56
57
|
|
|
57
58
|
async function open(next: LibraryLocation, remember = true): Promise<void> {
|
|
@@ -17,6 +17,7 @@ const STARTERS: StarterFolder[] = [
|
|
|
17
17
|
const api = useMediaApi()
|
|
18
18
|
const { t } = useI18n()
|
|
19
19
|
const toast = useToast()
|
|
20
|
+
const { presentError } = useApiErrorPresenter()
|
|
20
21
|
const { tree, ready } = useLibraryTree()
|
|
21
22
|
const isCreating = ref(false)
|
|
22
23
|
// Read with the page: a client navigation waits for the folders, so the welcome shows with it rather than after.
|
|
@@ -36,7 +37,7 @@ async function createStarters(): Promise<void> {
|
|
|
36
37
|
toast.add({ title: t('dms_media.first_run.created'), color: 'success', icon: 'i-ph-folders' })
|
|
37
38
|
await navigateDms(MEDIA_ROUTES.files)
|
|
38
39
|
} catch (error) {
|
|
39
|
-
|
|
40
|
+
presentError(error, { fallbackKey: 'dms_media.toasts.folder_create_failed' })
|
|
40
41
|
} finally {
|
|
41
42
|
isCreating.value = false
|
|
42
43
|
}
|
|
@@ -16,7 +16,7 @@ const props = defineProps<{
|
|
|
16
16
|
const emit = defineEmits<{ close: [string | null] }>()
|
|
17
17
|
const { t } = useI18n()
|
|
18
18
|
const api = useMediaApi()
|
|
19
|
-
const
|
|
19
|
+
const { presentError } = useApiErrorPresenter()
|
|
20
20
|
const format = useMediaFormat()
|
|
21
21
|
const nameDialog = useOverlay().create(NameDialog)
|
|
22
22
|
const { tree, refresh: reload } = useLibraryTree()
|
|
@@ -70,8 +70,8 @@ async function createFolder(): Promise<void> {
|
|
|
70
70
|
const { id } = await api.createFolder(name, parent?.id)
|
|
71
71
|
await reload()
|
|
72
72
|
target.value = id
|
|
73
|
-
} catch {
|
|
74
|
-
|
|
73
|
+
} catch (error) {
|
|
74
|
+
presentError(error, { fallbackKey: 'dms_media.toasts.folder_create_failed' })
|
|
75
75
|
} finally {
|
|
76
76
|
isCreating.value = false
|
|
77
77
|
}
|
|
@@ -12,6 +12,7 @@ const emit = defineEmits<{ saved: [MediaAsset] }>()
|
|
|
12
12
|
const isOpen = defineModel<boolean>('open', { default: false })
|
|
13
13
|
const { t } = useI18n()
|
|
14
14
|
const api = useMediaApi()
|
|
15
|
+
const { presentError } = useApiErrorPresenter()
|
|
15
16
|
|
|
16
17
|
const hasAlt = computed(() => !needsAltText(props.asset))
|
|
17
18
|
|
|
@@ -21,7 +22,7 @@ async function save(alt: string): Promise<boolean> {
|
|
|
21
22
|
emit('saved', { ...props.asset, alt })
|
|
22
23
|
return true
|
|
23
24
|
} catch (error) {
|
|
24
|
-
|
|
25
|
+
presentError(error, { fallbackKey: 'dms_media.field_widget.alt.save_failed' })
|
|
25
26
|
return false
|
|
26
27
|
}
|
|
27
28
|
}
|
|
@@ -30,6 +30,7 @@ export function useAssetFieldUploads(options: AssetFieldUploadOptions) {
|
|
|
30
30
|
const uploadToasts = useUploadToasts()
|
|
31
31
|
const toast = useToast()
|
|
32
32
|
const { t } = useI18n()
|
|
33
|
+
const { presentError } = useApiErrorPresenter()
|
|
33
34
|
const batchIds = ref<string[]>([])
|
|
34
35
|
const reportedFailures = new Set<string>()
|
|
35
36
|
|
|
@@ -96,10 +97,17 @@ export function useAssetFieldUploads(options: AssetFieldUploadOptions) {
|
|
|
96
97
|
FAILED_STATUSES.has(entry.status) && !reportedFailures.has(entry.id),
|
|
97
98
|
)) {
|
|
98
99
|
reportedFailures.add(item.id)
|
|
100
|
+
if (item.requestError !== undefined) {
|
|
101
|
+
presentError(item.requestError, {
|
|
102
|
+
fallbackKey: 'dms_media.field_widget.upload.failed',
|
|
103
|
+
params: { name: item.name },
|
|
104
|
+
})
|
|
105
|
+
continue
|
|
106
|
+
}
|
|
99
107
|
toast.add({
|
|
100
108
|
title: t('dms_media.field_widget.upload.failed', { name: item.name }),
|
|
101
109
|
description: failureText(item),
|
|
102
|
-
color: '
|
|
110
|
+
color: 'warning',
|
|
103
111
|
icon: 'i-ph-warning-circle',
|
|
104
112
|
})
|
|
105
113
|
}
|
|
@@ -49,13 +49,16 @@ export function useAssetLink() {
|
|
|
49
49
|
const { t } = useI18n()
|
|
50
50
|
const toast = useToast()
|
|
51
51
|
const { confirm } = useConfirm()
|
|
52
|
+
const { presentError } = useApiErrorPresenter()
|
|
52
53
|
|
|
53
54
|
async function linkOf(asset: Pick<MediaAsset, 'url'>): Promise<string> {
|
|
54
55
|
return `${await resolveLinksOrigin(() => $authFetch<LinksOrigin>(LINKS_ORIGIN_API))}${asset.url}`
|
|
55
56
|
}
|
|
56
57
|
|
|
57
58
|
/** Asks before a file becomes reachable by anyone holding its link. */
|
|
58
|
-
function confirmMakePublic(
|
|
59
|
+
function confirmMakePublic(
|
|
60
|
+
asset: Pick<MediaAsset, 'name'>,
|
|
61
|
+
): Promise<boolean> {
|
|
59
62
|
return confirm({
|
|
60
63
|
title: t('dms_media.asset_link.make_public_title', { name: asset.name }),
|
|
61
64
|
description: t('dms_media.asset_link.make_public_description'),
|
|
@@ -68,11 +71,16 @@ export function useAssetLink() {
|
|
|
68
71
|
})
|
|
69
72
|
}
|
|
70
73
|
|
|
71
|
-
function warnPrivate(
|
|
74
|
+
function warnPrivate(
|
|
75
|
+
asset: LinkedAsset,
|
|
76
|
+
options: CopyAssetLinkOptions,
|
|
77
|
+
): void {
|
|
72
78
|
const makePublic = options.makePublic
|
|
73
79
|
toast.add({
|
|
74
80
|
title: t('dms_media.toasts.link_copied'),
|
|
75
|
-
description: t('dms_media.asset_link.private_warning', {
|
|
81
|
+
description: t('dms_media.asset_link.private_warning', {
|
|
82
|
+
name: asset.name,
|
|
83
|
+
}),
|
|
76
84
|
color: 'warning',
|
|
77
85
|
icon: 'i-ph-lock-simple',
|
|
78
86
|
actions: makePublic
|
|
@@ -88,23 +96,33 @@ export function useAssetLink() {
|
|
|
88
96
|
})
|
|
89
97
|
}
|
|
90
98
|
|
|
91
|
-
async function copyLink(
|
|
99
|
+
async function copyLink(
|
|
100
|
+
asset: LinkedAsset,
|
|
101
|
+
options: CopyAssetLinkOptions = {},
|
|
102
|
+
): Promise<void> {
|
|
92
103
|
const link = await linkOf(asset).catch(() => asset.url)
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
104
|
+
const copied = await navigator.clipboard.writeText(link).then(
|
|
105
|
+
() => true,
|
|
106
|
+
() => false,
|
|
107
|
+
)
|
|
108
|
+
if (!copied) {
|
|
96
109
|
toast.add({
|
|
97
110
|
title: t('dms_media.asset_link.copy_failed'),
|
|
98
111
|
description: link,
|
|
99
|
-
color: '
|
|
112
|
+
color: 'warning',
|
|
100
113
|
icon: 'i-ph-warning',
|
|
101
114
|
duration: MANUAL_COPY_TOAST_MS,
|
|
102
115
|
ui: { description: 'select-all break-all font-mono' },
|
|
103
116
|
})
|
|
104
117
|
return
|
|
105
118
|
}
|
|
106
|
-
if (asset.effectiveVisibility === 'private')
|
|
107
|
-
|
|
119
|
+
if (asset.effectiveVisibility === 'private')
|
|
120
|
+
return warnPrivate(asset, options)
|
|
121
|
+
toast.add({
|
|
122
|
+
title: t('dms_media.toasts.link_copied'),
|
|
123
|
+
color: 'success',
|
|
124
|
+
icon: 'i-ph-link',
|
|
125
|
+
})
|
|
108
126
|
}
|
|
109
127
|
|
|
110
128
|
/**
|
|
@@ -112,7 +130,9 @@ export function useAssetLink() {
|
|
|
112
130
|
* the storage's origin, where the browser ignores a link's `download`, so the
|
|
113
131
|
* file is fetched and saved from memory.
|
|
114
132
|
*/
|
|
115
|
-
async function download(
|
|
133
|
+
async function download(
|
|
134
|
+
asset: Pick<MediaAsset, 'id' | 'name'>,
|
|
135
|
+
): Promise<boolean> {
|
|
116
136
|
try {
|
|
117
137
|
const { url } = await api.readUrl(asset.id)
|
|
118
138
|
const response = await fetch(url)
|
|
@@ -122,7 +142,7 @@ export function useAssetLink() {
|
|
|
122
142
|
setTimeout(() => URL.revokeObjectURL(objectUrl), OBJECT_URL_LIFETIME_MS)
|
|
123
143
|
return true
|
|
124
144
|
} catch (cause) {
|
|
125
|
-
|
|
145
|
+
presentError(cause, { fallbackKey: 'dms_media.toasts.download_failed' })
|
|
126
146
|
return false
|
|
127
147
|
}
|
|
128
148
|
}
|
|
@@ -137,7 +157,7 @@ export function useAssetLink() {
|
|
|
137
157
|
opened.location.href = url
|
|
138
158
|
} catch (cause) {
|
|
139
159
|
opened?.close()
|
|
140
|
-
|
|
160
|
+
presentError(cause, { fallbackKey: 'dms_media.asset_link.open_failed' })
|
|
141
161
|
}
|
|
142
162
|
}
|
|
143
163
|
|
|
@@ -48,6 +48,7 @@ export function useFileDetails(assetId: Ref<string | undefined>, order: Ref<File
|
|
|
48
48
|
const api = useMediaApi()
|
|
49
49
|
const { t } = useI18n()
|
|
50
50
|
const toast = useToast()
|
|
51
|
+
const { presentError } = useApiErrorPresenter()
|
|
51
52
|
const { tree, refresh: refreshTree } = useLibraryTree()
|
|
52
53
|
|
|
53
54
|
async function readSiblings(fetch: ApiDataContext['$authFetch'], signal: AbortSignal, folderId: string, id: string): Promise<SiblingsWindow> {
|
|
@@ -153,7 +154,7 @@ export function useFileDetails(assetId: Ref<string | undefined>, order: Ref<File
|
|
|
153
154
|
return true
|
|
154
155
|
} catch (cause) {
|
|
155
156
|
patch({ name: current.name })
|
|
156
|
-
|
|
157
|
+
presentError(cause, { fallbackKey: 'dms_media.toasts.rename_failed' })
|
|
157
158
|
return false
|
|
158
159
|
}
|
|
159
160
|
}
|
|
@@ -166,7 +167,7 @@ export function useFileDetails(assetId: Ref<string | undefined>, order: Ref<File
|
|
|
166
167
|
patch({ alt })
|
|
167
168
|
return true
|
|
168
169
|
} catch (cause) {
|
|
169
|
-
|
|
170
|
+
presentError(cause, { fallbackKey: 'dms_media.toasts.alt_failed' })
|
|
170
171
|
return false
|
|
171
172
|
}
|
|
172
173
|
}
|
|
@@ -178,7 +179,7 @@ export function useFileDetails(assetId: Ref<string | undefined>, order: Ref<File
|
|
|
178
179
|
await load()
|
|
179
180
|
return true
|
|
180
181
|
} catch (cause) {
|
|
181
|
-
|
|
182
|
+
presentError(cause, { fallbackKey: 'dms_media.toasts.visibility_failed' })
|
|
182
183
|
return false
|
|
183
184
|
}
|
|
184
185
|
}
|
|
@@ -204,7 +205,7 @@ export function useFileDetails(assetId: Ref<string | undefined>, order: Ref<File
|
|
|
204
205
|
await load()
|
|
205
206
|
toast.add({ title: t('dms_media.toasts.moved', { count: 1 }, 1), color: 'success' })
|
|
206
207
|
} catch (cause) {
|
|
207
|
-
|
|
208
|
+
presentError(cause, { fallbackKey: 'dms_media.toasts.move_failed' })
|
|
208
209
|
}
|
|
209
210
|
}
|
|
210
211
|
|
|
@@ -217,7 +218,7 @@ export function useFileDetails(assetId: Ref<string | undefined>, order: Ref<File
|
|
|
217
218
|
toast.add({ title: t('dms_media.toasts.deleted', { count: 1 }, 1), color: 'success', icon: 'i-ph-trash' })
|
|
218
219
|
return true
|
|
219
220
|
} catch (cause) {
|
|
220
|
-
|
|
221
|
+
presentError(cause, { fallbackKey: 'dms_media.toasts.delete_failed' })
|
|
221
222
|
return false
|
|
222
223
|
}
|
|
223
224
|
}
|
|
@@ -2,27 +2,46 @@ import type { UploadFailure, UploadItem } from './useUploadQueue'
|
|
|
2
2
|
|
|
3
3
|
const TYPES_SEPARATOR = ', '
|
|
4
4
|
|
|
5
|
-
export type UploadFailureSource = Pick<
|
|
5
|
+
export type UploadFailureSource = Pick<
|
|
6
|
+
UploadItem,
|
|
7
|
+
'failure' | 'details' | 'folderName' | 'message' | 'requestError'
|
|
8
|
+
>
|
|
6
9
|
|
|
7
10
|
/** Why a file did not upload, in the member's language, the same wherever it shows. */
|
|
8
11
|
export function useUploadFailureText() {
|
|
9
12
|
const uploads = useUploadQueue()
|
|
10
13
|
const format = useMediaFormat()
|
|
11
14
|
const { t } = useI18n()
|
|
15
|
+
const { resolveError } = useApiErrorPresenter()
|
|
12
16
|
|
|
13
17
|
function typesLabel(accepted: readonly string[] | undefined): string {
|
|
14
18
|
if (!accepted?.length) return ''
|
|
15
19
|
return fieldTypesLabel(accepted, t) || accepted.join(TYPES_SEPARATOR)
|
|
16
20
|
}
|
|
17
21
|
|
|
18
|
-
return function failureText(
|
|
22
|
+
return function failureText(
|
|
23
|
+
item: UploadFailureSource,
|
|
24
|
+
failure: UploadFailure = item.failure ?? 'server',
|
|
25
|
+
): string {
|
|
19
26
|
const { details, folderName, message } = item
|
|
20
|
-
|
|
21
|
-
size: format.formatBytes(
|
|
27
|
+
const params = {
|
|
28
|
+
size: format.formatBytes(
|
|
29
|
+
failure === 'quota'
|
|
30
|
+
? (details?.remaining ?? 0)
|
|
31
|
+
: uploads.maxUploadBytes.value,
|
|
32
|
+
),
|
|
22
33
|
types: typesLabel(details?.accepted),
|
|
23
34
|
name: folderName,
|
|
24
35
|
message: message ?? '',
|
|
25
36
|
address: details?.address ?? '',
|
|
26
|
-
}
|
|
37
|
+
}
|
|
38
|
+
if (item.requestError !== undefined)
|
|
39
|
+
return (
|
|
40
|
+
resolveError(item.requestError, {
|
|
41
|
+
fallbackKey: `dms_media.queue.reason.${failure}`,
|
|
42
|
+
params,
|
|
43
|
+
})?.message ?? ''
|
|
44
|
+
)
|
|
45
|
+
return t(`dms_media.queue.reason.${failure}`, params)
|
|
27
46
|
}
|
|
28
47
|
}
|
|
@@ -49,6 +49,7 @@ export interface UploadItem {
|
|
|
49
49
|
loaded: number
|
|
50
50
|
failure?: UploadFailure
|
|
51
51
|
message?: string
|
|
52
|
+
requestError?: unknown
|
|
52
53
|
details?: UploadRefusalDetails
|
|
53
54
|
resourceKey?: string
|
|
54
55
|
previewUrl?: string
|
|
@@ -97,7 +98,12 @@ interface ServerRefusal {
|
|
|
97
98
|
}
|
|
98
99
|
|
|
99
100
|
const CONCURRENCY = 2
|
|
100
|
-
const FINAL_STATUSES = new Set<UploadStatus>([
|
|
101
|
+
const FINAL_STATUSES = new Set<UploadStatus>([
|
|
102
|
+
'done',
|
|
103
|
+
'failed',
|
|
104
|
+
'rejected',
|
|
105
|
+
'cancelled',
|
|
106
|
+
])
|
|
101
107
|
const CANCELLABLE_STATUSES = new Set<UploadStatus>(['waiting', 'uploading'])
|
|
102
108
|
const FAILED_STATUSES = new Set<UploadStatus>(['failed', 'rejected'])
|
|
103
109
|
const HTTP_SUCCESS_LIMIT = 300
|
|
@@ -105,12 +111,21 @@ const HTTP_CLIENT_ERROR_MIN = 400
|
|
|
105
111
|
const HTTP_SERVER_ERROR_MIN = 500
|
|
106
112
|
const HTTP_PAYLOAD_TOO_LARGE = 413
|
|
107
113
|
/** Failures the same file would meet again: no retry is offered. */
|
|
108
|
-
const FINAL_FAILURES = new Set<UploadFailure>([
|
|
114
|
+
const FINAL_FAILURES = new Set<UploadFailure>([
|
|
115
|
+
'storage_limit',
|
|
116
|
+
'storage_blocked',
|
|
117
|
+
'storage_unreachable',
|
|
118
|
+
])
|
|
109
119
|
const MAX_REPORTED_FAILURES = 50
|
|
110
120
|
const VISIBLE_DUPLICATE_NAMES = 6
|
|
111
121
|
const MAX_REASON_LENGTH = 500
|
|
112
122
|
/** Headers a browser sets itself and refuses from a script. */
|
|
113
|
-
const FORBIDDEN_REQUEST_HEADERS = new Set([
|
|
123
|
+
const FORBIDDEN_REQUEST_HEADERS = new Set([
|
|
124
|
+
'content-length',
|
|
125
|
+
'host',
|
|
126
|
+
'connection',
|
|
127
|
+
'accept-encoding',
|
|
128
|
+
])
|
|
114
129
|
const REFUSALS_BY_STATUS: Record<number, UploadFailure> = {
|
|
115
130
|
400: 'invalid',
|
|
116
131
|
403: 'no_access',
|
|
@@ -119,7 +134,12 @@ const REFUSALS_BY_STATUS: Record<number, UploadFailure> = {
|
|
|
119
134
|
415: 'type_not_allowed',
|
|
120
135
|
422: 'invalid_image',
|
|
121
136
|
}
|
|
122
|
-
const SERVER_REASONS = new Set<UploadFailure>([
|
|
137
|
+
const SERVER_REASONS = new Set<UploadFailure>([
|
|
138
|
+
'type_not_allowed',
|
|
139
|
+
'field_type',
|
|
140
|
+
'quota',
|
|
141
|
+
'invalid_image',
|
|
142
|
+
])
|
|
123
143
|
|
|
124
144
|
const items = ref<UploadItem[]>([])
|
|
125
145
|
const isPaused = ref(false)
|
|
@@ -159,7 +179,8 @@ function isCancelled(id: string): boolean {
|
|
|
159
179
|
}
|
|
160
180
|
|
|
161
181
|
function api(): UploadApi {
|
|
162
|
-
if (!uploadApi)
|
|
182
|
+
if (!uploadApi)
|
|
183
|
+
throw new Error('useUploadQueue() must run in a component setup first')
|
|
163
184
|
return uploadApi
|
|
164
185
|
}
|
|
165
186
|
|
|
@@ -178,27 +199,43 @@ class UploadError extends Error {
|
|
|
178
199
|
}
|
|
179
200
|
|
|
180
201
|
/** A network error tells a dropped link from a storage this site may not reach (CORS, wrong address). */
|
|
181
|
-
async function networkFailure(
|
|
202
|
+
async function networkFailure(
|
|
203
|
+
url: string,
|
|
204
|
+
hasSentBytes: boolean,
|
|
205
|
+
): Promise<UploadError> {
|
|
182
206
|
const { failure, address } = await diagnoseStorageFailure(url, hasSentBytes)
|
|
183
207
|
if (failure === 'connection') return new UploadError('connection')
|
|
184
208
|
return new UploadError(failure, address, { address })
|
|
185
209
|
}
|
|
186
210
|
|
|
187
|
-
function putFile(
|
|
211
|
+
function putFile(
|
|
212
|
+
id: string,
|
|
213
|
+
url: string,
|
|
214
|
+
headers: Record<string, string>,
|
|
215
|
+
file: File,
|
|
216
|
+
): Promise<void> {
|
|
188
217
|
return new Promise<void>((resolve, reject) => {
|
|
189
218
|
const request = new XMLHttpRequest()
|
|
190
219
|
let sentBytes = 0
|
|
191
220
|
requests.set(id, request)
|
|
192
221
|
request.open('PUT', url)
|
|
193
222
|
for (const [name, value] of Object.entries(headers)) {
|
|
194
|
-
if (!FORBIDDEN_REQUEST_HEADERS.has(name.toLowerCase()))
|
|
223
|
+
if (!FORBIDDEN_REQUEST_HEADERS.has(name.toLowerCase()))
|
|
224
|
+
request.setRequestHeader(name, value)
|
|
195
225
|
}
|
|
196
226
|
request.upload.onprogress = (event) => {
|
|
197
227
|
sentBytes = event.loaded
|
|
198
228
|
patch(id, { loaded: event.loaded })
|
|
199
229
|
}
|
|
200
230
|
request.onload = () =>
|
|
201
|
-
request.status < HTTP_SUCCESS_LIMIT
|
|
231
|
+
request.status < HTTP_SUCCESS_LIMIT
|
|
232
|
+
? resolve()
|
|
233
|
+
: reject(
|
|
234
|
+
new UploadError(
|
|
235
|
+
storageFailure(request.status),
|
|
236
|
+
String(request.status),
|
|
237
|
+
),
|
|
238
|
+
)
|
|
202
239
|
request.onerror = () => void networkFailure(url, sentBytes > 0).then(reject)
|
|
203
240
|
request.onabort = () => reject(new UploadError('connection', 'aborted'))
|
|
204
241
|
request.send(file)
|
|
@@ -228,14 +265,20 @@ function describeFailure(error: unknown): FailureOutcome {
|
|
|
228
265
|
const reason = refusal.reason as UploadFailure | undefined
|
|
229
266
|
return {
|
|
230
267
|
status: 'rejected',
|
|
231
|
-
failure:
|
|
268
|
+
failure:
|
|
269
|
+
reason && SERVER_REASONS.has(reason)
|
|
270
|
+
? reason
|
|
271
|
+
: (REFUSALS_BY_STATUS[status] ?? 'invalid'),
|
|
232
272
|
message: refusal.message,
|
|
233
273
|
details: { accepted: refusal.accepted, remaining: refusal.remaining },
|
|
234
274
|
}
|
|
235
275
|
}
|
|
236
276
|
|
|
237
277
|
function discardStaged(item: UploadItem): void {
|
|
238
|
-
if (item.resourceKey)
|
|
278
|
+
if (item.resourceKey)
|
|
279
|
+
void api()
|
|
280
|
+
.abort(item.folderId, item.resourceKey)
|
|
281
|
+
.catch(() => undefined)
|
|
239
282
|
}
|
|
240
283
|
|
|
241
284
|
async function run(item: UploadItem): Promise<void> {
|
|
@@ -263,14 +306,22 @@ async function run(item: UploadItem): Promise<void> {
|
|
|
263
306
|
const current = findItem(item.id)
|
|
264
307
|
if (!current || current.status === 'cancelled') return
|
|
265
308
|
if (current.status === 'uploading') discardStaged(current)
|
|
266
|
-
patch(item.id, {
|
|
309
|
+
patch(item.id, {
|
|
310
|
+
...describeFailure(error),
|
|
311
|
+
requestError: error instanceof UploadError ? undefined : error,
|
|
312
|
+
resourceKey: undefined,
|
|
313
|
+
})
|
|
267
314
|
}
|
|
268
315
|
}
|
|
269
316
|
|
|
270
317
|
function pump(): void {
|
|
271
318
|
if (isPaused.value) return
|
|
272
|
-
const active = items.value.filter(
|
|
273
|
-
|
|
319
|
+
const active = items.value.filter(
|
|
320
|
+
(item) => item.status === 'uploading' || item.status === 'checking',
|
|
321
|
+
).length
|
|
322
|
+
const waiting = items.value.filter(
|
|
323
|
+
(item) => item.status === 'waiting' && !heldBatches.has(item.batchId),
|
|
324
|
+
)
|
|
274
325
|
for (const item of waiting.slice(0, Math.max(CONCURRENCY - active, 0))) {
|
|
275
326
|
void run(item).finally(() => {
|
|
276
327
|
reportSettledBatches()
|
|
@@ -280,10 +331,11 @@ function pump(): void {
|
|
|
280
331
|
}
|
|
281
332
|
|
|
282
333
|
function failureReason(item: UploadItem): string {
|
|
283
|
-
return (
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
334
|
+
return (
|
|
335
|
+
item.message && item.message !== item.failure
|
|
336
|
+
? `${item.failure}: ${item.message}`
|
|
337
|
+
: (item.failure ?? 'server')
|
|
338
|
+
).slice(0, MAX_REASON_LENGTH)
|
|
287
339
|
}
|
|
288
340
|
|
|
289
341
|
function buildReport(batchId: string, batch: UploadItem[]): UploadBatchReport {
|
|
@@ -303,7 +355,10 @@ function buildReport(batchId: string, batch: UploadItem[]): UploadBatchReport {
|
|
|
303
355
|
}
|
|
304
356
|
}
|
|
305
357
|
|
|
306
|
-
function outcomeOf(
|
|
358
|
+
function outcomeOf(
|
|
359
|
+
report: UploadBatchReport,
|
|
360
|
+
batch: UploadItem[],
|
|
361
|
+
): UploadBatchOutcome {
|
|
307
362
|
return {
|
|
308
363
|
batchId: report.batchId,
|
|
309
364
|
folderId: report.folderId,
|
|
@@ -317,7 +372,8 @@ function outcomeOf(report: UploadBatchReport, batch: UploadItem[]): UploadBatchO
|
|
|
317
372
|
|
|
318
373
|
function batchesById(): Map<string, UploadItem[]> {
|
|
319
374
|
const batches = new Map<string, UploadItem[]>()
|
|
320
|
-
for (const item of items.value)
|
|
375
|
+
for (const item of items.value)
|
|
376
|
+
batches.set(item.batchId, [...(batches.get(item.batchId) ?? []), item])
|
|
321
377
|
return batches
|
|
322
378
|
}
|
|
323
379
|
|
|
@@ -327,7 +383,10 @@ function reportSettledBatches(): void {
|
|
|
327
383
|
reportedBatches.add(batchId)
|
|
328
384
|
const report = buildReport(batchId, batch)
|
|
329
385
|
batchListeners.forEach((listener) => listener(outcomeOf(report, batch)))
|
|
330
|
-
if (report.uploaded + report.failed > 0)
|
|
386
|
+
if (report.uploaded + report.failed > 0)
|
|
387
|
+
void api()
|
|
388
|
+
.reportBatch(report)
|
|
389
|
+
.catch(() => undefined)
|
|
331
390
|
}
|
|
332
391
|
}
|
|
333
392
|
|
|
@@ -337,7 +396,13 @@ function reportInterruptedBatches(): void {
|
|
|
337
396
|
if (reportedBatches.has(batchId) || batch.every(isFinal)) continue
|
|
338
397
|
reportedBatches.add(batchId)
|
|
339
398
|
const settled = batch.map((item) =>
|
|
340
|
-
isFinal(item)
|
|
399
|
+
isFinal(item)
|
|
400
|
+
? item
|
|
401
|
+
: {
|
|
402
|
+
...item,
|
|
403
|
+
status: 'failed' as const,
|
|
404
|
+
failure: 'interrupted' as const,
|
|
405
|
+
},
|
|
341
406
|
)
|
|
342
407
|
void api()
|
|
343
408
|
.reportBatch(buildReport(batchId, settled), { keepalive: true })
|
|
@@ -366,23 +431,41 @@ function isTypeRefused(file: File): boolean {
|
|
|
366
431
|
return Boolean(allowed && !matchesUploadType(uploadMimetype(file), allowed))
|
|
367
432
|
}
|
|
368
433
|
|
|
369
|
-
function refusesFieldType(
|
|
370
|
-
|
|
434
|
+
function refusesFieldType(
|
|
435
|
+
file: File,
|
|
436
|
+
accepts: readonly string[] | null | undefined,
|
|
437
|
+
): boolean {
|
|
438
|
+
return (
|
|
439
|
+
Boolean(accepts?.length) &&
|
|
440
|
+
!matchesUploadType(uploadMimetype(file), accepts ?? [])
|
|
441
|
+
)
|
|
371
442
|
}
|
|
372
443
|
|
|
373
444
|
/** Splits the files a field taking `accepts` (any file when empty) refuses from those it takes. */
|
|
374
|
-
function splitByFieldType(
|
|
445
|
+
function splitByFieldType(
|
|
446
|
+
selected: readonly File[],
|
|
447
|
+
accepts?: readonly string[] | null,
|
|
448
|
+
): FileSplit {
|
|
375
449
|
const refused = selected.filter((file) => refusesFieldType(file, accepts))
|
|
376
450
|
return { kept: selected.filter((file) => !refused.includes(file)), refused }
|
|
377
451
|
}
|
|
378
452
|
|
|
379
453
|
/** Refusals known before sending: the server would answer the same. */
|
|
380
|
-
function initialRefusal(
|
|
454
|
+
function initialRefusal(
|
|
455
|
+
file: File,
|
|
456
|
+
destination: UploadDestination,
|
|
457
|
+
): Pick<UploadItem, 'status' | 'failure' | 'details'> {
|
|
381
458
|
if (file.size === 0) return { status: 'rejected', failure: 'empty' }
|
|
382
|
-
if (file.size > uploadLimits.value.maxUploadBytes)
|
|
383
|
-
|
|
459
|
+
if (file.size > uploadLimits.value.maxUploadBytes)
|
|
460
|
+
return { status: 'rejected', failure: 'too_large' }
|
|
461
|
+
if (isTypeRefused(file))
|
|
462
|
+
return { status: 'rejected', failure: 'type_not_allowed' }
|
|
384
463
|
if (refusesFieldType(file, destination.accepts))
|
|
385
|
-
return {
|
|
464
|
+
return {
|
|
465
|
+
status: 'rejected',
|
|
466
|
+
failure: 'field_type',
|
|
467
|
+
details: { accepted: destination.accepts ?? [] },
|
|
468
|
+
}
|
|
386
469
|
return { status: 'waiting' }
|
|
387
470
|
}
|
|
388
471
|
|
|
@@ -390,7 +473,11 @@ function revokePreview(item: UploadItem): void {
|
|
|
390
473
|
if (item.previewUrl) URL.revokeObjectURL(item.previewUrl)
|
|
391
474
|
}
|
|
392
475
|
|
|
393
|
-
function createItem(
|
|
476
|
+
function createItem(
|
|
477
|
+
file: File,
|
|
478
|
+
destination: UploadDestination,
|
|
479
|
+
batchId: string,
|
|
480
|
+
): UploadItem {
|
|
394
481
|
const id = nextId()
|
|
395
482
|
files.set(id, file)
|
|
396
483
|
return {
|
|
@@ -402,7 +489,9 @@ function createItem(file: File, destination: UploadDestination, batchId: string)
|
|
|
402
489
|
folderName: destination.folderName,
|
|
403
490
|
batchId,
|
|
404
491
|
loaded: 0,
|
|
405
|
-
previewUrl: file.type.startsWith('image/')
|
|
492
|
+
previewUrl: file.type.startsWith('image/')
|
|
493
|
+
? URL.createObjectURL(file)
|
|
494
|
+
: undefined,
|
|
406
495
|
...initialRefusal(file, destination),
|
|
407
496
|
}
|
|
408
497
|
}
|
|
@@ -418,16 +507,35 @@ function createKeepBothPrompt(): KeepBothPrompt {
|
|
|
418
507
|
return (folderName, names) => {
|
|
419
508
|
const hidden = names.length - VISIBLE_DUPLICATE_NAMES
|
|
420
509
|
return confirm({
|
|
421
|
-
title: t(
|
|
422
|
-
|
|
510
|
+
title: t(
|
|
511
|
+
'dms_media.dialogs.duplicates.title',
|
|
512
|
+
{ count: names.length },
|
|
513
|
+
names.length,
|
|
514
|
+
),
|
|
515
|
+
description: t('dms_media.dialogs.duplicates.description', {
|
|
516
|
+
name: folderName,
|
|
517
|
+
}),
|
|
423
518
|
color: 'warning',
|
|
424
519
|
icon: 'i-ph-copy',
|
|
425
520
|
confirmIcon: 'i-ph-copy',
|
|
426
521
|
confirmLabel: t('dms_media.dialogs.duplicates.keep_both'),
|
|
427
522
|
cancelLabel: t('dms_media.actions.cancel'),
|
|
428
523
|
impact: [
|
|
429
|
-
...names
|
|
430
|
-
|
|
524
|
+
...names
|
|
525
|
+
.slice(0, VISIBLE_DUPLICATE_NAMES)
|
|
526
|
+
.map((name) => ({ icon: 'i-ph-file', label: name })),
|
|
527
|
+
...(hidden > 0
|
|
528
|
+
? [
|
|
529
|
+
{
|
|
530
|
+
icon: 'i-ph-dots-three',
|
|
531
|
+
label: t(
|
|
532
|
+
'dms_media.dialogs.duplicates.more',
|
|
533
|
+
{ count: hidden },
|
|
534
|
+
hidden,
|
|
535
|
+
),
|
|
536
|
+
},
|
|
537
|
+
]
|
|
538
|
+
: []),
|
|
431
539
|
],
|
|
432
540
|
})
|
|
433
541
|
}
|
|
@@ -436,19 +544,23 @@ function createKeepBothPrompt(): KeepBothPrompt {
|
|
|
436
544
|
function rejectBatch(waiting: UploadItem[], error: unknown): void {
|
|
437
545
|
const outcome = describeFailure(error)
|
|
438
546
|
if (outcome.status !== 'rejected') return
|
|
439
|
-
waiting.forEach((item) => patch(item.id, outcome))
|
|
547
|
+
waiting.forEach((item) => patch(item.id, { ...outcome, requestError: error }))
|
|
440
548
|
}
|
|
441
549
|
|
|
442
550
|
/** "Keep both" renames the copies `name (2).ext`; "Cancel" leaves them out. */
|
|
443
551
|
async function resolveDuplicates(batchId: string): Promise<void> {
|
|
444
|
-
const waiting = items.value.filter(
|
|
552
|
+
const waiting = items.value.filter(
|
|
553
|
+
(item) => item.batchId === batchId && item.status === 'waiting',
|
|
554
|
+
)
|
|
445
555
|
if (!waiting.length) return
|
|
446
556
|
try {
|
|
447
557
|
const { names } = await api().freeNames(
|
|
448
558
|
waiting[0]!.folderId,
|
|
449
559
|
waiting.map((item) => item.name),
|
|
450
560
|
)
|
|
451
|
-
const duplicates = waiting.flatMap((item, index) =>
|
|
561
|
+
const duplicates = waiting.flatMap((item, index) =>
|
|
562
|
+
names[index] ? [{ item, free: names[index]! }] : [],
|
|
563
|
+
)
|
|
452
564
|
if (!duplicates.length) return
|
|
453
565
|
const keepBoth = await askKeepBoth(
|
|
454
566
|
waiting[0]!.folderName,
|
|
@@ -456,7 +568,12 @@ async function resolveDuplicates(batchId: string): Promise<void> {
|
|
|
456
568
|
)
|
|
457
569
|
for (const { item, free } of duplicates) {
|
|
458
570
|
if (findItem(item.id)?.status !== 'waiting') continue
|
|
459
|
-
patch(
|
|
571
|
+
patch(
|
|
572
|
+
item.id,
|
|
573
|
+
keepBoth
|
|
574
|
+
? { name: free }
|
|
575
|
+
: { status: 'cancelled', failure: 'duplicate' },
|
|
576
|
+
)
|
|
460
577
|
}
|
|
461
578
|
} catch (error) {
|
|
462
579
|
rejectBatch(waiting, error)
|
|
@@ -466,7 +583,9 @@ async function resolveDuplicates(batchId: string): Promise<void> {
|
|
|
466
583
|
function enqueue(selected: File[], destination: UploadDestination): string {
|
|
467
584
|
guardUnload()
|
|
468
585
|
const batchId = newBatchId()
|
|
469
|
-
items.value.push(
|
|
586
|
+
items.value.push(
|
|
587
|
+
...selected.map((file) => createItem(file, destination, batchId)),
|
|
588
|
+
)
|
|
470
589
|
heldBatches.add(batchId)
|
|
471
590
|
void resolveDuplicates(batchId).finally(() => {
|
|
472
591
|
heldBatches.delete(batchId)
|
|
@@ -480,12 +599,20 @@ function retry(id: string): void {
|
|
|
480
599
|
const item = findItem(id)
|
|
481
600
|
if (!item || item.status !== 'failed' || !files.has(id)) return
|
|
482
601
|
reportedBatches.delete(item.batchId)
|
|
483
|
-
patch(id, {
|
|
602
|
+
patch(id, {
|
|
603
|
+
status: 'waiting',
|
|
604
|
+
failure: undefined,
|
|
605
|
+
message: undefined,
|
|
606
|
+
details: undefined,
|
|
607
|
+
requestError: undefined,
|
|
608
|
+
})
|
|
484
609
|
pump()
|
|
485
610
|
}
|
|
486
611
|
|
|
487
612
|
function retryFailed(): void {
|
|
488
|
-
items.value
|
|
613
|
+
items.value
|
|
614
|
+
.filter((item) => item.status === 'failed')
|
|
615
|
+
.forEach((item) => retry(item.id))
|
|
489
616
|
}
|
|
490
617
|
|
|
491
618
|
/** Only before the server starts checking: past that point the file is already being added. */
|
|
@@ -501,7 +628,9 @@ function cancel(id: string): void {
|
|
|
501
628
|
}
|
|
502
629
|
|
|
503
630
|
function cancelRemaining(): void {
|
|
504
|
-
items.value
|
|
631
|
+
items.value
|
|
632
|
+
.filter((item) => CANCELLABLE_STATUSES.has(item.status))
|
|
633
|
+
.forEach((item) => cancel(item.id))
|
|
505
634
|
}
|
|
506
635
|
|
|
507
636
|
function setPaused(paused: boolean): void {
|
|
@@ -540,7 +669,12 @@ function onBatchSettled(listener: BatchListener): () => void {
|
|
|
540
669
|
return () => batchListeners.delete(listener)
|
|
541
670
|
}
|
|
542
671
|
|
|
543
|
-
const activeCount = computed(
|
|
672
|
+
const activeCount = computed(
|
|
673
|
+
() =>
|
|
674
|
+
items.value.filter(
|
|
675
|
+
(item) => !isFinal(item) && !heldBatches.has(item.batchId),
|
|
676
|
+
).length,
|
|
677
|
+
)
|
|
544
678
|
|
|
545
679
|
/**
|
|
546
680
|
* The upload queue every media screen shares: files go to storage two at a
|
|
@@ -6,6 +6,7 @@ type Translate = ReturnType<typeof useI18n>['t']
|
|
|
6
6
|
|
|
7
7
|
interface Announcer {
|
|
8
8
|
toast: ToastApi
|
|
9
|
+
presentError: ReturnType<typeof useApiErrorPresenter>['presentError']
|
|
9
10
|
t: Translate
|
|
10
11
|
failureText: (item: UploadFailureSource) => string
|
|
11
12
|
failedItems: (batchId: string) => readonly UploadItem[]
|
|
@@ -32,11 +33,19 @@ function toastId(batchId: string): string {
|
|
|
32
33
|
|
|
33
34
|
function uploadsLink(announcer: Announcer, labelKey: string) {
|
|
34
35
|
if (!announcer.showUploadsLink) return undefined
|
|
35
|
-
return [
|
|
36
|
+
return [
|
|
37
|
+
{
|
|
38
|
+
label: announcer.t(labelKey),
|
|
39
|
+
onClick: () => void navigateDms(MEDIA_ROUTES.uploads),
|
|
40
|
+
},
|
|
41
|
+
]
|
|
36
42
|
}
|
|
37
43
|
|
|
38
44
|
/** The reason most of the failed files share, or "various reasons" on a tie. */
|
|
39
|
-
function sharedReason(
|
|
45
|
+
function sharedReason(
|
|
46
|
+
announcer: Announcer,
|
|
47
|
+
failed: readonly UploadItem[],
|
|
48
|
+
): string {
|
|
40
49
|
const counts = new Map<string, number>()
|
|
41
50
|
for (const item of failed) {
|
|
42
51
|
const reason = announcer.failureText(item)
|
|
@@ -44,22 +53,36 @@ function sharedReason(announcer: Announcer, failed: readonly UploadItem[]): stri
|
|
|
44
53
|
}
|
|
45
54
|
const ranked = [...counts].sort((left, right) => right[1] - left[1])
|
|
46
55
|
const [top, runnerUp] = ranked
|
|
47
|
-
if (!top || (runnerUp && runnerUp[1] === top[1]))
|
|
56
|
+
if (!top || (runnerUp && runnerUp[1] === top[1]))
|
|
57
|
+
return announcer.t('dms_media.toasts.upload_failed_mixed')
|
|
48
58
|
return top[0]
|
|
49
59
|
}
|
|
50
60
|
|
|
51
61
|
function failureToast(announcer: Announcer, outcome: UploadBatchOutcome) {
|
|
52
62
|
const { t } = announcer
|
|
53
|
-
const failed = announcer
|
|
63
|
+
const failed = announcer
|
|
64
|
+
.failedItems(outcome.batchId)
|
|
65
|
+
.filter((item) => item.requestError === undefined)
|
|
54
66
|
const [only] = failed
|
|
55
67
|
const added = outcome.uploaded
|
|
56
|
-
? t(
|
|
68
|
+
? t(
|
|
69
|
+
'dms_media.toasts.upload_added',
|
|
70
|
+
{ count: outcome.uploaded, name: outcome.folderName },
|
|
71
|
+
outcome.uploaded,
|
|
72
|
+
)
|
|
57
73
|
: ''
|
|
58
74
|
const title =
|
|
59
75
|
failed.length === 1 && only
|
|
60
76
|
? t('dms_media.toasts.upload_failed_one', { name: only.name })
|
|
61
|
-
: t(
|
|
62
|
-
|
|
77
|
+
: t(
|
|
78
|
+
'dms_media.toasts.upload_failed',
|
|
79
|
+
{ count: failed.length, name: outcome.folderName },
|
|
80
|
+
failed.length,
|
|
81
|
+
)
|
|
82
|
+
const reason =
|
|
83
|
+
failed.length === 1 && only
|
|
84
|
+
? announcer.failureText(only)
|
|
85
|
+
: sharedReason(announcer, failed)
|
|
63
86
|
return { title, description: [reason, added].filter(Boolean).join(' · ') }
|
|
64
87
|
}
|
|
65
88
|
|
|
@@ -69,28 +92,49 @@ function settle(outcome: UploadBatchOutcome): void {
|
|
|
69
92
|
announcers.delete(outcome.batchId)
|
|
70
93
|
const { toast, t } = announcer
|
|
71
94
|
toast.remove(toastId(outcome.batchId))
|
|
72
|
-
|
|
95
|
+
const failed = announcer.failedItems(outcome.batchId)
|
|
96
|
+
for (const item of failed) {
|
|
97
|
+
if (item.requestError === undefined) continue
|
|
98
|
+
announcer.presentError(item.requestError, {
|
|
99
|
+
fallbackKey: 'dms_media.toasts.upload_failed_one',
|
|
100
|
+
params: { name: item.name },
|
|
101
|
+
actions: announcer.showUploadsLink
|
|
102
|
+
? [
|
|
103
|
+
{
|
|
104
|
+
labelKey: 'dms_media.upload_strip.see_details',
|
|
105
|
+
onSelect: () => navigateDms(MEDIA_ROUTES.uploads),
|
|
106
|
+
},
|
|
107
|
+
]
|
|
108
|
+
: undefined,
|
|
109
|
+
})
|
|
110
|
+
}
|
|
111
|
+
if (failed.some((item) => item.requestError === undefined))
|
|
73
112
|
return void toast.add({
|
|
74
113
|
...failureToast(announcer, outcome),
|
|
75
114
|
icon: 'i-ph-cloud-slash',
|
|
76
|
-
color: '
|
|
115
|
+
color: 'warning',
|
|
77
116
|
actions: uploadsLink(announcer, 'dms_media.upload_strip.see_details'),
|
|
78
117
|
})
|
|
79
|
-
if (!outcome.uploaded) return
|
|
118
|
+
if (outcome.failed || !outcome.uploaded) return
|
|
80
119
|
toast.add({
|
|
81
|
-
title: t(
|
|
120
|
+
title: t(
|
|
121
|
+
'dms_media.toasts.upload_added',
|
|
122
|
+
{ count: outcome.uploaded, name: outcome.folderName },
|
|
123
|
+
outcome.uploaded,
|
|
124
|
+
),
|
|
82
125
|
icon: 'i-ph-check-circle',
|
|
83
126
|
color: 'success',
|
|
84
127
|
})
|
|
85
128
|
}
|
|
86
129
|
|
|
87
130
|
/**
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
* was left.
|
|
131
|
+
* Shows batch progress, then request failures through the API presenter and
|
|
132
|
+
* local validation or success notices once every file settles, even after
|
|
133
|
+
* the page that started the batch was left.
|
|
91
134
|
*/
|
|
92
135
|
export function useUploadToasts() {
|
|
93
136
|
const toast = useToast()
|
|
137
|
+
const { presentError } = useApiErrorPresenter()
|
|
94
138
|
const { t } = useI18n()
|
|
95
139
|
const uploads = useUploadQueue()
|
|
96
140
|
const failureText = useUploadFailureText()
|
|
@@ -99,18 +143,31 @@ export function useUploadToasts() {
|
|
|
99
143
|
uploads.onBatchSettled(settle)
|
|
100
144
|
}
|
|
101
145
|
|
|
102
|
-
function announce(
|
|
146
|
+
function announce(
|
|
147
|
+
batchId: string,
|
|
148
|
+
count: number,
|
|
149
|
+
folderName: string,
|
|
150
|
+
options: UploadToastOptions = {},
|
|
151
|
+
): void {
|
|
103
152
|
const announcer: Announcer = {
|
|
104
153
|
toast,
|
|
154
|
+
presentError,
|
|
105
155
|
t,
|
|
106
156
|
failureText,
|
|
107
|
-
failedItems: (id) =>
|
|
157
|
+
failedItems: (id) =>
|
|
158
|
+
uploads.items.value.filter(
|
|
159
|
+
(item) => item.batchId === id && FAILED_STATUSES.has(item.status),
|
|
160
|
+
),
|
|
108
161
|
showUploadsLink: options.showUploadsLink ?? true,
|
|
109
162
|
}
|
|
110
163
|
announcers.set(batchId, announcer)
|
|
111
164
|
toast.add({
|
|
112
165
|
id: toastId(batchId),
|
|
113
|
-
title: t(
|
|
166
|
+
title: t(
|
|
167
|
+
'dms_media.toasts.uploading',
|
|
168
|
+
{ count, name: folderName },
|
|
169
|
+
count,
|
|
170
|
+
),
|
|
114
171
|
icon: 'i-ph-cloud-arrow-up',
|
|
115
172
|
color: 'neutral',
|
|
116
173
|
actions: uploadsLink(announcer, 'dms_media.upload_strip.see_progress'),
|
|
@@ -118,7 +175,10 @@ export function useUploadToasts() {
|
|
|
118
175
|
}
|
|
119
176
|
|
|
120
177
|
/** Names the files a field refused for their type, and says what it takes. */
|
|
121
|
-
function announceRefusedTypes(
|
|
178
|
+
function announceRefusedTypes(
|
|
179
|
+
refused: readonly File[],
|
|
180
|
+
accept: readonly string[],
|
|
181
|
+
): void {
|
|
122
182
|
if (refused.length === 0) return
|
|
123
183
|
const types = fieldTypesLabel(accept, t)
|
|
124
184
|
toast.add({
|
|
@@ -127,7 +187,9 @@ export function useUploadToasts() {
|
|
|
127
187
|
{ names: refused.map((file) => file.name).join(NAMES_SEPARATOR) },
|
|
128
188
|
refused.length,
|
|
129
189
|
),
|
|
130
|
-
description: types
|
|
190
|
+
description: types
|
|
191
|
+
? t('dms_media.field_widget.upload.accepts', { types })
|
|
192
|
+
: undefined,
|
|
131
193
|
color: 'warning',
|
|
132
194
|
icon: 'i-ph-file-x',
|
|
133
195
|
})
|
|
@@ -6,14 +6,14 @@
|
|
|
6
6
|
"main": "./dms.frontend.ts",
|
|
7
7
|
"packageManager": "pnpm@10.6.5",
|
|
8
8
|
"engines": {
|
|
9
|
-
"@antelopejs/dms-frontend": ">=0.9.
|
|
9
|
+
"@antelopejs/dms-frontend": ">=0.9.2 <0.10.0"
|
|
10
10
|
},
|
|
11
11
|
"scripts": {
|
|
12
12
|
"dev": "pnpm --dir ../playground frontend:dev",
|
|
13
13
|
"build": "pnpm --dir ../playground exec ajs dms build",
|
|
14
14
|
"typecheck": "pnpm --dir \"${DMS_FRONTEND_WORKSPACE:?Set DMS_FRONTEND_WORKSPACE to the generated Inertia workspace}\" typecheck",
|
|
15
15
|
"format": "prettier --write .",
|
|
16
|
-
"lint": "eslint .",
|
|
16
|
+
"lint": "eslint . && node ../node_modules/@antelopejs/dms/frontend-vue/scripts/check-api-error-presentation.mjs app",
|
|
17
17
|
"lint:fix": "eslint . --fix",
|
|
18
18
|
"test": "vitest run"
|
|
19
19
|
},
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { afterEach, expect, it, vi } from 'vitest'
|
|
2
|
+
import * as vue from 'vue'
|
|
3
|
+
import { useApiErrorPresenter } from '#dms-core/app/composables/useApiErrorPresenter'
|
|
4
|
+
|
|
5
|
+
afterEach(() => {
|
|
6
|
+
vi.unstubAllGlobals()
|
|
7
|
+
vi.unstubAllEnvs()
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it.each([
|
|
11
|
+
'names',
|
|
12
|
+
'presign',
|
|
13
|
+
'confirm',
|
|
14
|
+
'field',
|
|
15
|
+
'partial',
|
|
16
|
+
'success',
|
|
17
|
+
'local-field',
|
|
18
|
+
'local-batch',
|
|
19
|
+
])(
|
|
20
|
+
'presents the upload %s outcome without duplicate notices',
|
|
21
|
+
async (stage) => {
|
|
22
|
+
vi.resetModules()
|
|
23
|
+
vi.stubEnv('SSR', false)
|
|
24
|
+
for (const [name, value] of Object.entries(vue)) vi.stubGlobal(name, value)
|
|
25
|
+
const data = { message: 'error.read_only_demo', severity: 'warning' }
|
|
26
|
+
const error = Object.assign(new Error('Forbidden'), {
|
|
27
|
+
response: new Response(JSON.stringify(data), { status: 403 }),
|
|
28
|
+
data,
|
|
29
|
+
})
|
|
30
|
+
const notices = new Map<string | number, { title: string; color: string }>()
|
|
31
|
+
let sequence = 0
|
|
32
|
+
vi.stubGlobal('useToast', () => ({
|
|
33
|
+
add: (notice: { id?: string; title: string; color: string }) => {
|
|
34
|
+
const id = notice.id ?? ++sequence
|
|
35
|
+
notices.set(id, notice)
|
|
36
|
+
return { id }
|
|
37
|
+
},
|
|
38
|
+
update: (id: string | number, notice: { title: string; color: string }) =>
|
|
39
|
+
notices.set(id, notice),
|
|
40
|
+
remove: (id: string | number) => notices.delete(id),
|
|
41
|
+
}))
|
|
42
|
+
vi.stubGlobal('getPluginI18n', () => ({
|
|
43
|
+
i18n: {
|
|
44
|
+
tm: (key: string) =>
|
|
45
|
+
key === 'error.read_only_demo' ? 'This demo is read-only' : key,
|
|
46
|
+
rt: (message: string) => message,
|
|
47
|
+
},
|
|
48
|
+
}))
|
|
49
|
+
const state = vue.ref(new WeakMap())
|
|
50
|
+
vi.stubGlobal('useDmsState', () => state)
|
|
51
|
+
vi.stubGlobal('useApiErrorPresenter', useApiErrorPresenter)
|
|
52
|
+
vi.stubGlobal('useI18n', () => ({ t: (key: string) => key }))
|
|
53
|
+
vi.stubGlobal('useConfirm', () => ({ confirm: async () => false }))
|
|
54
|
+
vi.stubGlobal('onBeforeUnmount', () => undefined)
|
|
55
|
+
vi.stubGlobal('useUploadFailureText', () => () => 'Upload failed')
|
|
56
|
+
vi.stubGlobal('apiErrorStatus', () => 403)
|
|
57
|
+
vi.stubGlobal(
|
|
58
|
+
'uploadLimits',
|
|
59
|
+
vue.ref({ maxUploadBytes: 1000, allowedMimetypes: null }),
|
|
60
|
+
)
|
|
61
|
+
vi.stubGlobal('isActiveDocument', () => false)
|
|
62
|
+
vi.stubGlobal('newBatchId', () => 'batch-1')
|
|
63
|
+
vi.stubGlobal(
|
|
64
|
+
'XMLHttpRequest',
|
|
65
|
+
class {
|
|
66
|
+
upload = {}
|
|
67
|
+
status = 200
|
|
68
|
+
onload = () => undefined
|
|
69
|
+
open() {}
|
|
70
|
+
send() {
|
|
71
|
+
this.onload()
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
)
|
|
75
|
+
vi.stubGlobal('useUploadApi', () => ({
|
|
76
|
+
freeNames: async () => {
|
|
77
|
+
if (stage === 'names' || stage === 'field') throw error
|
|
78
|
+
return { names: [null, null] }
|
|
79
|
+
},
|
|
80
|
+
presign: async (_folderId: string, file: File) => {
|
|
81
|
+
if (
|
|
82
|
+
stage === 'presign' ||
|
|
83
|
+
(stage === 'partial' && file.name === 'world.txt')
|
|
84
|
+
)
|
|
85
|
+
throw error
|
|
86
|
+
return { resourceKey: 'key', uploadUrl: '/storage', headers: {} }
|
|
87
|
+
},
|
|
88
|
+
confirm: async () => {
|
|
89
|
+
if (stage === 'partial' || stage === 'success')
|
|
90
|
+
return { asset: { id: 'asset-1' } }
|
|
91
|
+
throw error
|
|
92
|
+
},
|
|
93
|
+
abort: async () => undefined,
|
|
94
|
+
reportBatch: async () => undefined,
|
|
95
|
+
}))
|
|
96
|
+
const { useUploadQueue } = await import('../app/composables/useUploadQueue')
|
|
97
|
+
vi.stubGlobal('useUploadQueue', useUploadQueue)
|
|
98
|
+
const { useUploadToasts } =
|
|
99
|
+
await import('../app/composables/useUploadToasts')
|
|
100
|
+
vi.stubGlobal('useUploadToasts', useUploadToasts)
|
|
101
|
+
const queue = useUploadQueue()
|
|
102
|
+
const toasts = useUploadToasts()
|
|
103
|
+
const settled = new Promise<void>((resolve) =>
|
|
104
|
+
queue.onBatchSettled(() => resolve()),
|
|
105
|
+
)
|
|
106
|
+
const files = stage.startsWith('local-')
|
|
107
|
+
? [new File([], 'empty.txt')]
|
|
108
|
+
: [new File(['hello'], 'hello.txt'), new File(['world'], 'world.txt')]
|
|
109
|
+
const destination = { folderId: 'folder-1', folderName: 'Docs' }
|
|
110
|
+
if (stage === 'field' || stage === 'local-field') {
|
|
111
|
+
const { useAssetFieldUploads } =
|
|
112
|
+
await import('../app/composables/useAssetFieldUploads')
|
|
113
|
+
await useAssetFieldUploads({
|
|
114
|
+
accept: () => undefined,
|
|
115
|
+
destination: () => destination,
|
|
116
|
+
room: () => undefined,
|
|
117
|
+
onUploaded: () => undefined,
|
|
118
|
+
}).upload(files)
|
|
119
|
+
} else {
|
|
120
|
+
const batchId = queue.enqueue(files, destination)
|
|
121
|
+
toasts.announce(batchId, files.length, 'Docs')
|
|
122
|
+
}
|
|
123
|
+
await settled
|
|
124
|
+
await vue.nextTick()
|
|
125
|
+
expect(
|
|
126
|
+
[...notices.values()].map(({ title, color }) => ({ title, color })),
|
|
127
|
+
).toEqual([
|
|
128
|
+
stage === 'success'
|
|
129
|
+
? { title: 'dms_media.toasts.upload_added', color: 'success' }
|
|
130
|
+
: stage === 'local-field'
|
|
131
|
+
? { title: 'dms_media.field_widget.upload.failed', color: 'warning' }
|
|
132
|
+
: stage === 'local-batch'
|
|
133
|
+
? { title: 'dms_media.toasts.upload_failed_one', color: 'warning' }
|
|
134
|
+
: { title: 'This demo is read-only', color: 'warning' },
|
|
135
|
+
])
|
|
136
|
+
},
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
it.each([true, false])(
|
|
140
|
+
'presents the clipboard outcome when copied is %s',
|
|
141
|
+
async (copied) => {
|
|
142
|
+
vi.resetModules()
|
|
143
|
+
const notices: { title: string; color: string; description?: string }[] = []
|
|
144
|
+
vi.stubGlobal('useMediaApi', () => ({}))
|
|
145
|
+
vi.stubGlobal('useAuthFetch', () => ({
|
|
146
|
+
$authFetch: async () => ({ origin: 'https://media.example.test/' }),
|
|
147
|
+
}))
|
|
148
|
+
vi.stubGlobal('useI18n', () => ({ t: (key: string) => key }))
|
|
149
|
+
vi.stubGlobal('useToast', () => ({
|
|
150
|
+
add: (notice: (typeof notices)[number]) => notices.push(notice),
|
|
151
|
+
}))
|
|
152
|
+
vi.stubGlobal('useConfirm', () => ({ confirm: async () => false }))
|
|
153
|
+
vi.stubGlobal('useApiErrorPresenter', useApiErrorPresenter)
|
|
154
|
+
vi.stubGlobal('getPluginI18n', () => ({ i18n: {} }))
|
|
155
|
+
vi.stubGlobal('useDmsState', () => vue.ref(new WeakMap()))
|
|
156
|
+
vi.stubGlobal('navigator', {
|
|
157
|
+
clipboard: {
|
|
158
|
+
writeText: async () => {
|
|
159
|
+
if (!copied) throw new Error('Clipboard denied')
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
})
|
|
163
|
+
const { useAssetLink } = await import('../app/composables/useAssetLink')
|
|
164
|
+
await useAssetLink().copyLink({
|
|
165
|
+
name: 'hello.txt',
|
|
166
|
+
url: '/media/hello.txt',
|
|
167
|
+
effectiveVisibility: 'public',
|
|
168
|
+
})
|
|
169
|
+
expect(notices).toHaveLength(1)
|
|
170
|
+
expect(notices[0]).toMatchObject(
|
|
171
|
+
copied
|
|
172
|
+
? {
|
|
173
|
+
title: 'dms_media.toasts.link_copied',
|
|
174
|
+
color: 'success',
|
|
175
|
+
}
|
|
176
|
+
: {
|
|
177
|
+
title: 'dms_media.asset_link.copy_failed',
|
|
178
|
+
description: 'https://media.example.test/media/hello.txt',
|
|
179
|
+
color: 'warning',
|
|
180
|
+
},
|
|
181
|
+
)
|
|
182
|
+
},
|
|
183
|
+
)
|
|
@@ -62,7 +62,7 @@ provideGlobal('useToast', () => ({ add: noop }))
|
|
|
62
62
|
provideGlobal('useConfirm', () => ({ confirm: async () => false }))
|
|
63
63
|
provideGlobal('useOverlay', () => ({ create: () => ({ open: async () => undefined }) }))
|
|
64
64
|
provideGlobal('useDrawer', () => ({ open: noop }))
|
|
65
|
-
provideGlobal('
|
|
65
|
+
provideGlobal('useApiErrorPresenter', () => ({ presentError: noop, resolveError: () => null }))
|
|
66
66
|
provideGlobal('useUnsavedChanges', noop)
|
|
67
67
|
provideGlobal('usePageLiveRefresh', noop)
|
|
68
68
|
provideGlobal('defineShortcuts', noop)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@antelopejs/dms-media",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Media library for the AntelopeJS DMS: uploads, folders with per-folder permissions, image sizes and light editing, with a Finder-style explorer.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"antelopejs",
|
|
@@ -74,8 +74,8 @@
|
|
|
74
74
|
},
|
|
75
75
|
"devDependencies": {
|
|
76
76
|
"@antelopejs/core": ">=1.17.1 <2",
|
|
77
|
-
"@antelopejs/dms": ">=0.
|
|
78
|
-
"@antelopejs/dms-frontend": "0.9.
|
|
77
|
+
"@antelopejs/dms": ">=0.13.1 <1.0.0",
|
|
78
|
+
"@antelopejs/dms-frontend": "0.9.2",
|
|
79
79
|
"@antelopejs/tooling-configs": "^0.2.12",
|
|
80
80
|
"@types/chai": "^4.3.20",
|
|
81
81
|
"@types/mocha": "^10",
|
|
@@ -19,8 +19,8 @@ Every data block carries `realtimeTopic: MEDIA_LIBRARY_TOPIC`; custom components
|
|
|
19
19
|
|
|
20
20
|
## Setup
|
|
21
21
|
|
|
22
|
-
- Media 0.
|
|
23
|
-
and `@antelopejs/interface-dms-media` >=0.3.0 <0.4.0. Media 0.
|
|
22
|
+
- Media 0.6.1 requires `@antelopejs/dms` >=0.11 (interface-dms >=0.9.0, renderer `@antelopejs/dms-frontend` 0.9)
|
|
23
|
+
and `@antelopejs/interface-dms-media` >=0.3.0 <0.4.0. Media 0.6.0 is the DMS 0.10 line; Media 0.4 uses media interface 0.2.
|
|
24
24
|
- `ajs project modules add @antelopejs/dms-media`, plus an `interface-file-storage` module (`file-storage-local`).
|
|
25
25
|
- Set the storage's `baseUrl` and the module's `publicBaseUrl` to `${@api.API_PUBLIC_BASE_URL}`, and list every
|
|
26
26
|
dashboard origin in the api module's `cors.allowedOrigins`: the browser `PUT`s files straight to the storage.
|