import { SandboxFileError , SandboxFiles } from "../types/Sandbox" ; import FileType from "file-type/browser" ; import { unzipFile , ZIP_MIMES } from "./files" ; import { snippetFromUrl } from "./snippets" ; export async function processZipSandbox ( file ) { const type = await FileType . fromBlob ( file ) ; if ( ! type || ! ZIP_MIMES . includes ( type . mime ) ) { throw SandboxFileError . WRONG_FORMAT ; } let files ; try { files = await unzipFile ( file ) ; } catch ( err ) { throw SandboxFileError . FAILED_UNZIP ; } if ( ! files [ 'index.html' ] ) { throw SandboxFileError . NO_INDEX_HTML ; } const indexContents = await files [ 'index.html' ] . text ( ) ; const parser = new DOMParser ( ) ; const doc = parser . parseFromString ( indexContents , "text/html" ) ; const snippet = doc . querySelector ( "#fxhash-snippet" ) ; if ( ! snippet ) { throw SandboxFileError . NO_SNIPPET ; } snippet . innerHTML = snippetFromUrl ; const newIndexContents = doc . documentElement . outerHTML ; files [ "index.html" ] = new Blob ( [ newIndexContents ] , { type : "text/html" } ) ; const record = { } ; for ( const name in files ) { if ( name . slice ( - 4 ) === ".svg" ) { files [ name ] = files [ name ] . slice ( 0 , files [ name ] . size , "image/svg+xml" ) ; } record [ name ] = { url : URL . createObjectURL ( files [ name ] ) } ; if ( name === "index.html" ) { record [ name ] . blob = files [ name ] ; } } return record ; }