Skip to content

Commit

Permalink
feat(server): do not automatically download android motion videos (#1…
Browse files Browse the repository at this point in the history
…1774)

feat(server): do not automatically download embedded android motion videos
  • Loading branch information
jrasm91 committed Aug 15, 2024
1 parent ed69712 commit 32c05ea
Show file tree
Hide file tree
Showing 13 changed files with 151 additions and 28 deletions.
2 changes: 0 additions & 2 deletions e2e/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.8'

name: immich-e2e

services:
Expand Down
26 changes: 26 additions & 0 deletions e2e/src/api/specs/user.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,32 @@ describe('/users', () => {
const after = await getMyPreferences({ headers: asBearerAuth(admin.accessToken) });
expect(after).toMatchObject({ download: { archiveSize: 1_234_567 } });
});

it('should require a boolean for download include embedded videos', async () => {
const { status, body } = await request(app)
.put(`/users/me/preferences`)
.send({ download: { includeEmbeddedVideos: 1_234_567.89 } })
.set('Authorization', `Bearer ${admin.accessToken}`);

expect(status).toBe(400);
expect(body).toEqual(errorDto.badRequest(['download.includeEmbeddedVideos must be a boolean value']));
});

it('should update download include embedded videos', async () => {
const before = await getMyPreferences({ headers: asBearerAuth(admin.accessToken) });
expect(before).toMatchObject({ download: { includeEmbeddedVideos: false } });

const { status, body } = await request(app)
.put(`/users/me/preferences`)
.send({ download: { includeEmbeddedVideos: true } })
.set('Authorization', `Bearer ${admin.accessToken}`);

expect(status).toBe(200);
expect(body).toMatchObject({ download: { includeEmbeddedVideos: true } });

const after = await getMyPreferences({ headers: asBearerAuth(admin.accessToken) });
expect(after).toMatchObject({ download: { includeEmbeddedVideos: true } });
});
});

describe('GET /users/:id', () => {
Expand Down
14 changes: 11 additions & 3 deletions mobile/openapi/lib/model/download_response.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 20 additions & 3 deletions mobile/openapi/lib/model/download_update.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion open-api/immich-openapi-specs.json
Original file line number Diff line number Diff line change
Expand Up @@ -8497,10 +8497,15 @@
"properties": {
"archiveSize": {
"type": "integer"
},
"includeEmbeddedVideos": {
"default": false,
"type": "boolean"
}
},
"required": [
"archiveSize"
"archiveSize",
"includeEmbeddedVideos"
],
"type": "object"
},
Expand All @@ -8527,6 +8532,9 @@
"archiveSize": {
"minimum": 1,
"type": "integer"
},
"includeEmbeddedVideos": {
"type": "boolean"
}
},
"type": "object"
Expand Down
2 changes: 2 additions & 0 deletions open-api/typescript-sdk/src/fetch-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export type AvatarResponse = {
};
export type DownloadResponse = {
archiveSize: number;
includeEmbeddedVideos: boolean;
};
export type EmailNotificationsResponse = {
albumInvite: boolean;
Expand Down Expand Up @@ -115,6 +116,7 @@ export type AvatarUpdate = {
};
export type DownloadUpdate = {
archiveSize?: number;
includeEmbeddedVideos?: boolean;
};
export type EmailNotificationsUpdate = {
albumInvite?: boolean;
Expand Down
7 changes: 6 additions & 1 deletion server/src/dtos/user-preferences.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ class EmailNotificationsUpdate {
albumUpdate?: boolean;
}

class DownloadUpdate {
class DownloadUpdate implements Partial<DownloadResponse> {
@Optional()
@IsInt()
@IsPositive()
@ApiProperty({ type: 'integer' })
archiveSize?: number;

@ValidateBoolean({ optional: true })
includeEmbeddedVideos?: boolean;
}

class PurchaseUpdate {
Expand Down Expand Up @@ -104,6 +107,8 @@ class EmailNotificationsResponse {
class DownloadResponse {
@ApiProperty({ type: 'integer' })
archiveSize!: number;

includeEmbeddedVideos: boolean = false;
}

class PurchaseResponse {
Expand Down
2 changes: 2 additions & 0 deletions server/src/entities/user-metadata.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface UserPreferences {
};
download: {
archiveSize: number;
includeEmbeddedVideos: boolean;
};
purchase: {
showSupportBadge: boolean;
Expand Down Expand Up @@ -65,6 +66,7 @@ export const getDefaultPreferences = (user: { email: string }): UserPreferences
},
download: {
archiveSize: HumanReadableSize.GiB * 4,
includeEmbeddedVideos: false,
},
purchase: {
showSupportBadge: true,
Expand Down
26 changes: 26 additions & 0 deletions server/src/services/download.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,5 +226,31 @@ describe(DownloadService.name, () => {
],
});
});

it('should skip the video portion of an android live photo by default', async () => {
const assetIds = [assetStub.livePhotoStillAsset.id];
const assets = [
assetStub.livePhotoStillAsset,
{ ...assetStub.livePhotoMotionAsset, originalPath: 'upload/encoded-video/uuid-MP.mp4' },
];

accessMock.asset.checkOwnerAccess.mockResolvedValue(new Set(assetIds));
assetMock.getByIds.mockImplementation(
(ids) =>
Promise.resolve(
ids.map((id) => assets.find((asset) => asset.id === id)).filter((asset) => !!asset),
) as Promise<AssetEntity[]>,
);

await expect(sut.getDownloadInfo(authStub.admin, { assetIds })).resolves.toEqual({
totalSize: 25_000,
archives: [
{
assetIds: [assetStub.livePhotoStillAsset.id],
size: 25_000,
},
],
});
});
});
});
16 changes: 14 additions & 2 deletions server/src/services/download.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BadRequestException, Inject, Injectable } from '@nestjs/common';
import { parse } from 'node:path';
import { AccessCore } from 'src/cores/access.core';
import { StorageCore } from 'src/cores/storage.core';
import { AssetIdsDto } from 'src/dtos/asset.dto';
import { AuthDto } from 'src/dtos/auth.dto';
import { DownloadArchiveInfo, DownloadInfoDto, DownloadResponseDto } from 'src/dtos/download.dto';
Expand All @@ -12,6 +13,7 @@ import { ILoggerRepository } from 'src/interfaces/logger.interface';
import { ImmichReadStream, IStorageRepository } from 'src/interfaces/storage.interface';
import { HumanReadableSize } from 'src/utils/bytes';
import { usePagination } from 'src/utils/pagination';
import { getPreferences } from 'src/utils/preferences';

@Injectable()
export class DownloadService {
Expand All @@ -32,12 +34,22 @@ export class DownloadService {
const archives: DownloadArchiveInfo[] = [];
let archive: DownloadArchiveInfo = { size: 0, assetIds: [] };

const preferences = getPreferences(auth.user);

const assetPagination = await this.getDownloadAssets(auth, dto);
for await (const assets of assetPagination) {
// motion part of live photos
const motionIds = assets.map((asset) => asset.livePhotoVideoId).filter<string>((id): id is string => !!id);
const motionIds = assets.map((asset) => asset.livePhotoVideoId).filter((id): id is string => !!id);
if (motionIds.length > 0) {
assets.push(...(await this.assetRepository.getByIds(motionIds, { exifInfo: true })));
const motionAssets = await this.assetRepository.getByIds(motionIds, { exifInfo: true });
for (const motionAsset of motionAssets) {
if (
!StorageCore.isAndroidMotionPath(motionAsset.originalPath) ||
preferences.download.includeEmbeddedVideos
) {
assets.push(motionAsset);
}
}
}

for (const asset of assets) {
Expand Down
31 changes: 21 additions & 10 deletions web/src/lib/components/user-settings-page/download-settings.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@
SettingInputFieldType,
} from '$lib/components/shared-components/settings/setting-input-field.svelte';
import { ByteUnit, convertFromBytes, convertToBytes } from '$lib/utils/byte-units';
import SettingSwitch from '$lib/components/shared-components/settings/setting-switch.svelte';
let archiveSize = convertFromBytes($preferences?.download?.archiveSize || 4, ByteUnit.GiB);
let includeEmbeddedVideos = $preferences?.download?.includeEmbeddedVideos || false;
const handleSave = async () => {
try {
const dto = { download: { archiveSize: Math.floor(convertToBytes(archiveSize, ByteUnit.GiB)) } };
const newPreferences = await updateMyPreferences({ userPreferencesUpdateDto: dto });
const newPreferences = await updateMyPreferences({
userPreferencesUpdateDto: {
download: {
archiveSize: Math.floor(convertToBytes(archiveSize, ByteUnit.GiB)),
includeEmbeddedVideos,
},
},
});
$preferences = newPreferences;
notificationController.show({ message: $t('saved_settings'), type: NotificationType.Info });
Expand All @@ -34,14 +42,17 @@
<div in:fade={{ duration: 500 }}>
<form autocomplete="off" on:submit|preventDefault>
<div class="ml-4 mt-4 flex flex-col gap-4">
<div class="ml-4">
<SettingInputField
inputType={SettingInputFieldType.NUMBER}
label={$t('archive_size')}
desc={$t('archive_size_description')}
bind:value={archiveSize}
/>
</div>
<SettingInputField
inputType={SettingInputFieldType.NUMBER}
label={$t('archive_size')}
desc={$t('archive_size_description')}
bind:value={archiveSize}
/>
<SettingSwitch
title={$t('download_include_embedded_motion_videos')}
subtitle={$t('download_include_embedded_motion_videos_description')}
bind:checked={includeEmbeddedVideos}
></SettingSwitch>
<div class="flex justify-end">
<Button type="submit" size="sm" on:click={() => handleSave()}>{$t('save')}</Button>
</div>
Expand Down
4 changes: 3 additions & 1 deletion web/src/lib/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@
"appears_in": "Appears in",
"archive": "Archive",
"archive_or_unarchive_photo": "Archive or unarchive photo",
"archive_size": "Archive Size",
"archive_size": "Archive size",
"archive_size_description": "Configure the archive size for downloads (in GiB)",
"archived_count": "{count, plural, other {Archived #}}",
"are_these_the_same_person": "Are these the same person?",
Expand Down Expand Up @@ -512,6 +512,8 @@
"do_not_show_again": "Do not show this message again",
"done": "Done",
"download": "Download",
"download_include_embedded_motion_videos": "Embedded videos",
"download_include_embedded_motion_videos_description": "Include videos embedded in motion photos as a separate file",
"download_settings": "Download",
"download_settings_description": "Manage settings related to asset download",
"downloading": "Downloading",
Expand Down
16 changes: 11 additions & 5 deletions web/src/lib/utils/asset-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,19 @@ export const downloadFile = async (asset: AssetResponseDto) => {
},
];

const isAndroidMotionVideo = (asset: AssetResponseDto) => {
return asset.originalPath.includes('encoded-video');
};

if (asset.livePhotoVideoId) {
const motionAsset = await getAssetInfo({ id: asset.livePhotoVideoId, key: getKey() });
assets.push({
filename: motionAsset.originalFileName,
id: asset.livePhotoVideoId,
size: motionAsset.exifInfo?.fileSizeInByte || 0,
});
if (!isAndroidMotionVideo(motionAsset) || get(preferences).download.includeEmbeddedVideos) {
assets.push({
filename: motionAsset.originalFileName,
id: asset.livePhotoVideoId,
size: motionAsset.exifInfo?.fileSizeInByte || 0,
});
}
}

for (const { filename, id, size } of assets) {
Expand Down

0 comments on commit 32c05ea

Please sign in to comment.