book.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. const express = require('express'),
  2. router = express.Router(),
  3. Document = require("../src/sequelize.js").Document,
  4. DocumentCover = require("../src/sequelize.js").DocumentCover,
  5. DocumentFile = require("../src/document.js").Document,
  6. templateArgs = require('../src/templateArgs.js');
  7. function getDocumentInFormat(docId, format) {
  8. return new Promise((res, rej) => {
  9. Document.findOne({ attributes: [ "path" ], where: { id: docId }}).then(path => {
  10. if (!path) {
  11. rej(404);
  12. return;
  13. }
  14. const document = new DocumentFile(path.path);
  15. var doc = document.convert(format);
  16. doc.ready.then(content => {
  17. if (!content) {
  18. doc.clean();
  19. rej(500);
  20. return;
  21. }
  22. res({ error: null, content: content });
  23. doc.clean();
  24. });
  25. });
  26. });
  27. }
  28. router.get('/:bookId/info.json', (req, res) => {
  29. Document.findOne({where: { id: req.params.bookId }}).then(doc => {
  30. if (!doc) {
  31. res.status(404);
  32. res.send("Book bot found");
  33. return;
  34. }
  35. res.set({ "Content-Type": "application/json" });
  36. res.send(JSON.stringify({
  37. title: doc.title,
  38. author: doc.author,
  39. identifier: doc.identifier,
  40. pageCount: doc.pageCount
  41. }));
  42. });
  43. });
  44. router.get('/:bookId/download.pdf', (req, res) => {
  45. templateArgs.needLogin(req, res).then(async () => {
  46. getDocumentInFormat(req.params.bookId, "pdf").then(doc => {
  47. res.set({
  48. "Content-Type": "application/pdf",
  49. });
  50. doc.content.pipe(res);
  51. }).catch(e => {
  52. res.status(e)
  53. res.send("Error");
  54. });
  55. });
  56. });
  57. router.get('/:bookId/download.epub', (req, res) => {
  58. templateArgs.needLogin(req, res).then(async () => {
  59. getDocumentInFormat(req.params.bookId, "epub").then(doc => {
  60. res.set({
  61. "Content-Type": "application/epub",
  62. });
  63. doc.content.pipe(res);
  64. }).catch(e => {
  65. res.status(e)
  66. res.send("Error");
  67. });
  68. });
  69. });
  70. router.get('/:bookId/cover.png', (req, res) => {
  71. templateArgs.needLogin(req, res).then(async () => {
  72. var cover = await DocumentCover.findOne({ where: {id: req.params.bookId }});
  73. if (!cover) {
  74. res.status(404);
  75. res.send("Not Found");
  76. } else {
  77. res.set({
  78. "Content-Type": "image/png",
  79. });
  80. res.send(cover.cover);
  81. }
  82. });
  83. });
  84. router.get('/:bookId/read/:pageNo.png', (req, res) => {
  85. templateArgs.needLogin(req, res).then(async () => {
  86. var path = await Document.findOne({ attributes: [ "path" ], where: { id: req.params.bookId }}),
  87. pageReq = parseInt(req.params.pageNo, 10);
  88. if (!path || pageReq <= 0) {
  89. res.status(404);
  90. res.send("Not Found");
  91. return;
  92. }
  93. const document = new DocumentFile(path.path),
  94. pageCount = document.getPageCount();
  95. if (pageReq > pageCount) {
  96. res.status(404);
  97. res.send("Not Found");
  98. return;
  99. }
  100. const content = await document.getPage(pageReq);
  101. if (content) {
  102. res.set({
  103. "Content-Type": "image/png",
  104. });
  105. res.send(content);
  106. } else {
  107. res.status(500);
  108. res.end("Error");
  109. }
  110. });
  111. });
  112. module.exports = router;