/** * HELPER FUNCTIONS ------------------------------------------------------------ * ----------------------------------------------------------------------------- */ /** * Check if given File-like object has matching MIME type or file extension as defined by `accept`. * @see: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/accept * @param {File|{name?, type?}} file - File object or object with file name and MIME type to check * @param {string} [accept] - native HTML comma-separated list of one or more file types allowed * @param {string[]} [accepts] - a list of precomputed `accept` values to make it faster * @returns {boolean|string|void} true - when accept not defined, else matched MIME type or extension */ export function canAccept({ name, type }: File | { name?; type?; }, accept?: string | undefined, accepts?: string[] | undefined): boolean | string | void; /** * Create JS File Object from given URL * @param {String} url - to fetch file, can be Base64 string, http url, dataURL, blobURL, etc... * @param {String} filename - to use * @param {FilePropertyBag|undefined} [options] - attributes to pass to new File() * @returns {Promise} file object if resolved */ export function fileFromUrl(url: string, filename: string, options?: FilePropertyBag | undefined): Promise; /** * Get File Format Extension from Data URL String * @param {String} dataUrl - see https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs * @returns {String} format - example: 'png' */ export function fileFormatFromDataUrl(dataUrl: string): string; /** * Compute Unique File ID for given FileInput props. * File can have different versions with the same `id`, thus need to combine other props as primary key. * @see `fileName()` for arguments and return value */ export function fileId({ name, ext, ...fileInput }: { [x: string]: any; name: any; ext: any; }): string; /** * Create File Name (with optional ID folder) from FileInput object in this format: * `{id}/{kind}_{i}.{ext}` - for use in File `src` string for frontend and backend. * * @example: possible outputs * const file = {id: 'test', kind: 'public', i: 'thumb', ext: 'jpg'} * >>> 'test/public_thumb.jpg' * const file = {id: 'test', kind: 'public', ext: 'jpg'} * >>> 'test/public.jpg' * const file = {id: 'test', i: 'thumb'} * >>> 'test/thumb' * const file = {id: 'test', ext: 'jpg'} * >>> 'test.jpg' * const file = {ext: 'jpg'} * >>> '.jpg' * * @param {FileInput|Object} fileInput - FileInput object (ex. from GraphQL) * @returns {string} filename - including optional ID folder */ export function fileName(fileInput: FileInput | Object): string; /** * Compute File Name with given Size label * * @param {String} filename - with extension, result of `fileName()` * @param {String} size - for given `filename` * @returns {String} filename with size added to the name. */ export function fileNameSized(filename: string, size: string): string; /** * Parse onChange(files) values to match Backend API * @param {FileInput[]|FileInput|object|object[]} files - single or list of FileInputs to parse * @returns {Object[]|Object|void} {file, kind, i, remove}[] - single or list of FileInputs for backend */ export function fileParser(files: FileInput[] | FileInput | object | object[]): Object[] | Object | void; /** * Parse onChange(files) values to have `kind` as given * @param {FileInput|object} fileInput * @param {String} kind - to use * @returns {{kind: string, remove: boolean}|{kind: string, file: object}} * fileInput for backend with kind inserted */ export function fileKindParser(fileInput: FileInput | object, kind: string): { kind: string; remove: boolean; } | { kind: string; file: object; }; /** * Create File Upload Folder from given Mongoose Document Instance * @param {Document} instance - Mongoose doc * @returns {String} folder - in this format `{ModelName}/{id}` */ export function folderFrom(instance: Document): string; /** * Load Image file * * @param {String} src - full image file path * @return {Promise} promise - resolves to loaded Image file or error */ export function loadImage(src: string): Promise; /** * Compute Preview Image src from dynamic `preview` attribute * @param {String|Object} preview - type.UrlOrBase64OrPreview * @param {String} [size] - one of thumb/medium/large/etc. * @param {String} [prefix] - url prefix (defaults to CDN url, if set in .env variable REACT_APP_CDN_URL) * @returns {String|Object|any} preview src ready for consumption by Components */ export function previewSize(preview: string | Object, size?: string | undefined, prefix?: string | undefined): string | Object | any; /** * Compute Image `preview` URL/pathname (can accept Base64 string) in different sizes for consumption by frontend: * - `preview` matches original `src` * - `preview.medium` matches medium size of `src` * - `preview.thumb` matches thumbnail size of `src` * * => pass in `null` as the second parameter to disable default fallback to IMAGE.SIZES. * * @example: * const preview = previewSizes(image) * preview == image.src >>> true * preview.medium >>> string * preview.thumb >>> string * * Scenarios: * 1. Production file: use `src` suffixed with 'medium' for default size, else `src` as 'original' * 2. Local dev file: user `src` as base64 data if it is of base64 type, else same as point 1. * @param {Object} - FileInput with `src` and optional `sizes` attribute (ex. sizes: [{key: 'medium', val: 99}]) * @param {String[]|Null} [resKeys] - use predefined image size keys when `FileInput.sizes` not available * @return {Object|Undefined} preview - string object with sizes attached as props of `preview` */ export function previewSizes({ src, sizes }: Object, resKeys?: string[] | null | undefined): Object | undefined; /** * Standardizes how the absolute file path is computed * @param {String} [filename] - required if absolute `path` not given (ex. photo.jpg) * @param {String} [folder] - file directory path relative to `workDir`, if `dir` not given, must start with slash (ex. '/User') * @param {String} [workDir] - absolute working directory path, defaults to UPLOAD.PATH * @param {String} [dir] - absolute directory path, excluding file name (ex. `/root/uploads`), defaults to `workDir` + `folder` * @param {String} [path] - required if `filename` not given, absolute directory path, including file name (ex. `/root/uploads/old_image.jpg`) * @returns {Object} {dir, path, name} * - `path` -> absolute path including filename, * - `dir` -> without filename * - `name` -> filename */ export function resolvePath({ filename, folder, dir, path, workDir }?: string | undefined): Object; /** * FILE VARIABLES ============================================================== * ============================================================================= */ export const CDN_URL: any; export namespace FILE { const EXT: {}; const FORMAT_BY_MIME_TYPE: { [x: string]: string; }; const MIME_TYPE_BY_EXT: { [x: number]: string; }; } export namespace IMAGE { export { SIZE_MB_16 as MAX_RES }; export const EXTS: any[]; export const MIME_TYPES: string[]; export const SIZES: { '': { res: number; }; medium: { width: number; height: number; fit: string; }; thumb: { width: number; height: number; fit: string; }; }; } export namespace UPLOAD { const PATH: string; } export namespace SOUND { namespace FILE { const TOUCH: any; } } import { SIZE_MB_16 } from "./constants.js";