LjFileHelper.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. var request = require('request');
  2. var fs = require('fs');
  3. var ffmpeg = require('fluent-ffmpeg');
  4. var ljConst = require("../const/LjConst.js");
  5. var path = require('path');
  6. var logHelper = require("./LjLogHelper");
  7. /**
  8. * The helper of file
  9. */
  10. class LjFileHelper
  11. {
  12. /**
  13. * Download file
  14. * @param fileUrl the file url
  15. * @returns {boolean}
  16. */
  17. static downloadFile(fileUrl)
  18. {
  19. //获取文件属性对象
  20. var filePropObj = this.getFileProperty(fileUrl);
  21. if (filePropObj == null)
  22. {
  23. return false;
  24. }
  25. //视频处理
  26. if (filePropObj.fileType == ljConst.constBase.VIDEO_TYPE_M3U8)
  27. {
  28. //未加密视频本地存储路径
  29. var localFileDir = filePropObj.folderDir + filePropObj.fileName + ".mp4"
  30. //M3U8视频转换MP4
  31. ffmpeg(fileUrl).format(ljConst.constBase.VIDEO_TYPE_MP4)
  32. .on('start', function (err)
  33. {
  34. logHelper.info("Starting down video:" + fileUrl);
  35. })
  36. .on('error', function (err)
  37. {
  38. logHelper.error("Succeeded to down video[" + fileUrl + "]: " + err.message);
  39. })
  40. .on('end', function ()
  41. {
  42. //加密视频存储路径
  43. let localFileDirNew = filePropObj.folderDir + filePropObj.fileName + "_new" + ".mp4"
  44. logHelper.info("Succeeded to down video:" + localFileDirNew);
  45. //加密存储
  46. this.writeEncryptedStream(localFileDir, localFileDirNew);
  47. console.info("Succeeded to encrypt video:" + localFileDirNew);
  48. //删除文件(加密存储完成后删除未加密文件)
  49. fs.unlink(localFileDir, function (err)
  50. {
  51. if (err)
  52. {
  53. //删除失败
  54. logHelper.info("Failed to delete video[" + err.message + "]:" + localFileDir);
  55. throw err;
  56. }
  57. else
  58. {
  59. logHelper.info('Succeeded to delete video:' + localFileDir)
  60. }
  61. })
  62. })
  63. .save(localFileDir);
  64. }//图片处理
  65. else
  66. {
  67. //未加密图片本地存储地址
  68. var localFileDir = filePropObj.folderDir + filePropObj.fileName + "." + filePropObj.fileType;
  69. //读取图片
  70. request(fileUrl).pipe(fs.createWriteStream(localFileDir)).on('close', function ()
  71. {
  72. //加密图片存储地址
  73. var localFileDirNew = filePropObj.folderDir + filePropObj.fileName + "_new." + filePropObj.fileType;
  74. logHelper.info("Succeeded to down image:" + localFileDirNew);
  75. //加密图片
  76. LjFileHelper.writeEncryptedStream(localFileDir, localFileDirNew);
  77. logHelper.info("Succeeded to image video:" + localFileDirNew);
  78. //删除文件(加密存储完成后删除未加密文件)
  79. fs.unlink(localFileDir, function (err)
  80. {
  81. if (err)
  82. {
  83. //删除失败
  84. logHelper.erro("Failed to delete image[" + err.message + "]:" + localFileDir);
  85. throw err;
  86. }
  87. else
  88. {
  89. logHelper.info('Succeeded to delete image:' + localFileDir)
  90. }
  91. })
  92. });
  93. }
  94. }
  95. /**
  96. * Gets file directory
  97. * @param fileName the file name
  98. * @returns {*|string} the file directory
  99. */
  100. static getFileDir(fileName)
  101. {
  102. var fileType = fileName.split(".")[1]
  103. var newFileDir = "";
  104. if (fileType == "m3u8")
  105. {
  106. fileName = fileName.replace(".m3u8", "_new.mp4")
  107. newFileDir = constObj.baseDir + fileName.replace(/\*/g, '\\');
  108. }
  109. else
  110. {
  111. fileName = fileName.replace(".", "_new.")
  112. newFileDir = constObj.baseDir + fileName.replace(/\*/g, '\\');
  113. }
  114. return newFileDir;
  115. }
  116. /**
  117. * Makes directory
  118. * @param dirname the directory name
  119. * @returns {boolean} true or false
  120. */
  121. static mkdirsSync(dirname)
  122. {
  123. if (fs.existsSync(dirname))
  124. {
  125. return true;
  126. }
  127. else
  128. {
  129. if (this.mkdirsSync(path.dirname(dirname)))
  130. {
  131. fs.mkdirSync(dirname);
  132. return true;
  133. }
  134. }
  135. }
  136. /**
  137. * Gets the file property object
  138. * @param fileUrl the file URL
  139. * @returns {null}
  140. */
  141. static getFileProperty(fileUrl)
  142. {
  143. if (fileUrl.startsWith("https://efunimgs.ai160.com") || fileUrl.startsWith("https://efunvideo.ai160.com"))
  144. {
  145. /*--文件夹目录-Begin--*/
  146. var folderDir = ljConst.constBase.BASE_DIR;
  147. var splitArr = fileUrl.split('/')
  148. var arrLen = splitArr.length;
  149. for (var i = 3; i < arrLen - 1; i++)
  150. {
  151. folderDir += splitArr[i] + "\\";
  152. }
  153. //创建文件夹目录
  154. this.mkdirsSync(folderDir);
  155. var filePropObj = {};
  156. filePropObj.folderDir = folderDir;
  157. //获取文件名称
  158. filePropObj.fileName = splitArr[arrLen - 1].split(".")[0];
  159. //获取文件类型
  160. filePropObj.fileType = splitArr[arrLen - 1].split(".")[1];
  161. /*--文件夹目录-Begin--*/
  162. return filePropObj;
  163. }
  164. return null;
  165. }
  166. static Encrypted(c)
  167. {
  168. var b = '';
  169. for (var i = 0; i < c.length; i++)
  170. {
  171. var ch = c[i];
  172. if (ch == '0')
  173. {
  174. ch = 'f';
  175. }
  176. else if (ch == 'f')
  177. {
  178. ch = '0';
  179. }
  180. b = b + ch;
  181. //console.log(ch);
  182. }
  183. return b;
  184. }
  185. static Decrypted(c)
  186. {
  187. var b = '';
  188. for (var i = 0; i < c.length; i++)
  189. {
  190. var ch = c[i];
  191. if (ch == 'f')
  192. {
  193. ch = '0';
  194. }
  195. else if (ch == '0')
  196. {
  197. ch = 'f';
  198. }
  199. b = b + ch;
  200. //console.log(ch);
  201. }
  202. return b;
  203. }
  204. /**
  205. * Writes encrypted content, which is read from given source file, into given dest file
  206. * @param fSrc the source file
  207. * @param fDest the dest file
  208. */
  209. static writeEncryptedStream(fSrc, fDest)
  210. {
  211. var rs = fs.createReadStream(fSrc);
  212. var ws = fs.createWriteStream(fDest);
  213. rs.on('data', function (chunk)
  214. {
  215. let b = new Buffer(chunk, 'hex');
  216. //console.log(b);
  217. let c = b.toString('hex');
  218. c = LjFileHelper.encryptedContent(c);
  219. let d = Buffer.from(c, 'hex');
  220. ws.write(d);
  221. });
  222. rs.on('end', function ()
  223. {
  224. ws.end();
  225. logHelper.info('Succeeded in writing into encrypted stream[' + fDest + '].');
  226. });
  227. rs.on('error', function (err)
  228. {
  229. console.log(err.stack);
  230. });
  231. }
  232. /**
  233. * Encrypts the content
  234. * @param c the content to be encrypted
  235. * @return the encrypted content
  236. */
  237. static encryptedContent(c)
  238. {
  239. var b = '';
  240. for (var i = 0; i < c.length; i++)
  241. {
  242. var ch = c[i];
  243. if (ch == '0')
  244. {
  245. ch = 'f';
  246. }
  247. else if (ch == 'f')
  248. {
  249. ch = '0';
  250. }
  251. else if (ch == '1')
  252. {
  253. ch = 'e';
  254. }
  255. else if (ch == 'e')
  256. {
  257. ch = '1';
  258. }
  259. else if (ch == '2')
  260. {
  261. ch = 'd';
  262. }
  263. else if (ch == 'd')
  264. {
  265. ch = '2';
  266. }
  267. else if (ch == '3')
  268. {
  269. ch = 'c';
  270. }
  271. else if (ch == 'c')
  272. {
  273. ch = '3';
  274. }
  275. b = b + ch;
  276. //console.log(ch);
  277. }
  278. return b;
  279. }
  280. /**
  281. * Decrypts the encrypted content
  282. * @param c the encrypted content
  283. * @return the decrypted content
  284. */
  285. static decryptedContent(c)
  286. {
  287. var b = '';
  288. for (var i = 0; i < c.length; i++)
  289. {
  290. var ch = c[i];
  291. if (ch == 'f')
  292. {
  293. ch = '0';
  294. }
  295. else if (ch == '0')
  296. {
  297. ch = 'f';
  298. }
  299. else if (ch == 'e')
  300. {
  301. ch = '1';
  302. }
  303. else if (ch == '1')
  304. {
  305. ch = 'e';
  306. }
  307. else if (ch == 'd')
  308. {
  309. ch = '2';
  310. }
  311. else if (ch == '2')
  312. {
  313. ch = 'd';
  314. }
  315. else if (ch == 'c')
  316. {
  317. ch = '3';
  318. }
  319. else if (ch == '3')
  320. {
  321. ch = 'c';
  322. }
  323. b = b + ch;
  324. //console.log(ch);
  325. }
  326. return b;
  327. }
  328. /**
  329. * Reads decrypted content, which is read from given encrypted file, into HTTP response stream
  330. * @param res the HTTP response stream
  331. * @param fDest the encrypted file
  332. */
  333. static readDecryptedStream(res, fDest, contentType)
  334. {
  335. res.writeHead(200, {'Content-Type': contentType});
  336. var rs = fs.createReadStream(fDest);
  337. rs.on('data', function (chunk)
  338. {
  339. let b = new Buffer(chunk, 'hex');
  340. let c = b.toString('hex');
  341. c = decryptedContent(c);
  342. let d = Buffer.from(c, 'hex');
  343. res.write(d);
  344. });
  345. rs.on('end', function ()
  346. {
  347. res.end();
  348. console.log('Succeeded in reading from decrypted stream[' + fDest + '].');
  349. });
  350. rs.on('error', function (err)
  351. {
  352. console.log(err.stack);
  353. });
  354. }
  355. /**
  356. * Is video
  357. * @param dir the directory
  358. * @returns {boolean}
  359. */
  360. isVideo(dir)
  361. {
  362. if (dir == "")
  363. {
  364. return false;
  365. }
  366. var fileType = dir.split(".")[1];
  367. if ("m3u8" == fileType)
  368. {
  369. return true;
  370. }
  371. return false;
  372. }
  373. }
  374. module.exports = LjFileHelper;