11import { ControllerHelper } from "../helpers/controllerHelper" ;
2+ import { Entity } from "../interfaces/types" ;
23import * as fs from "fs" ;
3- import { Adminizer } from "../lib/Adminizer " ;
4+ import multer from "multer " ;
45
5- export default function upload ( req : ReqType , res : ResType ) : void {
6+ export async function ckEditorUpload ( req : ReqType , res : ResType ) {
67 let entity = ControllerHelper . findEntityObject ( req ) ;
78
89 if ( req . adminizer . config . auth . enable ) {
@@ -20,37 +21,66 @@ export default function upload(req: ReqType, res: ResType): void {
2021 }
2122 }
2223
23- if ( req . method . toUpperCase ( ) === "POST" ) {
24- try {
25- // set upload directory
26- const dirDownload = `uploads/${ entity . type } /${ entity . name } /ckeditor` ;
27- const dir = `${ process . cwd ( ) } /.tmp/public/${ dirDownload } /` ;
28-
29- if ( ! fs . existsSync ( dir ) ) {
30- fs . mkdirSync ( dir , { recursive : true } ) ;
31- }
32-
33- // save file
34- const filenameOrig = req . body . name . replace ( ' ' , '_' ) ;
35- let filename = filenameOrig . replace ( / $ / , '_prefix' ) ;
36-
37- req . upload ( {
38- destination : dir ,
39- filename : ( ) => filename
40- } ) . single ( "image" ) ( req , res , ( err ) => {
41- if ( err ) {
42- Adminizer . logger . error ( "Error uploading file:" , err ) ;
43- return res . status ( 500 ) . send ( { error : err . message || "Internal Server Error" } ) ;
44- }
45-
46- return res . send ( {
47- msg : "success" ,
48- url : `/${ dirDownload } /${ filename } ` ,
49- } ) ;
50- } ) ;
51- } catch ( error ) {
52- Adminizer . logger . error ( "Error in uploadImage:" , error ) ;
53- res . status ( 500 ) . send ( { error : "Internal Server Error" } ) ;
54- }
55- }
24+ const dirDownload = `uploads/${ entity . type } /${ entity . name } /ckeditor` ;
25+
26+ await handleUpload ( req , res , dirDownload )
27+
28+ }
29+
30+ async function handleUpload ( req : ReqType , res : ResType , dirDownload : string ) {
31+ const upload = multer ( getUploadConfig ( dirDownload ) ) . single ( "file" ) ;
32+
33+ upload ( req , res , async ( err ) => {
34+ try {
35+ if ( err ) {
36+ let errorMessage = err . message ;
37+ if ( err . code === 'LIMIT_FILE_SIZE' ) {
38+ const maxSizeMB = ( 5 * 1024 * 1024 ) / ( 1024 * 1024 ) ;
39+ errorMessage = `${ req . i18n . __ ( 'The file exceeds the size limit' ) } ${ maxSizeMB } MB` ;
40+ }
41+ return res . status ( 400 ) . json ( { msg : "error" , error : errorMessage } ) ;
42+ }
43+
44+ return res . send ( {
45+ msg : "success" ,
46+ url : `/${ dirDownload } /${ req . file . filename } ` ,
47+ } ) ;
48+ } catch ( e ) {
49+ console . error ( e ) ;
50+ return res . status ( 500 ) . send ( { error : e . message || 'Upload failed' } ) ;
51+ }
52+ } ) ;
53+ }
54+
55+ function getUploadConfig ( dirDownload : string ) {
56+ return {
57+ storage : setStorage ( checkDirectory ( dirDownload ) ) ,
58+ limits : {
59+ fileSize : 5 * 1024 * 1024 ,
60+ } ,
61+ fileFilter : ( req : ReqType , file : any , cb : any ) => {
62+ cb ( null , true ) ;
63+ }
64+ } ;
5665}
66+
67+ function checkDirectory ( dirDownload : string ) : string {
68+ const outputDir = `${ process . cwd ( ) } /.tmp/public/${ dirDownload } ` ;
69+
70+ if ( ! fs . existsSync ( outputDir ) ) {
71+ fs . mkdirSync ( outputDir , { recursive : true } ) ;
72+ }
73+ return outputDir
74+ }
75+
76+ function setStorage ( outputDir : string ) {
77+ return multer . diskStorage ( {
78+ destination : ( req , file , cb ) => {
79+ cb ( null , outputDir ) ;
80+ } ,
81+ filename : ( req , file , cb ) => {
82+ const filename = req . body . name ;
83+ cb ( null , filename ) ;
84+ }
85+ } ) ;
86+ }
0 commit comments