HEX
Server: Apache/2
System: Linux s01 6.1.0-34-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.135-1 (2025-04-25) x86_64
User: beestg (1003)
PHP: 8.3.25
Disabled: exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname
Upload Files
File: /home/beestg/domains/beestgenot.nl/public_html/wp-content/plugins/code-snippets/js/utils/files.ts
import { getSnippetType } from './snippets'
import type { Snippet } from '../types/Snippet'

const SECOND_IN_MS = 1000
const TIMEOUT_SECONDS = 40

const MIME_INFO = <const> {
	php: ['php', 'text/php'],
	html: ['php', 'text/php'],
	css: ['css', 'text/css'],
	js: ['js', 'text/javascript'],
	json: ['json', 'application/json']
}

export const downloadAsFile = (content: BlobPart, filename: string, type: string) => {
	const link = document.createElement('a')
	link.download = filename
	link.href = URL.createObjectURL(new Blob([content], { type }))

	setTimeout(() => URL.revokeObjectURL(link.href), TIMEOUT_SECONDS * SECOND_IN_MS)
	setTimeout(() => link.click(), 0)
}

export const downloadSnippetExportFile = (
	content: BlobPart,
	{ id, name, scope }: Snippet,
	type?: keyof typeof MIME_INFO
) => {
	const [ext, mimeType] = MIME_INFO[type ?? getSnippetType(scope)]

	const sanitizedName = name.toLowerCase().replace(/[^\w-]+/g, '-').trim()

	const title = '' === sanitizedName ? `snippet-${id}` : sanitizedName
	const filename = `${title}.code-snippets.${ext}`

	downloadAsFile(content, filename, mimeType)
}