Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | 1x 1x 1x 6x 6x 6x 4x 4x 4x 4x 6x 5x 5x 5x 5x 4x 1x 1x 1x 1x 6x 6x 6x 1x 1x 5x 1x 4x 1x 1x 1x | import { config } from '../config/config'; import multer from 'multer'; import path from 'path'; import { randomUUID } from 'crypto'; import fs from 'fs'; import { Request } from 'express'; interface MulterRequest extends Request { file?: Express.Multer.File; } const createFilesStorage = () => { // Ensure the directory exists const filesResourcesDir = config.resources.filesDirectoryPath(); if (!fs.existsSync(filesResourcesDir)) { fs.mkdirSync(filesResourcesDir, { recursive: true }); } const filesStorage = multer.diskStorage({ destination: (req, file, cb) => { cb(null, `${filesResourcesDir}/`); }, filename: (req, file, cb) => { const ext = path.extname(file.originalname); const id = randomUUID(); cb(null, id + ext); } }); return multer({ storage: filesStorage, limits: { fileSize: config.resources.fileMaxSize() }, fileFilter: (req, file, cb) => { const allowedExts = ['.pdf', '.doc', '.docx', '.docs']; const allowedMimes = [ 'application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' ]; const ext = path.extname(file.originalname).toLowerCase(); const mime = file.mimetype; if (allowedExts.includes(ext) && allowedMimes.includes(mime)) { return cb(null, true); } else { return cb(new TypeError(`Invalid file type (${mime}). Allowed: ${allowedExts.join(', ')}`)); } } }); }; const createImagesStorage = () => { // Ensure the directory exists const imagesResourcesDir = config.resources.imagesDirectoryPath(); if (!fs.existsSync(imagesResourcesDir)) { fs.mkdirSync(imagesResourcesDir, { recursive: true }); } const imagesStorage = multer.diskStorage({ destination: (req, file, cb) => { cb(null, `${imagesResourcesDir}/`); }, filename: (req, file, cb) => { const ext = path.extname(file.originalname); const id = randomUUID(); cb(null, id + ext); } }); return multer({ storage: imagesStorage, limits: { fileSize: config.resources.imageMaxSize() }, fileFilter: (req, file, cb) => { const allowedTypes = /jpeg|jpg|png|gif/; const extname = allowedTypes.test(path.extname(file.originalname).toLowerCase()); const mimetype = allowedTypes.test(file.mimetype); if (extname && mimetype) { return cb(null, true); } else { return cb(new TypeError(`Invalid file type. Only images are allowed: ${allowedTypes}`)); } } }); }; const createResumesStorage = () => { // Ensure the directory exists const resumesDirectoryPath = config.resources.resumesDirectoryPath(); Iif (!fs.existsSync(resumesDirectoryPath)) { fs.mkdirSync(resumesDirectoryPath, { recursive: true }); } const resumesStorage = multer.diskStorage({ destination: (req, file, cb) => { cb(null, `${resumesDirectoryPath}/`); }, filename: (req, file, cb) => { const ext = path.extname(file.originalname); const id = randomUUID(); cb(null, id + ext); } }); return multer({ storage: resumesStorage, limits: { fileSize: config.resources.resumeMaxSize() }, fileFilter: (req, file, cb) => { const allowedTypes = /pdf|doc|docx|txt|text/; const extname = allowedTypes.test(path.extname(file.originalname).toLowerCase()); const mimetype = allowedTypes.test(file.mimetype); if (extname && mimetype) { return cb(null, true); } else { return cb(new TypeError(`Invalid file type. Only PDF, DOC, DOCX and TXT/TEXT files are allowed.`)); } } }); }; const uploadImage = (req: MulterRequest): Promise<string> => { return new Promise<string>((resolve, reject) => { createImagesStorage().single('file')(req, {} as any, (error) => { if (error) { if (error instanceof multer.MulterError || error instanceof TypeError) { return reject(error); } else if (!req.file) { return reject(new TypeError('No file uploaded.')); } else { return reject(new Error('Internal Server Error')); } } if (!req.file) { return reject(new TypeError('No file uploaded.')); } resolve(req.file.filename); }); }); }; const uploadFile = (req: MulterRequest): Promise<string> => { return new Promise<string>((resolve, reject) => { createFilesStorage().single('file')(req, {} as any, (error) => { if (error) { if (error instanceof multer.MulterError || error instanceof TypeError) { return reject(error); } else if (!req.file) { return reject(new TypeError('No file uploaded.')); } else { return reject(new Error('Internal Server Error')); } } if (!req.file) { return reject(new TypeError('No file uploaded.')); } resolve(req.file.filename); }); }); }; const uploadResume = (req: MulterRequest): Promise<string> => { return new Promise<string>((resolve, reject) => { createResumesStorage().single('file')(req, {} as any, (error) => { if (error) { if (error instanceof multer.MulterError || error instanceof TypeError) { return reject(error); } else Eif (!req.file) { return reject(new TypeError('No file uploaded.')); } else { return reject(new Error('Internal Server Error')); } } if (!req.file) { return reject(new TypeError('No file uploaded.')); } resolve(req.file.filename); }); }); }; const getResumePath = (filename: string): string => { const resumesDirectoryPath = config.resources.resumesDirectoryPath(); const resumePath = path.resolve(resumesDirectoryPath, filename); if (!fs.existsSync(resumePath)) { throw new TypeError('Resume not found'); } return resumePath; } const getResumeBuffer = (filename: string): Buffer => { const resumePath = getResumePath(filename); try { return fs.readFileSync(resumePath); } catch (error: any) { throw new Error(`Failed to read resume file: ${error.message}`); } } const resumeExists = (filename: string): boolean => { const resumesDirectoryPath = config.resources.resumesDirectoryPath(); const resumePath = path.resolve(resumesDirectoryPath, filename); return fs.existsSync(resumePath); }; export { uploadImage, uploadResume, getResumeBuffer, resumeExists, uploadFile }; |