LjFileHelper.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. var request = require('request');
  2. var fs = require('fs');
  3. var ffmpeg = require('fluent-ffmpeg');
  4. var ljConst = require("../common/LjConst.js");
  5. var path = require('path');
  6. var logHelper = require("./LjLogHelper");
  7. var apiHelper = require("../helper/LjApiHelper");
  8. var httpHelper = require("../helper/LjHttpHelper");
  9. /**
  10. * The helper of file
  11. */
  12. class LjFileHelper
  13. {
  14. /**
  15. * Download file
  16. * @param fileUrl the file url
  17. * @returns {boolean}
  18. */
  19. static downloadFile(fileUrl,opt)
  20. {
  21. //获取文件属性对象
  22. var filePropObj = this.getFileProperty(fileUrl);
  23. if (filePropObj == null)
  24. {
  25. return false;
  26. }
  27. //视频处理
  28. if (filePropObj.fileType == ljConst.VIDEO_TYPE_M3U8)
  29. {
  30. //未加密视频本地存储路径
  31. var localFileDir = filePropObj.folderDir + filePropObj.fileName + ".mp4"
  32. //M3U8视频转换MP4
  33. ffmpeg(fileUrl).format(ljConst.VIDEO_TYPE_MP4)
  34. .on('start', function (err)
  35. {
  36. logHelper.info("Starting down video:" + fileUrl);
  37. })
  38. .on('error', function (err)
  39. {
  40. logHelper.error("Succeeded to down video[" + fileUrl + "]: " + err.message);
  41. })
  42. .on('end', function ()
  43. {
  44. //加密视频存储路径
  45. let localFileDirNew = filePropObj.folderDir + filePropObj.fileName + "_new" + ".mp4"
  46. logHelper.info("Succeeded to down video:" + localFileDirNew);
  47. //加密存储
  48. LjFileHelper.writeEncryptedStream(localFileDir, localFileDirNew,function(){
  49. //获取文件基本信息
  50. fs.stat(localFileDirNew,function(err,stats)
  51. {
  52. if(err==null)
  53. {
  54. //文件大小
  55. var fileSize = stats.size;
  56. LjFileHelper.updateDownloadResSize(opt);
  57. logHelper.info("Succeeded to encrypt video:" + localFileDirNew +" with file size["+fileSize+"]");
  58. }
  59. });
  60. });
  61. //删除文件(加密存储完成后删除未加密文件)
  62. fs.unlink(localFileDir, function (err)
  63. {
  64. if (err)
  65. {
  66. //删除失败
  67. logHelper.info("Failed to delete video[" + err.message + "]:" + localFileDir);
  68. throw err;
  69. }
  70. else
  71. {
  72. logHelper.info('Succeeded to delete video:' + localFileDir)
  73. }
  74. })
  75. })
  76. .save(localFileDir);
  77. }//有声读物处理
  78. else if (filePropObj.fileType == ljConst.VIDEO_TYPE_MP3)
  79. {
  80. //未加密有声读物本地存储地址
  81. var localFileDir = filePropObj.folderDir + filePropObj.fileName + "." + filePropObj.fileType;
  82. //读取有声读物
  83. request(fileUrl).pipe(fs.createWriteStream(localFileDir)).on('close', function ()
  84. {
  85. //加密有声读物存储地址
  86. var localFileDirNew = filePropObj.folderDir + filePropObj.fileName + "_new." + filePropObj.fileType;
  87. logHelper.info("Succeeded to down audio:" + localFileDirNew);
  88. //加密有声读物
  89. LjFileHelper.writeEncryptedStream(localFileDir, localFileDirNew,function(){
  90. //获取文件基本信息
  91. fs.stat(localFileDirNew,function(err,stats)
  92. {
  93. if(err==null)
  94. {
  95. //文件大小
  96. var fileSize = stats.size;
  97. LjFileHelper.updateDownloadResSize(opt);
  98. logHelper.info("Succeeded to encrypt audio:" + localFileDirNew +" with file size["+fileSize+"]");
  99. }
  100. });
  101. });
  102. //删除文件(加密存储完成后删除未加密文件)
  103. fs.unlink(localFileDir, function (err)
  104. {
  105. if (err)
  106. {
  107. //删除失败
  108. logHelper.erro("Failed to delete audio[" + err.message + "]:" + localFileDir);
  109. throw err;
  110. }
  111. else
  112. {
  113. logHelper.info('Succeeded to delete audio:' + localFileDir)
  114. }
  115. })
  116. });
  117. }//图片处理
  118. else
  119. {
  120. //未加密图片本地存储地址
  121. var localFileDir = filePropObj.folderDir + filePropObj.fileName + "." + filePropObj.fileType;
  122. //读取图片
  123. request(fileUrl).pipe(fs.createWriteStream(localFileDir)).on('close', function ()
  124. {
  125. //加密图片存储地址
  126. var localFileDirNew = filePropObj.folderDir + filePropObj.fileName + "_new." + filePropObj.fileType;
  127. logHelper.info("Succeeded to down image:" + localFileDirNew);
  128. //加密图片
  129. LjFileHelper.writeEncryptedStream(localFileDir, localFileDirNew,function(){
  130. //获取文件基本信息
  131. fs.stat(localFileDirNew,function(err,stats)
  132. {
  133. if(err==null)
  134. {
  135. //文件大小
  136. var fileSize = stats.size;
  137. LjFileHelper.updateDownloadResSize(opt);
  138. logHelper.info("Succeeded to encrypt image:" + localFileDirNew +" with file size["+fileSize+"]");
  139. }
  140. });
  141. });
  142. //删除文件(加密存储完成后删除未加密文件)
  143. fs.unlink(localFileDir, function (err)
  144. {
  145. if (err)
  146. {
  147. //删除失败
  148. logHelper.error("Failed to delete image[" + err.message + "]:" + localFileDir);
  149. throw err;
  150. }
  151. else
  152. {
  153. logHelper.info('Succeeded to delete image:' + localFileDir)
  154. }
  155. })
  156. });
  157. }
  158. }
  159. /**
  160. * Delete file
  161. * @param fileUrl the file url
  162. * @returns {boolean}
  163. */
  164. static delFile(fileUrl)
  165. {
  166. //获取文件属性对象
  167. var filePropObj = this.getFileProperty(fileUrl);
  168. if (filePropObj == null)
  169. {
  170. return false;
  171. }
  172. //视频处理
  173. if (filePropObj.fileType == ljConst.VIDEO_TYPE_M3U8)
  174. {
  175. //加密视频存储路径
  176. let localFileDirNew = filePropObj.folderDir + filePropObj.fileName + "_new" + ".mp4"
  177. logHelper.info("Succeeded to down video:" + localFileDirNew);
  178. //删除加密文件
  179. fs.unlink(localFileDirNew, function (err)
  180. {
  181. if (err)
  182. {
  183. //删除失败
  184. logHelper.info("Failed to delete video[" + err.message + "]:" + localFileDir);
  185. throw err;
  186. }
  187. else
  188. {
  189. logHelper.info('Succeeded to delete video:' + localFileDir)
  190. }
  191. })
  192. }//图片处理
  193. else
  194. {
  195. //加密图片存储地址
  196. var localFileDirNew = filePropObj.folderDir + filePropObj.fileName + "_new." + filePropObj.fileType;
  197. logHelper.info("Succeeded to down image:" + localFileDirNew);
  198. //删除加密文件
  199. fs.unlink(localFileDirNew, function (err)
  200. {
  201. if (err)
  202. {
  203. //删除失败
  204. logHelper.erro("Failed to delete image[" + err.message + "]:" + localFileDir);
  205. throw err;
  206. }
  207. else
  208. {
  209. logHelper.info('Succeeded to delete image:' + localFileDir)
  210. }
  211. })
  212. }
  213. }
  214. /**
  215. * Gets file directory
  216. * @param fileName the file name
  217. * @returns {*|string} the file directory
  218. */
  219. static getFileDir(fileName)
  220. {
  221. var fileType = fileName.split(".")[1]
  222. var newFileDir = "";
  223. if (fileType == "m3u8")
  224. {
  225. fileName = fileName.replace(".m3u8", "_new.mp4")
  226. newFileDir = ljConst.BASE_DIR + fileName.replace(/\*/g, '\\');
  227. }
  228. else
  229. {
  230. fileName = fileName.replace(".", "_new.")
  231. newFileDir = ljConst.BASE_DIR + fileName.replace(/\*/g, '\\');
  232. }
  233. return newFileDir;
  234. }
  235. /**
  236. * Makes directory
  237. * @param dirname the directory name
  238. * @returns {boolean} true or false
  239. */
  240. static mkdirsSync(dirname)
  241. {
  242. if (fs.existsSync(dirname))
  243. {
  244. return true;
  245. }
  246. else
  247. {
  248. if (this.mkdirsSync(path.dirname(dirname)))
  249. {
  250. fs.mkdirSync(dirname);
  251. return true;
  252. }
  253. }
  254. }
  255. /**
  256. * Gets the file property object
  257. * @param fileUrl the file URL
  258. * @returns {null}
  259. */
  260. static getFileProperty(fileUrl)
  261. {
  262. /*--文件夹目录-Begin--*/
  263. var folderDir = ljConst.BASE_DIR;
  264. var splitArr = fileUrl.split('/')
  265. var arrLen = splitArr.length;
  266. for (var i = 3; i < arrLen - 1; i++)
  267. {
  268. folderDir += splitArr[i] + "\\";
  269. }
  270. //创建文件夹目录
  271. this.mkdirsSync(folderDir);
  272. var filePropObj = {};
  273. filePropObj.folderDir = folderDir;
  274. //获取文件名称
  275. filePropObj.fileName = splitArr[arrLen - 1].split(".")[0];
  276. //获取文件类型
  277. filePropObj.fileType = splitArr[arrLen - 1].split(".")[1];
  278. /*--文件夹目录-Begin--*/
  279. return filePropObj;
  280. }
  281. /**
  282. * Gets the file property object
  283. * @param fileUrl the file URL
  284. * @returns {null}
  285. */
  286. static getFilePropertyNoMkdir(fileUrl)
  287. {
  288. /*--文件夹目录-Begin--*/
  289. var folderDir = ljConst.BASE_DIR;
  290. var splitArr = fileUrl.split('/')
  291. var arrLen = splitArr.length;
  292. for (var i = 3; i < arrLen - 1; i++)
  293. {
  294. folderDir += splitArr[i] + "\\";
  295. }
  296. var filePropObj = {};
  297. filePropObj.folderDir = folderDir;
  298. //获取文件名称
  299. filePropObj.fileName = splitArr[arrLen - 1].split(".")[0];
  300. //获取文件类型
  301. filePropObj.fileType = splitArr[arrLen - 1].split(".")[1];
  302. /*--文件夹目录-Begin--*/
  303. return filePropObj;
  304. }
  305. static Encrypted(c)
  306. {
  307. var b = '';
  308. for (var i = 0; i < c.length; i++)
  309. {
  310. var ch = c[i];
  311. if (ch == '0')
  312. {
  313. ch = 'f';
  314. }
  315. else if (ch == 'f')
  316. {
  317. ch = '0';
  318. }
  319. b = b + ch;
  320. //console.log(ch);
  321. }
  322. return b;
  323. }
  324. static Decrypted(c)
  325. {
  326. var b = '';
  327. for (var i = 0; i < c.length; i++)
  328. {
  329. var ch = c[i];
  330. if (ch == 'f')
  331. {
  332. ch = '0';
  333. }
  334. else if (ch == '0')
  335. {
  336. ch = 'f';
  337. }
  338. b = b + ch;
  339. //console.log(ch);
  340. }
  341. return b;
  342. }
  343. /**
  344. * Writes encrypted content, which is read from given source file, into given dest file
  345. * @param fSrc the source file
  346. * @param fDest the dest file
  347. */
  348. static writeEncryptedStream(fSrc, fDest,callback)
  349. {
  350. var rs = fs.createReadStream(fSrc);
  351. var ws = fs.createWriteStream(fDest);
  352. rs.on('data', function (chunk)
  353. {
  354. let b = new Buffer(chunk, 'hex');
  355. //console.log(b);
  356. let c = b.toString('hex');
  357. c = LjFileHelper.encryptedContent(c);
  358. let d = Buffer.from(c, 'hex');
  359. ws.write(d);
  360. });
  361. rs.on('end', function ()
  362. {
  363. ws.end();
  364. logHelper.info('Succeeded in writing into encrypted stream[' + fDest + '].');
  365. callback();
  366. });
  367. rs.on('error', function (err)
  368. {
  369. logHelper.error(err.stack);
  370. });
  371. }
  372. /**
  373. * Encrypts the content
  374. * @param c the content to be encrypted
  375. * @return the encrypted content
  376. */
  377. static encryptedContent(c)
  378. {
  379. var b = '';
  380. for (var i = 0; i < c.length; i++)
  381. {
  382. var ch = c[i];
  383. if (ch == '0')
  384. {
  385. ch = 'f';
  386. }
  387. else if (ch == 'f')
  388. {
  389. ch = '0';
  390. }
  391. else if (ch == '1')
  392. {
  393. ch = 'e';
  394. }
  395. else if (ch == 'e')
  396. {
  397. ch = '1';
  398. }
  399. else if (ch == '2')
  400. {
  401. ch = 'd';
  402. }
  403. else if (ch == 'd')
  404. {
  405. ch = '2';
  406. }
  407. else if (ch == '3')
  408. {
  409. ch = 'c';
  410. }
  411. else if (ch == 'c')
  412. {
  413. ch = '3';
  414. }
  415. b = b + ch;
  416. //console.log(ch);
  417. }
  418. return b;
  419. }
  420. /**
  421. * Decrypts the encrypted content
  422. * @param c the encrypted content
  423. * @return the decrypted content
  424. */
  425. static decryptedContent(c)
  426. {
  427. var b = '';
  428. for (var i = 0; i < c.length; i++)
  429. {
  430. var ch = c[i];
  431. if (ch == 'f')
  432. {
  433. ch = '0';
  434. }
  435. else if (ch == '0')
  436. {
  437. ch = 'f';
  438. }
  439. else if (ch == 'e')
  440. {
  441. ch = '1';
  442. }
  443. else if (ch == '1')
  444. {
  445. ch = 'e';
  446. }
  447. else if (ch == 'd')
  448. {
  449. ch = '2';
  450. }
  451. else if (ch == '2')
  452. {
  453. ch = 'd';
  454. }
  455. else if (ch == 'c')
  456. {
  457. ch = '3';
  458. }
  459. else if (ch == '3')
  460. {
  461. ch = 'c';
  462. }
  463. b = b + ch;
  464. //console.log(ch);
  465. }
  466. return b;
  467. }
  468. /**
  469. * Reads decrypted content, which is read from given encrypted file, into HTTP response stream
  470. * @param res the HTTP response stream
  471. * @param fDest the encrypted file
  472. */
  473. static readDecryptedStream(res, fDest, contentType)
  474. {
  475. res.writeHead(200, {'Content-Type': contentType});
  476. var rs = fs.createReadStream(fDest);
  477. rs.on('data', function (chunk)
  478. {
  479. let b = new Buffer(chunk, 'hex');
  480. let c = b.toString('hex');
  481. c = LjFileHelper.decryptedContent(c);
  482. let d = Buffer.from(c, 'hex');
  483. res.write(d);
  484. });
  485. rs.on('end', function ()
  486. {
  487. res.end();
  488. logHelper.log('Succeeded in reading from decrypted stream[' + fDest + '].');
  489. });
  490. rs.on('error', function (err)
  491. {
  492. logHelper.log(err.stack);
  493. });
  494. }
  495. static updateDownloadResSize(opt)
  496. {
  497. //return false;
  498. opt.url = apiHelper.getApiForUpdateResSize(opt.lessonId);
  499. opt.method = "PUT";
  500. opt.path = "/callback/download/update/resource/size";
  501. httpHelper.request(opt, function (error, res, body)
  502. {
  503. logHelper.info(body);
  504. var retObj = JSON.parse(body);
  505. logHelper.debug(retObj);
  506. if (retObj.code == 200)
  507. {
  508. logHelper.info("Succeed to call api[" + opt.path + "]");
  509. return true;
  510. }
  511. else
  512. {
  513. logHelper.error("Failed to call api[" + opt.path + "],caused by error[" + retObj.message + "]");
  514. return false;
  515. }
  516. });
  517. }
  518. /**
  519. * Is video
  520. * @param dir the directory
  521. * @returns {boolean}
  522. */
  523. isVideo(dir)
  524. {
  525. if (dir == "")
  526. {
  527. return false;
  528. }
  529. var fileType = dir.split(".")[1];
  530. if ("m3u8" == fileType)
  531. {
  532. return true;
  533. }
  534. return false;
  535. }
  536. }
  537. module.exports = LjFileHelper;