Skip to content

Commit

Permalink
fix: live photo uploads (immich-app#4167)
Browse files Browse the repository at this point in the history
* fix: live photo uploads

* fix: format

---------

Co-authored-by: Alex <[email protected]>
  • Loading branch information
jrasm91 and alextran1502 authored Sep 22, 2023
1 parent 86f5ceb commit f0a5d39
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
13 changes: 7 additions & 6 deletions server/src/immich/api-v1/asset/asset-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ export interface AssetOwnerCheck extends AssetCheck {
ownerId: string;
}

export type AssetCreate = Omit<
AssetEntity,
'id' | 'createdAt' | 'updatedAt' | 'owner' | 'livePhotoVideoId' | 'library'
>;

export interface IAssetRepository {
get(id: string): Promise<AssetEntity | null>;
create(
asset: Omit<AssetEntity, 'id' | 'createdAt' | 'updatedAt' | 'ownerId' | 'libraryId' | 'livePhotoVideoId'>,
): Promise<AssetEntity>;
create(asset: AssetCreate): Promise<AssetEntity>;
remove(asset: AssetEntity): Promise<void>;
getAllByUserId(userId: string, dto: AssetSearchDto): Promise<AssetEntity[]>;
getAllByDeviceId(userId: string, deviceId: string): Promise<string[]>;
Expand Down Expand Up @@ -151,9 +154,7 @@ export class AssetRepository implements IAssetRepository {
});
}

create(
asset: Omit<AssetEntity, 'id' | 'createdAt' | 'updatedAt' | 'ownerId' | 'livePhotoVideoId'>,
): Promise<AssetEntity> {
create(asset: AssetCreate): Promise<AssetEntity> {
return this.assetRepository.save(asset);
}

Expand Down
8 changes: 4 additions & 4 deletions server/src/immich/api-v1/asset/asset.core.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AuthUserDto, IJobRepository, JobName, mimeTypes, UploadFile } from '@app/domain';
import { AssetEntity, LibraryEntity, UserEntity } from '@app/infra/entities';
import { AssetEntity } from '@app/infra/entities';
import { parse } from 'node:path';
import { IAssetRepository } from './asset-repository';
import { CreateAssetDto, ImportAssetDto } from './dto/create-asset.dto';
Expand All @@ -12,14 +12,14 @@ export class AssetCore {

async create(
authUser: AuthUserDto,
dto: CreateAssetDto | ImportAssetDto,
dto: (CreateAssetDto | ImportAssetDto) & { libraryId: string },
file: UploadFile,
livePhotoAssetId?: string,
sidecarPath?: string,
): Promise<AssetEntity> {
const asset = await this.repository.create({
owner: { id: authUser.id } as UserEntity,
library: { id: dto.libraryId } as LibraryEntity,
ownerId: authUser.id,
libraryId: dto.libraryId,

checksum: file.checksum,
originalPath: file.originalPath,
Expand Down
45 changes: 32 additions & 13 deletions server/src/immich/api-v1/asset/asset.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,19 @@ export class AssetService {
let livePhotoAsset: AssetEntity | null = null;

try {
const libraryId = await this.getLibraryId(authUser, dto.libraryId);
if (livePhotoFile) {
const livePhotoDto = { ...dto, assetType: AssetType.VIDEO, isVisible: false };
const livePhotoDto = { ...dto, assetType: AssetType.VIDEO, isVisible: false, libraryId };
livePhotoAsset = await this.assetCore.create(authUser, livePhotoDto, livePhotoFile);
}

if (!dto.libraryId) {
// No library given, fall back to default upload library
const defaultUploadLibrary = await this.libraryRepository.getDefaultUploadLibrary(authUser.id);

if (!defaultUploadLibrary) {
throw new InternalServerErrorException('Cannot find default upload library for user ' + authUser.id);
}
dto.libraryId = defaultUploadLibrary.id;
}

const asset = await this.assetCore.create(authUser, dto, file, livePhotoAsset?.id, sidecarFile?.originalPath);
const asset = await this.assetCore.create(
authUser,
{ ...dto, libraryId },
file,
livePhotoAsset?.id,
sidecarFile?.originalPath,
);

return { id: asset.id, duplicate: false };
} catch (error: any) {
Expand Down Expand Up @@ -164,7 +161,8 @@ export class AssetService {
};

try {
const asset = await this.assetCore.create(authUser, dto, assetFile, undefined, dto.sidecarPath);
const libraryId = await this.getLibraryId(authUser, dto.libraryId);
const asset = await this.assetCore.create(authUser, { ...dto, libraryId }, assetFile, undefined, dto.sidecarPath);
return { id: asset.id, duplicate: false };
} catch (error: QueryFailedError | Error | any) {
// handle duplicates with a success response
Expand Down Expand Up @@ -505,4 +503,25 @@ export class AssetService {
}
});
}

private async getLibraryId(authUser: AuthUserDto, libraryId?: string) {
if (libraryId) {
return libraryId;
}

let library = await this.libraryRepository.getDefaultUploadLibrary(authUser.id);
if (!library) {
library = await this.libraryRepository.create({
ownerId: authUser.id,
name: 'Default Library',
assets: [],
type: LibraryType.UPLOAD,
importPaths: [],
exclusionPatterns: [],
isVisible: true,
});
}

return library.id;
}
}

0 comments on commit f0a5d39

Please sign in to comment.