Nodejs 学习笔记-相片整理Demo(二)
好吧,开始代码!
确认一下需要哪些模块,由于只需要进行文件复制,那么基本用fs和util就行了。那就先用这两个试试,不够再说。
接下来需要定义一些要用到的变量,具体是什么,看注释吧。
1 var fs = require('fs'), 2 util = require('util'), 3 fromPath = 'G:\\相片备份\\相片\\日常', //基础目录 4 toPath = 'L:\\相片备份\\相片\\日常',//备份目录 5 copy = {}, //命名空间 6 fileCount = 0, //总数 7 dirCount = 0, //文件夹数量 8 startTime, //开始时间 9 copyFormat = ['png', 'jpg', 'gif', 'tif', '3gp', 'mpg', 'thm', 'mov', 'mp4', 'vob', 'ifo', 'bup', 'avi', 'bmp', 'zip', 'rar'],//复制的格式 10 pathSpliter = '\\', //路径分隔符 11 totalCount = 0, //总数 12 copyCount = 0, //复制数量 13 uncopyCount = 0;//没有复制的数量
定义好变量和引用的模块,下面开始写功能函数。
1.要检测一下两个路径是否存在,如果要复制到的路径不存在,就要创建一下。
1 //检测路径 2 copy.checkPath = function (path, callback) { 3 fs.exists(path, 4 function (exist) { 5 if (exist) { 6 console.log('路径存在!-' + path); 7 } else { 8 console.log('路径不存在!-' + path); 9 } 10 if (callback) { 11 callback(exist); 12 } 13 }); 14 }; 15 //检测源路径 16 copy.checkFromPath = function () { 17 startTime = new Date(); 18 copy.checkPath(fromPath, 19 function (exist) { 20 if (exist) { 21 copy.checkToPath(); 22 } else { 23 console.log('复制路径不存在,检查一下。'); 24 } 25 }); 26 }; 27 //创建路径 28 copy.createPath = function (path, callback) { 29 fs.mkdir(path, '0777', 30 function (err, result) { 31 //console.log('创建路径-' + path); 32 callback(); 33 }); 34 }; 35 //复制路径 36 copy.checkToPath = function () { 37 copy.checkPath(toPath, 38 function (exist) { 39 if (exist) { 40 copy.readPath(fromPath, copy.fs); 41 } else { 42 copy.createPath(toPath, 43 function () { 44 copy.readPath(fromPath, copy.fs); 45 }); 46 } 47 }); 48 };
2.路径准备完毕了,需要开始了解一下都有哪些文件需要复制了,然后根据规则逐条复制吧。
1 //数据文件系统 2 copy.fs = {}; 3 copy.readPath = function (path, fss) { 4 fs.readdir(path, 5 function (err, files) { 6 if (err) console.log(err); 7 else { 8 dirCount++; 9 copy.fillFilesTofs(path, files, fss); 10 } 11 }); 12 }; 13 //填充文件夹结构 14 copy.fillFilesTofs = function (path, files, fss) { 15 fss['path'] = path; 16 fss['children'] = files; 17 copy.copyFiles(fss); 18 }; 19 copy.getPathByTime = function (date, format) { 20 var result = { 21 path: '', 22 filename: '' 23 }; 24 var paths = []; 25 paths.push(toPath); 26 paths.push(date.getFullYear()); 27 var month = date.getMonth() + 1; 28 month = month.toString(); 29 if (month.length == 1) { 30 month = '0' + month; 31 32 } 33 paths.push(month); 34 var day = date.getDate(); 35 day = day.toString(); 36 if (day.length == 1) { 37 day = '0' + day; 38 39 } 40 paths.push(day); 41 //paths.push(date.getTime() + '.' + format); 42 var hour = date.getHours(); 43 hour = hour.toString(); 44 if (hour.length == 1) { 45 hour = '0' + hour; 46 47 } 48 var minutes = date.getMinutes(); 49 minutes = minutes.toString(); 50 if (minutes.length == 1) { 51 minutes = '0' + minutes; 52 53 } 54 var seconds = date.getSeconds(); 55 seconds = seconds.toString(); 56 if (seconds.length == 1) { 57 seconds = '0' + seconds; 58 59 } 60 return { 61 path: paths.join(pathSpliter), 62 filename: hour + minutes + seconds + '.' + format 63 }; 64 65 }; 66 //复制文件夹目录 67 copy.copyFiles = function (fss) { 68 if (fss.children.length > 0) { 69 var fullpath = fss.path + pathSpliter + fss.children[0]; 70 fs.stat(fullpath, 71 function (err, stat) { 72 if (err) throw err; 73 else { 74 if (stat.isFile()) { 75 var format = fss.children[0].split('.'); 76 format = format[format.length - 1]; 77 if (format && copyFormat.indexOf(format.toString().toLowerCase()) >= 0) { 78 format = format.toString().toLowerCase(); 79 var cTime = stat.mtime; 80 var newPath = copy.getPathByTime(cTime, format); 81 var cp = function () { 82 copy.copyFile(fullpath, newPath.path + pathSpliter + newPath.filename, stat, 83 function () { 84 fss.children.shift(); 85 copy.copyFiles(fss); 86 }); 87 }; 88 fs.exists(newPath.path, 89 function (exist) { 90 if (exist) { 91 cp(); 92 } 93 else { 94 copy.mkdir(newPath.path, 2, 95 function () { 96 cp(); 97 }); 98 } 99 }); 100 } else { 101 console.log('没有复制文件-' + fullpath); 102 console.log(format.toString().toLowerCase()); 103 fss.children.shift(); 104 copy.copyFiles(fss); 105 } 106 } else { 107 fss.children[0] = { 108 'path': fullpath, 109 'children': [], 110 'parentPath': fss.path 111 }; 112 copy.readPath(fullpath, fss.children[0]); 113 } 114 ; 115 } 116 }); 117 118 } else { 119 if (fss.parentPath) { 120 var parent = copy.getParent(fss.parentPath, copy.fs, 0); 121 if (parent) { 122 parent.children.shift(); 123 copy.copyFiles(parent); 124 } 125 } 126 ; 127 } 128 129 }; 130 //创建文件夹 131 copy.mkdir = function (path, deep, callback) { 132 var paths = path.split(pathSpliter); 133 var p = []; 134 if (deep == paths.length + 1) { 135 callback(); 136 } else { 137 for (var i = 0; i < deep; i++) { 138 p.push(paths[i]); 139 } 140 fs.exists(p.join(pathSpliter), 141 function (exist) { 142 if (exist) { 143 copy.mkdir(path, deep + 1, callback); 144 } else { 145 fs.mkdir(p.join(pathSpliter), '0777', 146 function () { 147 copy.mkdir(path, deep + 1, callback); 148 149 }); 150 } 151 }); 152 } 153 }; 154 //复制文件 155 copy.copyFile = function (fPath, tPath, fStat, callback) { 156 fs.exists(tPath, 157 function (exist) { 158 if (exist) { 159 fs.stat(tPath, 160 function (err, stat) { 161 if (err) throw err; 162 if (stat.ct == fStat.ct && stat.size == fStat.size) { 163 callback(); 164 } else { 165 var nTpath = tPath.split('.')[0] + '0.' + tPath.split('.')[1]; 166 copy.copyFile(fPath, nTpath, fStat, callback); 167 168 } 169 }); 170 } else { 171 var readStream = fs.createReadStream(fPath); 172 var writeStream = fs.createWriteStream(tPath); 173 console.log('copyFile-' + fPath + '-' + tPath + exist); 174 175 util.pump(readStream, writeStream, 176 function () { 177 fileCount++; 178 callback(); 179 }); 180 181 } 182 183 }); 184 }; 185 //获取父节点 186 copy.getParent = function (path, fss, deep) { 187 if (fss.path == path) { 188 return fss; 189 } else { 190 for (var i = 0; i < fss.children.length; i++) { 191 var p = copy.getParent(path, fss.children[i], deep++); 192 if (p) { 193 return p; 194 } 195 } 196 return null; 197 } 198 };