feat: allows upload through Local API
This commit is contained in:
22
src/uploads/getFileByPath.ts
Normal file
22
src/uploads/getFileByPath.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import fs from 'fs';
|
||||
import mime from 'mime';
|
||||
import { File } from './types';
|
||||
|
||||
const getFileByPath = (filePath: string): File => {
|
||||
if (typeof filePath === 'string') {
|
||||
const data = fs.readFileSync(filePath);
|
||||
const mimetype = mime.getType(filePath);
|
||||
|
||||
const name = filePath.split('/').pop();
|
||||
|
||||
return {
|
||||
data,
|
||||
mimetype,
|
||||
name,
|
||||
};
|
||||
}
|
||||
|
||||
return undefined;
|
||||
};
|
||||
|
||||
export default getFileByPath;
|
||||
21
src/uploads/saveBufferToFile.ts
Normal file
21
src/uploads/saveBufferToFile.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { Readable } from 'stream';
|
||||
import fs from 'fs';
|
||||
|
||||
/**
|
||||
* Save buffer data to a file.
|
||||
* @param {Buffer} buffer - buffer to save to a file.
|
||||
* @param {string} filePath - path to a file.
|
||||
*/
|
||||
const saveBufferToFile = async (buffer: Buffer, filePath: string): Promise<void> => {
|
||||
// Setup readable stream from buffer.
|
||||
let streamData = buffer;
|
||||
const readStream = new Readable();
|
||||
readStream._read = () => {
|
||||
readStream.push(streamData);
|
||||
streamData = null;
|
||||
};
|
||||
// Setup file system writable stream.
|
||||
return fs.writeFileSync(filePath, buffer);
|
||||
};
|
||||
|
||||
export default saveBufferToFile;
|
||||
@@ -41,3 +41,9 @@ export type Upload = {
|
||||
staticDir: string
|
||||
adminThumbnail?: string
|
||||
}
|
||||
|
||||
export type File = {
|
||||
data: Buffer
|
||||
mimetype: string
|
||||
name: string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user