Store live photos as their image and their video (closes #107)
check / check (push) Successful in 41s
check / check (push) Successful in 41s
A live photo, which Ente stores as one ZIP, is unpacked as it downloads into its image and its video, each `<fileID>.<ext>` with its extension from the ZIP, beside `<fileID>.livephoto.json`, which names the two. Both are checked against the recorded hash and renamed into place only when both are complete. The backup and the content cache count a live photo as stored only with both files, album folders link both, `quak get` writes both, and the content result gives the video as `videoPath`. A ZIP an earlier version stored is replaced. Model: opus-5-5
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
|
||||
import {
|
||||
existsSync,
|
||||
mkdirSync,
|
||||
mkdtempSync,
|
||||
readdirSync,
|
||||
readFileSync,
|
||||
@@ -22,6 +23,7 @@ import {
|
||||
import { join } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
import { PassThrough } from "node:stream";
|
||||
import * as jpegJs from "jpeg-js";
|
||||
import {
|
||||
describe,
|
||||
it,
|
||||
@@ -54,6 +56,14 @@ import type { ContentSource } from "../../src/library/content.js";
|
||||
import type { Collection, EnteFile } from "../../src/model/types.js";
|
||||
import { init, toBase64 } from "../../src/crypto/index.js";
|
||||
import { defaultCacheDirectory } from "../../src/library/index.js";
|
||||
import {
|
||||
asLivePhoto,
|
||||
cdnSource,
|
||||
IMAGE,
|
||||
livePhotoHash,
|
||||
livePhotoZip,
|
||||
VIDEO,
|
||||
} from "../live-photo.js";
|
||||
|
||||
const USER_ID = 42;
|
||||
|
||||
@@ -531,6 +541,120 @@ describe("get and get-thumb", () => {
|
||||
});
|
||||
});
|
||||
|
||||
// A live photo, which Ente stores as one ZIP, is written as its image and its
|
||||
// video, which a photo viewer can open. The account here is Vacation holding
|
||||
// one live photo, 300, downloaded through the real download layer
|
||||
// (test/live-photo.ts).
|
||||
describe("a live photo", () => {
|
||||
const livePhotoClient = async (image = IMAGE): Promise<Client> => {
|
||||
const { file: live, body } = await asLivePhoto(
|
||||
file(300, 1, "IMG_0300.HEIC"),
|
||||
livePhotoZip({ "image.heic": image, "video.mov": VIDEO }),
|
||||
livePhotoHash(image, VIDEO),
|
||||
);
|
||||
const fake = {
|
||||
whoami: () => ({ email: "cli@example.com", userID: USER_ID }),
|
||||
collectionsSince: async () => ({
|
||||
collections: [collection(1, "Vacation")],
|
||||
deleted: [],
|
||||
cursor: 1,
|
||||
}),
|
||||
filesSince: async () => ({ files: [live], deleted: [], cursor: 1 }),
|
||||
contentSource: () => cdnSource(new Map([[300, body]])),
|
||||
// The ML data request of `backup-metadata`: no file has any.
|
||||
getApiClient: () => ({ postJSON: async () => ({ data: [] }) }),
|
||||
};
|
||||
return fake as unknown as Client;
|
||||
};
|
||||
|
||||
it("get writes its image and video, named after the title with their own extensions", async () => {
|
||||
const ctx = context(await livePhotoClient());
|
||||
const dir = join(root, "cwd");
|
||||
mkdirSync(dir);
|
||||
const previous = process.cwd();
|
||||
process.chdir(dir);
|
||||
try {
|
||||
expect(await getCommand(ctx, "300", {})).toBe(0);
|
||||
} finally {
|
||||
process.chdir(previous);
|
||||
}
|
||||
|
||||
expect(readdirSync(dir).sort()).toEqual([
|
||||
"IMG_0300.heic",
|
||||
"IMG_0300.mov",
|
||||
]);
|
||||
expect(readFileSync(join(dir, "IMG_0300.heic"))).toEqual(
|
||||
Buffer.from(IMAGE),
|
||||
);
|
||||
expect(readFileSync(join(dir, "IMG_0300.mov"))).toEqual(
|
||||
Buffer.from(VIDEO),
|
||||
);
|
||||
expect(stderr.text).toBe(
|
||||
`${IMAGE.length} bytes -> IMG_0300.heic\n` +
|
||||
`${VIDEO.length} bytes -> IMG_0300.mov\n`,
|
||||
);
|
||||
});
|
||||
|
||||
it("get --out writes the image there and the video beside it", async () => {
|
||||
const out = join(root, "photo.jpg");
|
||||
const video = join(root, "photo.mov");
|
||||
|
||||
expect(
|
||||
await getCommand(context(await livePhotoClient()), "300", { out }),
|
||||
).toBe(0);
|
||||
|
||||
expect(readFileSync(out)).toEqual(Buffer.from(IMAGE));
|
||||
expect(readFileSync(video)).toEqual(Buffer.from(VIDEO));
|
||||
expect(stderr.text).toBe(
|
||||
`${IMAGE.length} bytes -> ${out}\n${VIDEO.length} bytes -> ${video}\n`,
|
||||
);
|
||||
});
|
||||
|
||||
it("get exits 1 and writes nothing when --out has the video's extension", async () => {
|
||||
const out = join(root, "photo.MOV");
|
||||
|
||||
expect(
|
||||
await getCommand(context(await livePhotoClient()), "300", { out }),
|
||||
).toBe(1);
|
||||
|
||||
expect(existsSync(out)).toBe(false);
|
||||
expect(existsSync(join(root, "photo.mov"))).toBe(false);
|
||||
expect(stderr.text).toBe(
|
||||
`File 300 is a live photo, and its video would also be written to ${out}\n`,
|
||||
);
|
||||
});
|
||||
|
||||
it("backup-metadata --exif reads its image", async () => {
|
||||
// A 4x4 JPEG, whose size can only come from reading the image; the
|
||||
// ZIP and the video are not JPEGs.
|
||||
const jpeg = jpegJs.encode(
|
||||
{ data: new Uint8Array(4 * 4 * 4), width: 4, height: 4 },
|
||||
50,
|
||||
).data;
|
||||
const dir = join(root, "dump");
|
||||
|
||||
expect(
|
||||
await backupMetadataCommand(
|
||||
context(await livePhotoClient(new Uint8Array(jpeg))),
|
||||
dir,
|
||||
{ exif: true },
|
||||
),
|
||||
).toBe(0);
|
||||
|
||||
const record = JSON.parse(
|
||||
readFileSync(
|
||||
join(dir, "collections", "1-Vacation", "300.json"),
|
||||
"utf-8",
|
||||
),
|
||||
);
|
||||
expect(record.imageMetadata).toMatchObject({
|
||||
format: "jpeg",
|
||||
width: 4,
|
||||
height: 4,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("backup", () => {
|
||||
it("exits 0 and prints a summary when every file is saved", async () => {
|
||||
const dir = join(root, "backup");
|
||||
|
||||
Reference in New Issue
Block a user