Skip to content

Commit

Permalink
✨ add generateFilePath func per #5
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkjrs committed Dec 27, 2023
1 parent 6684b3b commit 4fa8f7f
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import { IMAGE_EXTENSIONS, VIDEO_EXTENSIONS } from './constants';
import defaultOptions from './defaultOptions';
import crypto from 'crypto';
/**
* A fetcher for useSwr
*
Expand Down Expand Up @@ -54,5 +55,22 @@ function detectMediaType(url: string) {
function getOptions(options?: object | undefined) {
return Object.assign(options || {}, defaultOptions);
}

/**
* generateFilePath
*
* @description Given a file and Promo ID, generate a proper file path.
*
* @param file A File object
* @param pid A string Promo ID
*
* @returns string File path
*/
function generateFilePath(file: File, pid: string) {
const mimeType = file.type;
let ext: string = mimeType.slice(6);
const uuid = crypto.randomUUID();
return `${pid}/${uuid}.${ext}`;
}
// Fall back to default handling
export { fetcher, detectMediaType, getOptions };
export { fetcher, detectMediaType, getOptions, generateFilePath };

0 comments on commit 4fa8f7f

Please sign in to comment.