Co-authored-by: khakimvinh <kha.kim.vinh@gmail.com> Co-authored-by: Elliot DeNolf <denolfe@gmail.com>
26 lines
549 B
TypeScript
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;
|