fix: images in remotes
All checks were successful
Deploy to SFTP Server / build (push) Successful in 9m22s
All checks were successful
Deploy to SFTP Server / build (push) Successful in 9m22s
This commit is contained in:
@@ -8,11 +8,32 @@ export async function generateThumbHash(
|
||||
) {
|
||||
const scaleFactor = 100 / Math.max(image.width, image.height);
|
||||
|
||||
const smallWidth = Math.floor(image.width * scaleFactor);
|
||||
const smallHeight = Math.floor(image.height * scaleFactor);
|
||||
let smallWidth = Math.floor(image.width * scaleFactor);
|
||||
let smallHeight = Math.floor(image.height * scaleFactor);
|
||||
|
||||
try {
|
||||
const smallImg = await sharp(image.fsPath)
|
||||
const imagePath = (image as ImageMetadata & { fsPath: string }).fsPath ??
|
||||
image.src;
|
||||
|
||||
let sp;
|
||||
if (imagePath.startsWith("https://") || imagePath.startsWith("http://")) {
|
||||
const res = await fetch(imagePath);
|
||||
if (!res.ok) {
|
||||
return;
|
||||
}
|
||||
sp = sharp(await res.arrayBuffer());
|
||||
} else {
|
||||
sp = sharp(imagePath);
|
||||
}
|
||||
|
||||
if (!smallWidth || !smallHeight) {
|
||||
const meta = await sp.metadata();
|
||||
const scaleFactor = 100 / Math.max(meta.width, meta.height);
|
||||
smallWidth = Math.floor(meta.width * scaleFactor);
|
||||
smallHeight = Math.floor(meta.height * scaleFactor);
|
||||
}
|
||||
|
||||
const smallImg = await sp
|
||||
.resize(smallWidth, smallHeight)
|
||||
.withMetadata()
|
||||
.raw()
|
||||
@@ -22,7 +43,10 @@ export async function generateThumbHash(
|
||||
const buffer = rgbaToThumbHash(smallWidth, smallHeight, smallImg);
|
||||
return Buffer.from(buffer).toString("base64");
|
||||
} catch (error) {
|
||||
console.log(`Could not generate thumbhash for ${image.fsPath}`, error);
|
||||
console.log(
|
||||
`Could not generate thumbhash for ${image.fsPath ?? image.src}`,
|
||||
error,
|
||||
);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
@@ -45,9 +69,17 @@ const allowedExif = [
|
||||
|
||||
export async function getExifData(image: ImageMetadata) {
|
||||
if (image.format === "svg") return undefined; // SVGs don't have EXIF data")
|
||||
const imagePath = (image as ImageMetadata & { fsPath: string }).fsPath;
|
||||
const imagePath = (image as ImageMetadata & { fsPath: string }).fsPath ??
|
||||
image.src;
|
||||
|
||||
try {
|
||||
const buffer = await sharp(imagePath).toBuffer();
|
||||
let buffer: ArrayBuffer;
|
||||
if (imagePath.startsWith("https://") || imagePath.startsWith("http://")) {
|
||||
const res = await fetch(imagePath);
|
||||
buffer = await res.arrayBuffer();
|
||||
} else {
|
||||
buffer = await sharp(imagePath).toBuffer() as unknown as ArrayBuffer;
|
||||
}
|
||||
|
||||
const tags = await ExifReader.load(buffer, { async: true });
|
||||
|
||||
@@ -62,7 +94,7 @@ export async function getExifData(image: ImageMetadata) {
|
||||
|
||||
return hasExif ? out : undefined;
|
||||
} catch (error) {
|
||||
console.log(`Error reading EXIF data from ${imagePath}`, error);
|
||||
console.log(`Error reading EXIF data from ${JSON.stringify(image)}`, error);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user