HEX
Server: Apache
System: Linux srv4.garantili.com.tr 4.18.0-477.21.1.lve.1.el8.x86_64 #1 SMP Tue Sep 5 23:08:35 UTC 2023 x86_64
User: yenicep (1023)
PHP: 7.4.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home2/yenicep/garantili-kasko/mobile/controller/upload-bill.js
const uploadFile = require("../../middleware/upload");
const fs = require('fs');
const AppConstants = require("../../core/constants/app");

exports.upload = async (req, res) => {
    try {
        await uploadFile(req, res);
        if (req.file == undefined) {
            return res.status(400).send({ message: "Please upload a file!" });
        }
        res.status(200).send({
            message: "Uploaded the file successfully: " + req.file,
            policeNo: req.policeNo
        });
    } catch (err) {
        if (err.code == "LIMIT_FILE_SIZE") {
            return res.status(500).send({
                message: "File size cannot be larger than 2MB!",
            });
        }
        res.status(500).send({
            message: `Could not upload the file: ${req.file}. ${err}`,
        });
    }
};

exports.getListFiles = (req, res) => {
    const policeNo = req.body.policeNo;

    const directoryPath = __basedir + `/bills/${policeNo}/`;
    fs.readdir(directoryPath, function (err, files) {
        if (err) {
            res.status(500).send({
                message: "Unable to scan files!",
            });
        }
        let fileInfos = [];
        files.forEach((file) => {
            fileInfos.push({
                name: file,
                policeNo: policeNo,
                url: AppConstants.BASE_URL + 'bills/' + policeNo + "/" + file,
            });
        });
        res.status(200).send(fileInfos[0]);
    });
};