|
|
|
@ -0,0 +1,92 @@
|
|
|
|
|
var mysql = require('mysql');
|
|
|
|
|
var express = require('express');
|
|
|
|
|
var cors = require('cors');
|
|
|
|
|
var app = express();
|
|
|
|
|
var bodyParser = require('body-parser');
|
|
|
|
|
const crypto = require('crypto');
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
const path = require('path');
|
|
|
|
|
const fileUpload = require('express-fileupload');
|
|
|
|
|
const session = require('express-session');
|
|
|
|
|
const multer = require('multer');
|
|
|
|
|
|
|
|
|
|
var connection = mysql.createConnection({
|
|
|
|
|
host : 'localhost',
|
|
|
|
|
port : '13307',
|
|
|
|
|
user : 'root',
|
|
|
|
|
password : '123456',
|
|
|
|
|
database : 'mailbox'
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.use(cors())
|
|
|
|
|
|
|
|
|
|
//上传并识别功能
|
|
|
|
|
const upload = multer({ dest: 'uploads/' }); // 上传文件的临时存储目录 // Multer是一个Node.js中间件,用于处理multipart/form-data类型的表单数据
|
|
|
|
|
app.post('/api/upload', upload.single('image'), function(req, res) {// 单文件上传
|
|
|
|
|
if (!req.file) {
|
|
|
|
|
return res.status(400).send('No file uploaded.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const file = req.file;
|
|
|
|
|
const tempFilePath = file.path;
|
|
|
|
|
|
|
|
|
|
const hash = crypto.createHash('sha256');
|
|
|
|
|
const input = fs.createReadStream(tempFilePath);
|
|
|
|
|
|
|
|
|
|
input.on('data', (data) => {
|
|
|
|
|
hash.update(data);
|
|
|
|
|
});
|
|
|
|
|
input.on('end', () => {
|
|
|
|
|
const fileHash = hash.digest('hex');
|
|
|
|
|
const fileName = file.originalname;
|
|
|
|
|
const filePath = path.join(__dirname, 'uploads', fileName);
|
|
|
|
|
const user_name = req.query.user_name
|
|
|
|
|
// Check if file with same hash already exists in database
|
|
|
|
|
const sqlCheck = 'SELECT * FROM images WHERE hash = ?';
|
|
|
|
|
connection.query(sqlCheck, [fileHash], function(err, result) {
|
|
|
|
|
if (err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
return res.status(500).send('Error checking file in database');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result.length > 0) {
|
|
|
|
|
// If file with same hash exists, return "has been uploaded"
|
|
|
|
|
fs.unlinkSync(tempFilePath); // Delete the uploaded file
|
|
|
|
|
return res.json({ message: 'File with same hash already uploaded', fileName: '识别成功,结果为:' + result[0].goods});//如果文件已经存在,则返回这个文件的对应的匹配结果
|
|
|
|
|
} else {
|
|
|
|
|
fs.rename(tempFilePath, filePath, function(err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
return res.status(500).send('Error uploading file');
|
|
|
|
|
} else {
|
|
|
|
|
const sql = 'INSERT INTO images (name, path, hash, username) VALUES (?, ?, ?, ?)';
|
|
|
|
|
connection.query(sql, [fileName, filePath, fileHash, user_name], function(err, result) {
|
|
|
|
|
if (err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
return res.status(500).send('Error uploading file to database');
|
|
|
|
|
}
|
|
|
|
|
res.json({ message: 'File uploaded successfully', fileName: "识别失败,请重传文件:" + fileName });
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var server = app.listen(8087, '0.0.0.0', function () {
|
|
|
|
|
|
|
|
|
|
var host = server.address().address
|
|
|
|
|
var port = server.address().port
|
|
|
|
|
|
|
|
|
|
connection.connect(function(err){
|
|
|
|
|
if(err){
|
|
|
|
|
console.log('[query] - :'+err);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
console.log('[connection connect] succeed!');
|
|
|
|
|
});
|
|
|
|
|
console.log("应用实例,访问地址为 http://%s:%s", host, port)
|
|
|
|
|
})
|