{"version":3,"file":"GenericFile.cjs","sources":["../../src/GenericFile.ts"],"sourcesContent":["import { utf8 } from '@metaplex-foundation/umi-serializers';\nimport { generateRandomString } from './utils';\n\n/**\n * A generic definition of a File represented as a buffer with\n * extra metadata such as a file name, content type, and tags.\n *\n * @category Storage\n */\nexport type GenericFile = {\n  readonly buffer: Uint8Array;\n  readonly fileName: string;\n  readonly displayName: string;\n  readonly uniqueName: string;\n  readonly contentType: string | null;\n  readonly extension: string | null;\n  readonly tags: GenericFileTag[];\n};\n\n/**\n * Represent a custom tag that can be attached to a file.\n * @category Storage\n */\nexport type GenericFileTag = { name: string; value: string };\n\n/**\n * Alias for the native File interface from the browser.\n * @category Storage\n */\nexport type BrowserFile = File;\n\n/**\n * Represents the options that can be provided when creating a {@link GenericFile}.\n * @category Storage\n */\nexport type GenericFileOptions = {\n  displayName?: string;\n  uniqueName?: string;\n  contentType?: string;\n  extension?: string;\n  tags?: { name: string; value: string }[];\n};\n\n/**\n * Creates a new {@link GenericFile} from a buffer and a file name.\n * @category Storage\n */\nexport const createGenericFile = (\n  content: string | Uint8Array,\n  fileName: string,\n  options: GenericFileOptions = {}\n): GenericFile => ({\n  buffer: typeof content === 'string' ? utf8.serialize(content) : content,\n  fileName,\n  displayName: options.displayName ?? fileName,\n  uniqueName: options.uniqueName ?? generateRandomString(),\n  contentType: options.contentType ?? null,\n  extension: options.extension ?? getExtension(fileName),\n  tags: options.tags ?? [],\n});\n\n/**\n * Creates a new {@link GenericFile} from a {@link BrowserFile}.\n * @category Storage\n */\nexport const createGenericFileFromBrowserFile = async (\n  browserFile: BrowserFile,\n  options: GenericFileOptions = {}\n): Promise<GenericFile> =>\n  createGenericFile(\n    new Uint8Array(await browserFile.arrayBuffer()),\n    browserFile.name,\n    options\n  );\n\n/**\n * Creates a new {@link GenericFile} from a JSON object.\n * @category Storage\n */\nexport const createGenericFileFromJson = <T>(\n  json: T,\n  fileName = 'inline.json',\n  options: GenericFileOptions = {}\n): GenericFile =>\n  createGenericFile(JSON.stringify(json), fileName, {\n    contentType: 'application/json',\n    ...options,\n  });\n\n/**\n * Creates a new {@link BrowserFile} from a {@link GenericFile}.\n * @category Storage\n */\nexport const createBrowserFileFromGenericFile = (\n  file: GenericFile\n): BrowserFile => new File([file.buffer as BlobPart], file.fileName);\n\n/**\n * Returns the content of a {@link GenericFile} as a parsed JSON object.\n * @category Storage\n */\nexport const parseJsonFromGenericFile = <T>(file: GenericFile): T =>\n  JSON.parse(new TextDecoder().decode(file.buffer));\n\n/**\n * Returns the total size of a list of {@link GenericFile} in bytes.\n * @category Storage\n */\nexport const getBytesFromGenericFiles = (...files: GenericFile[]): number =>\n  files.reduce((acc, file) => acc + file.buffer.byteLength, 0);\n\n/**\n * Whether the given value is a {@link GenericFile}.\n * @category Storage\n */\nexport const isGenericFile = (file: any): file is GenericFile =>\n  file != null &&\n  typeof file === 'object' &&\n  'buffer' in file &&\n  'fileName' in file &&\n  'displayName' in file &&\n  'uniqueName' in file &&\n  'contentType' in file &&\n  'extension' in file &&\n  'tags' in file;\n\n/**\n * Returns the extension of a file name.\n * @category Storage\n */\nconst getExtension = (fileName: string): string | null => {\n  const lastDotIndex = fileName.lastIndexOf('.');\n\n  return lastDotIndex < 0 ? null : fileName.slice(lastDotIndex + 1);\n};\n"],"names":["createGenericFile","content","fileName","options","buffer","utf8","serialize","displayName","uniqueName","generateRandomString","contentType","extension","getExtension","tags","createGenericFileFromBrowserFile","browserFile","Uint8Array","arrayBuffer","name","createGenericFileFromJson","json","JSON","stringify","createBrowserFileFromGenericFile","file","File","parseJsonFromGenericFile","parse","TextDecoder","decode","getBytesFromGenericFiles","files","reduce","acc","byteLength","isGenericFile","lastDotIndex","lastIndexOf","slice"],"mappings":";;;;;;;AAGA;AACA;AACA;AACA;AACA;AACA;;AAmCA;AACA;AACA;AACA;AACaA,MAAAA,iBAAiB,GAAG,CAC/BC,OAA4B,EAC5BC,QAAgB,EAChBC,OAA2B,GAAG,EAAE,MACf;AACjBC,EAAAA,M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