const path=require(‘path‘)
const fs=require(‘fs‘)
const express=require(‘express‘)
const cors=require(‘cors‘)
const multer=require(‘multer‘)
const app=express()
const urlencode=require(‘urlencode‘)
const os=require(‘os‘)
app.use(cors())
function getIPAdress() {
var interfaces = os.networkInterfaces();
for (var devName in interfaces) {
var iface = interfaces[devName];
for (var i = 0; i < iface.length; i++) {
var alias = iface[i];
if (alias.family === ‘IPv4‘ && alias.address !== ‘127.0.0.1‘ && !alias.internal) {
return alias.address;
}
}
}
}
const myHost = getIPAdress();
const PORT=9999
const host=`${myHost}:${PORT}`
const pathfile=path.resolve(__dirname,‘..‘,‘files‘)
const pathphoto=path.resolve(__dirname,‘..‘,‘photo‘)
const video=path.resolve(__dirname,‘..‘,‘videos‘)
const movie=path.resolve(__dirname,‘..‘,‘videos‘)
var storage=multer.diskStorage({
destination:function(req,file,cb){
cb(null,pathphoto)
},
filename:function(req,file,cb){
let exts=file.originalname
cb(null,exts)
}
})
var shiping=multer.diskStorage({
destination:function(req,file,cb){
cb(null,movie)
},
filename:function(req,file,cb){
let exts=file.originalname
cb(null,exts)
}
})
var files=multer.diskStorage({
destination:function(req,file,cb){
cb(null,pathfile)
},
filename:function(req,file,cb){
let ex=file.originalname
cb(null,ex)
}
})
var uploadfile=multer({
storage:files
})
var uploadvideo=multer({
storage:shiping
})
var upload=multer({
storage:storage
})
app.use(‘/videos‘,express.static(path.resolve(video)))
app.get(‘/videos‘,(req,res)=>{
fs.readdir(video,(err,files)=>{
const list=files.map((file)=>{
return `http://${host}/videos/${file}`
})
res.send ({
status:200,
msg:"ok",
data:list
})
})
})
app.post(‘/img‘,upload.single(‘img‘),(req,res)=>{
res.send(‘ok‘)
})
app.post(‘/movie‘,uploadvideo.single(‘video‘),(req,res)=>{
res.send(‘ok‘)
})
app.post(‘/files‘,uploadfile.single(‘file‘),(req,res)=>{
res.send(‘ok‘)
})
app.get(‘/filelist‘,(req,res)=>{
fs.readdir(pathfile,(err,data)=>{
if(err){
console.log(err);
}else{
const filemessgae=data.map((file)=>{
return{
name:file,
size: (fs.statSync(path.join(pathfile,file)).size/1024) <1024 ? (fs.statSync(path.join(pathfile,file)).size/1024).toFixed(2) + ‘KB‘
:(fs.statSync(path.join(pathfile,file)).size/1048576).toFixed(2) + ‘MB‘
}
})
res.send({
data:filemessgae
})
}
})
})
app.get(‘/downloadFile/:filename/:realname‘,(req,res)=>{
var filename=req.params.filename;
var realname=req.params.realname
// var path="public/pdf/5ea56710fb5a2fdad7cabcf3.pdf";
var pathd=path.join(pathfile,filename)
var fileStream = fs.createReadStream(pathd);
realname=urlencode(realname,"GBK");
realname=realname.toString(‘iso8859-1‘)
res.setHeader(‘Content-type‘, ‘application/octet-stream‘);
// res.setHeader(‘Content-Disposition‘, ‘attachment;filename=1.txt‘);
res.setHeader(‘Content-Disposition‘, `attachment;filename=${realname}`);
fileStream.on(‘data‘, function (data) {
res.write(data, ‘binary‘);
});
fileStream.on(‘end‘, function () {
res.end();
console.log(‘The file has been downloaded successfully!‘);
});
})
app.use(‘/photolist‘,express.static(path.resolve(pathphoto)))
app.get(‘/photolist‘,(req,res)=>{
fs.readdir(pathphoto,(err,data)=>{
const photomessgae=data.map((file)=>{
return{
name:file,
address:`http://${host}/photolist/${file}`,
size: (fs.statSync(path.join(pathphoto,file)).size/1024) <1024 ? (fs.statSync(path.join(pathphoto,file)).size/1024).toFixed(2) + ‘KB‘
:(fs.statSync(path.join(pathphoto,file)).size/1048576).toFixed(2) + ‘MB‘
}
})
res.send({
data:photomessgae
})
})
})
app.get(‘/downloadimg/:filename/:realname‘,(req,res)=>{
var filename=req.params.filename;
var realname=req.params.realname
// var path="public/pdf/5ea56710fb5a2fdad7cabcf3.pdf";
console.log(pathphoto,filename);
var pathimg=path.join(pathphoto,filename)
var fileStream = fs.createReadStream(pathimg);
realname=urlencode(realname,"GBK");
realname=realname.toString(‘iso8859-1‘)
res.setHeader(‘Content-type‘, ‘application/octet-stream‘);
// res.setHeader(‘Content-Disposition‘, ‘attachment;filename=1.svg‘);
res.setHeader(‘Content-Disposition‘, `attachment;filename=${realname}`);
fileStream.on(‘data‘, function (data) {
res.write(data, ‘binary‘);
});
fileStream.on(‘end‘, function () {
res.end();
console.log(‘The file has been downloaded successfully!‘);
});
})
app.listen(PORT,()=>console.log(‘server run at 9999‘))