-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
394 lines (335 loc) · 12 KB
/
main.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// main.js
// Modules to control application life and create native browser window
const { Menu,app, BrowserWindow } = require('electron')
const path = require('path')
// ################################################################################
Menu.setApplicationMenu(null)
const express = require('express')
const multer = require('multer')
const fs = require('fs')
const cors = require('cors');
const getIp=require('./API/IP');
const getPort=require('./API/Port')
const getcpolarURL=require('./API/cpolarURL')
var ip=getIp
var port=getPort
var cpolarURL=getcpolarURL
console.log(port);
console.log(ip);
console.log(cpolarURL);
// const moment = require('moment')
// console.log(getIp);
var app_express = express()
let filePath_All = path.join(__dirname,'/uploadFiles/')
app_express.use(cors());
app_express.use(express.urlencoded({extended:false}))
// app.use(helmet())
var upload = multer({ dest: filePath_All ,
fileFilter(req, file, callback) {
// 解决中文名乱码的问题
file.originalname = Buffer.from(file.originalname, "latin1").toString(
"utf8"
);
callback(null, true);
},
})
// 基于express框架的node服务器
// app.use('/', function (req, res) {
// var fileName = "./client/index.html";
// fs.readFile(fileName, function (err, data) {
// if (err)
// console.log("对不起,您所访问的路径出错");
// else {
// res.write(data);
// }
// })
// app.use(express.static('./client'))
// })
// 允许所有类型的文件传递过来
// app.use(upload.any())
app_express.post('/file', upload.any(), function (req, res) {
let filePath = path.join(__dirname, '/uploadFiles')
var file_Info="什么都没有"
// var extendName = originalname.slice(originalname.lastIndexOf('.'))
// var fileName = originalname.slice(0, originalname.lastIndexOf('.'))
// console.log(extendName)
// console.log(fileName);
// console.log( originalname);s
// console.log(req.files[0].originalname);
// console.log(req.files);
// console.log(newName);
// console.log(req.files[0]);
//todo
//promise 解决异步赋值问题
for (let i = 0; i <req.files.length; i++) {
fs.exists(`${filePath}/${req.files[i].originalname}`, (exists) => {
if (exists) {
console.log("文件存在")
fs.rename(req.files[i].path,`${filePath}/${req.files[i].originalname}`,function(err){
if (err) {
console.log(`err:${err}`)
}else{
console.log("文件存在,已重覆盖")
}
})
file_Info=`{"msg":"文件存在,已重覆盖","status":1}`
}
else {
console.log("文件不存在")
fs.rename(req.files[i].path,`${filePath}/${req.files[i].originalname}`,function(err){
if (err) {
console.log(`err:${err}`)
}else{
console.log("文件上传成功")
}
})
file_Info=`{"msg":"文件上传成功","status":0}`
}
})
}
setTimeout(()=>{
res.send(file_Info)
},100)
/* function exists_Promise(){
var p = new Promise(function (resolve, reject) {
//做一些异步操作
if (req.files) {
let filePath = path.join(__dirname, '/uploadFiles')
let originalName = req.files[0].originalname
let newName = `${filePath}/${originalName}`
let fileObj = new Object()
fileObj.path = req.files[0].path
fileObj.filePath = filePath
fileObj.originalName = originalName
fileObj.newName = newName
fileObj.status = 1 //已存在
resolve(req)
}
else {
reject("没有任何文件传过来")
}
})
return p
}
function rename_Promise_1(){
var p = new Promise((resolve, reject) => {
if (req.files) {
let filePath = path.join(__dirname, '/uploadFiles')
let originalName = req.files[0].originalname
let newName = `${filePath}/${originalName}`
let fileObj = new Object()
fileObj.path = req.files[0].path
fileObj.filePath = filePath
fileObj.originalName = originalName
fileObj.newName = newName
fileObj.status = 1 //已存在
resolve(req)
}
else{
reject("没有任何文件传过来")
}
})
return p
}
function rename_Promise_2(){
var p = new Promise((resolve, reject) => {
if (req.files) {
let filePath = path.join(__dirname, '/uploadFiles')
let originalName = req.files[0].originalname
let newName = `${filePath}/${originalName}`
let fileObj = new Object()
fileObj.path = req.files[0].path
fileObj.filePath = filePath
fileObj.originalName = originalName
fileObj.newName = newName
fileObj.status = 0 //已存在
resolve(req)
}
else{
reject("没有任何文件传过来")
}
})
return p
}
exists_Promise()
.then((req) => {
console.log(req.files);
let filePath = path.join(__dirname, '/uploadFiles')
let originalName = req.files[0].originalname
let newName = `${filePath}/${originalName}`
let fileObj = new Object()
fileObj.path = req.files[0].path
fileObj.filePath = filePath
fileObj.originalName = originalName
fileObj.newName = newName
fileObj.status = 0 //不存在
fs.exists(`${fileObj.filePath}/${fileObj.originalName}`, (exists) => {
if (exists) {
console.log("文件存在");
return rename_Promise_1()
}
else {
console.log("文件不存在");
return rename_Promise_2()
}
})
})
.then((req) => {
console.log(`req:${req}`);
let filePath = path.join(__dirname, '/uploadFiles')
let originalName = req.files[0].originalname
let newName = `${filePath}/${originalName}`
let fileObj = new Object()
fileObj.path = req.files[0].path
fileObj.filePath = filePath
fileObj.originalName = originalName
fileObj.newName = newName
fileObj.status = 0 //不存在
if (fileObj) {
fs.rename(fileObj.path, fileObj.newName, function (err) {
if (err) {
res.send('重命名失败!')
console.log('重命名失败!');
} else {
res.send(`{status:2}`)
}
})
}
else {
fs.rename(fileObj.path, fileObj.newName, function (err) {
if (err) {
res.send('重命名失败!')
console.log('重命名失败!');
} else {
res.send('{status:1}')
}
})
}
})
.catch((err) => {
console.log(err);
})
*/
// for (let i = 0; i <= req.files.length - 1; i++) {
// // console.log(req.files[0].originalname);
// fs.exists(`${filePath}/${originalName}`, (exists) => {
// if (exists) {
// fs.rename(req.files[i].path, newName, function (err) {
// if (err) {
// res.send('失败!')
// } else {
// res.send(`{status:2}`)
// }
// })
// }
// else {
// for (let i = 0; i <= req.files.lentgh - 1; i++) {
// fs.rename(req.files[0].path, newName, function (err) {
// if (err) {
// res.send('失败!')
// } else {
// res.send('{status:1}')
// }
// })
// }
// }
// })
// }
})
app_express.post('/fileInfo', function (req, res) {
// var fileNames=fs.readdir('./uploadFiles')
// console.log(fileNames);
let filePath = path.join(__dirname,'/uploadFiles')
fs.readdir(filePath, (err, files) => {
if (err) {
return err
}
else {
// console.log(files);
var i=0
var filesLength=files.length
// console.log(filesLength);
res.send(files)
}
})
})
app_express.get('/getIp',function(req,res){
res.send(getIp)
})
app_express.get('/getPort',function(req,res){
res.send(`${port}`)
})
app_express.get('/getcpolarURL',function(req,res){
res.send(`${cpolarURL}`)
})
app_express.get('/getText', (req, res) => {
let str;
let filePath = path.join(__dirname,'/textArea.txt')
console.log(filePath);
fs.readFile(filePath, { encoding: "utf-8" }, function (err, fr) {
//readFile回调函数
if (err) {
console.log(err);
} else {
str = fr;
res.send(`${str}`)
}
});
})
app_express.post('/changeText',(req,res)=>{
let changeText = req.body
filePath = path.join(__dirname,'/textArea.txt')
fs.writeFile(filePath,changeText.input,(err)=>{
if(err){
return err
}
else{
res.send('保存成功')
}
})
})
app_express.use(express.static(path.join(__dirname,'/client')))
app_express.use(express.static(path.join(__dirname,'/uploadFiles')))
// 监听端口
app_express.listen(port, function () {
console.log(`${port} 文件系统服务启动中~~`)
});
// ################################################################################
const createWindow = () => {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 400,
height: 526,
// frame:false,
icon:path.join(__dirname,"/favicon.ico"),
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration:true,
contextIsolation: false
}
})
// 加载 index.html
mainWindow.loadFile('./mainIndex.html')
// 打开开发工具
// mainWindow.webContents.openDevTools()
}
// 这段程序将会在 Electron 结束初始化
// 和创建浏览器窗口的时候调用
// 部分 API 在 ready 事件触发后才能使用。
app.whenReady().then(() => {
createWindow()
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// Object
// 除了 macOS 外,当所有窗口都被关闭的时候退出程序。 There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})
// In this file you can include the rest of your app's specific main process
// code. 也可以拆分成几个文件,然后用 require 导入。