1 var nt
= require('./lib/nt');
2 var fs
= require('fs');
3 var path
= require('path');
4 var crcHash
= require("crc-hash");
5 var argv
= require('optimist').argv
;
6 var clc
= require('cli-color');
10 function readFilesFromDir(dir
){
11 if(dir
.indexOf('\\')!=-1){
12 dir
=dir
.replace(/\\/g,'/');
15 var list = fs.readdirSync(dir)
18 console.log('出错! 读取源目录失败');
22 list.forEach(function(filename) {
23 file = dir + '/' + filename
24 var stat = fs.statSync(file)
25 if (stat && stat.isDirectory()) {
26 readFilesFromDir(file)
34 SOURCE_FILES.push(fileobj)
40 var TORRENT=null,TARGET=null;
41 function readSourceDir(source,torrent,target){
42 console.log('reading source dir
')
43 console.log('读取源目录文件')
46 readFilesFromDir(source)
49 function get_file_crc(fileObj){
50 var rs = fs.createReadStream(fileObj.path);
51 var hash = crcHash.createHash("crc32");
52 rs.on('data
', hash.update.bind(hash));
53 rs.on('end
', function () {
55 fileObj.hash=hash.digest('hex
').toUpperCase();
58 rs.on('error
', function (e) {
60 fileObj.hash='error reading
';
62 console.log('Error
! got error reading CRC32
for '+fileObj.path)
63 console.log('错误!读取文件CRC32
失败 '+fileObj.path)
67 function readTargetTorrent(){
71 console.log('got CRC32
for files
in source dir
')
72 console.log('读取源目录文件完成')
74 nt.read(TORRENT, function(err, torrent) {
77 console.log('Error
! reading torrent failed
')
78 console.log('错误!读取种子失败')
81 var meta=torrent.metadata,
83 pieces_raw=info.pieces,
84 files = info.files || [{ path: [info.name], length: info.length,hash:info.hash }],
87 for(var i =0;i< files.length;i++){
92 for(var j =0;j<SOURCE_FILES.length;j++){
93 var ofile=SOURCE_FILES[j];
94 if(ofile.hash==hash&&ofile.size==size){
101 console.log('Total
'+total+' Matched
'+matched)
102 console.log('总计 '+total+' 个文件 匹配到 '+matched+' 个')
107 if(TARGET.indexOf('\\')==-1){
108 var linkpath=TARGET+info.name+'\\'+file.path.join('\\');
110 var linkpath=TARGET+'/'+info.name+'/'+file.path.join('/');
112 linkpath=linkpath.replace(/\\/g,'/')
113 console.log(linkpath);
116 fs.linkSync(file.matched.path, linkpath)
119 console.log('Error
! cross
-device link not permitted
');
120 console.log('出错! 无法在两个不同硬盘间使用链结');
124 console.log('Error
! failed to make link
');
125 console.log('出错! 链结文件失败');
132 console.log('Done Linking
!');
133 console.log('完成链接!');
137 console.log('Error
! reading torrent failed
')
138 console.log('错误!读取种子失败')
143 function mkdirs(dirpath) {
145 dirlist=dirpath.split('/');
147 for(var i =0;i< dirlist.length;i++){
149 for(var j =0;j<=i;j++){
150 dir.push(dirlist[j]);
158 function mktorrent(torrent,dir){
159 nt.makeWrite(torrent, 'http
://announce.test', dir,['.'],
160 function(err
, torrent
) {
163 console
.log('Error ! writing torrent failed!');
164 console
.log('种子制作失败!');
167 console
.log('Finished writing torrent!');
168 console
.log('种子制作完成!');
169 setTimeout(function(){
174 console
.log("\nsnowpt torrent helper.\n");
175 var ver
=process
.versions
.node
.split('.');
176 ver
=ver
[0]*1000000+ver
[1]*1000+ver
[2]
178 console
.log(clc
.blueBright("node version too low! now "+process
.versions
.node
+" require 4.4.0 +"));
179 console
.log(clc
.blueBright("nodeJs 版本过旧 可能出现bug ! 当前 "+process
.versions
.node
+" 推荐 4.4.0 +"));
183 if(argv
&&argv
._
&&argv
._
[0]=='make'){
184 var source
=argv
._
[1],
185 torrent
=argv
._
[2]||'out.torrent';
188 console
.log("can't find "+source
);
191 mktorrent(torrent
,source
)
192 }else if(argv
&&argv
._
&&argv
._
[0]=='link'){
193 var source
=argv
._
[1],
194 target
=argv
._
[2]||'output',
196 if(target
.split().pop()!='/'){
199 readSourceDir(source
,torrent
,target
)
202 console
.log("Usage:");
204 console
.log("make torrent: sthelper "+clc
.blueBright("make")+clc
.yellowBright("[source_dir]")+clc
.yellowBright("[torrent]"));
205 console
.log("link torrent: sthelper "+clc
.blueBright("link")+clc
.yellowBright("[source_dir]")+clc
.yellowBright("[target_dir]")+clc
.yellowBright("[map_torrent]"));