-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add convertJSONToDTCG & convertZIPToDTCG utils
- Loading branch information
1 parent
3678c9c
commit 6bd674c
Showing
5 changed files
with
184 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { ZipWriter, BlobWriter, TextReader } from '@zip.js/zip.js'; | ||
|
||
/** | ||
* Caution: browser-only utilities | ||
* Would be weird to support in NodeJS since end-user = developer | ||
* so the question would be: where to store the file, if we don't know | ||
* where the blob/files object came from to begin with | ||
*/ | ||
|
||
/** | ||
* @param {Blob} blob | ||
* @param {string} filename | ||
*/ | ||
function downloadBlob(blob, filename) { | ||
const url = URL.createObjectURL(blob); | ||
|
||
// Auto-download the ZIP through anchor | ||
const anchor = document.createElement('a'); | ||
anchor.href = url; | ||
anchor.download = filename; | ||
anchor.click(); | ||
URL.revokeObjectURL(url); | ||
} | ||
|
||
/** | ||
* @param {string | Blob} stringOrBlob | ||
* @param {string} filename | ||
*/ | ||
export function downloadJSON(stringOrBlob, filename = 'output.json') { | ||
/** @type {Blob} */ | ||
let jsonBlob; | ||
// check if it's a Blob.., instanceof is too strict e.g. Blob polyfills | ||
if (stringOrBlob.constructor.name === 'Blob') { | ||
jsonBlob = /** @type {Blob} */ (stringOrBlob); | ||
} else { | ||
jsonBlob = new Blob([stringOrBlob], { type: 'application/json' }); | ||
} | ||
downloadBlob(jsonBlob, filename); | ||
} | ||
|
||
/** | ||
* @param {Record<string, string> | Blob} filesOrBlob | ||
* @param {string} filename | ||
*/ | ||
export async function downloadZIP(filesOrBlob, filename = 'output.zip') { | ||
/** @type {Blob} */ | ||
let zipBlob; | ||
// check if it's a Blob.., instanceof is too strict e.g. Blob polyfills | ||
if (filesOrBlob.constructor.name === 'Blob') { | ||
zipBlob = /** @type {Blob} */ (filesOrBlob); | ||
} else { | ||
const zipWriter = new ZipWriter(new BlobWriter('application/zip')); | ||
|
||
await Promise.all( | ||
Object.entries(filesOrBlob).map(([key, value]) => zipWriter.add(key, new TextReader(value))), | ||
); | ||
|
||
// Close zip and make into URL | ||
zipBlob = await zipWriter.close(); | ||
} | ||
downloadBlob(zipBlob, filename); | ||
} |