Skip to content

Commit

Permalink
Default MIME image type: docs & refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
danburzo committed Aug 11, 2024
1 parent d7b655b commit 6c2ba67
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 30 deletions.
17 changes: 7 additions & 10 deletions src/inline-images.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parseSrcset, stringifySrcset } from 'srcset';
import { getMimetypeFromURL, imageMimetypes } from './util/file-mimetype.js';
import { getMimetypeFromURL, isImageURL } from './util/file-mimetype.js';
import fetchBase64 from './util/fetch-base64.js';

export default async function inlineImages(doc, fetchOptions = {}, out) {
Expand All @@ -9,17 +9,16 @@ export default async function inlineImages(doc, fetchOptions = {}, out) {
let src_promises = Array.from(
doc.querySelectorAll('picture source[src], img[src]')
).map(async el => {
let mime = getMimetypeFromURL(el.src, doc);
/*
For web pages using atypical URLs for images
let’s just use a generic MIME type and hope it works.
For an example, see:
https://github.com/danburzo/percollate/issues/174
*/
if (!mime || !imageMimetypes.has(mime)) {
mime = 'image';
}
let mime = isImageURL(el.src, doc)
? getMimetypeFromURL(el.src, doc)
: 'image';
if (out) {
out.write(el.src + '\n');
}
Expand Down Expand Up @@ -51,18 +50,16 @@ export default async function inlineImages(doc, fetchOptions = {}, out) {
stringifySrcset(
await Promise.all(
items.map(async item => {
let mime = getMimetypeFromURL(item.url, doc);

/*
For web pages using atypical URLs for images
let’s just use a generic MIME type and hope it works.
For an example, see:
https://github.com/danburzo/percollate/issues/174
*/
if (!mime || !imageMimetypes.has(mime)) {
mime = 'image';
}
let mime = isImageURL(item.url, doc)
? getMimetypeFromURL(item.url, doc)
: 'image';
let data = await fetchBase64(
item.url,
fetchOptions
Expand Down
20 changes: 12 additions & 8 deletions src/remote-resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { randomUUID as uuid } from 'node:crypto';
import { parseSrcset, stringifySrcset } from 'srcset';
import {
getMimetypeFromURL,
imageMimetypes,
extForMimetype
extForMimetype,
isImageURL
} from './util/file-mimetype.js';
import { getUrlOrigin } from './util/url-origin.js';

Expand All @@ -15,13 +15,17 @@ export default function remoteResources(doc) {
and return a uniquely generated file name instead.
*/
function collectAndReplace(src) {
let ext;
let mime = getMimetypeFromURL(src);
if (mime && imageMimetypes.has(mime)) {
ext = extForMimetype(mime);
} else {
/*
If image URLs don’t have an extension with which
to figure out the image format, use the generic
`image` MIME media type and the `.image` extension
for EPUB remote resources.
*/
let mime = 'image',
ext = '.image';
mime = 'image';
if (isImageURL(src, doc)) {
mime = getMimetypeFromURL(src, doc);
ext = extForMimetype(mime);
}
if (!srcs.has(src)) {
srcs.set(src, {
Expand Down
24 changes: 12 additions & 12 deletions src/util/file-mimetype.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import mimetype from 'mimetype';

const IMAGE_MIMETYPES = new Set([
'image/avif',
'image/bmp',
'image/gif',
'image/jpeg',
'image/png',
'image/svg+xml',
'image/tiff',
'image/webp'
]);

/*
Add newer image formats to the MIME type database.
*/
Expand All @@ -25,16 +36,5 @@ export function getMimetypeFromURL(src, doc) {
}

export function isImageURL(src, doc) {
return imageMimetypes.has(getMimetypeFromURL(src, doc));
return IMAGE_MIMETYPES.has(getMimetypeFromURL(src, doc));
}

export const imageMimetypes = new Set([
'image/avif',
'image/bmp',
'image/gif',
'image/jpeg',
'image/png',
'image/svg+xml',
'image/tiff',
'image/webp'
]);

0 comments on commit 6c2ba67

Please sign in to comment.