Skip to content

armgit / ente

Public Imported from GitHub
0

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

Take 1

Manav Rathid5e8777e0d78
diff --git a/web/packages/gallery/components/viewer/FileViewer.tsx b/web/packages/gallery/components/viewer/FileViewer.tsx
index 9deedd9180..837cce4d95 100644
--- a/web/packages/gallery/components/viewer/FileViewer.tsx
+++ b/web/packages/gallery/components/viewer/FileViewer.tsx
@@ -1,4 +1,4 @@
-/* eslint-disable */
+/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-nocheck
// TODO(PS): WIP gallery using upstream photoswipe
@@ -13,14 +13,18 @@ if (process.env.NEXT_PUBLIC_ENTE_WIP_PS5) {
throw new Error("Whoa");
}
-import { useModalVisibility } from "@/base/components/utils/modal";
+import {
+ useModalVisibility,
+ type ModalVisibilityProps,
+} from "@/base/components/utils/modal";
import { FileInfo } from "@/gallery/components/FileInfo";
import type { EnteFile } from "@/media/file.js";
import { Button, styled } from "@mui/material";
import { useCallback, useEffect, useRef, useState } from "react";
+import { fileInfoExifForFile, type FileInfoExif } from "./data-source";
import { FileViewerPhotoSwipe } from "./photoswipe";
-export interface FileViewerProps {
+export type FileViewerProps = ModalVisibilityProps & {
/**
* The list of files that are currently being displayed in the context in
* which the file viewer was invoked.
@@ -45,7 +49,7 @@ export interface FileViewerProps {
* If true then the viewer does not show controls for downloading the file.
*/
disableDownload?: boolean;
-}
+};
/**
* A PhotoSwipe based image and video viewer.
@@ -69,14 +73,25 @@ const FileViewer: React.FC<FileViewerProps> = ({
const [activeFile, setActiveFile] = useState<EnteFile | undefined>(
undefined,
);
+ // With semantics similar to activeFile, this is the exif data associated
+ // with the activeFile, if any.
+ const [activeFileExif, setActiveFileExif] = useState<
+ FileInfoExif | undefined
+ >(undefined);
const { show: showFileInfo, props: fileInfoVisibilityProps } =
useModalVisibility();
- const handleViewInfo = useCallback((file: EnteFile) => {
- setActiveFile(file);
- showFileInfo();
- }, []);
+ const handleViewInfo = useCallback(
+ (file: EnteFile) => {
+ setActiveFile(file);
+ setActiveFileExif(
+ fileInfoExifForFile(file, (exif) => setActiveFileExif(exif)),
+ );
+ showFileInfo();
+ },
+ [showFileInfo],
+ );
useEffect(() => {
if (!open) {
@@ -97,12 +112,18 @@ const FileViewer: React.FC<FileViewerProps> = ({
pswpRef.current?.closeIfNeeded();
pswpRef.current = undefined;
};
- }, [open]);
+ // TODO(PS): This is missing files, initialIndex, disableDownload,
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [open, onClose, handleViewInfo]);
return (
<Container>
<Button>Test</Button>
- <FileInfo {...fileInfoVisibilityProps} file={activeFile} />
+ <FileInfo
+ {...fileInfoVisibilityProps}
+ file={activeFile}
+ exif={activeFileExif}
+ />
</Container>
);
};
diff --git a/web/packages/gallery/components/viewer/data-source.ts b/web/packages/gallery/components/viewer/data-source.ts
index 44ece51f7a..2a3cb6aba2 100644
--- a/web/packages/gallery/components/viewer/data-source.ts
+++ b/web/packages/gallery/components/viewer/data-source.ts
@@ -137,6 +137,11 @@ class FileViewerDataSourceState {
* The exif data we have for a particular file (ID).
*/
fileInfoExifByFileID = new Map<number, FileInfoExif>();
+ /**
+ * The latest callback registered for notifications of exif data being
+ * available for a particular file (ID).
+ */
+ exifObserverByFileID = new Map<number, (exif: FileInfoExif) => void>();
}
/**
@@ -378,29 +383,22 @@ const withDimensions = (imageURL: string): Promise<Partial<ItemData>> =>
* synchronously returns the cached value (and the callback is never invoked).
*
* In rare cases, it is possible that this function gets called before
- * {@link updateFileInfoExifIfNeeded} has completed. In that case, it will
- * trigger a parallel extraction. The function will synchronously return
- * `undefined`, and then will call the provided callback once the extraction
- * results are available.
+ * {@link updateFileInfoExifIfNeeded} has completed, the function will
+ * synchronously return `undefined`, and then later call the provided
+ * {@link observer} once the extraction results are available.
*/
-export const fileInfoExifForItemData = (
- itemData: ItemData,
- // cb: (exifData: FileInfoExif) => void,
+export const fileInfoExifForFile = (
+ file: EnteFile,
+ observer: (exifData: FileInfoExif) => void,
) => {
- const exifData = _state.fileInfoExifByFileID.get(itemData.fileID);
+ const fileID = file.id;
+ const exifData = _state.fileInfoExifByFileID.get(fileID);
if (exifData) return exifData;
+ _state.exifObserverByFileID.set(fileID, observer);
return undefined;
- // updateFileInfoExifIfNeeded()
};
-// // TODO(PS): This is a video. Use a placeholder.
-// return { tags: undefined, parsed: undefined };
-// }
-
-// const
-// };
-
/**
* Update, if needed, the cached Exif data for with the given {@link itemData}.
*
@@ -412,34 +410,40 @@ export const fileInfoExifForItemData = (
* If required, it will extract the exif data from the file, massage it to a
* form suitable for use by {@link FileInfo}, and stash it in its caches.
*
- *
- *
* @see {@link forgetExifForItemData}.
*/
export const updateFileInfoExifIfNeeded = async (itemData: ItemData) => {
const { fileID, fileType, imageURL } = itemData;
+ // We already have it available.
if (_state.fileInfoExifByFileID.has(fileID)) return;
- if (fileType === FileType.video) return;
+
+ const updateNotifyAndReturn = (exifData: FileInfoExif) => {
+ _state.fileInfoExifByFileID.set(fileID, exifData);
+ _state.exifObserverByFileID.get(fileID)?.(exifData);
+ return exifData;
+ };
+
+ // For videos, insert a placeholder.
+ if (fileType === FileType.video) {
+ return updateNotifyAndReturn(createPlaceholderFileInfoExif());
+ }
+
+ // This is not a video, but the original image is not available yet.
if (!imageURL) return;
- let exifData: FileInfoExif;
try {
const blob = await (await fetch(imageURL)).blob();
const file = new File([blob], "");
const tags = await extractRawExif(file);
const parsed = parseExif(tags);
- exifData = { tags, parsed };
+ return updateNotifyAndReturn({ tags, parsed });
} catch (e) {
log.error("Failed to extract exif", e);
// Save the empty placeholder exif corresponding to the file, no point
// in unnecessarily retrying this, it will deterministically fail again.
- exifData = createPlaceholderFileInfoExif();
+ return updateNotifyAndReturn(createPlaceholderFileInfoExif());
}
-
- _state.fileInfoExifByFileID.set(fileID, exifData);
-
- return exifData;
};
const createPlaceholderFileInfoExif = (): FileInfoExif => ({
@@ -450,10 +454,15 @@ const createPlaceholderFileInfoExif = (): FileInfoExif => ({
/**
* Clear any cached {@link FileInfoExif} for the given {@link ItemData}.
*/
-export const forgetExifForItemData = ({ fileID }: ItemData) =>
+export const forgetExifForItemData = ({ fileID }: ItemData) => {
_state.fileInfoExifByFileID.delete(fileID);
+ _state.exifObserverByFileID.delete(fileID);
+};
/**
* Clear all cached {@link FileInfoExif}.
*/
-export const forgetExif = () => _state.fileInfoExifByFileID.clear();
+export const forgetExif = () => {
+ _state.fileInfoExifByFileID.clear();
+ _state.exifObserverByFileID.clear();
+};