Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit99571df

Browse files
convert image to base64 before rendering
1 parent9575f2e commit99571df

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

‎api/pin.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { blacklist } from "../src/common/blacklist.js";
33
import{
44
clampValue,
55
CONSTANTS,
6+
getBase64URIFromImage,
67
parseBoolean,
78
renderError,
89
}from"../src/common/utils.js";
@@ -69,6 +70,10 @@ export default async (req, res) => {
6970
}, s-maxage=${cacheSeconds}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
7071
);
7172

73+
repoData.openGraphImageUrl=awaitgetBase64URIFromImage(
74+
repoData.openGraphImageUrl,
75+
);
76+
7277
returnres.send(
7378
renderRepoCard(repoData,{
7479
hide_border:parseBoolean(hide_border),

‎src/common/utils.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,27 @@ const dateDiff = (d1, d2) => {
569569
returnMath.round(diff/(1000*60));
570570
};
571571

572+
constgetBase64URIFromImage=async(imageURL)=>{
573+
try{
574+
constresponse=awaitaxios.get(imageURL,{
575+
responseType:"arraybuffer",
576+
});
577+
578+
if(response.status===200){
579+
constbase64Image=Buffer.from(response.data,"binary").toString(
580+
"base64",
581+
);
582+
constmimeType=response.headers["content-type"];
583+
return`data:${mimeType};base64,${base64Image}`;
584+
}else{
585+
thrownewError("Failed to fetch the image.");
586+
}
587+
}catch(error){
588+
console.error("Error:",error);
589+
returnnull;
590+
}
591+
};
592+
572593
export{
573594
ERROR_CARD_LENGTH,
574595
renderError,
@@ -595,4 +616,5 @@ export {
595616
chunkArray,
596617
parseEmojis,
597618
dateDiff,
619+
getBase64URIFromImage,
598620
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp