-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileopt.js
124 lines (116 loc) · 3.13 KB
/
fileopt.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
var fs = require('fs');
var path = require('path');
var multiparty = require('multiparty');
//列目录
function dirList(dirpath, response) {
if (dirpath.indexOf("..") > 0) {
return;
}
realpath = path.join("./public", dirpath);
fs.readdir(realpath, function(err, files) {
if (err) {
throw err
};
var resultstr = "[";
files.forEach(function(file) {
var filepath = path.join(realpath, file);
state = fs.statSync(filepath);
resultstr += "{";
resultstr += "'dir':'" + dirpath + "',";
if (state.isDirectory()) {
resultstr += "'isdir':'" + "true" + "',";
} else {
resultstr += "'isdir':'" + "false" + "',";
}
resultstr += "'filename':'" + file + "'";
resultstr += "},";
})
resultstr += "]";
console.log(resultstr);
response.end(resultstr);
});
}
//创建文件
exports.createFile = function(dirpath, filename, response) {
if (dirpath.indexOf("..") > 0) {
return;
}
realpath = path.join("./public", dirpath);
var fullpath = path.join(realpath, filename);
fs.exists(fullpath, function(exists) {
if (!exists) {
fs.openSync(fullpath, "ax", 0644);
};
dirList(dirpath, response);
});
}
//删除文件或者文件夹
exports.deleteDir = function(dirpath, dirname, response) {
if (dirpath.indexOf("..") > 0) {
return;
}
realpath = path.join("./public", dirpath);
//console.log(realpath);
//console.log(dirpath);
recursionRemove = function(removepath) { //递归删除
var state = fs.statSync(removepath);
if (state.isDirectory()) { //目录
var array = fs.readdirSync(removepath);
for (var i = 0; i < array.length; i++) {
var subpath = path.join(removepath, array[i]);
recursionRemove(subpath);
}
fs.rmdirSync(removepath);
} else if (state.isFile()) { //文件
fs.unlinkSync(removepath);
}
};
var fullpath = path.join(realpath, dirname);
recursionRemove(fullpath);
/* console.log(realpath);
console.log(dirpath);
console.log(fullpath);*/
dirList(dirpath, response);
}
//文件改名
exports.renameFile = function(dirpath, oldfilename, newfilename, response) {
if (dirpath.indexOf("..") > 0) {
return;
}
realpath = path.join("./public", dirpath);
console.log(realpath);
var oldpath = path.join(realpath, oldfilename);
var newpath = path.join(realpath, newfilename);
console.log(oldpath);
console.log(newpath);
fs.exists(oldpath, function(exists) {
if (exists) {
fs.renameSync(oldpath, newpath);
};
dirList(dirpath, response);
});
}
//文件上传
exports.uploadFile = function(dirpath, files, response) {
if (dirpath.indexOf("..") > 0) {
return;
}
realpath = path.join("./public", dirpath);
if (files.upload != "undefined") {
var tempPath = files.upload[0].path; //临时文件路径
var filename = files.upload[0].originalFilename; //文件名
var filepath = path.join(realpath, filename); //存储路径
fs.exists(filepath, function(exists) {
if (!exists) {
fs.renameSync(tempPath, filepath); //把临时文件复制过来
fs.exists(tempPath, function(exists) {
if (exists) {
fs.unlinkSync(tempPath);
};
});
};
dirList(dirpath, response);
});
}
}
exports.dirList = dirList;