Skip to content

Instantly share code, notes, and snippets.

@te-online
Created July 3, 2024 14:33
Show Gist options
  • Save te-online/03b1f108dae23d4da17971b51543f180 to your computer and use it in GitHub Desktop.
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)
/**
* 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