All files / src/services resources_service.ts

85.71% Statements 48/56
75% Branches 24/32
100% Functions 14/14
85.71% Lines 48/56

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                      8x   3x 3x 1x     3x   2x     2x 2x 2x       3x           3x 3x 3x   3x 2x   1x           8x   6x 6x       6x   4x     4x 4x 4x       6x           5x 5x 5x   5x 4x   1x           8x 3x 3x 3x 2x 2x             1x     1x         8x 6x 6x 6x 1x 1x             5x 1x   4x          
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 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 Eif (!req.file) {
                    return reject(new TypeError('No file uploaded.'));
                } else {
                    return reject(new Error('Internal Server Error'));
                }
            }
            Iif (!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);
        });
    });
};
 
export { uploadImage, uploadResume };