LjFileHelper.js 17 KB

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