getMangaInfo.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. Manga Cafe - Get Manga Information
  3. This script get the information of the reading manga title
  4. Require paramter: folder
  5. */
  6. requirelib("filelib");
  7. requirelib("imagelib");
  8. var targetFolder = decodeURIComponent(folder) + "/";
  9. var tmp = decodeURIComponent(folder).split("/");
  10. var chapterName = tmp.pop();
  11. var parentFolder = tmp.join("/");
  12. var title = [tmp.pop(), chapterName];
  13. //Scan the manga content, process the image if nessary
  14. var pages = filelib.aglob(targetFolder + "*", "smart");
  15. var validPages = [];
  16. for (var i = 0; i < pages.length; i++){
  17. var thisPage = pages[i];
  18. var basename = thisPage.split("/").pop().split("."); basename.pop(); basename = basename.join(".");
  19. var ext = thisPage.split(".").pop();
  20. if (!filelib.isDir(thisPage) && (ext == "png" || ext == "jpg") && !(basename.indexOf("-left") > 0 || basename.indexOf("-right") > 0 )){
  21. //Check if it is 2 connected page. If yes, split it into two page, left and write
  22. var dimension = imagelib.getImageDimension(thisPage);
  23. var width = dimension[0];
  24. var height = dimension[1];
  25. if (width > height){
  26. //Check if cached split image exists
  27. var pathdata = thisPage.split("/");
  28. var filename = pathdata.pop();
  29. var dirname = pathdata.join("/");
  30. var basename = filename.split(".");
  31. var ext = basename.pop();
  32. basename = basename.join(".");
  33. var targetLeft = dirname + "/" + basename + "-left." + ext;
  34. var targetRight = dirname + "/" + basename + "-right." + ext;
  35. if (filelib.fileExists(targetLeft) && filelib.fileExists(targetRight)){
  36. //Serve the previous cropped files
  37. }else{
  38. //Cut and serve
  39. imagelib.cropImage(thisPage, targetLeft,0,0,width/2,height)
  40. imagelib.cropImage(thisPage, targetRight,width/2,0,width/2,height)
  41. }
  42. validPages.push(targetRight);
  43. validPages.push(targetLeft);
  44. }else{
  45. //This is a valid page. Serve it
  46. validPages.push(thisPage);
  47. }
  48. }
  49. }
  50. //Search for other chapter links
  51. var otherChapterCandidate = filelib.aglob(parentFolder + "/*", "smart");
  52. var otherChapters = [];
  53. for (var i =0; i < otherChapterCandidate.length; i++){
  54. var basename = otherChapterCandidate[i].split('/').pop();
  55. if (filelib.isDir(otherChapterCandidate[i]) && basename.substring(0,1) != "."){
  56. otherChapters.push(otherChapterCandidate[i]);
  57. }
  58. }
  59. var info = {
  60. title: title,
  61. pages: validPages,
  62. dir: targetFolder,
  63. otherChapterDir: otherChapters,
  64. }
  65. //Process the image file.
  66. sendJSONResp(JSON.stringify(info));