LjFileHelper.js 19 KB

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