-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a4be76e
commit 6bb1666
Showing
5 changed files
with
92 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const { asyncForEach } = require("../common/utils"); | ||
const { mediumCard } = require("../assets/card"); | ||
const { getUserData } = require("../route/api/medium"); | ||
|
||
async function parseToMediumCard(request) { | ||
const username = request.query.username; | ||
const offset = request.query.offset || 0; | ||
const width = request.query.width || config.card.width; | ||
const height = request.query.height || config.card.height; | ||
|
||
request.query.width = width; | ||
request.query.height = height; | ||
|
||
var resultData = await getUserData(username); | ||
const limit = request.query.limit == null ? config.default.limit | ||
: request.query.limit > resultData.length ? resultData.length | ||
: request.query.limit; | ||
let result = `<svg>`; | ||
|
||
result = `<svg xmlns="http:https://www.w3.org/2000/svg" xmlns:xlink="http:https://www.w3.org/1999/xlink" | ||
width="${(limit == 1 ? width : 2 * width) + | ||
config.default.margin_left + | ||
config.card.spacing}" | ||
version="1.2" | ||
height="${Math.round(limit / 2) * height + | ||
config.default.margin_top * 2 + | ||
config.card.spacing * Math.floor(limit / 2)}" | ||
viewBox="0 0 ${(limit == 1 ? width : 2 * width) + | ||
config.default.margin_left + | ||
config.card.spacing} ${Math.round(limit / 2) * height + | ||
config.default.margin_top * 2 + | ||
config.card.spacing * Math.floor(limit / 2)}">`; | ||
await asyncForEach( | ||
resultData.slice(offset, offset + limit), | ||
request.query, | ||
async (blog, index, settings) => { | ||
if (index >= limit) { | ||
return; | ||
} | ||
const mediumCardObj = await mediumCard(blog, settings, index); | ||
result += `<g transform="translate(${(index % 2 ? width + config.card.spacing : 0) + | ||
config.default.margin_left}, ${Math.floor(index / 2) * height + | ||
config.default.margin_top + | ||
(index > 1 ? config.card.spacing * Math.floor(index / 2) : 0)})">${mediumCardObj}</g>`; | ||
} | ||
); | ||
|
||
result += `</svg>`; | ||
return result; | ||
} | ||
|
||
module.exports.parseToMediumCard = parseToMediumCard; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const { parseToMediumCard } = require("../controller/medium_controller"); | ||
var router = require("express").Router(); | ||
|
||
router.get("/latest", async (request, response) => { | ||
try { | ||
if (!request.query.username) { | ||
response.write( | ||
JSON.stringify({ | ||
error: "username is required", | ||
}) | ||
); | ||
response.end(); | ||
return; | ||
} | ||
|
||
const result = await parseToMediumCard(request); | ||
|
||
response.setHeader( | ||
"Cache-Control", | ||
"public, no-cache, no-store, must-revalidate" | ||
); | ||
response.setHeader("Expires", "-1"); | ||
response.setHeader("Pragma", "no-cache"); | ||
response.writeHead(200, { "Content-Type": "image/svg+xml" }); | ||
|
||
response.write(result); | ||
response.end(); | ||
} catch (error) { | ||
console.log(error); | ||
response.send("Error while fetching the data" + error); | ||
} | ||
}); | ||
|
||
module.exports = router; |
6bb1666
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: