Files
payload/src/uploads/getFileByPath.ts
Dan Ribbens 7acf944a28 Feat/expose sharp options (#1029)
Co-authored-by: khakimvinh <kha.kim.vinh@gmail.com>
Co-authored-by: Elliot DeNolf <denolfe@gmail.com>
2022-09-12 10:29:07 -07:00

26 lines
549 B
TypeScript

import fs from 'fs';
import { fromFile } from 'file-type';
import path from 'path';
import { File } from './types';
const getFileByPath = async (filePath: string): Promise<File> => {
if (typeof filePath === 'string') {
const data = fs.readFileSync(filePath);
const mimetype = fromFile(filePath);
const { size } = fs.statSync(filePath);
const name = path.basename(filePath);
return {
data,
mimetype: (await mimetype).mime,
name,
size,
};
}
return undefined;
};
export default getFileByPath;