From 67b777be84e349888fe76e758d3538a99273ac64 Mon Sep 17 00:00:00 2001 From: Ivan Borshchov Date: Mon, 21 Aug 2023 14:52:09 +0300 Subject: [PATCH 1/2] fix compatiblity with adminjs-upload plugin --- src/buildRouter.ts | 50 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/src/buildRouter.ts b/src/buildRouter.ts index cb7e3d9..9853435 100644 --- a/src/buildRouter.ts +++ b/src/buildRouter.ts @@ -7,23 +7,49 @@ import fromPairs from 'lodash/fromPairs.js'; import * as mime from 'mime-types'; import path from 'path'; +import os from 'os'; +import fs from 'fs'; + + import { WrongArgumentError } from './errors.js'; import { log } from './logger.js'; const INVALID_ADMIN_JS_INSTANCE = 'You have to pass an instance of AdminJS to the buildRouter() function'; -const getFile = (fileField?: { - fieldname: string; - filename: string; - file: Record; -}) => { - if (!fileField?.file) { +async function writeFile(path, data) { + return new Promise((resolve, reject) => { + fs.writeFile(path, data, (err) => { + if (err) { + reject(err); + } + else { + resolve(null); + } + }); + }); +} + +const getFile = async (fileField) => { + if (fileField.type === 'file') { + fileField.name = fileField.filename; + const buffer = await fileField.toBuffer(); + const tmpFilePath = path.join(os.tmpdir(), fileField.filename); + await writeFile(tmpFilePath, buffer); + return { + name: fileField.filename, + path: tmpFilePath + }; + } return null; - } - const { file, filename } = fileField; - file.name = filename; - return file; +}; + +Array.prototype.asyncMap = async function (callback) { + const result = []; + for (let index = 0; index < this.length; index++) { + result.push(await callback(this[index], index, this)); + } + return result; }; export const buildRouter = async ( @@ -60,9 +86,9 @@ export const buildRouter = async ( { value: string; file?: File } >; const fields = fromPairs( - Object.keys((body ?? {}) as Record).map(key => [ + await Object.keys((body ?? {}) as Record).map(async key => [ key, - getFile(body[key] as any) ?? body[key].value, + await getFile(body[key] as any) ?? body[key].value, ]) ); const html = await controller[route.action]( From 8d326d08d19c6adf99fc38bd75ee266e18a0e4dc Mon Sep 17 00:00:00 2001 From: Ivan Borshchov Date: Mon, 21 Aug 2023 15:48:55 +0300 Subject: [PATCH 2/2] add ts-ignores (just fast fix) --- src/buildRouter.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/buildRouter.ts b/src/buildRouter.ts index 9853435..fe04a4f 100644 --- a/src/buildRouter.ts +++ b/src/buildRouter.ts @@ -44,10 +44,12 @@ const getFile = async (fileField) => { return null; }; +//@ts-ignore Array.prototype.asyncMap = async function (callback) { const result = []; for (let index = 0; index < this.length; index++) { - result.push(await callback(this[index], index, this)); + //@ts-ignore + result.push(await callback(this[index], index, this)); } return result; }; @@ -86,9 +88,10 @@ export const buildRouter = async ( { value: string; file?: File } >; const fields = fromPairs( - await Object.keys((body ?? {}) as Record).map(async key => [ + //@ts-ignore + await Object.keys((body ?? {}) as Record).asyncMap(async key => [ key, - await getFile(body[key] as any) ?? body[key].value, + (await getFile(body[key] as any)) ?? body[key].value, ]) ); const html = await controller[route.action](