server.js 3.95 KB
"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}');