π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[space][web] Speed up video posting and fix media layouts
Anand Baburajanbcef857bb85e
diff --git a/web/apps/space/src/components/PostComposer.tsx b/web/apps/space/src/components/PostComposer.tsxindex a3791c535d..3bcc8ed45b 100644--- a/web/apps/space/src/components/PostComposer.tsx+++ b/web/apps/space/src/components/PostComposer.tsx@@ -28,7 +28,9 @@ import {isSpaceVideoFile,spaceVideoCover,spaceVideoInfo,+ startSpaceVideoExport,type SpacePostVideoEdit,+ type SpaceVideoExport,} from "utils/post-video";import { useSpaceRouter } from "utils/route-transitions";import { spaceRoutes } from "utils/routes";@@ -71,6 +73,8 @@ export const SpacePostComposer: React.FC<{const previewURLsRef = React.useRef(new Set<string>());const publishedPreviewURLRef = React.useRef<string>(undefined);const preparingRef = React.useRef(new Map<number, AbortController>());+ const exportsRef = React.useRef(new Map<number, SpaceVideoExport>());+ const publishingRef = React.useRef(false);const draftsRef = React.useRef(drafts);draftsRef.current = drafts;const mountedRef = React.useRef(false);@@ -97,10 +101,13 @@ export const SpacePostComposer: React.FC<{mountedRef.current = true;const urls = previewURLsRef.current;const preparing = preparingRef.current;+ const exports = exportsRef.current;return () => {mountedRef.current = false;preparing.forEach((controller) => controller.abort());preparing.clear();+ if (!publishingRef.current) exports.forEach((job) => job.cancel());+ exports.clear();urls.forEach((url) => {if (url != publishedPreviewURLRef.current)URL.revokeObjectURL(url);@@ -228,12 +235,51 @@ export const SpacePostComposer: React.FC<{})();}, [displayName, drafts, placeholder, profile.avatarUrl]);+ React.useEffect(() => {+ if (+ publishingRef.current ||+ editorSnapshot ||+ drafts.some((draft) => !draft.photo && !draft.error)+ )+ return;+ for (const draft of drafts) {+ const edit = draft.videoEdit;+ if (!edit) continue;+ const previous = exportsRef.current.get(draft.id);+ if (+ previous?.edit.start == edit.start &&+ previous.edit.end == edit.end &&+ previous.edit.muted == edit.muted+ )+ continue;+ previous?.cancel();+ const job = startSpaceVideoExport(draft.file, edit);+ exportsRef.current.set(draft.id, job);+ void job.promise.catch((error: unknown) => {+ if (+ !publishingRef.current &&+ exportsRef.current.get(draft.id) == job+ )+ exportsRef.current.delete(draft.id);+ if (+ !(+ error instanceof DOMException &&+ error.name == "AbortError"+ )+ )+ log.warn("Failed to prepare draft video", error);+ });+ }+ }, [drafts, editorSnapshot]);+const addPhotos = (files: File[]) => {setActiveIndex(drafts.length);setDrafts((current) => [...current, ...draftPhotos(files)]);};const releasePreview = (draft: DraftPhoto) => {preparingRef.current.get(draft.id)?.abort();+ exportsRef.current.get(draft.id)?.cancel();+ exportsRef.current.delete(draft.id);for (const url of new Set([draft.photo?.imageUrl,draft.originalPhoto?.imageUrl,@@ -378,8 +424,10 @@ export const SpacePostComposer: React.FC<{: undefined}onEditDraftPhoto={() => {- if (!isPublishing)- setEditorSnapshot({ drafts, activeIndex });+ if (isPublishing) return;+ exportsRef.current.forEach((job) => job.cancel());+ exportsRef.current.clear();+ setEditorSnapshot({ drafts, activeIndex });}}onDraftPostExitStart={() => setIsPublishing(true)}onDraftPostExitAnimationStart={() => setIsExiting(true)}@@ -390,9 +438,18 @@ export const SpacePostComposer: React.FC<{preparationError || isPreparing || !drafts.length? undefined: (caption) => {+ publishingRef.current = true;publishedPreviewURLRef.current =drafts[0]!.photo!.imageUrl;- return onPublish(images, caption);+ return onPublish(+ images.map((image, index) => ({+ ...image,+ videoExport: exportsRef.current.get(+ drafts[index]!.id,+ ),+ })),+ caption,+ );}}photo={photos[0] ?? placeholder}diff --git a/web/apps/space/src/screens/HomeScreen.tsx b/web/apps/space/src/screens/HomeScreen.tsxindex 968a62b2fa..27324196c1 100644--- a/web/apps/space/src/screens/HomeScreen.tsx+++ b/web/apps/space/src/screens/HomeScreen.tsx@@ -59,10 +59,13 @@ import {spaceText,spaceTextMuted,} from "styles/colors";-import { minimumPostPhotoFrameAspectRatio } from "styles/tiles";import { spaceTouchTargetSize } from "styles/touch-targets";import { firstNameFrom, formatSpaceDate } from "utils/display";-import { spacePostPhotos, viewerPhotosFromPost } from "utils/post-photos";+import {+ spacePostFrameAspectRatio,+ spacePostPhotos,+ viewerPhotosFromPost,+} from "utils/post-photos";import { thumbHashDataURLFromBase64 } from "utils/thumbhash";const homeBackground = spaceAppBackgroundColor;@@ -383,6 +386,7 @@ interface FeedItemProps {photos?: FeedPostPhoto[];onPhotoIndexChange?: (index: number) => void;aspectRatio: number;+ frameAspectRatio?: number;avatarUrl: string | null;caption?: string;friendID: string;@@ -860,6 +864,7 @@ const FeedItem: React.FC<FeedItemProps> = ({photos,onPhotoIndexChange,aspectRatio,+ frameAspectRatio: pendingFrameAspectRatio,avatarUrl,caption,friendID,@@ -1064,27 +1069,11 @@ const FeedItem: React.FC<FeedItemProps> = ({activePhoto.width ??dimensionsFromAspectRatio(aspectRatio).width,};- const firstPhoto = feedPhotos[0]!;- const firstPhotoAspectRatio =- firstPhoto.width && firstPhoto.height- ? firstPhoto.width / firstPhoto.height- : aspectRatio;- const hasMatchingAspectRatios = feedPhotos.every(- (photo) =>- photo.width &&- photo.height &&- Math.abs(photo.width / photo.height / firstPhotoAspectRatio - 1) <- 0.01,- );const frameAspectRatio =- feedPhotos.length > 1- ? hasMatchingAspectRatios- ? firstPhotoAspectRatio- : 1- : Math.max(- minimumPostPhotoFrameAspectRatio,- photoDimensions.width / photoDimensions.height,- );+ pendingFrameAspectRatio ??+ spacePostFrameAspectRatio(+ feedPhotos.length > 1 ? feedPhotos : [photoDimensions],+ );const isPhotoReady = Boolean(displayImageUrl) && decodedPhoto.ready;const showSoundControl =!isPostUnavailable && shouldPreloadVideo && Boolean(activePhoto.video);@@ -2260,6 +2249,7 @@ export const HomeScreen: React.FC<HomeScreenProps> = ({}avatarUrl={item.avatarUrl ?? null}caption={item.caption}+ frameAspectRatio={item.frameAspectRatio}friendID={item.friendID}imageUrl={item.imageUrl}isAvatarPending={false}diff --git a/web/apps/space/src/services/media-cache.ts b/web/apps/space/src/services/media-cache.tsindex db6dcf2a25..8ea7c33866 100644--- a/web/apps/space/src/services/media-cache.ts+++ b/web/apps/space/src/services/media-cache.ts@@ -6,6 +6,13 @@ import {clearSpaceMediaLoadCooldown,loadSpaceMedia,} from "services/media-load";+import {+ cachedSpaceVideoBlob,+ cachedSpaceVideoBlobIfPresent,+ clearSpaceVideoCache,+ clearSpaceVideoLoads,+ rememberSpaceVideoBlob,+} from "services/video-cache";const maxSpaceMediaCacheEntries = 128;const spaceMediaURLCache = new Map<string, Promise<string>>();@@ -95,11 +102,13 @@ export const cachedSpaceMediaBlobURL = async (load: () => Promise<Uint8Array>,mediaType?: string,) => {+ const storageKey = await spaceMediaStorageKey(cacheKey);if (mediaType?.startsWith("video/"))return URL.createObjectURL(- blobForBytes(await loadSpaceMedia(load), mediaType),+ await cachedSpaceVideoBlob(storageKey, async () =>+ blobForBytes(await loadSpaceMedia(load), mediaType),+ ),);- const storageKey = await spaceMediaStorageKey(cacheKey);const cached = spaceMediaURLCache.get(storageKey);if (cached) {spaceMediaURLCache.delete(storageKey);@@ -119,8 +128,15 @@ export const cachedSpaceMediaBlobURL = async (return promise;};-export const cachedSpaceMediaBlobURLIfPresent = async (cacheKey: string) => {+export const cachedSpaceMediaBlobURLIfPresent = async (+ cacheKey: string,+ mediaType?: string,+) => {const storageKey = await spaceMediaStorageKey(cacheKey);+ if (mediaType?.startsWith("video/")) {+ const blob = await cachedSpaceVideoBlobIfPresent(storageKey);+ return blob ? URL.createObjectURL(blob) : undefined;+ }const cached = spaceMediaURLCache.get(storageKey);if (cached) {spaceMediaURLCache.delete(storageKey);@@ -156,6 +172,7 @@ export const rememberCachedSpaceMediaBlobURL = async (export const clearSpaceMediaURLCache = () => {clearSpaceMediaLoadCooldown();+ clearSpaceVideoLoads();for (const promise of spaceMediaURLCache.values()) {void promise.then((url) => URL.revokeObjectURL(url),@@ -167,9 +184,15 @@ export const clearSpaceMediaURLCache = () => {export const clearSpaceMediaCache = async () => {clearSpaceMediaURLCache();+ await clearSpaceVideoCache();try {await clearBlobCache("space-media");} catch (error) {log.warn("Failed to clear Space media cache", error);}};++export const rememberCachedSpaceVideoBlob = async (+ cacheKey: string,+ blob: Blob,+) => rememberSpaceVideoBlob(await spaceMediaStorageKey(cacheKey), blob);diff --git a/web/apps/space/src/services/space.ts b/web/apps/space/src/services/space.tsindex fdba029e63..cf3aa57ea2 100644--- a/web/apps/space/src/services/space.ts+++ b/web/apps/space/src/services/space.ts@@ -32,6 +32,7 @@ import {cachedSpaceMediaBlobURLIfPresent,clearSpaceMediaCache,rememberCachedSpaceMediaBlobURL,+ rememberCachedSpaceVideoBlob,spacePostMediaCacheKey,spaceProfileMediaCacheKey,} from "services/media-cache";@@ -1066,6 +1067,7 @@ export const loadCurrentSpacePostAssetURL: SpacePostAssetURLLoader = async () => {const cachedURL = await cachedSpaceMediaBlobURLIfPresent(postAssetCacheKey(asset),+ asset.mediaType,);if (cachedURL) return cachedURL;@@ -1255,15 +1257,21 @@ export const createCurrentMediaPost = async ({export const loadCurrentCreatedPost = async (spaceId: string,postId: number,- previews: File[],+ media: PreparedSpacePostMedia[],) => {const ctx = await ensureCurrentSpaceContext();try {const created = await ctx.getPost(spaceId, BigInt(postId), spaceId);const imageURLs = await Promise.all(- created.photos.map((photo, index) =>- cacheAccountPostAssetURL(photo, previews[index]!),- ),+ created.photos.map(async (photo, index) => {+ const prepared = media[index]!;+ if (photo.video && prepared.video)+ await rememberCachedSpaceVideoBlob(+ postAssetCacheKey(photo.video.asset),+ prepared.video.file,+ );+ return cacheAccountPostAssetURL(photo, prepared.file);+ }),);const post = await postFromAccountPost(ctx, created, false, spaceId);post.imageUrl = imageURLs[0];diff --git a/web/apps/space/src/services/video-cache.ts b/web/apps/space/src/services/video-cache.tsnew file mode 100644index 0000000000..08c7caa63b--- /dev/null+++ b/web/apps/space/src/services/video-cache.ts@@ -0,0 +1,94 @@+import log from "ente-base/log";++const cacheName = "space-videos";+const maxCacheBytes = 100 * 1024 * 1024;+const loads = new Map<string, Promise<Blob>>();+let writes = Promise.resolve();+let generation = 0;++export const cachedSpaceVideoBlobIfPresent = async (key: string) => {+ const pending = loads.get(key);+ if (pending) return pending;+ try {+ await writes;+ const response = await (await caches.open(cacheName)).match(`/${key}`);+ return await response?.blob();+ } catch (error) {+ log.warn("Failed to read Space video cache", error);+ return undefined;+ }+};++export const rememberSpaceVideoBlob = (key: string, blob: Blob) => {+ const current = generation;+ const write = writes+ .then(async () => {+ if (current != generation || blob.size > maxCacheBytes) return;+ const cache = await caches.open(cacheName);+ if (await cache.match(`/${key}`)) return;+ const entries = await Promise.all(+ (await cache.keys()).map(async (request) => ({+ request,+ size: Number(+ (await cache.match(request))?.headers.get(+ "Content-Length",+ ),+ ),+ })),+ );+ let size = entries.reduce((total, entry) => total + entry.size, 0);+ for (const entry of entries) {+ if (size + blob.size <= maxCacheBytes) break;+ await cache.delete(entry.request);+ size -= entry.size;+ }+ await cache.put(+ `/${key}`,+ new Response(blob, {+ headers: { "Content-Length": String(blob.size) },+ }),+ );+ })+ .catch((error: unknown) => {+ log.warn("Failed to write Space video cache", error);+ });+ writes = write;+ return write;+};++export const cachedSpaceVideoBlob = (+ key: string,+ load: () => Promise<Blob>,+) => {+ const pending = loads.get(key);+ if (pending) return pending;+ const current = generation;+ const promise = (async () => {+ const cached = await cachedSpaceVideoBlobIfPresent(key);+ if (cached) return cached;+ const blob = await load();+ if (current == generation) await rememberSpaceVideoBlob(key, blob);+ return blob;+ })().finally(() => {+ if (loads.get(key) == promise) loads.delete(key);+ });+ loads.set(key, promise);+ return promise;+};++export const clearSpaceVideoLoads = () => {+ generation++;+ loads.clear();+};++export const clearSpaceVideoCache = () => {+ clearSpaceVideoLoads();+ writes = writes+ .then(async () => {+ await caches.delete(cacheName);+ })+ .catch((error: unknown) => {+ log.warn("Failed to clear Space video cache", error);+ });+ return writes;+};diff --git a/web/apps/space/src/state/AppStateProvider.tsx b/web/apps/space/src/state/AppStateProvider.tsxindex e82b253a27..c9a20a8d1c 100644--- a/web/apps/space/src/state/AppStateProvider.tsx+++ b/web/apps/space/src/state/AppStateProvider.tsx@@ -46,6 +46,7 @@ import {failLocalFeedPost,} from "utils/local-feed-post";import { prepareSpacePostImageFromEdit } from "utils/post-image";+import { spacePostFrameAspectRatio } from "utils/post-photos";import { prepareSpaceVideo } from "utils/post-video";const postStatusDurationMs = 2000;@@ -152,6 +153,7 @@ export const SpaceAppStateProvider: React.FC<React.PropsWithChildren> = ({{avatarUrl: profile.avatarUrl,caption: caption.trim() || undefined,+ frameAspectRatio: spacePostFrameAspectRatio(images),friendID: spaceId,height: cover.height,id: localPostId,@@ -200,6 +202,9 @@ export const SpaceAppStateProvider: React.FC<React.PropsWithChildren> = ({attempt += 1;const controller = new AbortController();const { signal } = controller;+ const cancelExports = () =>+ images.forEach((image) => image.videoExport?.cancel());+ signal.addEventListener("abort", cancelExports, { once: true });postPreparationsRef.current.set(localPostId, controller);setPostPublication(publication);setLocalFeedPosts((current) =>@@ -242,6 +247,9 @@ export const SpaceAppStateProvider: React.FC<React.PropsWithChildren> = ({image.file,image.video,signal,+ attempt == 1+ ? image.videoExport?.promise+ : undefined,): await prepareSpacePostImageFromEdit(image.file,@@ -308,7 +316,6 @@ export const SpaceAppStateProvider: React.FC<React.PropsWithChildren> = ({Date.now() + postStatusDurationMs,});}- const previews = prepared.map((image) => image.file);let reading = false;const readback = async () => {if (reading) return;@@ -328,7 +335,7 @@ export const SpaceAppStateProvider: React.FC<React.PropsWithChildren> = ({return loadCurrentCreatedPost(spaceId,session.postId!,- previews,+ prepared,);},{@@ -396,6 +403,8 @@ export const SpaceAppStateProvider: React.FC<React.PropsWithChildren> = ({throw error;}})().finally(() => {+ signal.removeEventListener("abort", cancelExports);+ cancelExports();postPreparationsRef.current.delete(localPostId);pending = undefined;});diff --git a/web/apps/space/src/state/app-state.tsx b/web/apps/space/src/state/app-state.tsxindex 84b29635c0..6f87559043 100644--- a/web/apps/space/src/state/app-state.tsx+++ b/web/apps/space/src/state/app-state.tsx@@ -30,6 +30,7 @@ export interface RefreshSpaceProfileOptions {interface PendingSpaceFeedPost {avatarUrl?: string | null;caption?: string;+ frameAspectRatio: number;friendID: string;height?: number;id: string;diff --git a/web/apps/space/src/utils/post-image.ts b/web/apps/space/src/utils/post-image.tsindex ff30983305..4eb3e839b4 100644--- a/web/apps/space/src/utils/post-image.ts+++ b/web/apps/space/src/utils/post-image.ts@@ -1,7 +1,7 @@import { lowercaseExtension, nameAndExtension } from "ente-base/file-name";import { isHEICExtension } from "ente-media/formats";import { heicToJPEG } from "ente-media/heic-convert";-import type { SpacePostVideoEdit } from "utils/post-video";+import type { SpacePostVideoEdit, SpaceVideoExport } from "utils/post-video";import { thumbHashBase64FromCanvas } from "utils/thumbhash";interface PreparedSpaceImage {@@ -41,6 +41,7 @@ export interface SpacePostPhotoEdit {export interface SpaceDraftPostImage {video?: SpacePostVideoEdit;+ videoExport?: SpaceVideoExport;cropArea?: SpaceImageCropArea;file: File;height?: number;diff --git a/web/apps/space/src/utils/post-photos.ts b/web/apps/space/src/utils/post-photos.tsindex 27b8aeb2f7..1eecb4d776 100644--- a/web/apps/space/src/utils/post-photos.ts+++ b/web/apps/space/src/utils/post-photos.ts@@ -1,5 +1,6 @@import type { SpaceViewerPhoto } from "components/FileViewer";import type { SpacePostPhoto } from "services/space";+import { minimumPostPhotoFrameAspectRatio } from "../styles/tiles";export const maxSpacePostPhotos = 10;@@ -7,6 +8,27 @@ export const spacePostPhotos = (post: SpacePostPhoto & { photos?: SpacePostPhoto[] },): SpacePostPhoto[] => (post.photos?.length ? post.photos : [post]);+export const spacePostFrameAspectRatio = (+ photos: Pick<SpacePostPhoto, "width" | "height">[],+) => {+ const firstPhoto = photos[0]!;+ const aspectRatio =+ firstPhoto.width && firstPhoto.height+ ? firstPhoto.width / firstPhoto.height+ : 1;+ if (photos.length == 1)+ return Math.max(minimumPostPhotoFrameAspectRatio, aspectRatio);++ return photos.every(+ (photo) =>+ photo.width &&+ photo.height &&+ Math.abs(photo.width / photo.height / aspectRatio - 1) < 0.01,+ )+ ? aspectRatio+ : 1;+};+export const viewerPhotosFromPost = (post: Omit<SpaceViewerPhoto, "imageUrl"> & {imageUrl?: string;diff --git a/web/apps/space/src/utils/post-video.ts b/web/apps/space/src/utils/post-video.tsindex 5b45a478b8..6a32a3418d 100644--- a/web/apps/space/src/utils/post-video.ts+++ b/web/apps/space/src/utils/post-video.ts@@ -297,7 +297,7 @@ const exports = new WeakMap<{ key: string; file: File; durationMs: number }>();-export const prepareSpaceVideo = async (+const exportSpaceVideo = async (file: File,edit: SpacePostVideoEdit,signal?: AbortSignal,@@ -326,6 +326,7 @@ export const prepareSpaceVideo = async (if (output.size > 15 * 1024 * 1024 - 42)throw new Error("This video is too large. Try a shorter trim.");const outputDuration = await determineVideoDurationWeb(output, signal);+ signal?.throwIfAborted();if (!Number.isFinite(outputDuration) ||outputDuration <= 0 ||@@ -339,6 +340,29 @@ export const prepareSpaceVideo = async (};exports.set(file, exported);}+ return { file: exported.file, durationMs: exported.durationMs };+};++export const startSpaceVideoExport = (file: File, edit: SpacePostVideoEdit) => {+ const controller = new AbortController();+ return {+ edit: { ...edit },+ promise: exportSpaceVideo(file, edit, controller.signal),+ cancel: () => controller.abort(),+ };+};++export type SpaceVideoExport = ReturnType<typeof startSpaceVideoExport>;++export const prepareSpaceVideo = async (+ file: File,+ edit: SpacePostVideoEdit,+ signal?: AbortSignal,+ pendingExport?: SpaceVideoExport["promise"],+) => {+ signal?.throwIfAborted();+ const exported = await (pendingExport ??+ exportSpaceVideo(file, edit, signal));signal?.throwIfAborted();const cover = await spaceVideoCover(exported.file,diff --git a/web/apps/space/src/utils/profile-photo-layout.ts b/web/apps/space/src/utils/profile-photo-layout.tsindex f2c38a4c07..9f006afe6d 100644--- a/web/apps/space/src/utils/profile-photo-layout.ts+++ b/web/apps/space/src/utils/profile-photo-layout.ts@@ -49,16 +49,19 @@ export const profilePhotoRows = <Tile extends { aspectRatio: number }>(0,);const gaps = (rowSize - 1) * profilePhotoGap;- const height = Math.min(- (width - gaps) / aspectRatio,- tiles.length == 1- ? width / minimumPostPhotoFrameAspectRatio- : width * 0.75,- );+ let height = (width - gaps) / aspectRatio;+ if (rowSize == 1)+ height = Math.min(+ height,+ tiles.length == 1+ ? width / minimumPostPhotoFrameAspectRatio+ : width * 0.75,+ );rows.push({aspectRatio,height,- width: tiles.length == 1 ? width : height * aspectRatio + gaps,+ width:+ rowSize > 1 || tiles.length == 1 ? width : height * aspectRatio,tiles: rowTiles,});index += rowSize;diff --git a/web/apps/space/tests/media-cache.test.ts b/web/apps/space/tests/media-cache.test.tsindex 1cdbc9bb77..12b921b6be 100644--- a/web/apps/space/tests/media-cache.test.ts+++ b/web/apps/space/tests/media-cache.test.ts@@ -1,7 +1,10 @@import { afterEach, beforeEach, expect, test, vi } from "vitest";import {cachedSpaceMediaBlobURL,+ cachedSpaceMediaBlobURLIfPresent,+ clearSpaceMediaCache,clearSpaceMediaURLCache,+ rememberCachedSpaceVideoBlob,spacePostMediaCacheKey,spaceProfileMediaCacheKey,} from "../src/services/media-cache";@@ -15,7 +18,28 @@ vi.mock("ente-base/origins", () => ({}));vi.mock("ente-base/blob-cache", () => ({blobCache: () => Promise.resolve({ get: () => undefined, put: vi.fn() }),+ clearBlobCache: vi.fn(),}));+vi.mock("ente-base/log", () => ({ default: { warn: vi.fn() } }));++const stored = new Map<string, Response>();+const createURL = vi.fn<(blob: Blob | MediaSource) => string>();+let pageURL: string;+const cacheURL = (key: string | Request) =>+ typeof key == "string" ? new URL(key, pageURL).href : key.url;+const cache = {+ match: vi.fn((key: string | Request) =>+ Promise.resolve(stored.get(cacheURL(key))?.clone()),+ ),+ keys: () =>+ Promise.resolve([...stored.keys()].map((key) => new Request(key))),+ put: vi.fn((key: string, response: Response) => {+ stored.set(cacheURL(key), response);+ return Promise.resolve();+ }),+ delete: (key: string | Request) =>+ Promise.resolve(stored.delete(cacheURL(key))),+};const assets = [{ key: spaceProfileMediaCacheKey("space", "avatar", "avatar", 1) },@@ -27,13 +51,24 @@ const assets = [beforeEach(() => {vi.useFakeTimers();clearSpaceMediaURLCache();- vi.spyOn(URL, "createObjectURL").mockReturnValue("blob:media");+ stored.clear();+ pageURL = "https://space.test/app";+ createURL.mockReset().mockReturnValue("blob:media");+ vi.stubGlobal("caches", {+ open: () => Promise.resolve(cache),+ delete: () => {+ stored.clear();+ return Promise.resolve(true);+ },+ });+ vi.spyOn(URL, "createObjectURL").mockImplementation(createURL);vi.spyOn(URL, "revokeObjectURL").mockImplementation(() => undefined);});afterEach(() => {clearSpaceMediaURLCache();vi.restoreAllMocks();+ vi.unstubAllGlobals();vi.useRealTimers();});@@ -73,3 +108,128 @@ test.each(assets)(expect(download).toHaveBeenCalledTimes(assets.length);},);++test("video bytes survive player disposal and memory-cache clearing", async () => {+ const download = vi.fn().mockResolvedValue(new Uint8Array([1, 2, 3]));+ const first = await cachedSpaceMediaBlobURL("video", download, "video/mp4");+ URL.revokeObjectURL(first);+ clearSpaceMediaURLCache();+ await cachedSpaceMediaBlobURL("video", download, "video/mp4");+ expect(download).toHaveBeenCalledOnce();+ expect(createURL).toHaveBeenCalledTimes(2);+ expect(await (createURL.mock.calls[1]![0] as Blob).bytes()).toEqual(+ new Uint8Array([1, 2, 3]),+ );+});++test.each([+ ["/app", "/app/profile"],+ ["/app/profile", "/app"],+])("a video cached on %s is reused on %s", async (from, to) => {+ const download = vi.fn().mockResolvedValue(new Uint8Array([1, 2, 3]));+ pageURL = `https://space.test${from}`;+ await cachedSpaceMediaBlobURL("video", download, "video/mp4");+ pageURL = `https://space.test${to}`;+ clearSpaceMediaURLCache();+ expect(+ await cachedSpaceMediaBlobURLIfPresent("video", "video/mp4"),+ ).toBeDefined();+ await cachedSpaceMediaBlobURL("video", download, "video/mp4");+ await rememberCachedSpaceVideoBlob(+ "video",+ new Blob(["video"], { type: "video/mp4" }),+ );+ expect(download).toHaveBeenCalledOnce();+ expect(stored.size).toBe(1);+});++test("concurrent players share a download and receive their own playback URLs", async () => {+ createURL+ .mockReturnValueOnce("blob:first")+ .mockReturnValueOnce("blob:second");+ let finish!: (bytes: Uint8Array) => void;+ const download = vi.fn(+ () =>+ new Promise<Uint8Array>((resolve) => {+ finish = resolve;+ }),+ );+ const first = cachedSpaceMediaBlobURL("video", download, "video/mp4");+ const second = cachedSpaceMediaBlobURL("video", download, "video/mp4");+ await vi.waitFor(() => expect(download).toHaveBeenCalledOnce());+ finish(new Uint8Array([7]));+ expect(new Set(await Promise.all([first, second]))).toEqual(+ new Set(["blob:first", "blob:second"]),+ );+ expect(createURL).toHaveBeenCalledTimes(2);+ expect(createURL.mock.calls[0]![0]).toBe(createURL.mock.calls[1]![0]);+});++test("a published video is available from the cache without downloading", async () => {+ const blob = new Blob(["published video"], { type: "video/mp4" });+ await rememberCachedSpaceVideoBlob("video", blob);+ clearSpaceMediaURLCache();+ expect(await cachedSpaceMediaBlobURLIfPresent("video", "video/mp4")).toBe(+ "blob:media",+ );+ const cached = createURL.mock.calls[0]![0] as Blob;+ expect(await cached.text()).toBe("published video");+ expect(cached.type).toBe("video/mp4");+});++test("video caching evicts older clips to stay within 100 MiB", async () => {+ const blob = new Blob(["video"], { type: "video/mp4" });+ Object.defineProperty(blob, "size", { value: 40 * 1024 * 1024 });+ await rememberCachedSpaceVideoBlob("first", blob);+ await Promise.all([+ rememberCachedSpaceVideoBlob("second", blob),+ rememberCachedSpaceVideoBlob("second", blob),+ ]);+ expect(stored.size).toBe(2);+ expect(+ await cachedSpaceMediaBlobURLIfPresent("first", "video/mp4"),+ ).toBeDefined();+ await rememberCachedSpaceVideoBlob("third", blob);+ expect(+ await cachedSpaceMediaBlobURLIfPresent("first", "video/mp4"),+ ).toBeUndefined();+ expect(+ await cachedSpaceMediaBlobURLIfPresent("second", "video/mp4"),+ ).toBeDefined();+ expect(+ await cachedSpaceMediaBlobURLIfPresent("third", "video/mp4"),+ ).toBeDefined();+});++test("clearing media also clears saved videos and prevents late downloads from repopulating them", async () => {+ await rememberCachedSpaceVideoBlob(+ "saved",+ new Blob(["video"], { type: "video/mp4" }),+ );+ let finish!: (bytes: Uint8Array) => void;+ const download = vi.fn(+ () =>+ new Promise<Uint8Array>((resolve) => {+ finish = resolve;+ }),+ );+ const pending = cachedSpaceMediaBlobURL("pending", download, "video/mp4");+ await vi.waitFor(() => expect(download).toHaveBeenCalledOnce());+ await clearSpaceMediaCache();+ finish(new Uint8Array([1]));+ await pending;+ expect(stored.size).toBe(0);+});++test("a full browser cache does not prevent video playback", async () => {+ cache.put.mockRejectedValueOnce(+ new DOMException("Full", "QuotaExceededError"),+ );+ await expect(+ cachedSpaceMediaBlobURL(+ "video",+ () => Promise.resolve(new Uint8Array([1])),+ "video/mp4",+ ),+ ).resolves.toBe("blob:media");+});diff --git a/web/apps/space/tests/post-photos.test.ts b/web/apps/space/tests/post-photos.test.tsindex 241220dbca..5d9ad45fda 100644--- a/web/apps/space/tests/post-photos.test.ts+++ b/web/apps/space/tests/post-photos.test.ts@@ -1,5 +1,58 @@import { expect, test } from "vitest";-import { viewerPhotosFromPost } from "../src/utils/post-photos";+import {+ spacePostFrameAspectRatio,+ viewerPhotosFromPost,+} from "../src/utils/post-photos";++test("matching portrait videos use their full frame before and after publication", () => {+ const drafts = [+ { width: 1440, height: 2560 },+ { width: 720, height: 1280 },+ { width: 720, height: 1280 },+ ];+ const published = drafts.map(() => ({ width: 576, height: 1024 }));++ expect(spacePostFrameAspectRatio(drafts)).toBe(9 / 16);+ expect(spacePostFrameAspectRatio(published)).toBe(9 / 16);+});++test.each([0, 1, 2])(+ "mixed video ratios use a square frame with cover index %i",+ (coverIndex) => {+ const videos = [+ { width: 1920, height: 1080 },+ { width: 1080, height: 1080 },+ { width: 1080, height: 1920 },+ ];+ const drafts = [+ ...videos.slice(coverIndex),+ ...videos.slice(0, coverIndex),+ ];+ const published = drafts.map(({ width, height }) => ({+ width: width / 2,+ height: height / 2,+ }));++ expect(spacePostFrameAspectRatio(drafts)).toBe(1);+ expect(spacePostFrameAspectRatio(published)).toBe(1);+ },+);++test("a single portrait keeps the feed height limit", () => {+ expect(spacePostFrameAspectRatio([{ width: 720, height: 1280 }])).toBe(+ 3 / 4,+ );+});++test("matching ratios tolerate rounding during resizing", () => {+ expect(+ spacePostFrameAspectRatio([+ { width: 1920, height: 1080 },+ { width: 1024, height: 576 },+ { width: 854, height: 480 },+ ]),+ ).toBe(16 / 9);+});test("profile navigation groups photos under their post and resets each counter", () => {const posts = [diff --git a/web/apps/space/tests/post-publication.test.ts b/web/apps/space/tests/post-publication.test.tsindex 99e7b3e6af..242718f344 100644--- a/web/apps/space/tests/post-publication.test.ts+++ b/web/apps/space/tests/post-publication.test.ts@@ -1,3 +1,4 @@+import type { PostResponse } from "ente-space-wasm";import { beforeEach, expect, test, vi } from "vitest";import {createCurrentMediaPost,@@ -16,6 +17,7 @@ const mocks = vi.hoisted(() => ({},release: vi.fn(),cacheMedia: vi.fn(),+ cacheVideo: vi.fn(),cachePost: vi.fn(),logToDisk: vi.fn(),}));@@ -29,6 +31,7 @@ vi.mock("services/feed-cache", () => ({}));vi.mock("services/media-cache", () => ({rememberCachedSpaceMediaBlobURL: mocks.cacheMedia,+ rememberCachedSpaceVideoBlob: mocks.cacheVideo,spacePostMediaCacheKey: (spaceId: string, key: string) =>`${spaceId}:${key}`,clearSpaceMediaURLCache: vi.fn(),@@ -94,10 +97,10 @@ test("retains a committed session through a failed readback without creating anoconst postId = await createCurrentMediaPost(request);mocks.ctx.getPost.mockRejectedValueOnce(new Error("Offline"));await expect(- loadCurrentCreatedPost("self", postId, [file]),+ loadCurrentCreatedPost("self", postId, images),).rejects.toThrow("Offline");expect(await createCurrentMediaPost(request)).toBe(postId);- const post = await loadCurrentCreatedPost("self", postId, [file]);+ const post = await loadCurrentCreatedPost("self", postId, images);expect(post.postId).toBe(postId);expect(mocks.ctx.createMediaPost).toHaveBeenCalledTimes(1);expect(mocks.ctx.uploadPostPhotoAsset).toHaveBeenCalledTimes(1);@@ -106,7 +109,7 @@ test("retains a committed session through a failed readback without creating ano});test("hydrates a published post from its uploaded previews without another media download", async () => {- const post = await loadCurrentCreatedPost("self", 501, [file]);+ const post = await loadCurrentCreatedPost("self", 501, images);expect(post.imageUrl).toBe("blob:preview");expect(post.photos?.[0]).toMatchObject({imageUrl: "blob:preview",@@ -123,12 +126,31 @@ test("a cache failure after creation does not discard the saved post ID", asyncconst postId = await createCurrentMediaPost(request);mocks.cacheMedia.mockRejectedValueOnce(new Error("Cache unavailable"));await expect(- loadCurrentCreatedPost("self", postId, [file]),+ loadCurrentCreatedPost("self", postId, images),).rejects.toThrow("Cache unavailable");expect(await createCurrentMediaPost(request)).toBe(postId);expect(mocks.ctx.createMediaPost).toHaveBeenCalledTimes(1);});+test("readback caches the exported MP4 under the published video asset", async () => {+ const created = (await mocks.ctx.getPost()) as PostResponse;+ const videoAsset = { ...asset, objectKey: "video-1" };+ created.photos[0]!.video = { asset: videoAsset, durationMs: 1000 };+ const video = {+ file: new File(["video"], "video.mp4", { type: "video/mp4" }),+ durationMs: 1000,+ };+ const post = await loadCurrentCreatedPost("self", 501, [+ { ...images[0]!, video },+ ]);+ expect(mocks.cacheVideo).toHaveBeenCalledWith("self:video-1", video.file);+ expect(post.video?.asset).toEqual({+ ...videoAsset,+ mediaType: "video/mp4",+ });+ expect(mocks.ctx.downloadPostAsset).not.toHaveBeenCalled();+});+test("a rejected creation is still reported as a failure", async () => {mocks.ctx.createMediaPost.mockRejectedValue(new Error("Post limit reached"),diff --git a/web/apps/space/tests/profile-photo-layout.test.ts b/web/apps/space/tests/profile-photo-layout.test.tsindex b6775d2874..c8a098b53c 100644--- a/web/apps/space/tests/profile-photo-layout.test.ts+++ b/web/apps/space/tests/profile-photo-layout.test.ts@@ -40,6 +40,35 @@ test("three portraits share a compact row", () => {).toEqual([3, 3]);});+test.each([+ { ratios: [3 / 4, 0.45], count: 2 },+ { ratios: [0.3, 0.4, 0.5], count: 3 },+])(+ "a row of $count tall portraits fills the grid without cropping",+ ({ ratios }) => {+ const width = 328;+ const rows = profilePhotoRows(photos(ratios), width);+ expect(rows).toHaveLength(1);+ const row = rows[0]!;+ expect(row.width).toBe(width);+ expect(row.height).toBeGreaterThan(width * 0.75);+ expect(+ row.tiles.reduce(+ (sum, tile) => sum + tile.aspectRatio * row.height,+ 0,+ ) ++ (row.tiles.length - 1) * profilePhotoGap,+ ).toBeCloseTo(width);+ },+);++test("a lone portrait within a larger section keeps its height limit", () => {+ const rows = profilePhotoRows(photos([8, 0.25]), 328);+ expect(rows.map(({ tiles }) => tiles.length)).toEqual([1, 1]);+ expect(rows[1]!.height).toBe(246);+ expect(rows[1]!.width).toBe(61.5);+});+test("a landscape can share a row with two portraits to avoid a lone portrait", () => {const tiles = photos([3 / 4, 3 / 4, 16 / 9, 3 / 4, 9 / 16]);expect(@@ -113,7 +142,8 @@ test.each([288, 328, 358, 568])(),).toBeCloseTo(row.width);expect(row.width).toBeLessThanOrEqual(width + 0.001);- expect(row.height).toBeLessThanOrEqual(width * 0.75);+ if (row.tiles.length > 1) expect(row.width).toBe(width);+ else expect(row.height).toBeLessThanOrEqual(width * 0.75);expect(row.height).toBeGreaterThanOrEqual(Math.min(100, width / row.tiles[0]!.aspectRatio),);diff --git a/web/apps/space/tests/video-cover.test.ts b/web/apps/space/tests/video-cover.test.tsindex 6e2c00d243..e332990132 100644--- a/web/apps/space/tests/video-cover.test.ts+++ b/web/apps/space/tests/video-cover.test.ts@@ -1,18 +1,24 @@import { afterEach, beforeEach, expect, test, vi } from "vitest";import {+ prepareSpaceVideo,spaceVideoCover,spaceVideoFrames,spaceVideoInfo,+ startSpaceVideoExport,} from "../src/utils/post-video";const mocks = vi.hoisted(() => ({frames: vi.fn(),info: vi.fn(),log: vi.fn(),+ transcode: vi.fn(),+ duration: vi.fn(),}));vi.mock("../src/utils/video-encoding/web", () => ({extractVideoFramesWeb: mocks.frames,determineVideoInfoWeb: mocks.info,+ transcodeVideoWeb: mocks.transcode,+ determineVideoDurationWeb: mocks.duration,}));vi.mock("ente-base/log-web", () => ({ logToDisk: mocks.log }));vi.mock("utils/thumbhash", () => ({ thumbHashBase64FromCanvas: () => "hash" }));@@ -31,6 +37,8 @@ beforeEach(() => {});mocks.frames.mockResolvedValue([new Blob(["png"], { type: "image/png" })]);mocks.info.mockResolvedValue({ duration: 3.45, width: 1080, height: 1920 });+ mocks.transcode.mockResolvedValue(new Uint8Array([1, 2, 3]));+ mocks.duration.mockResolvedValue(3.45);vi.stubGlobal("createImageBitmap",vi@@ -252,3 +260,91 @@ test("canceling frame extraction does not leave decoded bitmaps or return a cove).rejects.toMatchObject({ name: "AbortError" });expect(createImageBitmap).not.toHaveBeenCalled();});++const videoEdit = { start: 0, end: 3.45, coverTime: 0, muted: false };++test("posting joins the draft export and uses the latest cover without encoding twice", async () => {+ let finish!: (bytes: Uint8Array) => void;+ mocks.transcode.mockImplementationOnce(+ () =>+ new Promise<Uint8Array>((resolve) => {+ finish = resolve;+ }),+ );+ const file = new File(["source"], "source.mov");+ const job = startSpaceVideoExport(file, videoEdit);+ await vi.waitFor(() => expect(mocks.transcode).toHaveBeenCalledOnce());+ const prepared = prepareSpaceVideo(+ file,+ { ...videoEdit, coverTime: 1.25 },+ undefined,+ job.promise,+ );+ finish(new Uint8Array([4, 5, 6]));+ const result = await prepared;+ expect(mocks.transcode).toHaveBeenCalledOnce();+ expect(result.video.file).toBe((await job.promise).file);+ expect(result.video.durationMs).toBe(3450);+ expect(mocks.frames).toHaveBeenCalledWith(+ result.video.file,+ [1.25],+ 960,+ undefined,+ );+});++test("completed exports survive cover changes while trim and mute edits produce new exports", async () => {+ const file = new File(["source"], "source.mov");+ const job = startSpaceVideoExport(file, videoEdit);+ await job.promise;+ job.cancel();+ await prepareSpaceVideo(file, { ...videoEdit, coverTime: 1 });+ expect(mocks.transcode).toHaveBeenCalledOnce();+ await startSpaceVideoExport(file, { ...videoEdit, start: 1 }).promise;+ expect(mocks.transcode).toHaveBeenCalledTimes(2);+ await startSpaceVideoExport(file, { ...videoEdit, start: 1, muted: true })+ .promise;+ expect(mocks.transcode).toHaveBeenCalledTimes(3);+});++test("discarding a draft cancels its export and allows a fresh attempt", async () => {+ mocks.transcode.mockImplementationOnce(+ (_file, _edit, _command, signal: AbortSignal) =>+ new Promise((_, reject) => {+ signal.addEventListener("abort", () =>+ reject(signal.reason as Error),+ );+ }),+ );+ const file = new File(["source"], "source.mov");+ const job = startSpaceVideoExport(file, videoEdit);+ const rejected = expect(job.promise).rejects.toMatchObject({+ name: "AbortError",+ });+ await vi.waitFor(() => expect(mocks.transcode).toHaveBeenCalledOnce());+ job.cancel();+ await rejected;+ await startSpaceVideoExport(file, videoEdit).promise;+ expect(mocks.transcode).toHaveBeenCalledTimes(2);+});++test("canceling during output validation does not cache an abandoned export", async () => {+ let finish!: (duration: number) => void;+ mocks.duration.mockImplementationOnce(+ () =>+ new Promise<number>((resolve) => {+ finish = resolve;+ }),+ );+ const file = new File(["source"], "source.mov");+ const job = startSpaceVideoExport(file, videoEdit);+ const rejected = expect(job.promise).rejects.toMatchObject({+ name: "AbortError",+ });+ await vi.waitFor(() => expect(mocks.duration).toHaveBeenCalledOnce());+ job.cancel();+ finish(3.45);+ await rejected;+ await startSpaceVideoExport(file, videoEdit).promise;+ expect(mocks.transcode).toHaveBeenCalledTimes(2);+});diff --git a/web/apps/space/tests/video-export.test.ts b/web/apps/space/tests/video-export.test.tsindex 65a279fa65..95782f2c4f 100644--- a/web/apps/space/tests/video-export.test.ts+++ b/web/apps/space/tests/video-export.test.ts@@ -6,7 +6,7 @@ const mocks = vi.hoisted(() => ({logToDisk: vi.fn(),workers: [] as {loaded: boolean;- terminate: ReturnType<typeof vi.fn>;+ terminate: ReturnType<typeof vi.fn<() => void>>;exec: ReturnType<typeof vi.fn>;unmount: ReturnType<typeof vi.fn>;readFile: ReturnType<typeof vi.fn>;@@ -179,6 +179,67 @@ test("a queued export can be cancelled without waiting for or stopping another eexpect(mocks.encode).toHaveBeenCalledTimes(1);});+test("canceling active and queued draft exports frees the software encoder for editor frames and restart", async () => {+ vi.stubGlobal("VideoEncoder", undefined);+ mocks.load.mockImplementation(() => {+ const worker = mocks.workers.at(-1)!;+ worker.readFile.mockImplementation((path: string) => {+ if (path == "output.json")+ return JSON.stringify({+ format: { duration: "2" },+ streams: [{}],+ });+ if (path == "frame.json")+ return JSON.stringify({+ format: {},+ packets: [{ pts_time: "0" }, { pts_time: "1" }],+ });+ return new Uint8Array([7]);+ });+ if (mocks.workers.length == 1) {+ const terminate = worker.terminate.getMockImplementation()!;+ worker.exec.mockImplementationOnce(+ () =>+ new Promise<number>((_, reject) => {+ worker.terminate.mockImplementation(() => {+ terminate();+ reject(new Error("Terminated"));+ });+ }),+ );+ }+ return Promise.resolve(true);+ });+ const { startSpaceVideoExport } = await import("../src/utils/post-video");+ const { extractVideoFramesWeb } =+ await import("../src/utils/video-encoding/web");+ const files = [+ new File(["first"], "first.mp4"),+ new File(["second"], "second.mp4"),+ ];+ const edit = { start: 0, end: 2, coverTime: 0, muted: false };+ const jobs = files.map((file) => startSpaceVideoExport(file, edit));+ const canceled = Promise.all(+ jobs.map((job) =>+ expect(job.promise).rejects.toMatchObject({ name: "AbortError" }),+ ),+ );+ await vi.waitFor(() =>+ expect(mocks.workers[0]?.exec).toHaveBeenCalledOnce(),+ );+ jobs.forEach((job) => job.cancel());+ const frames = extractVideoFramesWeb(files[0]!, [0, 1], 128);+ await canceled;+ expect(await frames).toHaveLength(2);+ expect(mocks.workers).toHaveLength(2);+ expect(mocks.workers[0]!.terminate).toHaveBeenCalledOnce();+ expect(mocks.workers[1]!.exec).toHaveBeenCalledTimes(2);+ const restarted = await Promise.all(+ files.map((file) => startSpaceVideoExport(file, edit).promise),+ );+ expect(restarted.map((video) => video.durationMs)).toEqual([2000, 2000]);+});+test("cancelling engine loading terminates it and allows the next export to load again", async () => {mocks.load.mockImplementationOnce(() => new Promise(() => undefined));const run = await exporter();diff --git a/web/packages/base/blob-cache.ts b/web/packages/base/blob-cache.tsindex b472573b75..459b37897d 100644--- a/web/packages/base/blob-cache.ts+++ b/web/packages/base/blob-cache.ts@@ -4,6 +4,7 @@ const blobCacheNames = ["thumbs","face-crops","space-media",+ "space-videos",// Desktop only."files",] as const;