Skip to content

Commit

Permalink
chore: clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasm91 committed Jun 14, 2024
1 parent 8e7bfdf commit 0fb7af8
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 48 deletions.
25 changes: 15 additions & 10 deletions e2e/src/api/specs/user-admin.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,18 +250,23 @@ describe('/admin/users', () => {
.set('Authorization', `Bearer ${admin.accessToken}`);

expect(status).toBe(200);
expect(body).toEqual({
avatar: { color: 'orange' },
memories: { enabled: false },
emailNotifications: { enabled: true, albumInvite: true, albumUpdate: true },
});
expect(body).toMatchObject({ avatar: { color: 'orange' } });

const after = await getUserPreferencesAdmin({ id: admin.userId }, { headers: asBearerAuth(admin.accessToken) });
expect(after).toEqual({
avatar: { color: 'orange' },
memories: { enabled: false },
emailNotifications: { enabled: true, albumInvite: true, albumUpdate: true },
});
expect(after).toMatchObject({ avatar: { color: 'orange' } });
});

it('should update download archive size', async () => {
const { status, body } = await request(app)
.put(`/admin/users/${admin.userId}/preferences`)
.send({ download: { archiveSize: 1_234_567 } })
.set('Authorization', `Bearer ${admin.accessToken}`);

expect(status).toBe(200);
expect(body).toMatchObject({ download: { archiveSize: 1_234_567 } });

const after = await getUserPreferencesAdmin({ id: admin.userId }, { headers: asBearerAuth(admin.accessToken) });
expect(after).toMatchObject({ download: { archiveSize: 1_234_567 } });
});
});

Expand Down
31 changes: 27 additions & 4 deletions e2e/src/api/specs/user.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,20 +174,43 @@ describe('/users', () => {
expect(after).toMatchObject({ memories: { enabled: false } });
});

it('should update avatar color', async () => {
const { status, body } = await request(app)
.put(`/users/me/preferences`)
.send({ avatar: { color: 'blue' } })
.set('Authorization', `Bearer ${admin.accessToken}`);

expect(status).toBe(200);
expect(body).toMatchObject({ avatar: { color: 'blue' } });

const after = await getMyPreferences({ headers: asBearerAuth(admin.accessToken) });
expect(after).toMatchObject({ avatar: { color: 'blue' } });
});

it('should require an integer for download archive size', async () => {
const { status, body } = await request(app)
.put(`/users/me/preferences`)
.send({ download: { archiveSize: 1_234_567.89 } })
.set('Authorization', `Bearer ${admin.accessToken}`);

expect(status).toBe(400);
expect(body).toEqual(errorDto.badRequest(['download.archiveSize must be an integer number']));
});

it('should update download archive size', async () => {
const before = await getMyPreferences({ headers: asBearerAuth(admin.accessToken) });
expect(before).toMatchObject({ download: { archiveSize: 2 ** 32 } });
expect(before).toMatchObject({ download: { archiveSize: 4 * 2 ** 30 } });

const { status, body } = await request(app)
.put(`/users/me/preferences`)
.send({ download: { archiveSize: 6 * 2 ** 30 } })
.send({ download: { archiveSize: 1_234_567 } })
.set('Authorization', `Bearer ${admin.accessToken}`);

expect(status).toBe(200);
expect(body).toMatchObject({ download: { archiveSize: 6 * 2 ** 30 } });
expect(body).toMatchObject({ download: { archiveSize: 1_234_567 } });

const after = await getMyPreferences({ headers: asBearerAuth(admin.accessToken) });
expect(after).toMatchObject({ download: { archiveSize: 3 * 2 ** 31 } });
expect(after).toMatchObject({ download: { archiveSize: 1_234_567 } });
});
});

Expand Down
4 changes: 2 additions & 2 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.

5 changes: 3 additions & 2 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.

5 changes: 3 additions & 2 deletions open-api/immich-openapi-specs.json
Original file line number Diff line number Diff line change
Expand Up @@ -8128,7 +8128,7 @@
"DownloadResponse": {
"properties": {
"archiveSize": {
"type": "number"
"type": "integer"
}
},
"required": [
Expand Down Expand Up @@ -8157,7 +8157,8 @@
"DownloadUpdate": {
"properties": {
"archiveSize": {
"type": "number"
"minimum": 1,
"type": "integer"
}
},
"type": "object"
Expand Down
7 changes: 5 additions & 2 deletions server/src/dtos/user-preferences.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { IsEnum, IsNumber, ValidateNested } from 'class-validator';
import { IsEnum, IsInt, IsPositive, ValidateNested } from 'class-validator';
import { UserAvatarColor, UserPreferences } from 'src/entities/user-metadata.entity';
import { Optional, ValidateBoolean } from 'src/validation';

Expand Down Expand Up @@ -29,7 +29,9 @@ class EmailNotificationsUpdate {

class DownloadUpdate {
@Optional()
@IsNumber()
@IsInt()
@IsPositive()
@ApiProperty({ type: 'integer' })
archiveSize?: number;
}

Expand Down Expand Up @@ -71,6 +73,7 @@ class EmailNotificationsResponse {
}

class DownloadResponse {
@ApiProperty({ type: 'integer' })
archiveSize!: number;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@
import SettingInputField, {
SettingInputFieldType,
} from '$lib/components/shared-components/settings/setting-input-field.svelte';
import { convertFromBytes, convertToBytes } from '$lib/utils/byte-converter';
const GiB = 2 ** 30;
let archiveSize = $preferences?.download?.archiveSize / GiB;
let archiveSize = convertFromBytes($preferences?.download?.archiveSize || 4, 'GiB');
const handleSave = async () => {
try {
const data = await updateMyPreferences({
userPreferencesUpdateDto: { download: { archiveSize: archiveSize * GiB } },
});
$preferences.download.archiveSize = data.download.archiveSize / GiB;
const dto = { download: { archiveSize: Math.floor(convertToBytes(archiveSize, 'GiB')) } };
const newPreferences = await updateMyPreferences({ userPreferencesUpdateDto: dto });
$preferences = newPreferences;
notificationController.show({ message: $t('saved_settings'), type: NotificationType.Info });
} catch (error) {
Expand All @@ -38,7 +37,7 @@
<div class="ml-4">
<SettingInputField
inputType={SettingInputFieldType.NUMBER}
title={$t('archive_size')}
label={$t('archive_size')}
desc={$t('archive_size_description')}
bind:value={archiveSize}
/>
Expand Down
6 changes: 3 additions & 3 deletions web/src/lib/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,9 @@
"appears_in": "Appears in",
"archive": "Archive",
"archive_or_unarchive_photo": "Archive or unarchive photo",
"archived": "Archived",
"archive_size": "Archive Size",
"archive_size_description": "Configure the archive size for downloads (in GiB)",
"archived": "Archived",
"asset_offline": "Asset offline",
"assets": "Assets",
"authorized_devices": "Authorized Devices",
Expand Down Expand Up @@ -415,9 +415,9 @@
"display_original_photos_setting_description": "Prefer to display the original photo when viewing an asset rather than thumbnails when the original asset is web-compatible. This may result in slower photo display speeds.",
"done": "Done",
"download": "Download",
"downloading": "Downloading",
"download_settings": "Download Settings",
"download_settings": "Download",
"download_settings_description": "Manage settings related to asset download",
"downloading": "Downloading",
"duplicates": "Duplicates",
"duration": "Duration",
"durations": {
Expand Down
9 changes: 9 additions & 0 deletions web/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,12 @@ export const handlePromiseError = <T>(promise: Promise<T>): void => {
export const s = (count: number) => (count === 1 ? '' : 's');

export const memoryLaneTitle = (yearsAgo: number) => `${yearsAgo} year${s(yearsAgo)} ago`;

export const withError = async <T>(fn: () => Promise<T>): Promise<[undefined, T] | [unknown, undefined]> => {
try {
const result = await fn();
return [undefined, result];
} catch (error) {
return [error, undefined];
}
};
24 changes: 10 additions & 14 deletions web/src/lib/utils/asset-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import type { AssetInteractionStore } from '$lib/stores/asset-interaction.store'
import { assetViewingStore } from '$lib/stores/asset-viewing.store';
import { BucketPosition, isSelectingAllAssets, type AssetStore } from '$lib/stores/assets.store';
import { downloadManager } from '$lib/stores/download';
import { downloadRequest, getKey, s } from '$lib/utils';
import { preferences } from '$lib/stores/user.store';
import { downloadRequest, getKey, s, withError } from '$lib/utils';
import { createAlbum } from '$lib/utils/album-utils';
import { asByteUnitString } from '$lib/utils/byte-units';
import { encodeHTMLSpecialChars } from '$lib/utils/string-utils';
Expand All @@ -14,14 +15,12 @@ import {
getAssetInfo,
getBaseUrl,
getDownloadInfo,
getMyPreferences,
updateAsset,
updateAssets,
type AlbumResponseDto,
type AssetResponseDto,
type AssetTypeEnum,
type DownloadInfoDto,
type DownloadResponseDto,
type UserResponseDto,
} from '@immich/sdk';
import { DateTime } from 'luxon';
Expand Down Expand Up @@ -95,22 +94,19 @@ export const downloadBlob = (data: Blob, filename: string) => {
URL.revokeObjectURL(url);
};

export const downloadArchive = async (fileName: string, options: DownloadInfoDto) => {
let downloadInfo: DownloadResponseDto | null = null;
const { download } = await getMyPreferences();
export const downloadArchive = async (fileName: string, options: Omit<DownloadInfoDto, 'archiveSize'>) => {
const $preferences = get(preferences);
const dto = { ...options, archiveSize: $preferences.download.archiveSize };

try {
downloadInfo = await getDownloadInfo({
downloadInfoDto: { archiveSize: download.archiveSize, ...options },
key: getKey(),
});
} catch (error) {
const [error, downloadInfo] = await withError(() => getDownloadInfo({ downloadInfoDto: dto, key: getKey() }));
if (error) {
handleError(error, 'Unable to download files');
return;
}

// TODO: prompt for big download
// const total = downloadInfo.totalSize;
if (!downloadInfo) {
return;
}

for (let index = 0; index < downloadInfo.archives.length; index++) {
const archive = downloadInfo.archives[index];
Expand Down
4 changes: 2 additions & 2 deletions web/src/lib/utils/byte-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @param unit unit to convert from
* @returns bytes (number)
*/
export function convertToBytes(size: number, unit: string): number {
export function convertToBytes(size: number, unit: 'GiB'): number {
let bytes = 0;

if (unit === 'GiB') {
Expand All @@ -26,7 +26,7 @@ export function convertToBytes(size: number, unit: string): number {
* @param unit unit to convert to
* @returns bytes (number)
*/
export function convertFromBytes(bytes: number, unit: string): number {
export function convertFromBytes(bytes: number, unit: 'GiB'): number {
let size = 0;

if (unit === 'GiB') {
Expand Down

0 comments on commit 0fb7af8

Please sign in to comment.