Files
simstudioai--sim/apps/sim/lib/pptx-renderer/utils/media.ts
T
wehub-resource-sync d25d482dc2
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

74 lines
2.1 KiB
TypeScript

/**
* Media utilities — MIME type detection, path resolution, and blob URL management.
*/
/**
* Determine MIME type from file extension.
* Covers images, video, and audio formats used in PPTX files.
*/
export function getMimeType(path: string): string {
const ext = path.split('.').pop()?.toLowerCase() || ''
const mimeMap: Record<string, string> = {
png: 'image/png',
jpg: 'image/jpeg',
jpeg: 'image/jpeg',
gif: 'image/gif',
svg: 'image/svg+xml',
bmp: 'image/bmp',
tiff: 'image/tiff',
tif: 'image/tiff',
emf: 'image/x-emf',
wmf: 'image/x-wmf',
webp: 'image/webp',
mp4: 'video/mp4',
m4v: 'video/mp4',
webm: 'video/webm',
avi: 'video/x-msvideo',
mp3: 'audio/mpeg',
wav: 'audio/wav',
m4a: 'audio/mp4',
ogg: 'audio/ogg',
}
return mimeMap[ext] || 'application/octet-stream'
}
/**
* Resolve a relative media path (from rels) to its canonical path in PptxFiles.media.
* Rels targets are relative like "../media/image1.png".
* Media paths in PptxFiles are like "ppt/media/image1.png".
*/
export function resolveMediaPath(target: string): string {
const fileName = target.split('/').pop() || ''
return `ppt/media/${fileName}`
}
/**
* Get or create a blob URL for a media file, using a cache to avoid duplicates.
*
* @param mediaPath - Canonical path (e.g. "ppt/media/image1.png")
* @param data - Raw media data (Uint8Array or ArrayBuffer)
* @param cache - Map to store/retrieve cached blob URLs
* @returns The blob URL string
*/
export function getOrCreateBlobUrl(
mediaPath: string,
data: Uint8Array | ArrayBuffer,
cache: Map<string, string>
): string {
let url = cache.get(mediaPath)
if (!url) {
const mime = getMimeType(mediaPath)
const blobPart = data instanceof ArrayBuffer ? data : copyToArrayBuffer(data)
const blob = new Blob([blobPart], { type: mime })
url = URL.createObjectURL(blob)
cache.set(mediaPath, url)
}
return url
}
function copyToArrayBuffer(data: Uint8Array): ArrayBuffer {
const copy = new Uint8Array(data.byteLength)
copy.set(data)
return copy.buffer
}