-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileupload.js
125 lines (113 loc) · 3.41 KB
/
fileupload.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
// 파일 업데이트 js 관련파일
//app.js
app.use("/uploads/project", express.static(__dirname + "/uploads/project"));
//project.js router
const { createProject } = require("../controllers/projectController");
const upload = require("../middlewares/upload");
router.post("/write", upload("uploads/project"), createProject);
//projectController.js
// 컨트롤러: 글쓰기
const createProject = async (req, res) => {
let connection;
try {
connection = await getConnection();
const {
projectName,
projectCompany,
projectIntro,
projectCategory,
projectUrl,
projectColor1,
projectColor2,
projectColor3,
projectColorM,
projectStart,
projectEnd,
projectOrder,
} = req.body;
const projectFile1 =
req.fileData && req.fileData.projectFile1
? req.fileData.projectFile1
: null;
const projectFile2 =
req.fileData && req.fileData.projectFile2
? req.fileData.projectFile2
: null;
const projectFile3 =
req.fileData && req.fileData.projectFile3
? req.fileData.projectFile3
: null;
const projectFile4 =
req.fileData && req.fileData.projectFile4
? req.fileData.projectFile4
: null;
const projectFile5 =
req.fileData && req.fileData.projectFile5
? req.fileData.projectFile5
: null;
const query = `
INSERT INTO project (
projectName, projectCompany, projectIntro, projectCategory,
projectStart, projectEnd, projectUrl, projectColor1,
projectColor2, projectColor3, projectColorM,
projectFile1, projectFile2, projectFile3, projectFile4, projectFile5, projectOrder
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`;
const values = [
projectName,
projectCompany,
projectIntro,
projectCategory,
formatDate(projectStart),
formatDate(projectEnd),
projectUrl,
projectColor1,
projectColor2,
projectColor3,
projectColorM,
projectFile1 ? projectFile1.filename : null,
projectFile2 ? projectFile2.filename : null,
projectFile3 ? projectFile3.filename : null,
projectFile4 ? projectFile4.filename : null,
projectFile5 ? projectFile5.filename : null,
projectOrder,
];
const result = await connection.query(query, values);
await connection.commit();
const insertedId = result[0].insertId;
res.status(200).json({ insertedId: insertedId });
console.log("Data saved to database - project");
} catch (error) {
await connection.rollback();
handleDatabaseError(error);
res.sendStatus(500);
} finally {
if (connection) {
connection.release();
}
}
};
//upload.js
const multer = require("multer");
const path = require("path");
const upload = (destination) => {
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, destination);
},
filename: (req, file, cb) => {
const uniqueSuffix = Date.now() + "-" + Math.round(Math.random() * 1e9);
const ext = path.extname(file.originalname);
const filename = file.fieldname + "-" + uniqueSuffix + ext;
req.fileData = req.fileData || {};
req.fileData[file.fieldname] = {
originalname: file.originalname,
filename: filename,
};
cb(null, filename);
},
});
return multer({ storage }).any();
};
module.exports = upload;