????

Your IP : 18.218.199.14


Current Path : C:/inetpub/vhost/sdoc.gdtsolutions.vn/package/app/routes/
Upload File :
Current File : C:/inetpub/vhost/sdoc.gdtsolutions.vn/package/app/routes/kyso.js

var router = require('express').Router();
const path = require('path');
const fs = require('fs');
var docCtrl = require('../controllers/tailieu/vanban').create();
const { savePath, scanPath } = require('../appconfig');
var upload = require('multer')({ dest: scanPath });

router.get('/:docId', async function (req, res, _next) {
  try {
    let doc = (await docCtrl.select('SELECT Document.UUID,[File].FileCode,Document.DocCode FROM Document INNER JOIN [File] ON Document.FileId = [File].UUID WHERE [File].[State] = 1 AND Document.UUID = @docId', { docId: req.params.docId }))[0];
    if (!doc) throw new Error('Không tồn tại văn bản trong cơ sở dữ liệu');
    let pdfPath = path.join(savePath, doc['FileCode'], doc['DocCode'] + '.pdf');
    if (!fs.existsSync(pdfPath)) throw new Error('Văn bản chưa được số hóa');
    res.setHeader('Content-Type', 'application/pdf');
    res.download(pdfPath);
  } catch (err) { console.log(err); res.status(400).send(err.message) }
});
router.post('/', upload.single('uploadfile'), function (req, res, _next) {
  let json = {
    Status: true,
    Message: '',
    FileName: '',
    FileServer: ''
  };
  docCtrl.signDocument(req.body.docId, req.file).then(function () {
    json['FileName'] = req.file.originalname;
    // json['FileServer'] = `${req.protocol}//${req.host}/sohoa/vanban/${result['UUID']}?download=true`;
    res.send(JSON.stringify(json))
  }).catch(function (err) {
    json['Status'] = false;
    json['Message'] = err.message;
    res.status(400).send(JSON.stringify(json))
  })
});

module.exports = router;