modified: makefile
[GalaxyCodeBases.git] / tools / torrent / sthelper / main.js
blobe303cf32f8b2ff4d1bab1d4410591c6616aca7a0
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');
7 var FileToHash=0;
8 var SOURCE_FILES=[]
10 function readFilesFromDir(dir){
11 if(dir.indexOf('\\')!=-1){
12 dir=dir.replace(/\\/g,'/');
14 try{
15 var list = fs.readdirSync(dir)
16 }catch(e){
17 console.log(e);
18 console.log('出错! 读取源目录失败');
19 process.exit();
22 list.forEach(function(filename) {
23 file = dir + '/' + filename
24 var stat = fs.statSync(file)
25 if (stat && stat.isDirectory()) {
26 readFilesFromDir(file)
27 }else {
28 var fileobj={
29 path:file,
30 size:stat.size,
31 name:filename
33 FileToHash++;
34 SOURCE_FILES.push(fileobj)
35 get_file_crc(fileobj)
40 var TORRENT=null,TARGET=null;
41 function readSourceDir(source,torrent,target){
42 console.log('reading source dir')
43 console.log('读取源目录文件')
44 TORRENT=torrent;
45 TARGET=target;
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 () {
54 FileToHash--;
55 fileObj.hash=hash.digest('hex').toUpperCase();
56 readTargetTorrent()
57 });
58 rs.on('error', function (e) {
59 FileToHash--;
60 fileObj.hash='error reading';
61 console.log(e)
62 console.log('Error ! got error reading CRC32 for '+fileObj.path)
63 console.log('错误!读取文件CRC32失败 '+fileObj.path)
64 process.exit();
65 });
67 function readTargetTorrent(){
68 if(FileToHash>0){
69 return;
71 console.log('got CRC32 for files in source dir')
72 console.log('读取源目录文件完成')
73 try{
74 nt.read(TORRENT, function(err, torrent) {
75 if (err){
76 console.log(err)
77 console.log('Error ! reading torrent failed')
78 console.log('错误!读取种子失败')
79 process.exit();
81 var meta=torrent.metadata,
82 info=meta.info,
83 pieces_raw=info.pieces,
84 files = info.files || [{ path: [info.name], length: info.length,hash:info.hash }],
85 total=0,matched=0;
87 for(var i =0;i< files.length;i++){
88 var file=files[i],
89 hash=file.hash,
90 size=file.length;
91 total++;
92 for(var j =0;j<SOURCE_FILES.length;j++){
93 var ofile=SOURCE_FILES[j];
94 if(ofile.hash==hash&&ofile.size==size){
95 file.matched=ofile;
96 matched++;
97 break;
101 console.log('Total '+total+' Matched '+matched)
102 console.log('总计 '+total+' 个文件 匹配到 '+matched+' ')
104 for(var i in files){
105 var file=files[i];
106 if(file.matched){
107 if(TARGET.indexOf('\\')==-1){
108 var linkpath=TARGET+info.name+'\\'+file.path.join('\\');
109 }else{
110 var linkpath=TARGET+'/'+info.name+'/'+file.path.join('/');
112 linkpath=linkpath.replace(/\\/g,'/')
113 console.log(linkpath);
114 mkdirs(linkpath);
115 try{
116 fs.linkSync(file.matched.path, linkpath)
117 }catch(e){
118 if(e.errno==52){
119 console.log('Error! cross-device link not permitted');
120 console.log('出错! 无法在两个不同硬盘间使用链结');
121 process.exit();
122 }else{
123 console.log(e);
124 console.log('Error! failed to make link');
125 console.log('出错! 链结文件失败');
126 process.exit();
132 console.log('Done Linking!');
133 console.log('完成链接!');
135 }catch(e){
136 console.log(e)
137 console.log('Error ! reading torrent failed')
138 console.log('错误!读取种子失败')
139 process.exit();
143 function mkdirs(dirpath) {
144 var list=[],
145 dirlist=dirpath.split('/');
146 dirlist.pop();
147 for(var i =0;i< dirlist.length;i++){
148 var dir=[];
149 for(var j =0;j<=i;j++){
150 dir.push(dirlist[j]);
152 dir=dir.join('/');
153 try{
154 fs.mkdirSync(dir)
155 }catch(e){}
158 function mktorrent(torrent,dir){
159 nt.makeWrite(torrent, 'http://announce.test', dir,['.'],
160 function(err, torrent) {
161 if (err){
162 console.log(err);
163 console.log('Error ! writing torrent failed!');
164 console.log('种子制作失败!');
165 process.exit();
167 console.log('Finished writing torrent!');
168 console.log('种子制作完成!');
169 setTimeout(function(){
170 process.exit();
171 },2000)
174 console.log("\nsnowpt torrent helper.\n");
175 var ver=process.versions.node.split('.');
176 ver=ver[0]*1000000+ver[1]*1000+ver[2]
177 if(ver<40040000){
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';
187 if(!source){
188 console.log("can't find "+source);
189 return;
191 mktorrent(torrent,source)
192 }else if(argv&&argv._&&argv._[0]=='link'){
193 var source=argv._[1],
194 target=argv._[2]||'output',
195 torrent=argv._[3];
196 if(target.split().pop()!='/'){
197 target=target+'/'
199 readSourceDir(source,torrent,target)
200 }else{
201 //print help info
202 console.log("Usage:");
203 console.log("");
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]"));
206 console.log("");
207 console.log("");
208 return 0;