server.js
3.95 KB
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
151
152
153
154
155
156
157
158
159
160
"use strict";
const express = require('express');
const multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
var dir = req.params.directoryName;
cb(null, './tensorflow/data/' + dir + '/');
},
filename: function (req, file, cb) {
cb(null, new Date().valueOf() + "_" + file.originalname);
}
});
var upload = multer({
storage: storage
});
const bodyParser = require('body-parser');
const fs = require('fs');
const path = require('path');
const PORT = 8080;
const HOST = '0.0.0.0';
const app = express();
const dataFolder = './tensorflow/data';
const testFolder = './tensorflow/test';
app.set('view engine', 'pug');
app.set('views', './views');
app.locals.pretty = true
app.use(bodyParser.urlencoded({extended:false}));
// Redirect Root to Home
app.get('/', (req, res) => {
res.redirect('./home/');
});
// Main Page
app.get('/home/', (req, res) => {
fs.readdir(dataFolder, function(error, filelist){
if(error)
console.log(error);
res.render('home', {fileList:filelist});
});
});
// Directory existence checking
app.post('/directory_check', (req, res) => {
var dir = req.body.directoryName;
// Directory exists
if(fs.existsSync(dataFolder + '/' + dir))
{
// Go back page
res.render('error_directoryAdd');
}
// Directory doesn't exist
else
{
// Make directory
fs.mkdirSync(dataFolder + '/' + dir);
console.log('Directory Create: ' + dir);
res.redirect('/home/' + dir + '/');
}
});
// Basic Directory Page
app.get('/home/:directoryName/', (req, res) => {
// 아래 주석 처리된 코드는 화면에서 이미지를 보여주기 위한 코드
// 그러나 client에게 이미지를 보여주기 위해서는 Amazon S3를 이용해야 함
// 아직 Amazon S3 기능을 추가하지 못함
// var directoryName = new String(req.params.directoryName);
// var ImageList = new Array();
// var Path = dataFolder + '/' + directoryName;
// fs.readdirSync(Path).forEach(function(file,index){
// var fileType = path.extname(file);
// if(fileType == ".jpg" || fileType == ".jpeg") {
// ImageList.push("." + Path + "/" + file);
// }
// });
// res.render('directory', {FileList:ImageList});
var directoryName = req.params.directoryName;
res.render('directory', {directoryName:directoryName});
});
// Image Upload Directory Page
app.post('/home/:directoryName/upload/', upload.array('userImage'), (req, res) => {
var directoryName = req.params.directoryName;
var imgFileArr = req.files;
console.log("files: " + req.files);
res.redirect('/home/' + directoryName + '/');
});
// Modify Directory name
app.get('/home/:directoryName/modify/', (req, res) => {
// exist query.newName
var directoryName = req.params.directoryName;
var newName = req.query.newName;
if (req.query.newName) {
// modify Directory name
var Path = dataFolder + '/' + directoryName;
fs.rename(Path, dataFolder + '/' + newName, function (err) {
if (err) {
console.log("Directory Rename error: " + err);
} else {
console.log("Directory Rename: " + directoryName + " -> " + newName);
}
});
res.redirect('/');
}
else {
res.render('directoryModifyCheck', {directoryName:JSON.stringify(directoryName)});
}
});
// Delete Directory
app.get('/home/:directoryName/delete/', (req, res) => {
// exist query.real
var directoryName = req.params.directoryName;
if (req.query.real) {
// Remove Directory and Files
var path = dataFolder + '/' + directoryName;
fs.readdirSync(path).forEach(function(file,index){
var curPath = path + "/" + file;
fs.unlinkSync(curPath);
});
fs.rmdirSync(path);
console.log('Directory Delete: ' + dir);
res.redirect('/');
}
else {
res.render('directoryDeleteCheck', {directoryName:JSON.stringify(directoryName)});
}
});
// Image Test Page
app.post('/test', (req, res) => {
var upload_test = multer({ dest: 'tensorflow/test/' });
upload_test.single('ImageTest')
var imgTest = req.file;
});
app.listen(PORT, HOST);
console.log('Running on http://${HOST}:${POST}');