Functions:storeFile - bettyblocks/cli GitHub Wiki

Storing files

In order to store files in the asset store and use the file in a file or image property, the runtime exposes the storeFile helper.

const storeFile = async (
  modelName: string,
  propertyName: string,
  file: string | {
    contentType: string;
    extension: string;
    fileBuffer: ArrayBuffer;
    fileName: string;
  }
  options?: {
    headers: { [key: string]: string };
  }
): Promise<string>

From URL

To store files from a URL this helper downloads the file, generates a file reference, uploads the file to the asset store and finally returns the file reference. You can assign the returned file reference to a file or image property in a create or update.

The storeFile helper expects three string arguments: the model name, the property name you want to upload a file for (this should be a file or image property) and the url of the file you want to store. It returns a promise which, once resolved, results in the file reference as a string. You can enter a fourth optional argument to pass headers which is an array of key value pairs (Where key is the name of the header and the value is the actual value itself.)

Example:

const uploadFile = async ({
  model: { name: modelName },
  property: [{ name: propertyName }],
  url,
}) => {
  const headers = [{ key: 'X-Auth-Token', value: 'token' }];
  const reference = await storeFile(modelName, propertyName, url, { headers });

  return {
    reference,
  };
}

From ArrayBuffer

Besides storing files using a URL this helper can also be used to store files using an ArrayBuffer. The only difference is the last argument, instead of a string this is now an object with the following structure:

{
  contentType: string;
  extension: string;
  fileBuffer: ArrayBuffer;
  fileName: string;
}

Example:

const file = await fetch(url);
const fileRef = await storeFile(
  "Import",
  "file",
  {
    contentType: "text/csv",
    extension: "csv",
    fileBuffer: file.blob().buffer,
    fileName: "Employee",
  },
  { headers: {} }
);
⚠️ **GitHub.com Fallback** ⚠️