const path = require('path');
const router = require('express').Router();
const fs = require('fs');


router.get("/", async function(req, res) {
    const filename = req.query.filename;
    console.log(filename);
    const filePath = path.join('uploads', filename);
    console.log(filePath);

    if (filename) {
        fs.access(filePath, fs.constants.F_OK, (err) => {
            if (err) {
                console.error('檔案不存在:', err);
                res.status(404).send('檔案不存在');
            } else {
                res.download(filePath, (err) => {
                    if (err) {
                        console.error('檔案下載失敗:', err);
                        res.status(500).send('檔案下載失敗');
                    } else {
                        console.log("downloaded");
                    }
                });
            }
        });
    } else {
        res.status(400).send('缺少 filename 參數');
    }


});
module.exports = router;
