Skip to content

armgit / ente

Public Imported from GitHub
0

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

Ontology wip

Manav Rathibac49c70580a
diff --git a/web/apps/photos/src/components/PhotoViewer/FileInfo/index.tsx b/web/apps/photos/src/components/PhotoViewer/FileInfo/index.tsx
index f029d19952..453f56bf33 100644
--- a/web/apps/photos/src/components/PhotoViewer/FileInfo/index.tsx
+++ b/web/apps/photos/src/components/PhotoViewer/FileInfo/index.tsx
@@ -1,6 +1,7 @@
import { EnteDrawer } from "@/base/components/EnteDrawer";
import { Titlebar } from "@/base/components/Titlebar";
import { UnidentifiedFaces } from "@/new/photos/components/PeopleList";
+import type { RawExifTags } from "@/new/photos/services/exif";
import { isMLEnabled } from "@/new/photos/services/ml";
import { EnteFile } from "@/new/photos/types/file";
import CopyButton from "@ente/shared/components/CodeBlock/CopyButton";
@@ -18,8 +19,7 @@ import LinkButton from "components/pages/gallery/LinkButton";
import { t } from "i18next";
import { AppContext } from "pages/_app";
import { GalleryContext } from "pages/gallery";
-import { useContext, useEffect, useMemo, useState } from "react";
-import { getEXIFLocation } from "services/exif";
+import React, { useContext, useEffect, useMemo, useState } from "react";
import { PublicCollectionGalleryContext } from "utils/publicCollectionGallery";
import {
getMapDisableConfirmationDialog,
@@ -41,12 +41,12 @@ export const FileInfoSidebar = styled((props: DialogProps) => (
},
});
-interface Iprops {
+interface FileInfoProps {
shouldDisableEdits?: boolean;
showInfo: boolean;
handleCloseInfo: () => void;
file: EnteFile;
- exif: any;
+ rawExif: RawExifTags | undefined;
scheduleUpdate: () => void;
refreshPhotoswipe: () => void;
fileToCollectionsMap?: Map<number, number[]>;
@@ -76,19 +76,19 @@ function getOpenStreetMapLink(location: {
return `https://www.openstreetmap.org/?mlat=${location.latitude}&mlon=${location.longitude}#map=15/${location.latitude}/${location.longitude}`;
}
-export function FileInfo({
+export const FileInfo: React.FC<FileInfoProps> = ({
shouldDisableEdits,
showInfo,
handleCloseInfo,
file,
- exif,
+ rawExif,
scheduleUpdate,
refreshPhotoswipe,
fileToCollectionsMap,
collectionNameMap,
showCollectionChips,
closePhotoViewer,
-}: Iprops) {
+}) => {
const appContext = useContext(AppContext);
const galleryContext = useContext(GalleryContext);
const publicCollectionGalleryContext = useContext(
@@ -113,19 +113,12 @@ export function FileInfo({
};
}
}
- if (exif) {
- const exifLocation = getEXIFLocation(exif);
- if (
- (exifLocation.latitude || exifLocation.latitude === 0) &&
- !(exifLocation.longitude === 0 && exifLocation.latitude === 0)
- ) {
- return exifLocation;
- }
- }
return null;
- }, [file, exif]);
+ }, [file]);
useEffect(() => {
+ setParsedExifData(parseInfoExif(rawExif));
+
if (!exif) {
setParsedExifData({});
return;
@@ -161,8 +154,7 @@ export function FileInfo({
if (exif["ISO"]) {
parsedExifData["ISO"] = `ISO${exif["ISO"]}`;
}
- setParsedExifData(parsedExifData);
- }, [exif]);
+ }, [rawExif]);
if (!file) {
return <></>;
@@ -345,4 +337,56 @@ export function FileInfo({
/>
</FileInfoSidebar>
);
+};
+
+interface ParsedInfoExif {
+ resolution?: string;
+ megaPixels?: string;
+ takenOnDevice?: string;
+ fNumber?: string;
+ exposureTime?: string;
+ iso?: string;
}
+
+/**
+ * Extract some immediate fields of interest and in the form that we want to
+ * display on the info panel for a file.
+ */
+const parseInfoExif = (rawExif: RawExifTags | undefined): ParsedInfoExif => {
+ const parsed = {};
+ if (!rawExif) return {};
+
+
+ if (rawExif.exif)
+ if (exif["fNumber"]) {
+ parsedExifData["fNumber"] = `f/${Math.ceil(exif["FNumber"])}`;
+ } else if (exif["ApertureValue"] && exif["FocalLength"]) {
+ parsedExifData["fNumber"] = `f/${Math.ceil(
+ exif["FocalLength"] / exif["ApertureValue"],
+ )}`;
+ }
+ const imageWidth = exif["ImageWidth"] ?? exif["ExifImageWidth"];
+ const imageHeight = exif["ImageHeight"] ?? exif["ExifImageHeight"];
+ if (imageWidth && imageHeight) {
+ parsedExifData["resolution"] = `${imageWidth} x ${imageHeight}`;
+ const megaPixels = Math.round((imageWidth * imageHeight) / 1000000);
+ if (megaPixels) {
+ parsedExifData["megaPixels"] = `${Math.round(
+ (imageWidth * imageHeight) / 1000000,
+ )}MP`;
+ }
+ }
+ if (exif["Make"] && exif["Model"]) {
+ parsedExifData["takenOnDevice"] =
+ `${exif["Make"]} ${exif["Model"]}`;
+ }
+ if (exif["ExposureTime"]) {
+ parsedExifData["exposureTime"] = `1/${
+ 1 / parseFloat(exif["ExposureTime"])
+ }`;
+ }
+ if (exif["ISO"]) {
+ parsedExifData["ISO"] = `ISO${exif["ISO"]}`;
+ }
+ setParsedExifData(parsedExifData);
+}, [exif]);
diff --git a/web/apps/photos/src/components/PhotoViewer/index.tsx b/web/apps/photos/src/components/PhotoViewer/index.tsx
index e951d6b872..768daf46f4 100644
--- a/web/apps/photos/src/components/PhotoViewer/index.tsx
+++ b/web/apps/photos/src/components/PhotoViewer/index.tsx
@@ -18,7 +18,7 @@ import { lowercaseExtension } from "@/base/file";
import { FILE_TYPE } from "@/media/file-type";
import { isHEICExtension, needsJPEGConversion } from "@/media/formats";
import downloadManager from "@/new/photos/services/download";
-import { extractRawExif, type RawExif } from "@/new/photos/services/exif";
+import { extractRawExif, type RawExifTags } from "@/new/photos/services/exif";
import type { LoadedLivePhotoSourceURL } from "@/new/photos/types/file";
import { FlexWrapper } from "@ente/shared/components/Container";
import EnteSpinner from "@ente/shared/components/EnteSpinner";
@@ -109,7 +109,7 @@ function PhotoViewer(props: Iprops) {
const [showInfo, setShowInfo] = useState(false);
const [rawExif, setRawExif] = useState<{
key: string;
- value: RawExif;
+ value: RawExifTags;
}>();
const rawExifCopy = useRef(null);
const [livePhotoBtnOptions, setLivePhotoBtnOptions] = useState(
diff --git a/web/packages/new/photos/services/exif.ts b/web/packages/new/photos/services/exif.ts
index c3faaa2a8a..9dd4612adb 100644
--- a/web/packages/new/photos/services/exif.ts
+++ b/web/packages/new/photos/services/exif.ts
@@ -66,9 +66,16 @@ interface ParsedExif {
*
* The library we use is https://github.com/mattiasw/ExifReader.
*/
-export const extractExif = async (file: File) => {
- const tags = await extractTags(file);
+export const extractExif = async (file: File) =>
+ await extractRawExif(file).then(parseExif);
+/**
+ * Parse an already extracted {@link RawExifTags}.
+ *
+ * See: {@link extractExif}. This function does just the parsing step, for use
+ * when we've already extracted the raw data.
+ */
+export const parseExif = (tags: RawExifTags) => {
const location = parseLocation(tags);
const creationDate = parseCreationDate(tags);
const dimensions = parseDimensions(tags);
@@ -79,14 +86,6 @@ export const extractExif = async (file: File) => {
return metadata;
};
-const extractTags = async (blob: Blob) => {
- // See: [Note: Definining "raw" Exif] for the choice of options.
- return await ExifReader.load(await blob.arrayBuffer(), {
- async: true,
- expanded: true,
- });
-};
-
/**
* Parse a single "best" creation date for an image from the metadata embedded
* in the file.
@@ -95,7 +94,7 @@ const extractTags = async (blob: Blob) => {
* of dates, so we use some an a heuristic ordering (based on experience with
* the photos we find out in the wild) to pick a "best" date.
*/
-const parseCreationDate = (tags: ExifReader.ExpandedTags) => {
+const parseCreationDate = (tags: RawExifTags) => {
const { DateTimeOriginal, DateTimeDigitized, MetadataDate, DateTime } =
parseDates(tags);
return DateTimeOriginal ?? DateTimeDigitized ?? MetadataDate ?? DateTime;
@@ -120,13 +119,13 @@ interface ParsedExifDates {
* Extract dates from Exif and other metadata for the given file.
*/
export const extractExifDates = (file: File): Promise<ParsedExifDates> =>
- extractTags(file).then(parseDates);
+ extractRawExif(file).then(parseDates);
/**
* Parse all date related fields from the metadata embedded in the file,
* grouping them into chunks that somewhat reflect the Exif ontology.
*/
-const parseDates = (tags: ExifReader.ExpandedTags) => {
+const parseDates = (tags: RawExifTags) => {
// We have come across real examples of customer photos with Exif dates set
// to "0000:00:00 00:00:00". So ignore any date whose epoch is 0, so that we
// can try with a subsequent (possibly correct) date in the sequence.
@@ -215,7 +214,7 @@ const parseDates = (tags: ExifReader.ExpandedTags) => {
* than assuming UTC, which, while deterministic, is going to incorrect in an
* overwhelming majority of cases.
*/
-const parseExifDates = ({ exif }: ExifReader.ExpandedTags) => ({
+const parseExifDates = ({ exif }: RawExifTags) => ({
DateTimeOriginal: parseExifDate(
exif?.DateTimeOriginal,
exif?.OffsetTimeOriginal,
@@ -262,7 +261,7 @@ const parseExifDate = (
*
* For a list of XMP tags, see https://exiftool.org/TagNames/XMP.html.
*/
-const parseXMPDates = ({ xmp }: ExifReader.ExpandedTags) => ({
+const parseXMPDates = ({ xmp }: RawExifTags) => ({
/* XMP namespace is indicated for each group */
// exif:
DateTimeOriginal: parseXMPDate(xmp?.DateTimeOriginal),
@@ -306,7 +305,7 @@ const parseXMPDate = (xmpTag: ExifReader.XmpTag | undefined) => {
/**
* Parse date related tags from IPTC.
*/
-const parseIPTCDates = ({ iptc }: ExifReader.ExpandedTags) => ({
+const parseIPTCDates = ({ iptc }: RawExifTags) => ({
DateTimeOriginal: parseIPTCDate(
iptc?.["Date Created"],
iptc?.["Time Created"],
@@ -368,7 +367,7 @@ const parseIPTCDate = (
/**
* Parse GPS location from the metadata embedded in the file.
*/
-const parseLocation = (tags: ExifReader.ExpandedTags) => {
+const parseLocation = (tags: RawExifTags) => {
const latitude = tags.gps?.Latitude;
const longitude = tags.gps?.Longitude;
return latitude !== undefined && longitude !== undefined
@@ -380,7 +379,7 @@ const parseLocation = (tags: ExifReader.ExpandedTags) => {
* Parse the width and height of the image from the metadata embedded in the
* file.
*/
-const parseDimensions = (tags: ExifReader.ExpandedTags) => {
+const parseDimensions = (tags: RawExifTags) => {
// Go through all possiblities in order, returning the first pair with both
// the width and height defined, and non-zero.
const pair = (w: number | undefined, h: number | undefined) =>
@@ -433,7 +432,7 @@ const parseXMPNum = (xmpTag: ExifReader.XmpTag | undefined) => {
return n;
};
-export type RawExif = Omit<ExifReader.ExpandedTags, "Thumbnail" | "xmp"> & {
+export type RawExifTags = Omit<ExifReader.ExpandedTags, "Thumbnail" | "xmp"> & {
xmp?: ExifReader.XmpTags;
};
@@ -498,8 +497,11 @@ export type RawExif = Omit<ExifReader.ExpandedTags, "Thumbnail" | "xmp"> & {
* while allowing us to consume this JSON in arbitrary clients without needing
* to know about ExifReader specifically.
*/
-export const extractRawExif = async (blob: Blob): Promise<RawExif> => {
- const tags = await extractTags(blob);
+export const extractRawExif = async (blob: Blob): Promise<RawExifTags> => {
+ const tags = await ExifReader.load(await blob.arrayBuffer(), {
+ async: true,
+ expanded: true,
+ });
// Remove the embedded thumbnail (if any).
delete tags.Thumbnail;