Skip to content

armgit / ente

Public Imported from GitHub
0

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

added segmented live-photo loading

Abhinav72cd25e5af76
diff --git a/apps/photos/src/components/PhotoFrame.tsx b/apps/photos/src/components/PhotoFrame.tsx
index 3c1256d663..5d9f2e47ad 100644
--- a/apps/photos/src/components/PhotoFrame.tsx
+++ b/apps/photos/src/components/PhotoFrame.tsx
@@ -3,7 +3,10 @@ import PreviewCard from './pages/gallery/PreviewCard';
import { useContext, useEffect, useState } from 'react';
import { EnteFile } from 'types/file';
import { styled } from '@mui/material';
-import DownloadManager, { SourceURLs } from 'services/download';
+import DownloadManager, {
+ LivePhotoSourceURL,
+ SourceURL,
+} from 'services/download';
import AutoSizer from 'react-virtualized-auto-sizer';
import PhotoViewer from 'components/PhotoViewer';
import { TRASH_SECTION } from 'constants/collection';
@@ -190,7 +193,7 @@ const PhotoFrame = ({
const updateSrcURL = async (
index: number,
id: number,
- srcURLs: SourceURLs,
+ srcURLs: SourceURL,
forceUpdate?: boolean
) => {
const file = displayFiles[index];
@@ -407,23 +410,81 @@ const PhotoFrame = ({
addLogLine(`[${item.id}] new file src request`);
fetching[item.id] = true;
const srcURLs = await DownloadManager.getFileForPreview(item);
- try {
- await updateSrcURL(index, item.id, srcURLs);
- addLogLine(
- `[${item.id}] calling invalidateCurrItems for src, source loaded :${item.isSourceLoaded}`
- );
- instance.invalidateCurrItems();
- if ((instance as any).isOpen()) {
- instance.updateSize(true);
+ if (item.metadata.fileType === FILE_TYPE.LIVE_PHOTO) {
+ const srcImgURL = srcURLs.url as LivePhotoSourceURL;
+ const imageURL = await srcImgURL.image;
+
+ const dummyImgSrcUrl: SourceURL = {
+ url: imageURL,
+ isOriginal: false,
+ isRenderable: !!imageURL,
+ type: 'normal',
+ };
+ try {
+ await updateSrcURL(index, item.id, dummyImgSrcUrl);
+ addLogLine(
+ `[${item.id}] calling invalidateCurrItems for src, source loaded :${item.isSourceLoaded}`
+ );
+ instance.invalidateCurrItems();
+ if ((instance as any).isOpen()) {
+ instance.updateSize(true);
+ }
+ } catch (e) {
+ if (e.message !== CustomError.URL_ALREADY_SET) {
+ logError(
+ e,
+ 'updating photoswipe after src url update failed'
+ );
+ }
}
- } catch (e) {
- if (e.message !== CustomError.URL_ALREADY_SET) {
- logError(
- e,
- 'updating photoswipe after src url update failed'
+
+ const videoURL = await srcImgURL.video;
+ const loadedLivePhotoSrcURL: SourceURL = {
+ url: { video: videoURL, image: imageURL },
+ isOriginal: false,
+ isRenderable: !!videoURL,
+ type: 'livePhoto',
+ };
+ try {
+ await updateSrcURL(
+ index,
+ item.id,
+ loadedLivePhotoSrcURL,
+ true
+ );
+ addLogLine(
+ `[${item.id}] calling invalidateCurrItems for src, source loaded :${item.isSourceLoaded}`
);
+ instance.invalidateCurrItems();
+ if ((instance as any).isOpen()) {
+ instance.updateSize(true);
+ }
+ } catch (e) {
+ if (e.message !== CustomError.URL_ALREADY_SET) {
+ logError(
+ e,
+ 'updating photoswipe after src url update failed'
+ );
+ }
+ }
+ } else {
+ try {
+ await updateSrcURL(index, item.id, srcURLs);
+ addLogLine(
+ `[${item.id}] calling invalidateCurrItems for src, source loaded :${item.isSourceLoaded}`
+ );
+ instance.invalidateCurrItems();
+ if ((instance as any).isOpen()) {
+ instance.updateSize(true);
+ }
+ } catch (e) {
+ if (e.message !== CustomError.URL_ALREADY_SET) {
+ logError(
+ e,
+ 'updating photoswipe after src url update failed'
+ );
+ }
}
- throw e;
}
} catch (e) {
logError(e, 'getSlideData failed get src url failed');
diff --git a/apps/photos/src/services/download/index.ts b/apps/photos/src/services/download/index.ts
index f0e14cd167..a2c10926cc 100644
--- a/apps/photos/src/services/download/index.ts
+++ b/apps/photos/src/services/download/index.ts
@@ -18,15 +18,21 @@ import { APPS } from '@ente/shared/apps/constants';
import { PhotosDownloadClient } from './clients/photos';
import { PublicAlbumsDownloadClient } from './clients/publicAlbums';
-export type SourceURLs = {
- url:
- | {
- image: string;
- video: string;
- }
- | string;
+export type LivePhotoSourceURL = {
+ image: Promise<string>;
+ video: Promise<string>;
+};
+
+export type LoadedLivePhotoSourceURL = {
+ image: string;
+ video: string;
+};
+
+export type SourceURL = {
+ url: string | LivePhotoSourceURL | LoadedLivePhotoSourceURL;
isOriginal: boolean;
isRenderable: boolean;
+ type: 'normal' | 'livePhoto';
};
export type OnDownloadProgress = (event: {
@@ -54,8 +60,8 @@ class DownloadManager {
private thumbnailCache: LimitedCache;
private cryptoWorker: Remote<DedicatedCryptoWorker>;
- private fileObjectURLPromises = new Map<number, Promise<SourceURLs>>();
- private fileConversionPromises = new Map<number, Promise<SourceURLs>>();
+ private fileObjectURLPromises = new Map<number, Promise<SourceURL>>();
+ private fileConversionPromises = new Map<number, Promise<SourceURL>>();
private thumbnailObjectURLPromises = new Map<number, Promise<string>>();
private fileDownloadProgress = new Map<number, number>();
@@ -183,7 +189,7 @@ class DownloadManager {
getFileForPreview = async (
file: EnteFile,
forceConvert = false
- ): Promise<SourceURLs> => {
+ ): Promise<SourceURL> => {
try {
if (!this.ready) {
throw Error(CustomError.DOWNLOAD_MANAGER_NOT_READY);
@@ -228,13 +234,14 @@ class DownloadManager {
if (!this.ready) {
throw Error(CustomError.DOWNLOAD_MANAGER_NOT_READY);
}
- const getFilePromise = async (): Promise<SourceURLs> => {
+ const getFilePromise = async (): Promise<SourceURL> => {
const fileStream = await this.downloadFile(file);
const fileBlob = await new Response(fileStream).blob();
return {
url: URL.createObjectURL(fileBlob),
isOriginal: true,
isRenderable: false,
+ type: 'normal',
};
};
if (!this.fileObjectURLPromises.has(file.id)) {
diff --git a/apps/photos/src/types/file/index.ts b/apps/photos/src/types/file/index.ts
index 8e24db033c..65003549bf 100644
--- a/apps/photos/src/types/file/index.ts
+++ b/apps/photos/src/types/file/index.ts
@@ -1,4 +1,4 @@
-import { SourceURLs } from 'services/download';
+import { SourceURL } from 'services/download';
import {
EncryptedMagicMetadata,
MagicMetadataCore,
@@ -51,7 +51,7 @@ export interface EnteFile
isTrashed?: boolean;
key: string;
src?: string;
- srcURLs?: SourceURLs;
+ srcURLs?: SourceURL;
msrc?: string;
html?: string;
w?: number;
diff --git a/apps/photos/src/utils/file/index.ts b/apps/photos/src/utils/file/index.ts
index e8e5919a15..750d7de15c 100644
--- a/apps/photos/src/utils/file/index.ts
+++ b/apps/photos/src/utils/file/index.ts
@@ -10,7 +10,10 @@ import {
} from 'types/file';
import { decodeLivePhoto } from 'services/livePhotoService';
import { getFileType } from 'services/typeDetectionService';
-import DownloadManager, { SourceURLs } from 'services/download';
+import DownloadManager, {
+ LivePhotoSourceURL,
+ SourceURL,
+} from 'services/download';
import { logError } from '@ente/shared/sentry';
import { User } from '@ente/shared/user/types';
import { getData, LS_KEYS } from '@ente/shared/storage/localStorage';
@@ -282,8 +285,8 @@ export async function getRenderableFileURL(
fileBlob: Blob,
originalFileURL: string,
forceConvert: boolean
-): Promise<SourceURLs> {
- let srcURLs: SourceURLs['url'];
+): Promise<SourceURL> {
+ let srcURLs: SourceURL['url'];
switch (file.metadata.fileType) {
case FILE_TYPE.IMAGE: {
const convertedBlob = await getRenderableImage(
@@ -337,6 +340,10 @@ export async function getRenderableFileURL(
url: srcURLs,
isOriginal,
isRenderable: !!srcURLs,
+ type:
+ file.metadata.fileType === FILE_TYPE.LIVE_PHOTO
+ ? 'livePhoto'
+ : 'normal',
};
}
@@ -344,25 +351,43 @@ async function getRenderableLivePhotoURL(
file: EnteFile,
fileBlob: Blob,
forceConvert: boolean
-): Promise<{ image: string; video: string }> {
+): Promise<LivePhotoSourceURL> {
const livePhoto = await decodeLivePhoto(file, fileBlob);
- const imageBlob = new Blob([livePhoto.image]);
- const videoBlob = new Blob([livePhoto.video]);
- const convertedImageBlob = await getRenderableImage(
- livePhoto.imageNameTitle,
- imageBlob
- );
- const convertedVideoBlob = await getPlayableVideo(
- livePhoto.videoNameTitle,
- videoBlob,
- forceConvert
- );
- const convertedImageURL = URL.createObjectURL(convertedImageBlob);
- const convertedVideoURL = URL.createObjectURL(convertedVideoBlob);
+
+ const getRenderableLivePhotoImageURL = async () => {
+ try {
+ const imageBlob = new Blob([livePhoto.image]);
+ const convertedImageBlob = await getRenderableImage(
+ livePhoto.imageNameTitle,
+ imageBlob
+ );
+
+ return URL.createObjectURL(convertedImageBlob);
+ } catch (e) {
+ //ignore and return null
+ return null;
+ }
+ };
+
+ const getRenderableLivePhotoVideoURL = async () => {
+ try {
+ const videoBlob = new Blob([livePhoto.video]);
+
+ const convertedVideoBlob = await getPlayableVideo(
+ livePhoto.videoNameTitle,
+ videoBlob,
+ forceConvert
+ );
+ return URL.createObjectURL(convertedVideoBlob);
+ } catch (e) {
+ //ignore and return null
+ return null;
+ }
+ };
return {
- image: convertedImageURL,
- video: convertedVideoURL,
+ image: getRenderableLivePhotoImageURL(),
+ video: getRenderableLivePhotoVideoURL(),
};
}
diff --git a/apps/photos/src/utils/photoFrame/index.ts b/apps/photos/src/utils/photoFrame/index.ts
index ad6806ea70..dc2d19d421 100644
--- a/apps/photos/src/utils/photoFrame/index.ts
+++ b/apps/photos/src/utils/photoFrame/index.ts
@@ -1,7 +1,7 @@
import { FILE_TYPE } from 'constants/file';
import { EnteFile } from 'types/file';
import { logError } from '@ente/shared/sentry';
-import { SourceURLs } from 'services/download';
+import { LivePhotoSourceURL, SourceURL } from 'services/download';
const WAIT_FOR_VIDEO_PLAYBACK = 1 * 1000;
@@ -69,7 +69,7 @@ export function updateFileMsrcProps(file: EnteFile, url: string) {
}
}
-export async function updateFileSrcProps(file: EnteFile, srcURLs: SourceURLs) {
+export async function updateFileSrcProps(file: EnteFile, srcURLs: SourceURL) {
const { url, isRenderable } = srcURLs;
file.w = window.innerWidth;
file.h = window.innerHeight;
@@ -89,19 +89,26 @@ export async function updateFileSrcProps(file: EnteFile, srcURLs: SourceURLs) {
</video>
`;
} else if (file.metadata.fileType === FILE_TYPE.LIVE_PHOTO) {
- const { image: imageURL, video: videoURL } = url as {
- image: string;
- video: string;
- };
- file.html = `
+ if (srcURLs.type === 'normal') {
+ file.html = `
<div class = 'pswp-item-container'>
- <img id = "live-photo-image-${file.id}" src="${imageURL}" onContextMenu="return false;"/>
- <video id = "live-photo-video-${file.id}" loop muted onContextMenu="return false;">
- <source src="${videoURL}" />
- Your browser does not support the video tag.
- </video>
+ <img id = "live-photo-image-${file.id}" src="${url}" onContextMenu="return false;"/>
</div>
`;
+ } else {
+ const { image: imageURL, video: videoURL } =
+ url as LivePhotoSourceURL;
+
+ file.html = `
+ <div class = 'pswp-item-container'>
+ <img id = "live-photo-image-${file.id}" src="${imageURL}" onContextMenu="return false;"/>
+ <video id = "live-photo-video-${file.id}" loop muted onContextMenu="return false;">
+ <source src="${videoURL}" />
+ Your browser does not support the video tag.
+ </video>
+ </div>
+ `;
+ }
} else if (file.metadata.fileType === FILE_TYPE.IMAGE) {
file.src = url as string;
} else {