-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload.js
150 lines (138 loc) · 4.12 KB
/
upload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const express = require('express');
const fs = require('fs');
const ogr2ogr = require('ogr2ogr');
const path = require('path');
const { exec } = require('child_process');
const fileUpload = require('express-fileupload');
module.exports = function(app, pool) {
if (!app) {
return;
}
app.use(fileUpload({
useTempFiles: true,
tempFileDir : `${__dirname}/temp/`
}));
function rmr(dirName) {
return new Promise((resolve, reject)=>{
exec(`rm -r "${dirName}"`, (err, stdout, stderr)=>{
if (err) {
reject(err);
return;
}
resolve();
})
})
}
async function unArchiveFile(fileName) {
let parsedPath = path.parse(fileName);
let tempDir = `${__dirname}/temp/${parsedPath.base}`;
try {
await rmr(tempDir)
} catch (err) {
// ignore
}
return new Promise((resolve, reject)=>{
exec (`unzip -d "${tempDir}" "${fileName}"`, (err, stdout, stderr)=>{
if (err) {
reject('failed to execute unzip');
return;
}
console.log(`${stdout}`);
console.log(`${stderr}`);
try {
fs.unlinkSync(fileName);
fs.renameSync(tempDir, fileName);
} catch (err) {
reject(`failed to mv zip files to directory: ${err.message}`);
return;
}
resolve()
})
})
}
app.post('/admin/upload', async (req, res) => {
let uploadFile = req.files.uploadfile;
const fileName = uploadFile.name;
if (!fileName || fileName === "" ) {
return res.json({file: "none"});
}
let dest = `${__dirname}/admin/files/${fileName}`;
try {
await (rmr(dest));
} catch (err) {
// ignore
}
uploadFile.mv(
dest,
async function (err) {
if (err) {
return res.status(500).send(err.message);
}
try {
await unArchiveFile(`${__dirname}/admin/files/${fileName}`);
} catch (err) {
// ignore
}
res.json({
file: `${fileName}`
})
}
)
});
app.get('/admin/upload', (req, res) =>{
url = req.query.fetch;
console.log(url);
res.json({
file: 'index.html'
});
})
app.delete('/admin/upload', express.json({type: '*/*'}), (req, res) => {
fs.unlinkSync(`${__dirname}/admin/files/${req.body.file}`);
res.json({
file: 'done'
});
});
function listFiles(dirname) {
let files = fs.readdirSync(dirname);
files = files.map(file=>{
let stat = fs.statSync(path.join(dirname, file));
let result = {
name: file,
size: stat.size,
file: !!(stat.mode & 0100000),
dir: !!(stat.mode & 0040000),
ctime: stat.ctime,
mtime: stat.mtime,
atime: stat.atime,
permissions: `${(stat.mode & 0040000)?'d':(stat.mode & 0100000)?'f':'-'}${stat.mode & 400?'r':'-'}${stat.mode & 200?'w':'-'}${stat.mode & 100?'x':'-'}${stat.mode & 40?'r':'-'}${stat.mode & 20?'w':'-'}${stat.mode&10?'x':'-'}${stat.mode&4?'r':'-'}${stat.mode&2?'w':'-'}${stat.mode&1?'x':'-'}`,
nlink: stat.nlink,
uid: stat.uid,
gid: stat.gid
}
if (result.dir) {
result.files = listFiles(path.join(dirname, result.name));
}
return result;
})
return files;
}
app.get('/admin/list', (req, res)=>{
let files = listFiles(`${__dirname}/admin/files`);
res.json(files);
})
app.get('/admin/import', (req, res)=>{
let tablename = path.parse(req.query.file).name.toLowerCase();
tablename = tablename.replace(/\./g, '_').replace(/ /g, '_');
ogr2ogr(`${__dirname}/admin/files/${req.query.file}`)
.format('PostgreSQL')
.destination(`PG:host=${pool.$cn.host} user=${pool.$cn.user} dbname=${pool.$cn.database} password=${pool.$cn.password} port=${pool.$cn.port?pool.$cn.port:5432}`)
.options(['-nlt', 'PROMOTE_TO_MULTI', '-overwrite', '-lco', 'GEOMETRY_NAME=geom', '-nln', tablename])
.exec((err, data) => {
if (err) {
res.json({error: err.message});
return;
}
res.json({result: "ok"});
})
})
}