Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanoverna committed Dec 22, 2023
1 parent ee56d97 commit 556cfad
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
17 changes: 15 additions & 2 deletions src/hooks/sourceNodes/createTypes/utils/getBase64.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,27 @@ const resizeUrl = require('./resizeUrl');
const queryString = require('query-string');
const { fetchRemoteFile } = require('gatsby-core-utils');

var magicNumbersToMimeType = {
ffd8ffe0: 'image/jpeg',
'89504e47': 'image/png',
'47494638': 'image/gif',
'52494646': 'image/webp',
};

async function download(url, cache) {
const absolutePath = await fetchRemoteFile({ url, cache });
const base64 = (await fs.readFile(absolutePath)).toString(`base64`);
const fileBody = await fs.readFile(absolutePath);
const base64 = fileBody.toString(`base64`);
const magicNumber = fileBody.toString('hex', 0, 4);

const extension = path
.extname(absolutePath)
.split('.')
.pop();
return `data:image/${extension};base64,${base64}`;

const mimeType = magicNumbersToMimeType[magicNumber] || `image/${extension}`;

return `data:${mimeType};base64,${base64}`;
}

module.exports = async (
Expand Down
14 changes: 13 additions & 1 deletion test/__snapshots__/graphql.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2585,7 +2585,19 @@ https://www.datocms-assets.com/34723/1620629638-plant.jpeg?auto=format&dpr=0.6&f
"appeareance": Object {
"addons": Array [],
"editor": "seo",
"parameters": Object {},
"parameters": Object {
"fields": Array [
"title",
"description",
"image",
"twitter_card",
],
"previews": Array [
"google",
"twitter",
"facebook",
],
},
},
"fieldType": "seo",
"id": "DatoCmsField-1400203",
Expand Down

0 comments on commit 556cfad

Please sign in to comment.