Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance #517 #526

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions apps/web/app/api/assets/[assetId]/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createContextFromRequest } from "@/server/api/client";
import { and, eq } from "drizzle-orm";
import sharp from "sharp";

import { assets } from "@hoarder/db/schema";
import { readAsset } from "@hoarder/shared/assetdb";
Expand Down Expand Up @@ -45,12 +46,34 @@ export async function GET(
},
});
} else {
return new Response(asset, {
let finalAsset = asset;

const { searchParams } = new URL(request.url);
if (searchParams.has("preview")) {
finalAsset = await cropImage(asset);
}

return new Response(finalAsset, {
status: 200,
headers: {
"Content-Length": asset.length.toString(),
"Content-type": metadata.contentType,
},
});
}
}

async function cropImage(buffer: Buffer): Promise<Buffer> {
try {
const metadata = await sharp(buffer).metadata();
if (!metadata.height || metadata.height < 1000) {
return buffer;
}
return await sharp(buffer)
// cropping to 1000 pixels which is just a conservative guess and not something that is actually calculated
.extract({ left: 0, top: 0, width: metadata.width!, height: 1000 })
.toBuffer();
} catch (error) {
console.error("Error cropping image:", error);
throw error;
}
}
6 changes: 4 additions & 2 deletions apps/web/components/dashboard/bookmarks/LinkCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ function LinkImage({
unoptimized={unoptimized}
className={className}
alt="card banner"
fill={true}
fill
// below 640px only 1 bookmark, until 768px 1 bookmark + sidebar, until 1024px 2 bookmarks + sidebar, after that 3 bookmarks + sidebar
sizes="(max-width: 640px) 100vw, (max-width: 768px) 60vw, (max-width: 1024px) 40vw, 25vw"
src={url}
/>
);

const imageDetails = getBookmarkLinkImageUrl(link);
const imageDetails = getBookmarkLinkImageUrl(link, true);

let img: React.ReactNode;
if (isBookmarkStillCrawling(bookmark)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/shared-react/utils/assetUtils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export function getAssetUrl(assetId: string) {
return `/api/assets/${assetId}`;
export function getAssetUrl(assetId: string, preview?: boolean) {
return `/api/assets/${assetId}${preview ? "?preview" : ""}`;
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as for why i am using the querystring "?preview" instead of "?preview=true" (or something similar):
The element would then URL encode the "=" sign, which breaks the URL for some reason and then route.ts will not be called anymore.

15 changes: 12 additions & 3 deletions packages/shared-react/utils/bookmarkUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,21 @@ import { getAssetUrl } from "./assetUtils";

const MAX_LOADING_MSEC = 30 * 1000;

export function getBookmarkLinkImageUrl(bookmark: ZBookmarkedLink) {
export function getBookmarkLinkImageUrl(
bookmark: ZBookmarkedLink,
preview?: boolean,
) {
if (bookmark.imageAssetId) {
return { url: getAssetUrl(bookmark.imageAssetId), localAsset: true };
return {
url: getAssetUrl(bookmark.imageAssetId, preview),
localAsset: true,
};
}
if (bookmark.screenshotAssetId) {
return { url: getAssetUrl(bookmark.screenshotAssetId), localAsset: true };
return {
url: getAssetUrl(bookmark.screenshotAssetId, preview),
localAsset: true,
};
}
return bookmark.imageUrl
? { url: bookmark.imageUrl, localAsset: false }
Expand Down
Loading