Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | 6x 4x 3x 3x 2x 6x 6x 9x 9x 9x 9x 9x 9x 7x 6x 6x 9x 9x 6x 6x 5x 6x 6x 6x 4x 3x 3x 3x 1x 2x 2x 2x 6x | /**
* Re-uploads every image embedded in a parsed import zip (see
* entityExportBundle.ts's parseEntityBundleZip) to the import-staging S3
* area, then rewrites the bundle's image references from their in-zip path
* to the real staged key — so the backend's existing copyImageObject/
* copyImageFields (entities/import.js, already used for a live source
* entity's key) can copy from them into the final entity/item namespace
* unchanged.
*/
import type { EntityField } from '@/stores/entities'
import type { EntityExportBundle } from './entityExportBundle'
import api from '@/services/api'
import { uploadToS3 } from './s3Upload'
function imageFieldIds(fields: EntityField[]): Set<string> {
return new Set(
fields
.filter((field) => field.type === 'image' || field.type === 'image_set')
.map((field) => field.fieldId)
.filter((id): id is string => !!id)
)
}
const MIME_TYPE_BY_EXTENSION: Record<string, string> = {
jpg: 'image/jpeg',
jpeg: 'image/jpeg',
png: 'image/png',
gif: 'image/gif',
webp: 'image/webp',
svg: 'image/svg+xml',
bmp: 'image/bmp'
}
// The zip format has no per-entry MIME type, so a blob extracted from it via
// JSZip always comes back with an empty `.type` — the extension (preserved
// in the filename by the backend's export) is the only signal left to infer
// one from. The backend's presigned-url endpoint rejects anything that
// doesn't start with `image/`, so this must never fall back to a generic
// type like `application/octet-stream`.
function inferImageMimeType(filename: string): string {
const extension = filename.split('.').pop()?.toLowerCase() ?? ''
return MIME_TYPE_BY_EXTENSION[extension] || 'image/jpeg'
}
async function uploadToStaging(blob: Blob, filename: string): Promise<string> {
const fileType = blob.type.startsWith('image/') ? blob.type : inferImageMimeType(filename)
const presign = await api.post('/v1/upload/presigned-url', {
fileName: filename,
fileType,
fieldName: 'import',
scope: 'import-staging'
})
const { uploadUrl, uploadFields, key } = presign.data
// The backend only ever issues a presigned POST (see s3Upload.ts) — a Blob
// from JSZip has no filename and, per inferImageMimeType above, often no
// reliable `.type` either, so it's wrapped in a File carrying the exact
// type that was just sent to the presign request, matching the policy S3
// will check the upload against.
const file = new File([blob], filename, { type: fileType })
await uploadToS3(file, uploadUrl, uploadFields)
return key
}
export async function stageImportImages(
bundle: EntityExportBundle,
imageBlobs: Map<string, Blob>
): Promise<EntityExportBundle> {
if (imageBlobs.size === 0) return bundle
const zipPathToStagedKey = new Map<string, string>()
for (const [zipPath, blob] of imageBlobs) {
const filename = zipPath.split('/').pop() || 'image'
zipPathToStagedKey.set(zipPath, await uploadToStaging(blob, filename))
}
for (const entry of bundle.entities) {
if (entry.entity.picture && zipPathToStagedKey.has(entry.entity.picture)) {
entry.entity.picture = zipPathToStagedKey.get(entry.entity.picture)
}
Iif (entry.entity.icon && zipPathToStagedKey.has(entry.entity.icon)) {
entry.entity.icon = zipPathToStagedKey.get(entry.entity.icon)
}
const fieldIds = imageFieldIds(entry.entity.fields)
for (const item of entry.items) {
for (const fieldId of fieldIds) {
const value = item[fieldId]
Iif (!value) continue
if (Array.isArray(value)) {
item[fieldId] = value.map((v) =>
typeof v === 'string' && zipPathToStagedKey.has(v) ? zipPathToStagedKey.get(v) : v
)
} else Eif (typeof value === 'string' && zipPathToStagedKey.has(value)) {
item[fieldId] = zipPathToStagedKey.get(value)
}
}
}
}
return bundle
}
|