2023-07-09 00:37:40 -04:00
|
|
|
import { AssetEntity } from '@app/infra/entities';
|
2023-07-14 21:31:42 -04:00
|
|
|
import { BadRequestException, Inject, Logger } from '@nestjs/common';
|
2023-06-15 14:05:30 -04:00
|
|
|
import { DateTime } from 'luxon';
|
2023-06-30 12:24:28 -04:00
|
|
|
import { extname } from 'path';
|
2023-07-14 21:31:42 -04:00
|
|
|
import sanitize from 'sanitize-filename';
|
2023-07-09 00:37:40 -04:00
|
|
|
import { AccessCore, IAccessRepository, Permission } from '../access';
|
2023-05-21 08:26:06 +02:00
|
|
|
import { AuthUserDto } from '../auth';
|
2023-07-14 21:31:42 -04:00
|
|
|
import { ICryptoRepository } from '../crypto';
|
2023-07-10 13:56:45 -04:00
|
|
|
import { mimeTypes } from '../domain.constant';
|
2023-06-30 12:24:28 -04:00
|
|
|
import { HumanReadableSize, usePagination } from '../domain.util';
|
2023-08-18 10:31:48 -04:00
|
|
|
import { IJobRepository, JobName } from '../job';
|
2023-09-04 15:45:59 -04:00
|
|
|
import { IStorageRepository, ImmichReadStream, StorageCore, StorageFolder } from '../storage';
|
2023-03-02 21:47:08 -05:00
|
|
|
import { IAssetRepository } from './asset.repository';
|
2023-08-04 17:07:15 -04:00
|
|
|
import {
|
2023-08-16 16:04:55 -04:00
|
|
|
AssetBulkUpdateDto,
|
2023-08-04 17:07:15 -04:00
|
|
|
AssetIdsDto,
|
2023-08-18 10:31:48 -04:00
|
|
|
AssetJobName,
|
|
|
|
|
AssetJobsDto,
|
2023-08-24 21:28:50 +02:00
|
|
|
AssetStatsDto,
|
2023-08-04 17:07:15 -04:00
|
|
|
DownloadArchiveInfo,
|
2023-08-15 11:49:32 -04:00
|
|
|
DownloadInfoDto,
|
2023-08-04 17:07:15 -04:00
|
|
|
DownloadResponseDto,
|
2023-08-24 21:28:50 +02:00
|
|
|
MapMarkerDto,
|
2023-08-04 17:07:15 -04:00
|
|
|
MemoryLaneDto,
|
|
|
|
|
TimeBucketAssetDto,
|
|
|
|
|
TimeBucketDto,
|
2023-09-04 15:45:59 -04:00
|
|
|
mapStats,
|
2023-08-04 17:07:15 -04:00
|
|
|
} from './dto';
|
2023-08-24 21:28:50 +02:00
|
|
|
import {
|
|
|
|
|
AssetResponseDto,
|
|
|
|
|
MapMarkerResponseDto,
|
|
|
|
|
MemoryLaneResponseDto,
|
|
|
|
|
TimeBucketResponseDto,
|
2023-09-04 15:45:59 -04:00
|
|
|
mapAsset,
|
2023-08-24 21:28:50 +02:00
|
|
|
} from './response-dto';
|
2023-02-25 09:12:03 -05:00
|
|
|
|
2023-07-09 00:37:40 -04:00
|
|
|
export enum UploadFieldName {
|
|
|
|
|
ASSET_DATA = 'assetData',
|
|
|
|
|
LIVE_PHOTO_DATA = 'livePhotoData',
|
|
|
|
|
SIDECAR_DATA = 'sidecarData',
|
|
|
|
|
PROFILE_DATA = 'file',
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-14 21:31:42 -04:00
|
|
|
export interface UploadRequest {
|
|
|
|
|
authUser: AuthUserDto | null;
|
|
|
|
|
fieldName: UploadFieldName;
|
|
|
|
|
file: UploadFile;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-09 00:37:40 -04:00
|
|
|
export interface UploadFile {
|
|
|
|
|
checksum: Buffer;
|
|
|
|
|
originalPath: string;
|
|
|
|
|
originalName: string;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-25 09:12:03 -05:00
|
|
|
export class AssetService {
|
2023-07-14 21:31:42 -04:00
|
|
|
private logger = new Logger(AssetService.name);
|
2023-06-30 12:24:28 -04:00
|
|
|
private access: AccessCore;
|
2023-07-14 21:31:42 -04:00
|
|
|
private storageCore = new StorageCore();
|
2023-06-30 12:24:28 -04:00
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
@Inject(IAccessRepository) accessRepository: IAccessRepository,
|
|
|
|
|
@Inject(IAssetRepository) private assetRepository: IAssetRepository,
|
2023-07-14 21:31:42 -04:00
|
|
|
@Inject(ICryptoRepository) private cryptoRepository: ICryptoRepository,
|
2023-08-18 10:31:48 -04:00
|
|
|
@Inject(IJobRepository) private jobRepository: IJobRepository,
|
2023-06-30 12:24:28 -04:00
|
|
|
@Inject(IStorageRepository) private storageRepository: IStorageRepository,
|
|
|
|
|
) {
|
|
|
|
|
this.access = new AccessCore(accessRepository);
|
|
|
|
|
}
|
2023-05-21 08:26:06 +02:00
|
|
|
|
2023-07-14 21:31:42 -04:00
|
|
|
canUploadFile({ authUser, fieldName, file }: UploadRequest): true {
|
|
|
|
|
this.access.requireUploadAccess(authUser);
|
|
|
|
|
|
|
|
|
|
const filename = file.originalName;
|
|
|
|
|
|
|
|
|
|
switch (fieldName) {
|
|
|
|
|
case UploadFieldName.ASSET_DATA:
|
|
|
|
|
if (mimeTypes.isAsset(filename)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case UploadFieldName.LIVE_PHOTO_DATA:
|
|
|
|
|
if (mimeTypes.isVideo(filename)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case UploadFieldName.SIDECAR_DATA:
|
|
|
|
|
if (mimeTypes.isSidecar(filename)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case UploadFieldName.PROFILE_DATA:
|
|
|
|
|
if (mimeTypes.isProfile(filename)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.logger.error(`Unsupported file type ${filename}`);
|
|
|
|
|
throw new BadRequestException(`Unsupported file type ${filename}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getUploadFilename({ authUser, fieldName, file }: UploadRequest): string {
|
|
|
|
|
this.access.requireUploadAccess(authUser);
|
|
|
|
|
|
|
|
|
|
const originalExt = extname(file.originalName);
|
|
|
|
|
|
|
|
|
|
const lookup = {
|
|
|
|
|
[UploadFieldName.ASSET_DATA]: originalExt,
|
|
|
|
|
[UploadFieldName.LIVE_PHOTO_DATA]: '.mov',
|
|
|
|
|
[UploadFieldName.SIDECAR_DATA]: '.xmp',
|
|
|
|
|
[UploadFieldName.PROFILE_DATA]: originalExt,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return sanitize(`${this.cryptoRepository.randomUUID()}${lookup[fieldName]}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getUploadFolder({ authUser, fieldName }: UploadRequest): string {
|
|
|
|
|
authUser = this.access.requireUploadAccess(authUser);
|
|
|
|
|
|
|
|
|
|
let folder = this.storageCore.getFolderLocation(StorageFolder.UPLOAD, authUser.id);
|
|
|
|
|
if (fieldName === UploadFieldName.PROFILE_DATA) {
|
|
|
|
|
folder = this.storageCore.getFolderLocation(StorageFolder.PROFILE, authUser.id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.storageRepository.mkdirSync(folder);
|
|
|
|
|
|
|
|
|
|
return folder;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-21 08:26:06 +02:00
|
|
|
getMapMarkers(authUser: AuthUserDto, options: MapMarkerDto): Promise<MapMarkerResponseDto[]> {
|
|
|
|
|
return this.assetRepository.getMapMarkers(authUser.id, options);
|
|
|
|
|
}
|
2023-06-14 20:47:18 -05:00
|
|
|
|
2023-06-15 14:05:30 -04:00
|
|
|
async getMemoryLane(authUser: AuthUserDto, dto: MemoryLaneDto): Promise<MemoryLaneResponseDto[]> {
|
|
|
|
|
const target = DateTime.fromJSDate(dto.timestamp);
|
2023-06-14 20:47:18 -05:00
|
|
|
|
2023-06-15 14:05:30 -04:00
|
|
|
const onRequest = async (yearsAgo: number): Promise<MemoryLaneResponseDto> => {
|
|
|
|
|
const assets = await this.assetRepository.getByDate(authUser.id, target.minus({ years: yearsAgo }).toJSDate());
|
|
|
|
|
return {
|
|
|
|
|
title: `${yearsAgo} year${yearsAgo > 1 ? 's' : ''} since...`,
|
|
|
|
|
assets: assets.map((a) => mapAsset(a)),
|
|
|
|
|
};
|
|
|
|
|
};
|
2023-06-14 20:47:18 -05:00
|
|
|
|
2023-06-15 14:05:30 -04:00
|
|
|
const requests: Promise<MemoryLaneResponseDto>[] = [];
|
|
|
|
|
for (let i = 1; i <= dto.years; i++) {
|
|
|
|
|
requests.push(onRequest(i));
|
2023-06-14 20:47:18 -05:00
|
|
|
}
|
|
|
|
|
|
2023-06-15 14:05:30 -04:00
|
|
|
return Promise.all(requests).then((results) => results.filter((result) => result.assets.length > 0));
|
2023-06-14 20:47:18 -05:00
|
|
|
}
|
2023-06-30 12:24:28 -04:00
|
|
|
|
2023-08-11 12:00:51 -04:00
|
|
|
private async timeBucketChecks(authUser: AuthUserDto, dto: TimeBucketDto) {
|
|
|
|
|
if (dto.albumId) {
|
|
|
|
|
await this.access.requirePermission(authUser, Permission.ALBUM_READ, [dto.albumId]);
|
|
|
|
|
} else if (dto.userId) {
|
2023-08-15 19:02:38 +03:00
|
|
|
if (dto.isArchived !== false) {
|
|
|
|
|
await this.access.requirePermission(authUser, Permission.ARCHIVE_READ, [dto.userId]);
|
|
|
|
|
}
|
2023-08-11 12:00:51 -04:00
|
|
|
await this.access.requirePermission(authUser, Permission.LIBRARY_READ, [dto.userId]);
|
|
|
|
|
} else {
|
|
|
|
|
dto.userId = authUser.id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-04 17:07:15 -04:00
|
|
|
async getTimeBuckets(authUser: AuthUserDto, dto: TimeBucketDto): Promise<TimeBucketResponseDto[]> {
|
2023-08-11 12:00:51 -04:00
|
|
|
await this.timeBucketChecks(authUser, dto);
|
|
|
|
|
return this.assetRepository.getTimeBuckets(dto);
|
2023-08-04 17:07:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getByTimeBucket(authUser: AuthUserDto, dto: TimeBucketAssetDto): Promise<AssetResponseDto[]> {
|
2023-08-11 12:00:51 -04:00
|
|
|
await this.timeBucketChecks(authUser, dto);
|
|
|
|
|
const assets = await this.assetRepository.getByTimeBucket(dto.timeBucket, dto);
|
2023-08-04 17:07:15 -04:00
|
|
|
return assets.map(mapAsset);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-30 12:24:28 -04:00
|
|
|
async downloadFile(authUser: AuthUserDto, id: string): Promise<ImmichReadStream> {
|
|
|
|
|
await this.access.requirePermission(authUser, Permission.ASSET_DOWNLOAD, id);
|
|
|
|
|
|
|
|
|
|
const [asset] = await this.assetRepository.getByIds([id]);
|
|
|
|
|
if (!asset) {
|
|
|
|
|
throw new BadRequestException('Asset not found');
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-10 13:56:45 -04:00
|
|
|
return this.storageRepository.createReadStream(asset.originalPath, mimeTypes.lookup(asset.originalPath));
|
2023-06-30 12:24:28 -04:00
|
|
|
}
|
|
|
|
|
|
2023-08-15 11:49:32 -04:00
|
|
|
async getDownloadInfo(authUser: AuthUserDto, dto: DownloadInfoDto): Promise<DownloadResponseDto> {
|
2023-06-30 12:24:28 -04:00
|
|
|
const targetSize = dto.archiveSize || HumanReadableSize.GiB * 4;
|
|
|
|
|
const archives: DownloadArchiveInfo[] = [];
|
|
|
|
|
let archive: DownloadArchiveInfo = { size: 0, assetIds: [] };
|
|
|
|
|
|
|
|
|
|
const assetPagination = await this.getDownloadAssets(authUser, 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);
|
|
|
|
|
if (motionIds.length > 0) {
|
|
|
|
|
assets.push(...(await this.assetRepository.getByIds(motionIds)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const asset of assets) {
|
|
|
|
|
archive.size += Number(asset.exifInfo?.fileSizeInByte || 0);
|
|
|
|
|
archive.assetIds.push(asset.id);
|
|
|
|
|
|
|
|
|
|
if (archive.size > targetSize) {
|
|
|
|
|
archives.push(archive);
|
|
|
|
|
archive = { size: 0, assetIds: [] };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (archive.assetIds.length > 0) {
|
|
|
|
|
archives.push(archive);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
totalSize: archives.reduce((total, item) => (total += item.size), 0),
|
|
|
|
|
archives,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async downloadArchive(authUser: AuthUserDto, dto: AssetIdsDto): Promise<ImmichReadStream> {
|
|
|
|
|
await this.access.requirePermission(authUser, Permission.ASSET_DOWNLOAD, dto.assetIds);
|
|
|
|
|
|
|
|
|
|
const zip = this.storageRepository.createZipStream();
|
|
|
|
|
const assets = await this.assetRepository.getByIds(dto.assetIds);
|
2023-07-01 00:44:55 -04:00
|
|
|
const paths: Record<string, number> = {};
|
2023-06-30 12:24:28 -04:00
|
|
|
|
|
|
|
|
for (const { originalPath, originalFileName } of assets) {
|
|
|
|
|
const ext = extname(originalPath);
|
|
|
|
|
let filename = `${originalFileName}${ext}`;
|
2023-07-01 00:44:55 -04:00
|
|
|
const count = paths[filename] || 0;
|
|
|
|
|
paths[filename] = count + 1;
|
|
|
|
|
if (count !== 0) {
|
|
|
|
|
filename = `${originalFileName}+${count}${ext}`;
|
2023-06-30 12:24:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
zip.addFile(originalPath, filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
zip.finalize();
|
|
|
|
|
|
|
|
|
|
return { stream: zip.stream };
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 11:49:32 -04:00
|
|
|
private async getDownloadAssets(authUser: AuthUserDto, dto: DownloadInfoDto): Promise<AsyncGenerator<AssetEntity[]>> {
|
2023-06-30 12:24:28 -04:00
|
|
|
const PAGINATION_SIZE = 2500;
|
|
|
|
|
|
|
|
|
|
if (dto.assetIds) {
|
|
|
|
|
const assetIds = dto.assetIds;
|
|
|
|
|
await this.access.requirePermission(authUser, Permission.ASSET_DOWNLOAD, assetIds);
|
|
|
|
|
const assets = await this.assetRepository.getByIds(assetIds);
|
|
|
|
|
return (async function* () {
|
|
|
|
|
yield assets;
|
|
|
|
|
})();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (dto.albumId) {
|
|
|
|
|
const albumId = dto.albumId;
|
|
|
|
|
await this.access.requirePermission(authUser, Permission.ALBUM_DOWNLOAD, albumId);
|
|
|
|
|
return usePagination(PAGINATION_SIZE, (pagination) => this.assetRepository.getByAlbumId(pagination, albumId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (dto.userId) {
|
|
|
|
|
const userId = dto.userId;
|
|
|
|
|
await this.access.requirePermission(authUser, Permission.LIBRARY_DOWNLOAD, userId);
|
|
|
|
|
return usePagination(PAGINATION_SIZE, (pagination) => this.assetRepository.getByUserId(pagination, userId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new BadRequestException('assetIds, albumId, or userId is required');
|
|
|
|
|
}
|
2023-07-14 09:30:17 -04:00
|
|
|
|
|
|
|
|
async getStatistics(authUser: AuthUserDto, dto: AssetStatsDto) {
|
|
|
|
|
const stats = await this.assetRepository.getStatistics(authUser.id, dto);
|
|
|
|
|
return mapStats(stats);
|
|
|
|
|
}
|
2023-08-16 16:04:55 -04:00
|
|
|
|
|
|
|
|
async updateAll(authUser: AuthUserDto, dto: AssetBulkUpdateDto) {
|
|
|
|
|
const { ids, ...options } = dto;
|
|
|
|
|
await this.access.requirePermission(authUser, Permission.ASSET_UPDATE, ids);
|
|
|
|
|
await this.assetRepository.updateAll(ids, options);
|
|
|
|
|
}
|
2023-08-18 10:31:48 -04:00
|
|
|
|
|
|
|
|
async run(authUser: AuthUserDto, dto: AssetJobsDto) {
|
|
|
|
|
await this.access.requirePermission(authUser, Permission.ASSET_UPDATE, dto.assetIds);
|
|
|
|
|
|
|
|
|
|
for (const id of dto.assetIds) {
|
|
|
|
|
switch (dto.name) {
|
|
|
|
|
case AssetJobName.REFRESH_METADATA:
|
|
|
|
|
await this.jobRepository.queue({ name: JobName.METADATA_EXTRACTION, data: { id } });
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case AssetJobName.REGENERATE_THUMBNAIL:
|
|
|
|
|
await this.jobRepository.queue({ name: JobName.GENERATE_JPEG_THUMBNAIL, data: { id } });
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case AssetJobName.TRANSCODE_VIDEO:
|
|
|
|
|
await this.jobRepository.queue({ name: JobName.VIDEO_CONVERSION, data: { id } });
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-25 09:12:03 -05:00
|
|
|
}
|