Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Support face crops

Manav Rathi01f31c352b90
diff --git a/web/packages/new/photos/components/ItemCards.tsx b/web/packages/new/photos/components/ItemCards.tsx
index 95aab19609..9a5de57b20 100644
--- a/web/packages/new/photos/components/ItemCards.tsx
+++ b/web/packages/new/photos/components/ItemCards.tsx
@@ -6,6 +6,7 @@ import downloadManager from "@/new/photos/services/download";
import { type EnteFile } from "@/new/photos/types/file";
import { styled } from "@mui/material";
import React, { useEffect, useState } from "react";
+import { faceCrop } from "../services/ml";
interface ItemCardProps {
/**
@@ -16,11 +17,17 @@ interface ItemCardProps {
* Optional file whose thumbnail (if any) should be should be shown.
*/
coverFile?: EnteFile | undefined;
+ /**
+ * Optional ID of a specific face within {@link coverFile} to show.
+ *
+ * Precondition: {@link faceID} must be an ID of a face that belongs to the
+ * given {@link coverFile}.
+ */
+ coverFaceID?: string | undefined;
/**
* Optional boolean indicating if the user is currently scrolling.
*
- * This is used as a hint by the cover file downloader to prioritize
- * downloads.
+ * This is used as a hint by the file downloader to prioritize downloads.
*/
isScrolling?: boolean;
/**
@@ -28,25 +35,65 @@ interface ItemCardProps {
*/
onClick?: () => void;
}
+
/**
* A generic card that can be be used to represent collections, files, people -
* anything that (usually) has an associated "cover photo".
+ *
+ * Usually, we provide it a {@link coverFile} prop to set the file whose
+ * thumbnail should be shown in the card. However, an additional
+ * {@link coverFaceID} prop can be used to show the face crop for that specific
+ * face within the cover file.
+ *
+ * Note that while the common use case is to use this with a cover photo (and an
+ * additional cover faceID), both of these are optional and the item card can
+ * also be used as a static component without an associated cover image by
+ * covering it with an opaque overlay.
*/
export const ItemCard: React.FC<React.PropsWithChildren<ItemCardProps>> = ({
TileComponent,
coverFile,
+ coverFaceID,
isScrolling,
onClick,
children,
}) => {
- const [coverImageURL, setCoverImageURL] = useState("");
+ const [coverImageURL, setCoverImageURL] = useState<string | undefined>();
useEffect(() => {
if (!coverFile) return;
- void downloadManager
- .getThumbnailForPreview(coverFile, isScrolling)
- .then((url) => url && setCoverImageURL(url));
- }, [coverFile, isScrolling]);
+
+ let didCancel = false;
+ let thisObjectURL: string | undefined;
+
+ const go = async () => {
+ if (coverFaceID) {
+ const blob = await faceCrop(coverFaceID, coverFile);
+ if (!didCancel) {
+ thisObjectURL = blob
+ ? URL.createObjectURL(blob)
+ : undefined;
+ setCoverImageURL(thisObjectURL);
+ }
+ } else {
+ const url = await downloadManager.getThumbnailForPreview(
+ coverFile,
+ isScrolling,
+ );
+ if (!didCancel) {
+ thisObjectURL = url;
+ setCoverImageURL(thisObjectURL);
+ }
+ }
+ };
+
+ void go();
+
+ return () => {
+ didCancel = true;
+ if (thisObjectURL) URL.revokeObjectURL(thisObjectURL);
+ };
+ }, [coverFile, coverFaceID, isScrolling]);
return (
<TileComponent {...{ onClick }}>
diff --git a/web/packages/new/photos/services/download.ts b/web/packages/new/photos/services/download.ts
index 4d76e29ac2..a126fdf724 100644
--- a/web/packages/new/photos/services/download.ts
+++ b/web/packages/new/photos/services/download.ts
@@ -144,6 +144,18 @@ class DownloadManagerImpl {
return thumb;
}
+ /**
+ * Resolves with an object URL that points to the file's thumbnail.
+ *
+ * The thumbnail will be downloaded (unless {@link localOnly} is true) and
+ * cached.
+ *
+ * The optional {@link localOnly} parameter can be set to indicate that this
+ * is being called as part of a scroll, so the downloader should not attempt
+ * to download the file but should instead fulfill the request from the
+ * cache. This avoids an unbounded flurry of requests on scroll, only
+ * downloading when the position has quiescized.
+ */
async getThumbnailForPreview(
file: EnteFile,
localOnly = false,