Skip to content

Instantly share code, notes, and snippets.

@zenweasel
Last active February 4, 2022 00:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zenweasel/2c1eb9b540231a00734c724b602f6ecb to your computer and use it in GitHub Desktop.
Save zenweasel/2c1eb9b540231a00734c724b602f6ecb to your computer and use it in GitHub Desktop.
import fs from "fs";
import { Blob } from "buffer";
import { Readable } from "stream";
import pkg from "@reactioncommerce/file-collections";
// import * as buffer from "buffer";
global.Blob = Blob;
const { FileRecord } = pkg;
export default async function uploadImages(context) {
const { collections: { Media } } = context;
const data = fs.readFileSync("./custom-packages/sample-data/src/images/fatbear.jpg");
const blob = new Blob([data], { type: "image/jpg" });
const fileRecord = FileRecord.fromBlob(blob);
fileRecord.name("fatbear.jpg");
await Media.insert(fileRecord);
const writeStream = await Media.stores[0].createWriteStream(fileRecord);
await new Promise((resolve, reject) => {
writeStream.once("error", (err) => err && reject(err));
writeStream.once("stored", resolve);
Readable.from(data).pipe(writeStream);
});
console.log("done uploading images");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment