forked from apxtri/apxtrib
50 lines
1.6 KiB
JavaScript
Executable File
50 lines
1.6 KiB
JavaScript
Executable File
// Upload de file
|
|
const express = require( 'express' );
|
|
const path = require( 'path' );
|
|
const jsonfile = require( 'jsonfile' );
|
|
const fs = require( 'fs' );
|
|
|
|
// Classes
|
|
const UploadFile = require( '../models/UploadFiles' );
|
|
// Middlewares
|
|
const checkHeaders = require( '../middlewares/checkHeaders' );
|
|
const isAuthenticated = require( '../middlewares/isAuthenticated' );
|
|
const router = express.Router();
|
|
|
|
router.post( '/', checkHeaders, ( req, res ) => {
|
|
console.log( 'route uploadFile post ' );
|
|
const saveFile = UploadFile.add( req, req.session.header );
|
|
res.send( saveFile );
|
|
// res.send({ status: 200, payload: { info: 'fine' } });
|
|
} );
|
|
|
|
router.post( '/json', checkHeaders, ( req, res ) => {
|
|
console.log( 'route uploadFile post de fichier json ' );
|
|
// a callback can be pass to req.body to run a specific process after upload
|
|
const saveFile = UploadFile.addjson( req.body, req.session.header );
|
|
console.log( saveFile );
|
|
res.send( saveFile );
|
|
// res.send({ status: 200, payload: { info: 'fine' } });
|
|
} );
|
|
|
|
router.get( '/:filename', checkHeaders, isAuthenticated, ( req, res ) => {
|
|
console.log( 'route uploadFile get ', req.params.filename );
|
|
const pushFile = UploadFile.get(
|
|
req.params.filename.replace( /______/g, '/' ),
|
|
req.session.header
|
|
);
|
|
if( pushFile.status == 200 ) {
|
|
if( path.extname( pushFile.payload.file ) === '.json' ) {
|
|
jsonfile.readFile( pushFile.payload.file, ( err, p ) => {
|
|
if( err ) console.error( err );
|
|
res.jsonp( p );
|
|
} );
|
|
} else {
|
|
res.download( pushFile.payload.file, path.basename( pushFile.payload.file ) );
|
|
}
|
|
} else {
|
|
res.send( pushFile );
|
|
}
|
|
} );
|
|
module.exports = router;
|