Update file-type api usage
This commit is contained in:
parent
299a9324f6
commit
41e3762e57
@ -54,7 +54,7 @@ class Uploader {
|
|||||||
express.post("/uploads/new/:token", Uploader.routeUploadFile);
|
express.post("/uploads/new/:token", Uploader.routeUploadFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
static routeGetFile(req, res) {
|
static async routeGetFile(req, res) {
|
||||||
const name = req.params.name;
|
const name = req.params.name;
|
||||||
|
|
||||||
const nameRegex = /^[0-9a-f]{16}$/;
|
const nameRegex = /^[0-9a-f]{16}$/;
|
||||||
@ -66,7 +66,7 @@ class Uploader {
|
|||||||
const folder = name.substring(0, 2);
|
const folder = name.substring(0, 2);
|
||||||
const uploadPath = Helper.getFileUploadPath();
|
const uploadPath = Helper.getFileUploadPath();
|
||||||
const filePath = path.join(uploadPath, folder, name);
|
const filePath = path.join(uploadPath, folder, name);
|
||||||
const detectedMimeType = Uploader.getFileType(filePath);
|
const detectedMimeType = await Uploader.getFileType(filePath);
|
||||||
|
|
||||||
// doesn't exist
|
// doesn't exist
|
||||||
if (detectedMimeType === null) {
|
if (detectedMimeType === null) {
|
||||||
@ -232,12 +232,12 @@ class Uploader {
|
|||||||
|
|
||||||
// Returns null if an error occurred (e.g. file not found)
|
// Returns null if an error occurred (e.g. file not found)
|
||||||
// Returns a string with the type otherwise
|
// Returns a string with the type otherwise
|
||||||
static getFileType(filePath) {
|
static async getFileType(filePath) {
|
||||||
try {
|
try {
|
||||||
const buffer = readChunk.sync(filePath, 0, fileType.minimumBytes);
|
const buffer = readChunk.sync(filePath, 0, fileType.minimumBytes);
|
||||||
|
|
||||||
// returns {ext, mime} if found, null if not.
|
// returns {ext, mime} if found, null if not.
|
||||||
const file = fileType(buffer);
|
const file = await fileType.fromBuffer(buffer);
|
||||||
|
|
||||||
// if a file type was detected correctly, return it
|
// if a file type was detected correctly, return it
|
||||||
if (file) {
|
if (file) {
|
||||||
|
Loading…
Reference in New Issue
Block a user