Skip to content

Commit

Permalink
Load covers from server domain
Browse files Browse the repository at this point in the history
  • Loading branch information
Wysogota committed Aug 29, 2022
1 parent 6d1ce87 commit 292be9e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
20 changes: 20 additions & 0 deletions server/src/controllers/cover.controller.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const axios = require('axios');
const mangadex = require('../api/mangadex');

module.exports.getMangaCovers = async (req, res, next) => {
Expand All @@ -8,4 +9,23 @@ module.exports.getMangaCovers = async (req, res, next) => {
} catch (error) {
next(error);
}
};

module.exports.getCover = async (req, res, next) => {
try {
const { params: { mangaId, coverName } } = req;
const coverUrl = `https://uploads.mangadex.org/covers/${mangaId}/${coverName}`;
const image = await axios.get(coverUrl, { responseType: 'arraybuffer' });
const cover = Buffer.from(image.data, 'base64');

res
.status(200)
.set({
'Content-Type': image.headers['content-type'],
'Content-Length': image.headers['content-length']
})
.send(cover);
} catch (error) {
next(error);
}
};
3 changes: 2 additions & 1 deletion server/src/models/mangadex/Cover.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ module.exports = class Cover {
}

getCoverUrl = (obj) => {
const { PORT, DOMAIN } = process.env;
const coverName = obj.attributes.fileName;
const coverUrl = `https://uploads.mangadex.org/covers/${this.mangaId}/${coverName}`;
const coverUrl = `http://${DOMAIN}:${PORT}/api/cover/${this.mangaId}/${coverName}`;
this.urls = {
raw: coverUrl,
'256': coverUrl + '.256.jpg',
Expand Down
3 changes: 2 additions & 1 deletion server/src/models/mangadex/Manga.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ module.exports = class Manga {
selectRelationship = (type) => this.relationships.filter((item) => item.type === type)[0];

getCoverUrl = () => {
const { PORT, DOMAIN } = process.env;
const coverName = this.selectRelationship('cover_art').attributes.fileName;
const coverUrl = `https://uploads.mangadex.org/covers/${this.id}/${coverName}`;
const coverUrl = `http://${DOMAIN}:${PORT}/api/cover/${this.id}/${coverName}`;
this.selectRelationship('cover_art').attributes.urls = {
raw: coverUrl,
'256': coverUrl + '.256.jpg',
Expand Down
1 change: 1 addition & 0 deletions server/src/routes/cover.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ const Controller = require('../controllers/cover.controller');
const router = Router();

router.get('/', Controller.getMangaCovers);
router.get('/:mangaId/:coverName', Controller.getCover);

module.exports = router;

0 comments on commit 292be9e

Please sign in to comment.