Created
July 3, 2024 14:33
-
-
Save te-online/03b1f108dae23d4da17971b51543f180 to your computer and use it in GitHub Desktop.
Convert base64 PNG string to image file (migrating from database to object storage)
This file contains hidden or 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
/** | |
* Usage: Put a base64 encoded PNG image string into a file called image.txt | |
* Run: `node image-covert.mjs --file image.txt` | |
* Receive: Output file with random UUID for storage in object storage | |
*/ | |
import fs from "node:fs/promises"; | |
import crypto from "crypto"; | |
import { parseArgs } from "node:util"; | |
const uuid = crypto.randomUUID(); | |
const args = process.argv; | |
const options = { | |
file: { | |
type: "string", | |
}, | |
}; | |
const { values } = parseArgs({ | |
args, | |
options, | |
allowPositionals: true, | |
}); | |
const input = await fs.readFile(`./${values.file}`); | |
await fs.writeFile(`./${uuid}.png`, Buffer.from(input.toString(), "base64")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment