You've already forked immich
mirror of
https://github.com/immich-app/immich.git
synced 2025-08-10 23:22:22 +02:00
WIP refactor container and queuing system (#206)
* refactor microservices to machine-learning * Update tGithub issue template with correct task syntax * Added microservices container * Communicate between service based on queue system * added dependency * Fixed problem with having to import BullQueue into the individual service * Added todo * refactor server into monorepo with microservices * refactor database and entity to library * added simple migration * Move migrations and database config to library * Migration works in library * Cosmetic change in logging message * added user dto * Fixed issue with testing not able to find the shared library * Clean up library mapping path * Added webp generator to microservices * Update Github Action build latest * Fixed issue NPM cannot install due to conflict witl Bull Queue * format project with prettier * Modified docker-compose file * Add GH Action for Staging build: * Fixed GH action job name * Modified GH Action to only build & push latest when pushing to main * Added Test 2e2 Github Action * Added Test 2e2 Github Action * Implemented microservice to extract exif * Added cronjob to scan and generate webp thumbnail at midnight * Refactor to ireduce hit time to database when running microservices * Added error handling to asset services that handle read file from disk * Added video transcoding queue to process one video at a time * Fixed loading spinner on web while loading covering the info panel * Add mechanism to show new release announcement to web and mobile app (#209) * Added changelog page * Fixed issues based on PR comments * Fixed issue with video transcoding run on the server * Change entry point content for backward combatibility when starting up server * Added announcement box * Added error handling to failed silently when the app version checking is not able to make the request to GITHUB * Added new version announcement overlay * Update message * Added messages * Added logic to check and show announcement * Add method to handle saving new version * Added button to dimiss the acknowledge message * Up version for deployment to the app store
This commit is contained in:
174
server/apps/immich/src/api-v1/asset/asset.controller.ts
Normal file
174
server/apps/immich/src/api-v1/asset/asset.controller.ts
Normal file
@@ -0,0 +1,174 @@
|
||||
import {
|
||||
Controller,
|
||||
Post,
|
||||
UseInterceptors,
|
||||
UploadedFiles,
|
||||
Body,
|
||||
UseGuards,
|
||||
Get,
|
||||
Param,
|
||||
ValidationPipe,
|
||||
StreamableFile,
|
||||
Query,
|
||||
Response,
|
||||
Headers,
|
||||
Delete,
|
||||
Logger,
|
||||
Patch,
|
||||
} from '@nestjs/common';
|
||||
import { JwtAuthGuard } from '../../modules/immich-jwt/guards/jwt-auth.guard';
|
||||
import { AssetService } from './asset.service';
|
||||
import { FileFieldsInterceptor } from '@nestjs/platform-express';
|
||||
import { assetUploadOption } from '../../config/asset-upload.config';
|
||||
import { AuthUserDto, GetAuthUser } from '../../decorators/auth-user.decorator';
|
||||
import { CreateAssetDto } from './dto/create-asset.dto';
|
||||
import { ServeFileDto } from './dto/serve-file.dto';
|
||||
import { AssetEntity } from '@app/database/entities/asset.entity';
|
||||
import { GetAllAssetQueryDto } from './dto/get-all-asset-query.dto';
|
||||
import { Response as Res } from 'express';
|
||||
import { GetNewAssetQueryDto } from './dto/get-new-asset-query.dto';
|
||||
import { BackgroundTaskService } from '../../modules/background-task/background-task.service';
|
||||
import { DeleteAssetDto } from './dto/delete-asset.dto';
|
||||
import { SearchAssetDto } from './dto/search-asset.dto';
|
||||
import { CommunicationGateway } from '../communication/communication.gateway';
|
||||
import { InjectQueue } from '@nestjs/bull';
|
||||
import { Queue } from 'bull';
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Controller('asset')
|
||||
export class AssetController {
|
||||
constructor(
|
||||
private wsCommunicateionGateway: CommunicationGateway,
|
||||
private assetService: AssetService,
|
||||
private backgroundTaskService: BackgroundTaskService,
|
||||
|
||||
@InjectQueue('asset-uploaded-queue')
|
||||
private assetUploadedQueue: Queue,
|
||||
) {}
|
||||
|
||||
@Post('upload')
|
||||
@UseInterceptors(
|
||||
FileFieldsInterceptor(
|
||||
[
|
||||
{ name: 'assetData', maxCount: 1 },
|
||||
{ name: 'thumbnailData', maxCount: 1 },
|
||||
],
|
||||
assetUploadOption,
|
||||
),
|
||||
)
|
||||
async uploadFile(
|
||||
@GetAuthUser() authUser,
|
||||
@UploadedFiles() uploadFiles: { assetData: Express.Multer.File[]; thumbnailData?: Express.Multer.File[] },
|
||||
@Body(ValidationPipe) assetInfo: CreateAssetDto,
|
||||
) {
|
||||
for (const file of uploadFiles.assetData) {
|
||||
try {
|
||||
const savedAsset = await this.assetService.createUserAsset(authUser, assetInfo, file.path, file.mimetype);
|
||||
|
||||
if (uploadFiles.thumbnailData != null && savedAsset) {
|
||||
const assetWithThumbnail = await this.assetService.updateThumbnailInfo(
|
||||
savedAsset,
|
||||
uploadFiles.thumbnailData[0].path,
|
||||
);
|
||||
|
||||
await this.assetUploadedQueue.add(
|
||||
'asset-uploaded',
|
||||
{ asset: assetWithThumbnail, fileName: file.originalname, fileSize: file.size, hasThumbnail: true },
|
||||
{ jobId: savedAsset.id },
|
||||
);
|
||||
} else {
|
||||
await this.assetUploadedQueue.add(
|
||||
'asset-uploaded',
|
||||
{ asset: savedAsset, fileName: file.originalname, fileSize: file.size, hasThumbnail: false },
|
||||
{ jobId: savedAsset.id },
|
||||
);
|
||||
}
|
||||
|
||||
this.wsCommunicateionGateway.server.to(savedAsset.userId).emit('on_upload_success', JSON.stringify(savedAsset));
|
||||
} catch (e) {
|
||||
Logger.error(`Error receiving upload file ${e}`);
|
||||
}
|
||||
}
|
||||
|
||||
return 'ok';
|
||||
}
|
||||
|
||||
@Get('/download')
|
||||
async downloadFile(
|
||||
@GetAuthUser() authUser: AuthUserDto,
|
||||
@Response({ passthrough: true }) res: Res,
|
||||
@Query(ValidationPipe) query: ServeFileDto,
|
||||
) {
|
||||
return this.assetService.downloadFile(query, res);
|
||||
}
|
||||
|
||||
@Get('/file')
|
||||
async serveFile(
|
||||
@Headers() headers,
|
||||
@GetAuthUser() authUser: AuthUserDto,
|
||||
@Response({ passthrough: true }) res: Res,
|
||||
@Query(ValidationPipe) query: ServeFileDto,
|
||||
): Promise<StreamableFile> {
|
||||
return this.assetService.serveFile(authUser, query, res, headers);
|
||||
}
|
||||
|
||||
@Get('/thumbnail/:assetId')
|
||||
async getAssetThumbnail(@Param('assetId') assetId: string): Promise<StreamableFile> {
|
||||
return await this.assetService.getAssetThumbnail(assetId);
|
||||
}
|
||||
|
||||
@Get('/allObjects')
|
||||
async getCuratedObject(@GetAuthUser() authUser: AuthUserDto) {
|
||||
return this.assetService.getCuratedObject(authUser);
|
||||
}
|
||||
|
||||
@Get('/allLocation')
|
||||
async getCuratedLocation(@GetAuthUser() authUser: AuthUserDto) {
|
||||
return this.assetService.getCuratedLocation(authUser);
|
||||
}
|
||||
|
||||
@Get('/searchTerm')
|
||||
async getAssetSearchTerm(@GetAuthUser() authUser: AuthUserDto) {
|
||||
return this.assetService.getAssetSearchTerm(authUser);
|
||||
}
|
||||
|
||||
@Post('/search')
|
||||
async searchAsset(@GetAuthUser() authUser: AuthUserDto, @Body(ValidationPipe) searchAssetDto: SearchAssetDto) {
|
||||
return this.assetService.searchAsset(authUser, searchAssetDto);
|
||||
}
|
||||
|
||||
@Get('/')
|
||||
async getAllAssets(@GetAuthUser() authUser: AuthUserDto) {
|
||||
return await this.assetService.getAllAssets(authUser);
|
||||
}
|
||||
|
||||
@Get('/:deviceId')
|
||||
async getUserAssetsByDeviceId(@GetAuthUser() authUser: AuthUserDto, @Param('deviceId') deviceId: string) {
|
||||
return await this.assetService.getUserAssetsByDeviceId(authUser, deviceId);
|
||||
}
|
||||
|
||||
@Get('/assetById/:assetId')
|
||||
async getAssetById(@GetAuthUser() authUser: AuthUserDto, @Param('assetId') assetId) {
|
||||
return await this.assetService.getAssetById(authUser, assetId);
|
||||
}
|
||||
|
||||
@Delete('/')
|
||||
async deleteAssetById(@GetAuthUser() authUser: AuthUserDto, @Body(ValidationPipe) assetIds: DeleteAssetDto) {
|
||||
const deleteAssetList: AssetEntity[] = [];
|
||||
|
||||
for (const id of assetIds.ids) {
|
||||
const assets = await this.assetService.getAssetById(authUser, id);
|
||||
deleteAssetList.push(assets);
|
||||
}
|
||||
|
||||
const result = await this.assetService.deleteAssetById(authUser, assetIds);
|
||||
|
||||
result.forEach((res) => {
|
||||
deleteAssetList.filter((a) => a.id == res.id && res.status == 'success');
|
||||
});
|
||||
|
||||
await this.backgroundTaskService.deleteFileOnDisk(deleteAssetList);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
29
server/apps/immich/src/api-v1/asset/asset.module.ts
Normal file
29
server/apps/immich/src/api-v1/asset/asset.module.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AssetService } from './asset.service';
|
||||
import { AssetController } from './asset.controller';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { AssetEntity } from '@app/database/entities/asset.entity';
|
||||
import { BullModule } from '@nestjs/bull';
|
||||
import { BackgroundTaskModule } from '../../modules/background-task/background-task.module';
|
||||
import { BackgroundTaskService } from '../../modules/background-task/background-task.service';
|
||||
import { CommunicationModule } from '../communication/communication.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
CommunicationModule,
|
||||
BackgroundTaskModule,
|
||||
TypeOrmModule.forFeature([AssetEntity]),
|
||||
BullModule.registerQueue({
|
||||
name: 'asset-uploaded-queue',
|
||||
defaultJobOptions: {
|
||||
attempts: 3,
|
||||
removeOnComplete: true,
|
||||
removeOnFail: false,
|
||||
},
|
||||
}),
|
||||
],
|
||||
controllers: [AssetController],
|
||||
providers: [AssetService, BackgroundTaskService],
|
||||
exports: [],
|
||||
})
|
||||
export class AssetModule {}
|
384
server/apps/immich/src/api-v1/asset/asset.service.ts
Normal file
384
server/apps/immich/src/api-v1/asset/asset.service.ts
Normal file
@@ -0,0 +1,384 @@
|
||||
import { BadRequestException, Injectable, InternalServerErrorException, Logger, StreamableFile } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { AuthUserDto } from '../../decorators/auth-user.decorator';
|
||||
import { CreateAssetDto } from './dto/create-asset.dto';
|
||||
import { AssetEntity, AssetType } from '@app/database/entities/asset.entity';
|
||||
import _ from 'lodash';
|
||||
import { createReadStream, stat } from 'fs';
|
||||
import { ServeFileDto } from './dto/serve-file.dto';
|
||||
import { Response as Res } from 'express';
|
||||
import { promisify } from 'util';
|
||||
import { DeleteAssetDto } from './dto/delete-asset.dto';
|
||||
import { SearchAssetDto } from './dto/search-asset.dto';
|
||||
|
||||
const fileInfo = promisify(stat);
|
||||
|
||||
@Injectable()
|
||||
export class AssetService {
|
||||
constructor(
|
||||
@InjectRepository(AssetEntity)
|
||||
private assetRepository: Repository<AssetEntity>,
|
||||
) {}
|
||||
|
||||
public async updateThumbnailInfo(asset: AssetEntity, thumbnailPath: string): Promise<AssetEntity> {
|
||||
const updatedAsset = await this.assetRepository
|
||||
.createQueryBuilder('assets')
|
||||
.update<AssetEntity>(AssetEntity, { ...asset, resizePath: thumbnailPath })
|
||||
.where('assets.id = :id', { id: asset.id })
|
||||
.returning('*')
|
||||
.updateEntity(true)
|
||||
.execute();
|
||||
|
||||
return updatedAsset.raw[0];
|
||||
}
|
||||
|
||||
public async createUserAsset(authUser: AuthUserDto, assetInfo: CreateAssetDto, path: string, mimeType: string) {
|
||||
const asset = new AssetEntity();
|
||||
asset.deviceAssetId = assetInfo.deviceAssetId;
|
||||
asset.userId = authUser.id;
|
||||
asset.deviceId = assetInfo.deviceId;
|
||||
asset.type = assetInfo.assetType || AssetType.OTHER;
|
||||
asset.originalPath = path;
|
||||
asset.createdAt = assetInfo.createdAt;
|
||||
asset.modifiedAt = assetInfo.modifiedAt;
|
||||
asset.isFavorite = assetInfo.isFavorite;
|
||||
asset.mimeType = mimeType;
|
||||
asset.duration = assetInfo.duration;
|
||||
|
||||
try {
|
||||
return await this.assetRepository.save(asset);
|
||||
} catch (e) {
|
||||
Logger.error(`Error Create New Asset ${e}`, 'createUserAsset');
|
||||
}
|
||||
}
|
||||
|
||||
public async getUserAssetsByDeviceId(authUser: AuthUserDto, deviceId: string) {
|
||||
const rows = await this.assetRepository.find({
|
||||
where: {
|
||||
userId: authUser.id,
|
||||
deviceId: deviceId,
|
||||
},
|
||||
select: ['deviceAssetId'],
|
||||
});
|
||||
|
||||
const res = [];
|
||||
rows.forEach((v) => res.push(v.deviceAssetId));
|
||||
return res;
|
||||
}
|
||||
|
||||
public async getAllAssets(authUser: AuthUserDto) {
|
||||
try {
|
||||
return await this.assetRepository.find({
|
||||
where: {
|
||||
userId: authUser.id,
|
||||
},
|
||||
relations: ['exifInfo'],
|
||||
order: {
|
||||
createdAt: 'DESC',
|
||||
},
|
||||
});
|
||||
} catch (e) {
|
||||
Logger.error(e, 'getAllAssets');
|
||||
}
|
||||
}
|
||||
|
||||
public async findOne(deviceId: string, assetId: string): Promise<AssetEntity> {
|
||||
const rows = await this.assetRepository.query(
|
||||
'SELECT * FROM assets a WHERE a."deviceAssetId" = $1 AND a."deviceId" = $2',
|
||||
[assetId, deviceId],
|
||||
);
|
||||
|
||||
if (rows.lengh == 0) {
|
||||
throw new BadRequestException('Not Found');
|
||||
}
|
||||
|
||||
return rows[0] as AssetEntity;
|
||||
}
|
||||
|
||||
public async getAssetById(authUser: AuthUserDto, assetId: string) {
|
||||
return await this.assetRepository.findOne({
|
||||
where: {
|
||||
id: assetId,
|
||||
},
|
||||
relations: ['exifInfo'],
|
||||
});
|
||||
}
|
||||
|
||||
public async downloadFile(query: ServeFileDto, res: Res) {
|
||||
try {
|
||||
let file = null;
|
||||
const asset = await this.findOne(query.did, query.aid);
|
||||
|
||||
if (query.isThumb === 'false' || !query.isThumb) {
|
||||
const { size } = await fileInfo(asset.originalPath);
|
||||
res.set({
|
||||
'Content-Type': asset.mimeType,
|
||||
'Content-Length': size,
|
||||
});
|
||||
file = createReadStream(asset.originalPath);
|
||||
} else {
|
||||
const { size } = await fileInfo(asset.resizePath);
|
||||
res.set({
|
||||
'Content-Type': 'image/jpeg',
|
||||
'Content-Length': size,
|
||||
});
|
||||
file = createReadStream(asset.resizePath);
|
||||
}
|
||||
|
||||
return new StreamableFile(file);
|
||||
} catch (e) {
|
||||
Logger.error('Error download asset ', e);
|
||||
throw new InternalServerErrorException(`Failed to download asset ${e}`, 'DownloadFile');
|
||||
}
|
||||
}
|
||||
|
||||
public async getAssetThumbnail(assetId: string) {
|
||||
try {
|
||||
const asset = await this.assetRepository.findOne({ id: assetId });
|
||||
|
||||
if (asset.webpPath && asset.webpPath.length > 0) {
|
||||
return new StreamableFile(createReadStream(asset.webpPath));
|
||||
} else {
|
||||
return new StreamableFile(createReadStream(asset.resizePath));
|
||||
}
|
||||
} catch (e) {
|
||||
Logger.error('Error serving asset thumbnail ', e);
|
||||
throw new InternalServerErrorException('Failed to serve asset thumbnail', 'GetAssetThumbnail');
|
||||
}
|
||||
}
|
||||
|
||||
public async serveFile(authUser: AuthUserDto, query: ServeFileDto, res: Res, headers: any) {
|
||||
let file = null;
|
||||
const asset = await this.findOne(query.did, query.aid);
|
||||
|
||||
if (!asset) {
|
||||
throw new BadRequestException('Asset does not exist');
|
||||
}
|
||||
|
||||
// Handle Sending Images
|
||||
if (asset.type == AssetType.IMAGE || query.isThumb == 'true') {
|
||||
/**
|
||||
* Serve file viewer on the web
|
||||
*/
|
||||
if (query.isWeb) {
|
||||
res.set({
|
||||
'Content-Type': 'image/jpeg',
|
||||
});
|
||||
return new StreamableFile(createReadStream(asset.resizePath));
|
||||
}
|
||||
|
||||
try {
|
||||
/**
|
||||
* Serve thumbnail image for both web and mobile app
|
||||
*/
|
||||
if (query.isThumb === 'false' || !query.isThumb) {
|
||||
res.set({
|
||||
'Content-Type': asset.mimeType,
|
||||
});
|
||||
file = createReadStream(asset.originalPath);
|
||||
} else {
|
||||
if (asset.webpPath && asset.webpPath.length > 0) {
|
||||
res.set({
|
||||
'Content-Type': 'image/webp',
|
||||
});
|
||||
|
||||
file = createReadStream(asset.webpPath);
|
||||
} else {
|
||||
res.set({
|
||||
'Content-Type': 'image/jpeg',
|
||||
});
|
||||
file = createReadStream(asset.resizePath);
|
||||
}
|
||||
}
|
||||
|
||||
file.on('error', (error) => {
|
||||
Logger.log(`Cannot create read stream ${error}`);
|
||||
return new BadRequestException('Cannot Create Read Stream');
|
||||
});
|
||||
|
||||
return new StreamableFile(file);
|
||||
} catch (e) {
|
||||
Logger.error('Error serving IMAGE asset ', e);
|
||||
throw new InternalServerErrorException(`Failed to serve image asset ${e}`, 'ServeFile');
|
||||
}
|
||||
} else if (asset.type == AssetType.VIDEO) {
|
||||
try {
|
||||
// Handle Video
|
||||
let videoPath = asset.originalPath;
|
||||
let mimeType = asset.mimeType;
|
||||
|
||||
if (query.isWeb && asset.mimeType == 'video/quicktime') {
|
||||
videoPath = asset.encodedVideoPath == '' ? asset.originalPath : asset.encodedVideoPath;
|
||||
mimeType = asset.encodedVideoPath == '' ? asset.mimeType : 'video/mp4';
|
||||
}
|
||||
|
||||
const { size } = await fileInfo(videoPath);
|
||||
const range = headers.range;
|
||||
|
||||
if (range) {
|
||||
/** Extracting Start and End value from Range Header */
|
||||
let [start, end] = range.replace(/bytes=/, '').split('-');
|
||||
start = parseInt(start, 10);
|
||||
end = end ? parseInt(end, 10) : size - 1;
|
||||
|
||||
if (!isNaN(start) && isNaN(end)) {
|
||||
start = start;
|
||||
end = size - 1;
|
||||
}
|
||||
if (isNaN(start) && !isNaN(end)) {
|
||||
start = size - end;
|
||||
end = size - 1;
|
||||
}
|
||||
|
||||
// Handle unavailable range request
|
||||
if (start >= size || end >= size) {
|
||||
console.error('Bad Request');
|
||||
// Return the 416 Range Not Satisfiable.
|
||||
res.status(416).set({
|
||||
'Content-Range': `bytes */${size}`,
|
||||
});
|
||||
|
||||
throw new BadRequestException('Bad Request Range');
|
||||
}
|
||||
|
||||
/** Sending Partial Content With HTTP Code 206 */
|
||||
|
||||
res.status(206).set({
|
||||
'Content-Range': `bytes ${start}-${end}/${size}`,
|
||||
'Accept-Ranges': 'bytes',
|
||||
'Content-Length': end - start + 1,
|
||||
'Content-Type': mimeType,
|
||||
});
|
||||
|
||||
const videoStream = createReadStream(videoPath, { start: start, end: end });
|
||||
|
||||
return new StreamableFile(videoStream);
|
||||
} else {
|
||||
res.set({
|
||||
'Content-Type': mimeType,
|
||||
});
|
||||
|
||||
return new StreamableFile(createReadStream(videoPath));
|
||||
}
|
||||
} catch (e) {
|
||||
Logger.error('Error serving VIDEO asset ', e);
|
||||
throw new InternalServerErrorException(`Failed to serve video asset ${e}`, 'ServeFile');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async deleteAssetById(authUser: AuthUserDto, assetIds: DeleteAssetDto) {
|
||||
const result = [];
|
||||
|
||||
const target = assetIds.ids;
|
||||
for (const assetId of target) {
|
||||
const res = await this.assetRepository.delete({
|
||||
id: assetId,
|
||||
userId: authUser.id,
|
||||
});
|
||||
|
||||
if (res.affected) {
|
||||
result.push({
|
||||
id: assetId,
|
||||
status: 'success',
|
||||
});
|
||||
} else {
|
||||
result.push({
|
||||
id: assetId,
|
||||
status: 'failed',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async getAssetSearchTerm(authUser: AuthUserDto): Promise<string[]> {
|
||||
const possibleSearchTerm = new Set<string>();
|
||||
const rows = await this.assetRepository.query(
|
||||
`
|
||||
select distinct si.tags, si.objects, e.orientation, e."lensModel", e.make, e.model , a.type, e.city, e.state, e.country
|
||||
from assets a
|
||||
left join exif e on a.id = e."assetId"
|
||||
left join smart_info si on a.id = si."assetId"
|
||||
where a."userId" = $1;
|
||||
`,
|
||||
[authUser.id],
|
||||
);
|
||||
|
||||
rows.forEach((row) => {
|
||||
// tags
|
||||
row['tags']?.map((tag) => possibleSearchTerm.add(tag?.toLowerCase()));
|
||||
|
||||
// objects
|
||||
row['objects']?.map((object) => possibleSearchTerm.add(object?.toLowerCase()));
|
||||
|
||||
// asset's tyoe
|
||||
possibleSearchTerm.add(row['type']?.toLowerCase());
|
||||
|
||||
// image orientation
|
||||
possibleSearchTerm.add(row['orientation']?.toLowerCase());
|
||||
|
||||
// Lens model
|
||||
possibleSearchTerm.add(row['lensModel']?.toLowerCase());
|
||||
|
||||
// Make and model
|
||||
possibleSearchTerm.add(row['make']?.toLowerCase());
|
||||
possibleSearchTerm.add(row['model']?.toLowerCase());
|
||||
|
||||
// Location
|
||||
possibleSearchTerm.add(row['city']?.toLowerCase());
|
||||
possibleSearchTerm.add(row['state']?.toLowerCase());
|
||||
possibleSearchTerm.add(row['country']?.toLowerCase());
|
||||
});
|
||||
|
||||
return Array.from(possibleSearchTerm).filter((x) => x != null);
|
||||
}
|
||||
|
||||
async searchAsset(authUser: AuthUserDto, searchAssetDto: SearchAssetDto) {
|
||||
const query = `
|
||||
SELECT a.*
|
||||
FROM assets a
|
||||
LEFT JOIN smart_info si ON a.id = si."assetId"
|
||||
LEFT JOIN exif e ON a.id = e."assetId"
|
||||
|
||||
WHERE a."userId" = $1
|
||||
AND
|
||||
(
|
||||
TO_TSVECTOR('english', ARRAY_TO_STRING(si.tags, ',')) @@ PLAINTO_TSQUERY('english', $2) OR
|
||||
TO_TSVECTOR('english', ARRAY_TO_STRING(si.objects, ',')) @@ PLAINTO_TSQUERY('english', $2) OR
|
||||
e.exif_text_searchable_column @@ PLAINTO_TSQUERY('english', $2)
|
||||
);
|
||||
`;
|
||||
|
||||
return await this.assetRepository.query(query, [authUser.id, searchAssetDto.searchTerm]);
|
||||
}
|
||||
|
||||
async getCuratedLocation(authUser: AuthUserDto) {
|
||||
return await this.assetRepository.query(
|
||||
`
|
||||
select distinct on (e.city) a.id, e.city, a."resizePath", a."deviceAssetId", a."deviceId"
|
||||
from assets a
|
||||
left join exif e on a.id = e."assetId"
|
||||
where a."userId" = $1
|
||||
and e.city is not null
|
||||
and a.type = 'IMAGE';
|
||||
`,
|
||||
[authUser.id],
|
||||
);
|
||||
}
|
||||
|
||||
async getCuratedObject(authUser: AuthUserDto) {
|
||||
return await this.assetRepository.query(
|
||||
`
|
||||
select distinct on (unnest(si.objects)) a.id, unnest(si.objects) as "object", a."resizePath", a."deviceAssetId", a."deviceId"
|
||||
from assets a
|
||||
left join smart_info si on a.id = si."assetId"
|
||||
where a."userId" = $1
|
||||
and si.objects is not null
|
||||
`,
|
||||
[authUser.id],
|
||||
);
|
||||
}
|
||||
}
|
28
server/apps/immich/src/api-v1/asset/dto/create-asset.dto.ts
Normal file
28
server/apps/immich/src/api-v1/asset/dto/create-asset.dto.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { IsNotEmpty, IsOptional } from 'class-validator';
|
||||
import { AssetType } from '@app/database/entities/asset.entity';
|
||||
|
||||
export class CreateAssetDto {
|
||||
@IsNotEmpty()
|
||||
deviceAssetId: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
deviceId: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
assetType: AssetType;
|
||||
|
||||
@IsNotEmpty()
|
||||
createdAt: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
modifiedAt: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
isFavorite: boolean;
|
||||
|
||||
@IsNotEmpty()
|
||||
fileExtension: string;
|
||||
|
||||
@IsOptional()
|
||||
duration: string;
|
||||
}
|
48
server/apps/immich/src/api-v1/asset/dto/create-exif.dto.ts
Normal file
48
server/apps/immich/src/api-v1/asset/dto/create-exif.dto.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { IsNotEmpty, IsOptional } from 'class-validator';
|
||||
|
||||
export class CreateExifDto {
|
||||
@IsNotEmpty()
|
||||
assetId: string;
|
||||
|
||||
@IsOptional()
|
||||
make: string;
|
||||
|
||||
@IsOptional()
|
||||
model: string;
|
||||
|
||||
@IsOptional()
|
||||
imageName: string;
|
||||
|
||||
@IsOptional()
|
||||
exifImageWidth: number;
|
||||
|
||||
@IsOptional()
|
||||
exifImageHeight: number;
|
||||
|
||||
@IsOptional()
|
||||
fileSizeInByte: number;
|
||||
|
||||
@IsOptional()
|
||||
orientation: string;
|
||||
|
||||
@IsOptional()
|
||||
dateTimeOriginal: Date;
|
||||
|
||||
@IsOptional()
|
||||
modifiedDate: Date;
|
||||
|
||||
@IsOptional()
|
||||
lensModel: string;
|
||||
|
||||
@IsOptional()
|
||||
fNumber: number;
|
||||
|
||||
@IsOptional()
|
||||
focalLenght: number;
|
||||
|
||||
@IsOptional()
|
||||
iso: number;
|
||||
|
||||
@IsOptional()
|
||||
exposureTime: number;
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
import { IsNotEmpty } from 'class-validator';
|
||||
|
||||
export class DeleteAssetDto {
|
||||
@IsNotEmpty()
|
||||
ids: string[];
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
import { IsNotEmpty, IsOptional } from 'class-validator';
|
||||
|
||||
export class GetAllAssetQueryDto {
|
||||
@IsOptional()
|
||||
nextPageKey: string;
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
import { AssetEntity } from '@app/database/entities/asset.entity';
|
||||
|
||||
export class GetAllAssetReponseDto {
|
||||
data: Array<{ date: string; assets: Array<AssetEntity> }>;
|
||||
count: number;
|
||||
nextPageKey: string;
|
||||
}
|
6
server/apps/immich/src/api-v1/asset/dto/get-asset.dto.ts
Normal file
6
server/apps/immich/src/api-v1/asset/dto/get-asset.dto.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { IsNotEmpty } from 'class-validator';
|
||||
|
||||
export class GetAssetDto {
|
||||
@IsNotEmpty()
|
||||
deviceId: string;
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
import { IsNotEmpty } from 'class-validator';
|
||||
|
||||
export class GetNewAssetQueryDto {
|
||||
@IsNotEmpty()
|
||||
latestDate: string;
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
import { IsNotEmpty } from 'class-validator';
|
||||
|
||||
export class SearchAssetDto {
|
||||
@IsNotEmpty()
|
||||
searchTerm: string;
|
||||
}
|
20
server/apps/immich/src/api-v1/asset/dto/serve-file.dto.ts
Normal file
20
server/apps/immich/src/api-v1/asset/dto/serve-file.dto.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { Transform } from 'class-transformer';
|
||||
import { IsBoolean, IsBooleanString, IsNotEmpty, IsOptional } from 'class-validator';
|
||||
|
||||
export class ServeFileDto {
|
||||
//assetId
|
||||
@IsNotEmpty()
|
||||
aid: string;
|
||||
|
||||
//deviceId
|
||||
@IsNotEmpty()
|
||||
did: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsBooleanString()
|
||||
isThumb: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsBooleanString()
|
||||
isWeb: string;
|
||||
}
|
@@ -0,0 +1,4 @@
|
||||
import { PartialType } from '@nestjs/mapped-types';
|
||||
import { CreateAssetDto } from './create-asset.dto';
|
||||
|
||||
export class UpdateAssetDto extends PartialType(CreateAssetDto) {}
|
@@ -0,0 +1,4 @@
|
||||
import { PartialType } from '@nestjs/mapped-types';
|
||||
import { CreateExifDto } from './create-exif.dto';
|
||||
|
||||
export class UpdateExifDto extends PartialType(CreateExifDto) {}
|
29
server/apps/immich/src/api-v1/auth/auth.controller.ts
Normal file
29
server/apps/immich/src/api-v1/auth/auth.controller.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { Body, Controller, Post, UseGuards, ValidationPipe } from '@nestjs/common';
|
||||
import { AuthUserDto, GetAuthUser } from '../../decorators/auth-user.decorator';
|
||||
import { JwtAuthGuard } from '../../modules/immich-jwt/guards/jwt-auth.guard';
|
||||
import { AuthService } from './auth.service';
|
||||
import { LoginCredentialDto } from './dto/login-credential.dto';
|
||||
import { SignUpDto } from './dto/sign-up.dto';
|
||||
|
||||
@Controller('auth')
|
||||
export class AuthController {
|
||||
constructor(private readonly authService: AuthService) {}
|
||||
|
||||
@Post('/login')
|
||||
async login(@Body(ValidationPipe) loginCredential: LoginCredentialDto) {
|
||||
return await this.authService.login(loginCredential);
|
||||
}
|
||||
|
||||
@Post('/admin-sign-up')
|
||||
async adminSignUp(@Body(ValidationPipe) signUpCrendential: SignUpDto) {
|
||||
return await this.authService.adminSignUp(signUpCrendential);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post('/validateToken')
|
||||
async validateToken(@GetAuthUser() authUser: AuthUserDto) {
|
||||
return {
|
||||
authStatus: true,
|
||||
};
|
||||
}
|
||||
}
|
16
server/apps/immich/src/api-v1/auth/auth.module.ts
Normal file
16
server/apps/immich/src/api-v1/auth/auth.module.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AuthService } from './auth.service';
|
||||
import { AuthController } from './auth.controller';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { UserEntity } from '@app/database/entities/user.entity';
|
||||
import { ImmichJwtService } from '../../modules/immich-jwt/immich-jwt.service';
|
||||
import { ImmichJwtModule } from '../../modules/immich-jwt/immich-jwt.module';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
import { jwtConfig } from '../../config/jwt.config';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([UserEntity]), ImmichJwtModule, JwtModule.register(jwtConfig)],
|
||||
controllers: [AuthController],
|
||||
providers: [AuthService, ImmichJwtService],
|
||||
})
|
||||
export class AuthModule {}
|
101
server/apps/immich/src/api-v1/auth/auth.service.ts
Normal file
101
server/apps/immich/src/api-v1/auth/auth.service.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
import { BadRequestException, Injectable, InternalServerErrorException, Logger } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { UserEntity } from '@app/database/entities/user.entity';
|
||||
import { LoginCredentialDto } from './dto/login-credential.dto';
|
||||
import { ImmichJwtService } from '../../modules/immich-jwt/immich-jwt.service';
|
||||
import { JwtPayloadDto } from './dto/jwt-payload.dto';
|
||||
import { SignUpDto } from './dto/sign-up.dto';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
import { mapUser, User } from '../user/response-dto/user';
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
constructor(
|
||||
@InjectRepository(UserEntity)
|
||||
private userRepository: Repository<UserEntity>,
|
||||
private immichJwtService: ImmichJwtService,
|
||||
) {}
|
||||
|
||||
private async validateUser(loginCredential: LoginCredentialDto): Promise<UserEntity> {
|
||||
const user = await this.userRepository.findOne(
|
||||
{ email: loginCredential.email },
|
||||
{
|
||||
select: [
|
||||
'id',
|
||||
'email',
|
||||
'password',
|
||||
'salt',
|
||||
'firstName',
|
||||
'lastName',
|
||||
'isAdmin',
|
||||
'profileImagePath',
|
||||
'isFirstLoggedIn',
|
||||
],
|
||||
},
|
||||
);
|
||||
|
||||
const isAuthenticated = await this.validatePassword(user.password, loginCredential.password, user.salt);
|
||||
|
||||
if (user && isAuthenticated) {
|
||||
return user;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public async login(loginCredential: LoginCredentialDto) {
|
||||
const validatedUser = await this.validateUser(loginCredential);
|
||||
|
||||
if (!validatedUser) {
|
||||
throw new BadRequestException('Incorrect email or password');
|
||||
}
|
||||
|
||||
const payload = new JwtPayloadDto(validatedUser.id, validatedUser.email);
|
||||
|
||||
return {
|
||||
accessToken: await this.immichJwtService.generateToken(payload),
|
||||
userId: validatedUser.id,
|
||||
userEmail: validatedUser.email,
|
||||
firstName: validatedUser.firstName,
|
||||
lastName: validatedUser.lastName,
|
||||
isAdmin: validatedUser.isAdmin,
|
||||
profileImagePath: validatedUser.profileImagePath,
|
||||
isFirstLogin: validatedUser.isFirstLoggedIn,
|
||||
};
|
||||
}
|
||||
|
||||
public async adminSignUp(signUpCredential: SignUpDto): Promise<User> {
|
||||
const adminUser = await this.userRepository.findOne({ where: { isAdmin: true } });
|
||||
|
||||
if (adminUser) {
|
||||
throw new BadRequestException('The server already has an admin');
|
||||
}
|
||||
|
||||
const newAdminUser = new UserEntity();
|
||||
newAdminUser.email = signUpCredential.email;
|
||||
newAdminUser.salt = await bcrypt.genSalt();
|
||||
newAdminUser.password = await this.hashPassword(signUpCredential.password, newAdminUser.salt);
|
||||
newAdminUser.firstName = signUpCredential.firstName;
|
||||
newAdminUser.lastName = signUpCredential.lastName;
|
||||
newAdminUser.isAdmin = true;
|
||||
|
||||
try {
|
||||
const savedNewAdminUserUser = await this.userRepository.save(newAdminUser);
|
||||
|
||||
return mapUser(savedNewAdminUserUser);
|
||||
} catch (e) {
|
||||
Logger.error('e', 'signUp');
|
||||
throw new InternalServerErrorException('Failed to register new admin user');
|
||||
}
|
||||
}
|
||||
|
||||
private async hashPassword(password: string, salt: string): Promise<string> {
|
||||
return bcrypt.hash(password, salt);
|
||||
}
|
||||
|
||||
private async validatePassword(hasedPassword: string, inputPassword: string, salt: string): Promise<boolean> {
|
||||
const hash = await bcrypt.hash(inputPassword, salt);
|
||||
return hash === hasedPassword;
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
export class JwtPayloadDto {
|
||||
constructor(userId: string, email: string) {
|
||||
this.userId = userId;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
userId: string;
|
||||
email: string;
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
import { IsNotEmpty } from 'class-validator';
|
||||
|
||||
export class LoginCredentialDto {
|
||||
@IsNotEmpty()
|
||||
email: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
password: string;
|
||||
}
|
15
server/apps/immich/src/api-v1/auth/dto/sign-up.dto.ts
Normal file
15
server/apps/immich/src/api-v1/auth/dto/sign-up.dto.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { IsNotEmpty } from 'class-validator';
|
||||
|
||||
export class SignUpDto {
|
||||
@IsNotEmpty()
|
||||
email: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
password: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
firstName: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
lastName: string;
|
||||
}
|
@@ -0,0 +1,47 @@
|
||||
import { OnGatewayConnection, OnGatewayDisconnect, WebSocketGateway, WebSocketServer } from '@nestjs/websockets';
|
||||
import { CommunicationService } from './communication.service';
|
||||
import { Socket, Server } from 'socket.io';
|
||||
import { ImmichJwtService } from '../../modules/immich-jwt/immich-jwt.service';
|
||||
import { Logger } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { UserEntity } from '@app/database/entities/user.entity';
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
@WebSocketGateway()
|
||||
export class CommunicationGateway implements OnGatewayConnection, OnGatewayDisconnect {
|
||||
constructor(
|
||||
private immichJwtService: ImmichJwtService,
|
||||
|
||||
@InjectRepository(UserEntity)
|
||||
private userRepository: Repository<UserEntity>,
|
||||
) {}
|
||||
|
||||
@WebSocketServer() server: Server;
|
||||
|
||||
handleDisconnect(client: Socket) {
|
||||
client.leave(client.nsp.name);
|
||||
|
||||
Logger.log(`Client ${client.id} disconnected`);
|
||||
}
|
||||
|
||||
async handleConnection(client: Socket, ...args: any[]) {
|
||||
Logger.log(`New websocket connection: ${client.id}`, 'NewWebSocketConnection');
|
||||
const accessToken = client.handshake.headers.authorization.split(' ')[1];
|
||||
const res = await this.immichJwtService.validateToken(accessToken);
|
||||
|
||||
if (!res.status) {
|
||||
client.emit('error', 'unauthorized');
|
||||
client.disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
const user = await this.userRepository.findOne({ where: { id: res.userId } });
|
||||
if (!user) {
|
||||
client.emit('error', 'unauthorized');
|
||||
client.disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
client.join(user.id);
|
||||
}
|
||||
}
|
@@ -0,0 +1,16 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { CommunicationService } from './communication.service';
|
||||
import { CommunicationGateway } from './communication.gateway';
|
||||
import { ImmichJwtModule } from '../../modules/immich-jwt/immich-jwt.module';
|
||||
import { ImmichJwtService } from '../../modules/immich-jwt/immich-jwt.service';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
import { jwtConfig } from '../../config/jwt.config';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { UserEntity } from '@app/database/entities/user.entity';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([UserEntity]), ImmichJwtModule, JwtModule.register(jwtConfig)],
|
||||
providers: [CommunicationGateway, CommunicationService, ImmichJwtService],
|
||||
exports: [CommunicationGateway],
|
||||
})
|
||||
export class CommunicationModule {}
|
@@ -0,0 +1,4 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class CommunicationService {}
|
@@ -0,0 +1,22 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, ValidationPipe } from '@nestjs/common';
|
||||
import { AuthUserDto, GetAuthUser } from '../../decorators/auth-user.decorator';
|
||||
import { JwtAuthGuard } from '../../modules/immich-jwt/guards/jwt-auth.guard';
|
||||
import { DeviceInfoService } from './device-info.service';
|
||||
import { CreateDeviceInfoDto } from './dto/create-device-info.dto';
|
||||
import { UpdateDeviceInfoDto } from './dto/update-device-info.dto';
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Controller('device-info')
|
||||
export class DeviceInfoController {
|
||||
constructor(private readonly deviceInfoService: DeviceInfoService) {}
|
||||
|
||||
@Post()
|
||||
async create(@Body(ValidationPipe) createDeviceInfoDto: CreateDeviceInfoDto, @GetAuthUser() authUser: AuthUserDto) {
|
||||
return await this.deviceInfoService.create(createDeviceInfoDto, authUser);
|
||||
}
|
||||
|
||||
@Patch()
|
||||
async update(@Body(ValidationPipe) updateDeviceInfoDto: UpdateDeviceInfoDto, @GetAuthUser() authUser: AuthUserDto) {
|
||||
return this.deviceInfoService.update(authUser.id, updateDeviceInfoDto);
|
||||
}
|
||||
}
|
@@ -0,0 +1,12 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { DeviceInfoService } from './device-info.service';
|
||||
import { DeviceInfoController } from './device-info.controller';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { DeviceInfoEntity } from '@app/database/entities/device-info.entity';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([DeviceInfoEntity])],
|
||||
controllers: [DeviceInfoController],
|
||||
providers: [DeviceInfoService],
|
||||
})
|
||||
export class DeviceInfoModule {}
|
@@ -0,0 +1,63 @@
|
||||
import { BadRequestException, HttpCode, Injectable, Logger, Res } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { AuthUserDto } from '../../decorators/auth-user.decorator';
|
||||
import { CreateDeviceInfoDto } from './dto/create-device-info.dto';
|
||||
import { UpdateDeviceInfoDto } from './dto/update-device-info.dto';
|
||||
import { DeviceInfoEntity } from '@app/database/entities/device-info.entity';
|
||||
|
||||
@Injectable()
|
||||
export class DeviceInfoService {
|
||||
constructor(
|
||||
@InjectRepository(DeviceInfoEntity)
|
||||
private deviceRepository: Repository<DeviceInfoEntity>,
|
||||
) {}
|
||||
|
||||
async create(createDeviceInfoDto: CreateDeviceInfoDto, authUser: AuthUserDto) {
|
||||
const res = await this.deviceRepository.findOne({
|
||||
deviceId: createDeviceInfoDto.deviceId,
|
||||
userId: authUser.id,
|
||||
});
|
||||
|
||||
if (res) {
|
||||
Logger.log('Device Info Exist', 'createDeviceInfo');
|
||||
return res;
|
||||
}
|
||||
|
||||
const deviceInfo = new DeviceInfoEntity();
|
||||
deviceInfo.deviceId = createDeviceInfoDto.deviceId;
|
||||
deviceInfo.deviceType = createDeviceInfoDto.deviceType;
|
||||
deviceInfo.userId = authUser.id;
|
||||
|
||||
try {
|
||||
return await this.deviceRepository.save(deviceInfo);
|
||||
} catch (e) {
|
||||
Logger.error('Error creating new device info', 'createDeviceInfo');
|
||||
}
|
||||
}
|
||||
|
||||
async update(userId: string, updateDeviceInfoDto: UpdateDeviceInfoDto) {
|
||||
const deviceInfo = await this.deviceRepository.findOne({
|
||||
where: { deviceId: updateDeviceInfoDto.deviceId, userId: userId },
|
||||
});
|
||||
|
||||
if (!deviceInfo) {
|
||||
throw new BadRequestException('Device Not Found');
|
||||
}
|
||||
|
||||
const res = await this.deviceRepository.update(
|
||||
{
|
||||
id: deviceInfo.id,
|
||||
},
|
||||
updateDeviceInfoDto,
|
||||
);
|
||||
|
||||
if (res.affected == 1) {
|
||||
return await this.deviceRepository.findOne({
|
||||
where: { deviceId: updateDeviceInfoDto.deviceId, userId: userId },
|
||||
});
|
||||
} else {
|
||||
throw new BadRequestException('Bad Request');
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,13 @@
|
||||
import { IsNotEmpty, IsOptional } from 'class-validator';
|
||||
import { DeviceType } from '@app/database/entities/device-info.entity';
|
||||
|
||||
export class CreateDeviceInfoDto {
|
||||
@IsNotEmpty()
|
||||
deviceId: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
deviceType: DeviceType;
|
||||
|
||||
@IsOptional()
|
||||
isAutoBackup: boolean;
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
import { PartialType } from '@nestjs/mapped-types';
|
||||
import { IsOptional } from 'class-validator';
|
||||
import { DeviceType } from '@app/database/entities/device-info.entity';
|
||||
import { CreateDeviceInfoDto } from './create-device-info.dto';
|
||||
|
||||
export class UpdateDeviceInfoDto extends PartialType(CreateDeviceInfoDto) {}
|
@@ -0,0 +1,9 @@
|
||||
export class ServerInfoDto {
|
||||
diskSize: String;
|
||||
diskUse: String;
|
||||
diskAvailable: String;
|
||||
diskSizeRaw: number;
|
||||
diskUseRaw: number;
|
||||
diskAvailableRaw: number;
|
||||
diskUsagePercentage: number;
|
||||
}
|
@@ -0,0 +1,36 @@
|
||||
import { Controller, Get, UseGuards } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { JwtAuthGuard } from '../../modules/immich-jwt/guards/jwt-auth.guard';
|
||||
import { ServerInfoService } from './server-info.service';
|
||||
import { serverVersion } from '../../constants/server_version.constant';
|
||||
|
||||
@Controller('server-info')
|
||||
export class ServerInfoController {
|
||||
constructor(private readonly serverInfoService: ServerInfoService, private readonly configService: ConfigService) {}
|
||||
|
||||
@Get()
|
||||
async getServerInfo() {
|
||||
return await this.serverInfoService.getServerInfo();
|
||||
}
|
||||
|
||||
@Get('/ping')
|
||||
async getServerPulse() {
|
||||
return {
|
||||
res: 'pong',
|
||||
};
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('/mapbox')
|
||||
async getMapboxInfo() {
|
||||
return {
|
||||
isEnable: this.configService.get('ENABLE_MAPBOX'),
|
||||
mapboxSecret: this.configService.get('MAPBOX_KEY'),
|
||||
};
|
||||
}
|
||||
|
||||
@Get('/version')
|
||||
async getServerVersion() {
|
||||
return serverVersion;
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ServerInfoService } from './server-info.service';
|
||||
import { ServerInfoController } from './server-info.controller';
|
||||
|
||||
@Module({
|
||||
controllers: [ServerInfoController],
|
||||
providers: [ServerInfoService],
|
||||
})
|
||||
export class ServerInfoModule {}
|
@@ -0,0 +1,50 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { ServerInfoDto } from './dto/server-info.dto';
|
||||
import diskusage from 'diskusage';
|
||||
|
||||
@Injectable()
|
||||
export class ServerInfoService {
|
||||
async getServerInfo() {
|
||||
const diskInfo = await diskusage.check('./upload');
|
||||
|
||||
const usagePercentage = (((diskInfo.total - diskInfo.free) / diskInfo.total) * 100).toFixed(2);
|
||||
|
||||
const serverInfo = new ServerInfoDto();
|
||||
serverInfo.diskAvailable = ServerInfoService.getHumanReadableString(diskInfo.available);
|
||||
serverInfo.diskSize = ServerInfoService.getHumanReadableString(diskInfo.total);
|
||||
serverInfo.diskUse = ServerInfoService.getHumanReadableString(diskInfo.total - diskInfo.free);
|
||||
serverInfo.diskAvailableRaw = diskInfo.available;
|
||||
serverInfo.diskSizeRaw = diskInfo.total;
|
||||
serverInfo.diskUseRaw = diskInfo.total - diskInfo.free;
|
||||
serverInfo.diskUsagePercentage = parseFloat(usagePercentage);
|
||||
|
||||
return serverInfo;
|
||||
}
|
||||
|
||||
private static getHumanReadableString(sizeInByte: number) {
|
||||
const pepibyte = 1.126 * Math.pow(10, 15);
|
||||
const tebibyte = 1.1 * Math.pow(10, 12);
|
||||
const gibibyte = 1.074 * Math.pow(10, 9);
|
||||
const mebibyte = 1.049 * Math.pow(10, 6);
|
||||
const kibibyte = 1024;
|
||||
// Pebibyte
|
||||
if (sizeInByte >= pepibyte) {
|
||||
// Pe
|
||||
return `${(sizeInByte / pepibyte).toFixed(1)}PB`;
|
||||
} else if (tebibyte <= sizeInByte && sizeInByte < pepibyte) {
|
||||
// Te
|
||||
return `${(sizeInByte / tebibyte).toFixed(1)}TB`;
|
||||
} else if (gibibyte <= sizeInByte && sizeInByte < tebibyte) {
|
||||
// Gi
|
||||
return `${(sizeInByte / gibibyte).toFixed(1)}GB`;
|
||||
} else if (mebibyte <= sizeInByte && sizeInByte < gibibyte) {
|
||||
// Mega
|
||||
return `${(sizeInByte / mebibyte).toFixed(1)}MB`;
|
||||
} else if (kibibyte <= sizeInByte && sizeInByte < mebibyte) {
|
||||
// Kibi
|
||||
return `${(sizeInByte / kibibyte).toFixed(1)}KB`;
|
||||
} else {
|
||||
return `${sizeInByte}B`;
|
||||
}
|
||||
}
|
||||
}
|
10
server/apps/immich/src/api-v1/sharing/dto/add-assets.dto.ts
Normal file
10
server/apps/immich/src/api-v1/sharing/dto/add-assets.dto.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { IsNotEmpty } from 'class-validator';
|
||||
import { AssetEntity } from '@app/database/entities/asset.entity';
|
||||
|
||||
export class AddAssetsDto {
|
||||
@IsNotEmpty()
|
||||
albumId: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
assetIds: string[];
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
import { IsNotEmpty } from 'class-validator';
|
||||
|
||||
export class AddUsersDto {
|
||||
@IsNotEmpty()
|
||||
albumId: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
sharedUserIds: string[];
|
||||
}
|
@@ -0,0 +1,13 @@
|
||||
import { IsNotEmpty, IsOptional } from 'class-validator';
|
||||
import { AssetEntity } from '@app/database/entities/asset.entity';
|
||||
|
||||
export class CreateSharedAlbumDto {
|
||||
@IsNotEmpty()
|
||||
albumName: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
sharedWithUserIds: string[];
|
||||
|
||||
@IsOptional()
|
||||
assetIds: string[];
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
import { IsNotEmpty } from 'class-validator';
|
||||
|
||||
export class RemoveAssetsDto {
|
||||
@IsNotEmpty()
|
||||
albumId: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
assetIds: string[];
|
||||
}
|
@@ -0,0 +1,12 @@
|
||||
import { IsNotEmpty } from 'class-validator';
|
||||
|
||||
export class UpdateShareAlbumDto {
|
||||
@IsNotEmpty()
|
||||
albumId: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
albumName: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
ownerId: string;
|
||||
}
|
61
server/apps/immich/src/api-v1/sharing/sharing.controller.ts
Normal file
61
server/apps/immich/src/api-v1/sharing/sharing.controller.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, ValidationPipe, Query } from '@nestjs/common';
|
||||
import { SharingService } from './sharing.service';
|
||||
import { CreateSharedAlbumDto } from './dto/create-shared-album.dto';
|
||||
import { JwtAuthGuard } from '../../modules/immich-jwt/guards/jwt-auth.guard';
|
||||
import { AuthUserDto, GetAuthUser } from '../../decorators/auth-user.decorator';
|
||||
import { AddAssetsDto } from './dto/add-assets.dto';
|
||||
import { AddUsersDto } from './dto/add-users.dto';
|
||||
import { RemoveAssetsDto } from './dto/remove-assets.dto';
|
||||
import { UpdateShareAlbumDto } from './dto/update-shared-album.dto';
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Controller('shared')
|
||||
export class SharingController {
|
||||
constructor(private readonly sharingService: SharingService) {}
|
||||
|
||||
@Post('/createAlbum')
|
||||
async create(@GetAuthUser() authUser, @Body(ValidationPipe) createSharedAlbumDto: CreateSharedAlbumDto) {
|
||||
return await this.sharingService.create(authUser, createSharedAlbumDto);
|
||||
}
|
||||
|
||||
@Post('/addUsers')
|
||||
async addUsers(@Body(ValidationPipe) addUsersDto: AddUsersDto) {
|
||||
return await this.sharingService.addUsersToAlbum(addUsersDto);
|
||||
}
|
||||
|
||||
@Post('/addAssets')
|
||||
async addAssets(@Body(ValidationPipe) addAssetsDto: AddAssetsDto) {
|
||||
return await this.sharingService.addAssetsToAlbum(addAssetsDto);
|
||||
}
|
||||
|
||||
@Get('/allSharedAlbums')
|
||||
async getAllSharedAlbums(@GetAuthUser() authUser) {
|
||||
return await this.sharingService.getAllSharedAlbums(authUser);
|
||||
}
|
||||
|
||||
@Get('/:albumId')
|
||||
async getAlbumInfo(@GetAuthUser() authUser, @Param('albumId') albumId: string) {
|
||||
return await this.sharingService.getAlbumInfo(authUser, albumId);
|
||||
}
|
||||
|
||||
@Delete('/removeAssets')
|
||||
async removeAssetFromAlbum(@GetAuthUser() authUser, @Body(ValidationPipe) removeAssetsDto: RemoveAssetsDto) {
|
||||
console.log('removeAssets');
|
||||
return await this.sharingService.removeAssetsFromAlbum(authUser, removeAssetsDto);
|
||||
}
|
||||
|
||||
@Delete('/:albumId')
|
||||
async deleteAlbum(@GetAuthUser() authUser, @Param('albumId') albumId: string) {
|
||||
return await this.sharingService.deleteAlbum(authUser, albumId);
|
||||
}
|
||||
|
||||
@Delete('/leaveAlbum/:albumId')
|
||||
async leaveAlbum(@GetAuthUser() authUser, @Param('albumId') albumId: string) {
|
||||
return await this.sharingService.leaveAlbum(authUser, albumId);
|
||||
}
|
||||
|
||||
@Patch('/updateInfo')
|
||||
async updateAlbumInfo(@GetAuthUser() authUser, @Body(ValidationPipe) updateAlbumInfoDto: UpdateShareAlbumDto) {
|
||||
return await this.sharingService.updateAlbumTitle(authUser, updateAlbumInfoDto);
|
||||
}
|
||||
}
|
24
server/apps/immich/src/api-v1/sharing/sharing.module.ts
Normal file
24
server/apps/immich/src/api-v1/sharing/sharing.module.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { SharingService } from './sharing.service';
|
||||
import { SharingController } from './sharing.controller';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { AssetEntity } from '@app/database/entities/asset.entity';
|
||||
import { UserEntity } from '@app/database/entities/user.entity';
|
||||
import { AssetSharedAlbumEntity } from '@app/database/entities/asset-shared-album.entity';
|
||||
import { SharedAlbumEntity } from '@app/database/entities/shared-album.entity';
|
||||
import { UserSharedAlbumEntity } from '@app/database/entities/user-shared-album.entity';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([
|
||||
AssetEntity,
|
||||
UserEntity,
|
||||
SharedAlbumEntity,
|
||||
AssetSharedAlbumEntity,
|
||||
UserSharedAlbumEntity,
|
||||
]),
|
||||
],
|
||||
controllers: [SharingController],
|
||||
providers: [SharingService],
|
||||
})
|
||||
export class SharingModule {}
|
199
server/apps/immich/src/api-v1/sharing/sharing.service.ts
Normal file
199
server/apps/immich/src/api-v1/sharing/sharing.service.ts
Normal file
@@ -0,0 +1,199 @@
|
||||
import { BadRequestException, Injectable, NotFoundException, UnauthorizedException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { getConnection, Repository } from 'typeorm';
|
||||
import { AuthUserDto } from '../../decorators/auth-user.decorator';
|
||||
import { AssetEntity } from '@app/database/entities/asset.entity';
|
||||
import { UserEntity } from '@app/database/entities/user.entity';
|
||||
import { AddAssetsDto } from './dto/add-assets.dto';
|
||||
import { CreateSharedAlbumDto } from './dto/create-shared-album.dto';
|
||||
import { AssetSharedAlbumEntity } from '@app/database/entities/asset-shared-album.entity';
|
||||
import { SharedAlbumEntity } from '@app/database/entities/shared-album.entity';
|
||||
import { UserSharedAlbumEntity } from '@app/database/entities/user-shared-album.entity';
|
||||
import _ from 'lodash';
|
||||
import { AddUsersDto } from './dto/add-users.dto';
|
||||
import { RemoveAssetsDto } from './dto/remove-assets.dto';
|
||||
import { UpdateShareAlbumDto } from './dto/update-shared-album.dto';
|
||||
|
||||
@Injectable()
|
||||
export class SharingService {
|
||||
constructor(
|
||||
@InjectRepository(AssetEntity)
|
||||
private assetRepository: Repository<AssetEntity>,
|
||||
|
||||
@InjectRepository(UserEntity)
|
||||
private userRepository: Repository<UserEntity>,
|
||||
|
||||
@InjectRepository(SharedAlbumEntity)
|
||||
private sharedAlbumRepository: Repository<SharedAlbumEntity>,
|
||||
|
||||
@InjectRepository(AssetSharedAlbumEntity)
|
||||
private assetSharedAlbumRepository: Repository<AssetSharedAlbumEntity>,
|
||||
|
||||
@InjectRepository(UserSharedAlbumEntity)
|
||||
private userSharedAlbumRepository: Repository<UserSharedAlbumEntity>,
|
||||
) {}
|
||||
|
||||
async create(authUser: AuthUserDto, createSharedAlbumDto: CreateSharedAlbumDto) {
|
||||
return await getConnection().transaction(async (transactionalEntityManager) => {
|
||||
// Create album entity
|
||||
const newSharedAlbum = new SharedAlbumEntity();
|
||||
newSharedAlbum.ownerId = authUser.id;
|
||||
newSharedAlbum.albumName = createSharedAlbumDto.albumName;
|
||||
|
||||
const sharedAlbum = await transactionalEntityManager.save(newSharedAlbum);
|
||||
|
||||
// Add shared users
|
||||
for (const sharedUserId of createSharedAlbumDto.sharedWithUserIds) {
|
||||
const newSharedUser = new UserSharedAlbumEntity();
|
||||
newSharedUser.albumId = sharedAlbum.id;
|
||||
newSharedUser.sharedUserId = sharedUserId;
|
||||
|
||||
await transactionalEntityManager.save(newSharedUser);
|
||||
}
|
||||
|
||||
// Add shared assets
|
||||
const newRecords: AssetSharedAlbumEntity[] = [];
|
||||
|
||||
for (const assetId of createSharedAlbumDto.assetIds) {
|
||||
const newAssetSharedAlbum = new AssetSharedAlbumEntity();
|
||||
newAssetSharedAlbum.assetId = assetId;
|
||||
newAssetSharedAlbum.albumId = sharedAlbum.id;
|
||||
|
||||
newRecords.push(newAssetSharedAlbum);
|
||||
}
|
||||
|
||||
if (!sharedAlbum.albumThumbnailAssetId && newRecords.length > 0) {
|
||||
sharedAlbum.albumThumbnailAssetId = newRecords[0].assetId;
|
||||
await transactionalEntityManager.save(sharedAlbum);
|
||||
}
|
||||
|
||||
await transactionalEntityManager.save([...newRecords]);
|
||||
|
||||
return sharedAlbum;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all shared album, including owned and shared one.
|
||||
* @param authUser AuthUserDto
|
||||
* @returns All Shared Album And Its Members
|
||||
*/
|
||||
async getAllSharedAlbums(authUser: AuthUserDto) {
|
||||
const ownedAlbums = await this.sharedAlbumRepository.find({
|
||||
where: { ownerId: authUser.id },
|
||||
relations: ['sharedUsers', 'sharedUsers.userInfo'],
|
||||
});
|
||||
|
||||
const isSharedWithAlbums = await this.userSharedAlbumRepository.find({
|
||||
where: {
|
||||
sharedUserId: authUser.id,
|
||||
},
|
||||
relations: ['albumInfo', 'albumInfo.sharedUsers', 'albumInfo.sharedUsers.userInfo'],
|
||||
select: ['albumInfo'],
|
||||
});
|
||||
|
||||
return [...ownedAlbums, ...isSharedWithAlbums.map((o) => o.albumInfo)].sort(
|
||||
(a, b) => new Date(b.createdAt).valueOf() - new Date(a.createdAt).valueOf(),
|
||||
);
|
||||
}
|
||||
|
||||
async getAlbumInfo(authUser: AuthUserDto, albumId: string) {
|
||||
const albumOwner = await this.sharedAlbumRepository.findOne({ where: { ownerId: authUser.id } });
|
||||
const personShared = await this.userSharedAlbumRepository.findOne({
|
||||
where: { albumId: albumId, sharedUserId: authUser.id },
|
||||
});
|
||||
|
||||
if (!(albumOwner || personShared)) {
|
||||
throw new UnauthorizedException('Unauthorized Album Access');
|
||||
}
|
||||
|
||||
const albumInfo = await this.sharedAlbumRepository.findOne({
|
||||
where: { id: albumId },
|
||||
relations: ['sharedUsers', 'sharedUsers.userInfo', 'sharedAssets', 'sharedAssets.assetInfo'],
|
||||
});
|
||||
|
||||
if (!albumInfo) {
|
||||
throw new NotFoundException('Album Not Found');
|
||||
}
|
||||
const sortedSharedAsset = albumInfo.sharedAssets.sort(
|
||||
(a, b) => new Date(a.assetInfo.createdAt).valueOf() - new Date(b.assetInfo.createdAt).valueOf(),
|
||||
);
|
||||
|
||||
albumInfo.sharedAssets = sortedSharedAsset;
|
||||
|
||||
return albumInfo;
|
||||
}
|
||||
|
||||
async addUsersToAlbum(addUsersDto: AddUsersDto) {
|
||||
const newRecords: UserSharedAlbumEntity[] = [];
|
||||
|
||||
for (const sharedUserId of addUsersDto.sharedUserIds) {
|
||||
const newEntity = new UserSharedAlbumEntity();
|
||||
newEntity.albumId = addUsersDto.albumId;
|
||||
newEntity.sharedUserId = sharedUserId;
|
||||
|
||||
newRecords.push(newEntity);
|
||||
}
|
||||
|
||||
return await this.userSharedAlbumRepository.save([...newRecords]);
|
||||
}
|
||||
|
||||
async deleteAlbum(authUser: AuthUserDto, albumId: string) {
|
||||
return await this.sharedAlbumRepository.delete({ id: albumId, ownerId: authUser.id });
|
||||
}
|
||||
|
||||
async leaveAlbum(authUser: AuthUserDto, albumId: string) {
|
||||
return await this.userSharedAlbumRepository.delete({ albumId: albumId, sharedUserId: authUser.id });
|
||||
}
|
||||
|
||||
async removeUsersFromAlbum() {}
|
||||
|
||||
async removeAssetsFromAlbum(authUser: AuthUserDto, removeAssetsDto: RemoveAssetsDto) {
|
||||
let deleteAssetCount = 0;
|
||||
const album = await this.sharedAlbumRepository.findOne({ id: removeAssetsDto.albumId });
|
||||
|
||||
if (album.ownerId != authUser.id) {
|
||||
throw new BadRequestException("You don't have permission to remove assets in this album");
|
||||
}
|
||||
|
||||
for (const assetId of removeAssetsDto.assetIds) {
|
||||
const res = await this.assetSharedAlbumRepository.delete({ albumId: removeAssetsDto.albumId, assetId: assetId });
|
||||
if (res.affected == 1) deleteAssetCount++;
|
||||
}
|
||||
|
||||
return deleteAssetCount == removeAssetsDto.assetIds.length;
|
||||
}
|
||||
|
||||
async addAssetsToAlbum(addAssetsDto: AddAssetsDto) {
|
||||
const newRecords: AssetSharedAlbumEntity[] = [];
|
||||
|
||||
for (const assetId of addAssetsDto.assetIds) {
|
||||
const newAssetSharedAlbum = new AssetSharedAlbumEntity();
|
||||
newAssetSharedAlbum.assetId = assetId;
|
||||
newAssetSharedAlbum.albumId = addAssetsDto.albumId;
|
||||
|
||||
newRecords.push(newAssetSharedAlbum);
|
||||
}
|
||||
|
||||
// Add album thumbnail if not exist.
|
||||
const album = await this.sharedAlbumRepository.findOne({ id: addAssetsDto.albumId });
|
||||
|
||||
if (!album.albumThumbnailAssetId && newRecords.length > 0) {
|
||||
album.albumThumbnailAssetId = newRecords[0].assetId;
|
||||
await this.sharedAlbumRepository.save(album);
|
||||
}
|
||||
|
||||
return await this.assetSharedAlbumRepository.save([...newRecords]);
|
||||
}
|
||||
|
||||
async updateAlbumTitle(authUser: AuthUserDto, updateShareAlbumDto: UpdateShareAlbumDto) {
|
||||
if (authUser.id != updateShareAlbumDto.ownerId) {
|
||||
throw new BadRequestException('Unauthorized to change album info');
|
||||
}
|
||||
|
||||
const sharedAlbum = await this.sharedAlbumRepository.findOne({ where: { id: updateShareAlbumDto.albumId } });
|
||||
sharedAlbum.albumName = updateShareAlbumDto.albumName;
|
||||
|
||||
return await this.sharedAlbumRepository.save(sharedAlbum);
|
||||
}
|
||||
}
|
27
server/apps/immich/src/api-v1/user/dto/create-user.dto.ts
Normal file
27
server/apps/immich/src/api-v1/user/dto/create-user.dto.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { IsNotEmpty, IsOptional } from 'class-validator';
|
||||
|
||||
export class CreateUserDto {
|
||||
@IsNotEmpty()
|
||||
email: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
password: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
firstName: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
lastName: string;
|
||||
|
||||
@IsOptional()
|
||||
profileImagePath?: string;
|
||||
|
||||
@IsOptional()
|
||||
isAdmin?: boolean;
|
||||
|
||||
@IsOptional()
|
||||
isFirstLoggedIn?: boolean;
|
||||
|
||||
@IsOptional()
|
||||
id?: string;
|
||||
}
|
@@ -0,0 +1,4 @@
|
||||
import { PartialType } from '@nestjs/mapped-types';
|
||||
import { CreateUserDto } from './create-user.dto';
|
||||
|
||||
export class UpdateUserDto extends PartialType(CreateUserDto) {}
|
19
server/apps/immich/src/api-v1/user/response-dto/user.ts
Normal file
19
server/apps/immich/src/api-v1/user/response-dto/user.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { UserEntity } from '../../../../../../libs/database/src/entities/user.entity';
|
||||
|
||||
export interface User {
|
||||
id: string;
|
||||
email: string;
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
export function mapUser(entity: UserEntity): User {
|
||||
return {
|
||||
id: entity.id,
|
||||
email: entity.email,
|
||||
firstName: entity.firstName,
|
||||
lastName: entity.lastName,
|
||||
createdAt: entity.createdAt,
|
||||
};
|
||||
}
|
66
server/apps/immich/src/api-v1/user/user.controller.ts
Normal file
66
server/apps/immich/src/api-v1/user/user.controller.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Body,
|
||||
Patch,
|
||||
Param,
|
||||
Delete,
|
||||
UseGuards,
|
||||
ValidationPipe,
|
||||
Put,
|
||||
Query,
|
||||
UseInterceptors,
|
||||
UploadedFile,
|
||||
Response,
|
||||
} from '@nestjs/common';
|
||||
import { UserService } from './user.service';
|
||||
import { JwtAuthGuard } from '../../modules/immich-jwt/guards/jwt-auth.guard';
|
||||
import { AuthUserDto, GetAuthUser } from '../../decorators/auth-user.decorator';
|
||||
import { CreateUserDto } from './dto/create-user.dto';
|
||||
import { AdminRolesGuard } from '../../middlewares/admin-role-guard.middleware';
|
||||
import { UpdateUserDto } from './dto/update-user.dto';
|
||||
import { FileInterceptor } from '@nestjs/platform-express';
|
||||
import { profileImageUploadOption } from '../../config/profile-image-upload.config';
|
||||
import { Response as Res } from 'express';
|
||||
|
||||
@Controller('user')
|
||||
export class UserController {
|
||||
constructor(private readonly userService: UserService) {}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get()
|
||||
async getAllUsers(@GetAuthUser() authUser: AuthUserDto, @Query('isAll') isAll: boolean) {
|
||||
return await this.userService.getAllUsers(authUser, isAll);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@UseGuards(AdminRolesGuard)
|
||||
@Post()
|
||||
async createNewUser(@Body(ValidationPipe) createUserDto: CreateUserDto) {
|
||||
return await this.userService.createUser(createUserDto);
|
||||
}
|
||||
|
||||
@Get('/count')
|
||||
async getUserCount(@Query('isAdmin') isAdmin: boolean) {
|
||||
return await this.userService.getUserCount(isAdmin);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Put()
|
||||
async updateUser(@Body(ValidationPipe) updateUserDto: UpdateUserDto) {
|
||||
return await this.userService.updateUser(updateUserDto);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@UseInterceptors(FileInterceptor('file', profileImageUploadOption))
|
||||
@Post('/profile-image')
|
||||
async createProfileImage(@GetAuthUser() authUser: AuthUserDto, @UploadedFile() fileInfo: Express.Multer.File) {
|
||||
return await this.userService.createProfileImage(authUser, fileInfo);
|
||||
}
|
||||
|
||||
@Get('/profile-image/:userId')
|
||||
async getProfileImage(@Param('userId') userId: string, @Response({ passthrough: true }) res: Res) {
|
||||
return await this.userService.getUserProfileImage(userId, res);
|
||||
}
|
||||
}
|
16
server/apps/immich/src/api-v1/user/user.module.ts
Normal file
16
server/apps/immich/src/api-v1/user/user.module.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { UserService } from './user.service';
|
||||
import { UserController } from './user.controller';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { UserEntity } from '@app/database/entities/user.entity';
|
||||
import { ImmichJwtModule } from '../../modules/immich-jwt/immich-jwt.module';
|
||||
import { ImmichJwtService } from '../../modules/immich-jwt/immich-jwt.service';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
import { jwtConfig } from '../../config/jwt.config';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([UserEntity]), ImmichJwtModule, JwtModule.register(jwtConfig)],
|
||||
controllers: [UserController],
|
||||
providers: [UserService, ImmichJwtService],
|
||||
})
|
||||
export class UserModule {}
|
152
server/apps/immich/src/api-v1/user/user.service.ts
Normal file
152
server/apps/immich/src/api-v1/user/user.service.ts
Normal file
@@ -0,0 +1,152 @@
|
||||
import { BadRequestException, Injectable, InternalServerErrorException, Logger, StreamableFile } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Not, Repository } from 'typeorm';
|
||||
import { AuthUserDto } from '../../decorators/auth-user.decorator';
|
||||
import { CreateUserDto } from './dto/create-user.dto';
|
||||
import { UpdateUserDto } from './dto/update-user.dto';
|
||||
import { UserEntity } from '@app/database/entities/user.entity';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
import { createReadStream } from 'fs';
|
||||
import { Response as Res } from 'express';
|
||||
import { mapUser, User } from './response-dto/user';
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
constructor(
|
||||
@InjectRepository(UserEntity)
|
||||
private userRepository: Repository<UserEntity>,
|
||||
) {}
|
||||
|
||||
async getAllUsers(authUser: AuthUserDto, isAll: boolean) {
|
||||
if (isAll) {
|
||||
return await this.userRepository.find();
|
||||
}
|
||||
|
||||
return await this.userRepository.find({
|
||||
where: { id: Not(authUser.id) },
|
||||
order: {
|
||||
createdAt: 'DESC',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async getUserCount(isAdmin: boolean) {
|
||||
let users;
|
||||
|
||||
if (isAdmin) {
|
||||
users = await this.userRepository.find({ where: { isAdmin: true } });
|
||||
} else {
|
||||
users = await this.userRepository.find();
|
||||
}
|
||||
|
||||
return {
|
||||
userCount: users.length,
|
||||
};
|
||||
}
|
||||
|
||||
async createUser(createUserDto: CreateUserDto): Promise<User> {
|
||||
const user = await this.userRepository.findOne({ where: { email: createUserDto.email } });
|
||||
|
||||
if (user) {
|
||||
throw new BadRequestException('User exists');
|
||||
}
|
||||
|
||||
const newUser = new UserEntity();
|
||||
newUser.email = createUserDto.email;
|
||||
newUser.salt = await bcrypt.genSalt();
|
||||
newUser.password = await this.hashPassword(createUserDto.password, newUser.salt);
|
||||
newUser.firstName = createUserDto.firstName;
|
||||
newUser.lastName = createUserDto.lastName;
|
||||
newUser.isAdmin = false;
|
||||
|
||||
try {
|
||||
const savedUser = await this.userRepository.save(newUser);
|
||||
|
||||
return mapUser(savedUser);
|
||||
} catch (e) {
|
||||
Logger.error(e, 'Create new user');
|
||||
throw new InternalServerErrorException('Failed to register new user');
|
||||
}
|
||||
}
|
||||
|
||||
private async hashPassword(password: string, salt: string): Promise<string> {
|
||||
return bcrypt.hash(password, salt);
|
||||
}
|
||||
|
||||
async updateUser(updateUserDto: UpdateUserDto) {
|
||||
const user = await this.userRepository.findOne(updateUserDto.id);
|
||||
|
||||
user.lastName = updateUserDto.lastName || user.lastName;
|
||||
user.firstName = updateUserDto.firstName || user.firstName;
|
||||
user.profileImagePath = updateUserDto.profileImagePath || user.profileImagePath;
|
||||
user.isFirstLoggedIn = updateUserDto.isFirstLoggedIn || user.isFirstLoggedIn;
|
||||
|
||||
// If payload includes password - Create new password for user
|
||||
if (updateUserDto.password) {
|
||||
user.salt = await bcrypt.genSalt();
|
||||
user.password = await this.hashPassword(updateUserDto.password, user.salt);
|
||||
}
|
||||
|
||||
if (updateUserDto.isAdmin) {
|
||||
const adminUser = await this.userRepository.findOne({ where: { isAdmin: true } });
|
||||
|
||||
if (adminUser) {
|
||||
throw new BadRequestException('Admin user exists');
|
||||
}
|
||||
|
||||
user.isAdmin = true;
|
||||
}
|
||||
|
||||
try {
|
||||
const updatedUser = await this.userRepository.save(user);
|
||||
|
||||
return {
|
||||
id: updatedUser.id,
|
||||
email: updatedUser.email,
|
||||
firstName: updatedUser.firstName,
|
||||
lastName: updatedUser.lastName,
|
||||
isAdmin: updatedUser.isAdmin,
|
||||
profileImagePath: updatedUser.profileImagePath,
|
||||
};
|
||||
} catch (e) {
|
||||
Logger.error(e, 'Create new user');
|
||||
throw new InternalServerErrorException('Failed to register new user');
|
||||
}
|
||||
}
|
||||
|
||||
async createProfileImage(authUser: AuthUserDto, fileInfo: Express.Multer.File) {
|
||||
try {
|
||||
await this.userRepository.update(authUser.id, {
|
||||
profileImagePath: fileInfo.path,
|
||||
});
|
||||
|
||||
return {
|
||||
userId: authUser.id,
|
||||
profileImagePath: fileInfo.path,
|
||||
};
|
||||
} catch (e) {
|
||||
Logger.error(e, 'Create User Profile Image');
|
||||
throw new InternalServerErrorException('Failed to create new user profile image');
|
||||
}
|
||||
}
|
||||
|
||||
async getUserProfileImage(userId: string, res: Res) {
|
||||
try {
|
||||
const user = await this.userRepository.findOne({ id: userId });
|
||||
|
||||
if (!user.profileImagePath) {
|
||||
// throw new BadRequestException('User does not have a profile image');
|
||||
res.status(404).send('User does not have a profile image');
|
||||
return;
|
||||
}
|
||||
|
||||
res.set({
|
||||
'Content-Type': 'image/jpeg',
|
||||
});
|
||||
const fileStream = createReadStream(user.profileImagePath);
|
||||
return new StreamableFile(fileStream);
|
||||
} catch (e) {
|
||||
console.log('error getting user profile');
|
||||
}
|
||||
}
|
||||
}
|
13
server/apps/immich/src/app.controller.ts
Normal file
13
server/apps/immich/src/app.controller.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { Controller, Get, Res, Headers } from '@nestjs/common';
|
||||
import { Response } from 'express';
|
||||
@Controller()
|
||||
export class AppController {
|
||||
constructor() {}
|
||||
|
||||
@Get()
|
||||
async redirectToWebpage(@Res({ passthrough: true }) res: Response, @Headers() headers) {
|
||||
const host = headers.host;
|
||||
|
||||
return res.redirect(`http://${host}:2285`);
|
||||
}
|
||||
}
|
65
server/apps/immich/src/app.module.ts
Normal file
65
server/apps/immich/src/app.module.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
|
||||
import { UserModule } from './api-v1/user/user.module';
|
||||
import { AssetModule } from './api-v1/asset/asset.module';
|
||||
import { AuthModule } from './api-v1/auth/auth.module';
|
||||
import { ImmichJwtModule } from './modules/immich-jwt/immich-jwt.module';
|
||||
import { DeviceInfoModule } from './api-v1/device-info/device-info.module';
|
||||
import { AppLoggerMiddleware } from './middlewares/app-logger.middleware';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import { immichAppConfig } from './config/app.config';
|
||||
import { BullModule } from '@nestjs/bull';
|
||||
import { ServerInfoModule } from './api-v1/server-info/server-info.module';
|
||||
import { BackgroundTaskModule } from './modules/background-task/background-task.module';
|
||||
import { CommunicationModule } from './api-v1/communication/communication.module';
|
||||
import { SharingModule } from './api-v1/sharing/sharing.module';
|
||||
import { AppController } from './app.controller';
|
||||
import { ScheduleModule } from '@nestjs/schedule';
|
||||
import { ScheduleTasksModule } from './modules/schedule-tasks/schedule-tasks.module';
|
||||
import { DatabaseModule } from '@app/database';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot(immichAppConfig),
|
||||
|
||||
DatabaseModule,
|
||||
UserModule,
|
||||
|
||||
AssetModule,
|
||||
|
||||
AuthModule,
|
||||
|
||||
ImmichJwtModule,
|
||||
|
||||
DeviceInfoModule,
|
||||
|
||||
BullModule.forRootAsync({
|
||||
useFactory: async () => ({
|
||||
redis: {
|
||||
host: process.env.REDIS_HOSTNAME || 'immich_redis',
|
||||
port: 6379,
|
||||
},
|
||||
}),
|
||||
}),
|
||||
|
||||
ServerInfoModule,
|
||||
|
||||
BackgroundTaskModule,
|
||||
|
||||
CommunicationModule,
|
||||
|
||||
SharingModule,
|
||||
|
||||
ScheduleModule.forRoot(),
|
||||
|
||||
ScheduleTasksModule,
|
||||
],
|
||||
controllers: [AppController],
|
||||
providers: [],
|
||||
})
|
||||
export class AppModule implements NestModule {
|
||||
configure(consumer: MiddlewareConsumer): void {
|
||||
if (process.env.NODE_ENV == 'development') {
|
||||
// consumer.apply(AppLoggerMiddleware).forRoutes('*');
|
||||
}
|
||||
}
|
||||
}
|
22
server/apps/immich/src/config/app.config.ts
Normal file
22
server/apps/immich/src/config/app.config.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { ConfigModuleOptions } from '@nestjs/config';
|
||||
import Joi from 'joi';
|
||||
|
||||
export const immichAppConfig: ConfigModuleOptions = {
|
||||
envFilePath: '.env',
|
||||
isGlobal: true,
|
||||
validationSchema: Joi.object({
|
||||
NODE_ENV: Joi.string().required().valid('development', 'production', 'staging').default('development'),
|
||||
DB_USERNAME: Joi.string().required(),
|
||||
DB_PASSWORD: Joi.string().required(),
|
||||
DB_DATABASE_NAME: Joi.string().required(),
|
||||
UPLOAD_LOCATION: Joi.string().required(),
|
||||
JWT_SECRET: Joi.string().required(),
|
||||
ENABLE_MAPBOX: Joi.boolean().required().valid(true, false),
|
||||
MAPBOX_KEY: Joi.any().when('ENABLE_MAPBOX', {
|
||||
is: false,
|
||||
then: Joi.string().optional().allow(null, ''),
|
||||
otherwise: Joi.string().required(),
|
||||
}),
|
||||
VITE_SERVER_ENDPOINT: Joi.string().required(),
|
||||
}),
|
||||
};
|
58
server/apps/immich/src/config/asset-upload.config.ts
Normal file
58
server/apps/immich/src/config/asset-upload.config.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { HttpException, HttpStatus } from '@nestjs/common';
|
||||
import { MulterOptions } from '@nestjs/platform-express/multer/interfaces/multer-options.interface';
|
||||
import { existsSync, mkdirSync } from 'fs';
|
||||
import { diskStorage } from 'multer';
|
||||
import { extname } from 'path';
|
||||
import { Request } from 'express';
|
||||
import { APP_UPLOAD_LOCATION } from '../constants/upload_location.constant';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { CreateAssetDto } from '../api-v1/asset/dto/create-asset.dto';
|
||||
|
||||
export const assetUploadOption: MulterOptions = {
|
||||
fileFilter: (req: Request, file: any, cb: any) => {
|
||||
if (file.mimetype.match(/\/(jpg|jpeg|png|gif|mp4|x-msvideo|quicktime|heic|heif|dng|webp)$/)) {
|
||||
cb(null, true);
|
||||
} else {
|
||||
cb(new HttpException(`Unsupported file type ${extname(file.originalname)}`, HttpStatus.BAD_REQUEST), false);
|
||||
}
|
||||
},
|
||||
|
||||
storage: diskStorage({
|
||||
destination: (req: Request, file: Express.Multer.File, cb: any) => {
|
||||
const basePath = APP_UPLOAD_LOCATION;
|
||||
const fileInfo = req.body as CreateAssetDto;
|
||||
|
||||
const yearInfo = new Date(fileInfo.createdAt).getFullYear();
|
||||
const monthInfo = new Date(fileInfo.createdAt).getMonth();
|
||||
|
||||
if (file.fieldname == 'assetData') {
|
||||
const originalUploadFolder = `${basePath}/${req.user['id']}/original/${req.body['deviceId']}`;
|
||||
|
||||
if (!existsSync(originalUploadFolder)) {
|
||||
mkdirSync(originalUploadFolder, { recursive: true });
|
||||
}
|
||||
|
||||
// Save original to disk
|
||||
cb(null, originalUploadFolder);
|
||||
} else if (file.fieldname == 'thumbnailData') {
|
||||
const thumbnailUploadFolder = `${basePath}/${req.user['id']}/thumb/${req.body['deviceId']}`;
|
||||
|
||||
if (!existsSync(thumbnailUploadFolder)) {
|
||||
mkdirSync(thumbnailUploadFolder, { recursive: true });
|
||||
}
|
||||
|
||||
// Save thumbnail to disk
|
||||
cb(null, thumbnailUploadFolder);
|
||||
}
|
||||
},
|
||||
|
||||
filename: (req: Request, file: Express.Multer.File, cb: any) => {
|
||||
const fileNameUUID = randomUUID();
|
||||
if (file.fieldname == 'assetData') {
|
||||
cb(null, `${fileNameUUID}${req.body['fileExtension'].toLowerCase()}`);
|
||||
} else if (file.fieldname == 'thumbnailData') {
|
||||
cb(null, `${fileNameUUID}.jpeg`);
|
||||
}
|
||||
},
|
||||
}),
|
||||
};
|
7
server/apps/immich/src/config/jwt.config.ts
Normal file
7
server/apps/immich/src/config/jwt.config.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { JwtModuleOptions } from '@nestjs/jwt';
|
||||
import { jwtSecret } from '../constants/jwt.constant';
|
||||
|
||||
export const jwtConfig: JwtModuleOptions = {
|
||||
secret: jwtSecret,
|
||||
signOptions: { expiresIn: '36500d' },
|
||||
};
|
36
server/apps/immich/src/config/profile-image-upload.config.ts
Normal file
36
server/apps/immich/src/config/profile-image-upload.config.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { HttpException, HttpStatus } from '@nestjs/common';
|
||||
import { MulterOptions } from '@nestjs/platform-express/multer/interfaces/multer-options.interface';
|
||||
import { existsSync, mkdirSync } from 'fs';
|
||||
import { diskStorage } from 'multer';
|
||||
import { extname } from 'path';
|
||||
import { Request } from 'express';
|
||||
import { APP_UPLOAD_LOCATION } from '../constants/upload_location.constant';
|
||||
|
||||
export const profileImageUploadOption: MulterOptions = {
|
||||
fileFilter: (req: Request, file: any, cb: any) => {
|
||||
if (file.mimetype.match(/\/(jpg|jpeg|png|heic|heif|dng|webp)$/)) {
|
||||
cb(null, true);
|
||||
} else {
|
||||
cb(new HttpException(`Unsupported file type ${extname(file.originalname)}`, HttpStatus.BAD_REQUEST), false);
|
||||
}
|
||||
},
|
||||
|
||||
storage: diskStorage({
|
||||
destination: (req: Request, file: Express.Multer.File, cb: any) => {
|
||||
const basePath = APP_UPLOAD_LOCATION;
|
||||
const profileImageLocation = `${basePath}/${req.user['id']}/profile`;
|
||||
|
||||
if (!existsSync(profileImageLocation)) {
|
||||
mkdirSync(profileImageLocation, { recursive: true });
|
||||
}
|
||||
|
||||
cb(null, profileImageLocation);
|
||||
},
|
||||
|
||||
filename: (req: Request, file: Express.Multer.File, cb: any) => {
|
||||
const userId = req.user['id'];
|
||||
|
||||
cb(null, `${userId}${extname(file.originalname)}`);
|
||||
},
|
||||
}),
|
||||
};
|
1
server/apps/immich/src/constants/jwt.constant.ts
Normal file
1
server/apps/immich/src/constants/jwt.constant.ts
Normal file
@@ -0,0 +1 @@
|
||||
export const jwtSecret = process.env.JWT_SECRET;
|
@@ -0,0 +1,9 @@
|
||||
// major.minor.patch+build
|
||||
// check mobile/pubspec.yml for current release version
|
||||
|
||||
export const serverVersion = {
|
||||
major: 1,
|
||||
minor: 11,
|
||||
patch: 0,
|
||||
build: 17,
|
||||
};
|
@@ -0,0 +1 @@
|
||||
export const APP_UPLOAD_LOCATION = './upload';
|
21
server/apps/immich/src/decorators/auth-user.decorator.ts
Normal file
21
server/apps/immich/src/decorators/auth-user.decorator.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { createParamDecorator, ExecutionContext, UnauthorizedException } from '@nestjs/common';
|
||||
import { UserEntity } from '@app/database/entities/user.entity';
|
||||
// import { AuthUserDto } from './dto/auth-user.dto';
|
||||
|
||||
export class AuthUserDto {
|
||||
id: string;
|
||||
email: string;
|
||||
}
|
||||
|
||||
export const GetAuthUser = createParamDecorator((data, ctx: ExecutionContext): AuthUserDto => {
|
||||
const req = ctx.switchToHttp().getRequest();
|
||||
|
||||
const { id, email } = req.user as UserEntity;
|
||||
|
||||
const authUser: any = {
|
||||
id: id.toString(),
|
||||
email,
|
||||
};
|
||||
|
||||
return authUser;
|
||||
});
|
26
server/apps/immich/src/main.ts
Normal file
26
server/apps/immich/src/main.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { Logger } from '@nestjs/common';
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { NestExpressApplication } from '@nestjs/platform-express';
|
||||
import { AppModule } from './app.module';
|
||||
import { RedisIoAdapter } from './middlewares/redis-io.adapter.middleware';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create<NestExpressApplication>(AppModule);
|
||||
|
||||
app.enableCors();
|
||||
|
||||
app.set('trust proxy');
|
||||
|
||||
app.useWebSocketAdapter(new RedisIoAdapter(app));
|
||||
|
||||
await app.listen(3000, () => {
|
||||
if (process.env.NODE_ENV == 'development') {
|
||||
Logger.log('Running Immich Server in DEVELOPMENT environment', 'ImmichServer');
|
||||
}
|
||||
|
||||
if (process.env.NODE_ENV == 'production') {
|
||||
Logger.log('Running Immich Server in PRODUCTION environment', 'ImmichServer');
|
||||
}
|
||||
});
|
||||
}
|
||||
bootstrap();
|
@@ -0,0 +1,32 @@
|
||||
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
|
||||
import { Reflector } from '@nestjs/core';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { UserEntity } from '@app/database/entities/user.entity';
|
||||
import { ImmichJwtService } from '../modules/immich-jwt/immich-jwt.service';
|
||||
|
||||
@Injectable()
|
||||
export class AdminRolesGuard implements CanActivate {
|
||||
constructor(
|
||||
private reflector: Reflector,
|
||||
private jwtService: ImmichJwtService,
|
||||
@InjectRepository(UserEntity)
|
||||
private userRepository: Repository<UserEntity>,
|
||||
) {}
|
||||
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
const request = context.switchToHttp().getRequest();
|
||||
|
||||
if (request.headers['authorization']) {
|
||||
const bearerToken = request.headers['authorization'].split(' ')[1];
|
||||
const { userId } = await this.jwtService.validateToken(bearerToken);
|
||||
|
||||
const user = await this.userRepository.findOne(userId);
|
||||
|
||||
return user.isAdmin;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
22
server/apps/immich/src/middlewares/app-logger.middleware.ts
Normal file
22
server/apps/immich/src/middlewares/app-logger.middleware.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { Injectable, NestMiddleware, Logger } from '@nestjs/common';
|
||||
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
|
||||
@Injectable()
|
||||
export class AppLoggerMiddleware implements NestMiddleware {
|
||||
private logger = new Logger('HTTP');
|
||||
|
||||
use(request: Request, response: Response, next: NextFunction): void {
|
||||
const { ip, method, path: url, baseUrl } = request;
|
||||
const userAgent = request.get('user-agent') || '';
|
||||
|
||||
response.on('close', () => {
|
||||
const { statusCode } = response;
|
||||
const contentLength = response.get('content-length');
|
||||
|
||||
this.logger.log(`${method} ${baseUrl} ${statusCode} ${contentLength} - ${userAgent} ${ip}`);
|
||||
});
|
||||
|
||||
next();
|
||||
}
|
||||
}
|
@@ -0,0 +1,24 @@
|
||||
import { IoAdapter } from '@nestjs/platform-socket.io';
|
||||
import { RedisClient } from 'redis';
|
||||
import { ServerOptions } from 'socket.io';
|
||||
import { createAdapter } from 'socket.io-redis';
|
||||
|
||||
const redis_host = process.env.REDIS_HOSTNAME || 'immich_redis';
|
||||
// const pubClient = createClient({ url: `redis://${redis_host}:6379` });
|
||||
// const subClient = pubClient.duplicate();
|
||||
|
||||
const pubClient = new RedisClient({
|
||||
host: redis_host,
|
||||
port: 6379,
|
||||
});
|
||||
|
||||
const subClient = pubClient.duplicate();
|
||||
const redisAdapter = createAdapter({ pubClient, subClient });
|
||||
|
||||
export class RedisIoAdapter extends IoAdapter {
|
||||
createIOServer(port: number, options?: ServerOptions): any {
|
||||
const server = super.createIOServer(port, options);
|
||||
server.adapter(redisAdapter);
|
||||
return server;
|
||||
}
|
||||
}
|
@@ -0,0 +1,25 @@
|
||||
import { BullModule } from '@nestjs/bull';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { AssetEntity } from '@app/database/entities/asset.entity';
|
||||
import { ExifEntity } from '@app/database/entities/exif.entity';
|
||||
import { SmartInfoEntity } from '@app/database/entities/smart-info.entity';
|
||||
import { BackgroundTaskProcessor } from './background-task.processor';
|
||||
import { BackgroundTaskService } from './background-task.service';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
BullModule.registerQueue({
|
||||
name: 'background-task',
|
||||
defaultJobOptions: {
|
||||
attempts: 3,
|
||||
removeOnComplete: true,
|
||||
removeOnFail: false,
|
||||
},
|
||||
}),
|
||||
TypeOrmModule.forFeature([AssetEntity, ExifEntity, SmartInfoEntity]),
|
||||
],
|
||||
providers: [BackgroundTaskService, BackgroundTaskProcessor],
|
||||
exports: [BackgroundTaskService, BullModule],
|
||||
})
|
||||
export class BackgroundTaskModule {}
|
@@ -0,0 +1,39 @@
|
||||
import { InjectQueue, Process, Processor } from '@nestjs/bull';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Job, Queue } from 'bull';
|
||||
import { Repository } from 'typeorm';
|
||||
import { AssetEntity } from '@app/database/entities/asset.entity';
|
||||
import fs from 'fs';
|
||||
import { Logger } from '@nestjs/common';
|
||||
import axios from 'axios';
|
||||
import { SmartInfoEntity } from '@app/database/entities/smart-info.entity';
|
||||
|
||||
@Processor('background-task')
|
||||
export class BackgroundTaskProcessor {
|
||||
constructor(
|
||||
@InjectRepository(AssetEntity)
|
||||
private assetRepository: Repository<AssetEntity>,
|
||||
|
||||
@InjectRepository(SmartInfoEntity)
|
||||
private smartInfoRepository: Repository<SmartInfoEntity>,
|
||||
) {}
|
||||
|
||||
@Process('delete-file-on-disk')
|
||||
async deleteFileOnDisk(job) {
|
||||
const { assets }: { assets: AssetEntity[] } = job.data;
|
||||
|
||||
for (const asset of assets) {
|
||||
fs.unlink(asset.originalPath, (err) => {
|
||||
if (err) {
|
||||
console.log('error deleting ', asset.originalPath);
|
||||
}
|
||||
});
|
||||
|
||||
fs.unlink(asset.resizePath, (err) => {
|
||||
if (err) {
|
||||
console.log('error deleting ', asset.originalPath);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,23 @@
|
||||
import { InjectQueue } from '@nestjs/bull/dist/decorators';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Queue } from 'bull';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { AssetEntity } from '@app/database/entities/asset.entity';
|
||||
|
||||
@Injectable()
|
||||
export class BackgroundTaskService {
|
||||
constructor(
|
||||
@InjectQueue('background-task')
|
||||
private backgroundTaskQueue: Queue,
|
||||
) {}
|
||||
|
||||
async deleteFileOnDisk(assets: AssetEntity[]) {
|
||||
await this.backgroundTaskQueue.add(
|
||||
'delete-file-on-disk',
|
||||
{
|
||||
assets,
|
||||
},
|
||||
{ jobId: randomUUID() },
|
||||
);
|
||||
}
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
|
||||
@Injectable()
|
||||
export class JwtAuthGuard extends AuthGuard('jwt') {}
|
@@ -0,0 +1,14 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ImmichJwtService } from './immich-jwt.service';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
import { jwtConfig } from '../../config/jwt.config';
|
||||
import { JwtStrategy } from './strategies/jwt.strategy';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { UserEntity } from '@app/database/entities/user.entity';
|
||||
|
||||
@Module({
|
||||
imports: [JwtModule.register(jwtConfig), TypeOrmModule.forFeature([UserEntity])],
|
||||
providers: [ImmichJwtService, JwtStrategy],
|
||||
exports: [ImmichJwtService],
|
||||
})
|
||||
export class ImmichJwtModule {}
|
@@ -0,0 +1,31 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { JwtPayloadDto } from '../../api-v1/auth/dto/jwt-payload.dto';
|
||||
import { jwtSecret } from '../../constants/jwt.constant';
|
||||
|
||||
@Injectable()
|
||||
export class ImmichJwtService {
|
||||
constructor(private jwtService: JwtService) {}
|
||||
|
||||
public async generateToken(payload: JwtPayloadDto) {
|
||||
return this.jwtService.sign({
|
||||
...payload,
|
||||
});
|
||||
}
|
||||
|
||||
public async validateToken(accessToken: string) {
|
||||
try {
|
||||
const payload = await this.jwtService.verify(accessToken, { secret: jwtSecret });
|
||||
return {
|
||||
userId: payload['userId'],
|
||||
status: true,
|
||||
};
|
||||
} catch (e) {
|
||||
Logger.error('Error validating token from websocket request', 'ValidateWebsocketToken');
|
||||
return {
|
||||
userId: null,
|
||||
status: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,33 @@
|
||||
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
||||
import { PassportStrategy } from '@nestjs/passport';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { ExtractJwt, Strategy } from 'passport-jwt';
|
||||
import { Repository } from 'typeorm';
|
||||
import { JwtPayloadDto } from '../../../api-v1/auth/dto/jwt-payload.dto';
|
||||
import { UserEntity } from '@app/database/entities/user.entity';
|
||||
import { jwtSecret } from '../../../constants/jwt.constant';
|
||||
|
||||
@Injectable()
|
||||
export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
|
||||
constructor(
|
||||
@InjectRepository(UserEntity)
|
||||
private usersRepository: Repository<UserEntity>,
|
||||
) {
|
||||
super({
|
||||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
||||
ignoreExpiration: false,
|
||||
secretOrKey: jwtSecret,
|
||||
});
|
||||
}
|
||||
|
||||
async validate(payload: JwtPayloadDto) {
|
||||
const { userId } = payload;
|
||||
const user = await this.usersRepository.findOne({ id: userId });
|
||||
|
||||
if (!user) {
|
||||
throw new UnauthorizedException('Failure to validate JWT payload');
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
@@ -0,0 +1,30 @@
|
||||
import { BullModule } from '@nestjs/bull';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { AssetEntity } from '@app/database/entities/asset.entity';
|
||||
import { ScheduleTasksService } from './schedule-tasks.service';
|
||||
import { MicroservicesModule } from '../../../../microservices/src/microservices.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([AssetEntity]),
|
||||
BullModule.registerQueue({
|
||||
name: 'video-conversion-queue',
|
||||
defaultJobOptions: {
|
||||
attempts: 3,
|
||||
removeOnComplete: true,
|
||||
removeOnFail: false,
|
||||
},
|
||||
}),
|
||||
BullModule.registerQueue({
|
||||
name: 'thumbnail-generator-queue',
|
||||
defaultJobOptions: {
|
||||
attempts: 3,
|
||||
removeOnComplete: true,
|
||||
removeOnFail: false,
|
||||
},
|
||||
}),
|
||||
],
|
||||
providers: [ScheduleTasksService],
|
||||
})
|
||||
export class ScheduleTasksModule {}
|
@@ -0,0 +1,60 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { Cron, CronExpression } from '@nestjs/schedule';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { AssetEntity } from '@app/database/entities/asset.entity';
|
||||
import { InjectQueue } from '@nestjs/bull';
|
||||
import { Queue } from 'bull';
|
||||
import { randomUUID } from 'crypto';
|
||||
|
||||
@Injectable()
|
||||
export class ScheduleTasksService {
|
||||
constructor(
|
||||
@InjectRepository(AssetEntity)
|
||||
private assetRepository: Repository<AssetEntity>,
|
||||
|
||||
@InjectQueue('thumbnail-generator-queue')
|
||||
private thumbnailGeneratorQueue: Queue,
|
||||
|
||||
@InjectQueue('video-conversion-queue')
|
||||
private videoConversionQueue: Queue,
|
||||
) {}
|
||||
|
||||
@Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT)
|
||||
async webpConversion() {
|
||||
Logger.log('Starting Schedule Webp Conversion Tasks', 'CronjobWebpGenerator');
|
||||
|
||||
const assets = await this.assetRepository.find({
|
||||
where: {
|
||||
webpPath: '',
|
||||
},
|
||||
});
|
||||
|
||||
if (assets.length == 0) {
|
||||
Logger.log('All assets has webp file - aborting task', 'CronjobWebpGenerator');
|
||||
return;
|
||||
}
|
||||
|
||||
for (const asset of assets) {
|
||||
await this.thumbnailGeneratorQueue.add('generate-webp-thumbnail', { asset: asset }, { jobId: randomUUID() });
|
||||
}
|
||||
}
|
||||
|
||||
@Cron(CronExpression.EVERY_DAY_AT_1AM)
|
||||
async videoConversion() {
|
||||
const assets = await this.assetRepository.find({
|
||||
where: {
|
||||
type: 'VIDEO',
|
||||
mimeType: 'video/quicktime',
|
||||
encodedVideoPath: '',
|
||||
},
|
||||
order: {
|
||||
createdAt: 'DESC',
|
||||
},
|
||||
});
|
||||
|
||||
for (const asset of assets) {
|
||||
await this.videoConversionQueue.add('mp4-conversion', { asset }, { jobId: randomUUID() });
|
||||
}
|
||||
}
|
||||
}
|
13
server/apps/immich/test/jest-e2e.json
Normal file
13
server/apps/immich/test/jest-e2e.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"moduleFileExtensions": ["js", "json", "ts"],
|
||||
"rootDir": ".",
|
||||
"testEnvironment": "node",
|
||||
"testRegex": ".e2e-spec.ts$",
|
||||
"transform": {
|
||||
"^.+\\.(t|j)s$": "ts-jest"
|
||||
},
|
||||
"moduleNameMapper": {
|
||||
"@app/database/config/(.*)": "<rootDir>../../../libs/database/src/config/$1",
|
||||
"@app/database/entities/(.*)": "<rootDir>../../../libs/database/src/entities/$1"
|
||||
}
|
||||
}
|
37
server/apps/immich/test/test-utils.ts
Normal file
37
server/apps/immich/test/test-utils.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { getConnection } from 'typeorm';
|
||||
import { CanActivate, ExecutionContext } from '@nestjs/common';
|
||||
import { TestingModuleBuilder } from '@nestjs/testing';
|
||||
import { AuthUserDto } from '../src/decorators/auth-user.decorator';
|
||||
import { JwtAuthGuard } from '../src/modules/immich-jwt/guards/jwt-auth.guard';
|
||||
|
||||
type CustomAuthCallback = () => AuthUserDto;
|
||||
|
||||
export async function clearDb() {
|
||||
const entities = getConnection().entityMetadatas;
|
||||
for (const entity of entities) {
|
||||
const repository = getConnection().getRepository(entity.name);
|
||||
await repository.query(`TRUNCATE ${entity.tableName} RESTART IDENTITY CASCADE;`);
|
||||
}
|
||||
}
|
||||
|
||||
export function getAuthUser(): AuthUserDto {
|
||||
return {
|
||||
id: '3108ac14-8afb-4b7e-87fd-39ebb6b79750',
|
||||
email: 'test@email.com',
|
||||
};
|
||||
}
|
||||
|
||||
export function auth(builder: TestingModuleBuilder): TestingModuleBuilder {
|
||||
return authCustom(builder, getAuthUser);
|
||||
}
|
||||
|
||||
export function authCustom(builder: TestingModuleBuilder, callback: CustomAuthCallback): TestingModuleBuilder {
|
||||
const canActivate: CanActivate = {
|
||||
canActivate: (context: ExecutionContext) => {
|
||||
const req = context.switchToHttp().getRequest();
|
||||
req.user = callback();
|
||||
return true;
|
||||
},
|
||||
};
|
||||
return builder.overrideGuard(JwtAuthGuard).useValue(canActivate);
|
||||
}
|
120
server/apps/immich/test/user.e2e-spec.ts
Normal file
120
server/apps/immich/test/user.e2e-spec.ts
Normal file
@@ -0,0 +1,120 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import request from 'supertest';
|
||||
import { clearDb, authCustom } from './test-utils';
|
||||
import { databaseConfig } from '@app/database/config/database.config';
|
||||
import { UserModule } from '../src/api-v1/user/user.module';
|
||||
import { ImmichJwtModule } from '../src/modules/immich-jwt/immich-jwt.module';
|
||||
import { UserService } from '../src/api-v1/user/user.service';
|
||||
import { CreateUserDto } from '../src/api-v1/user/dto/create-user.dto';
|
||||
import { User } from '../src/api-v1/user/response-dto/user';
|
||||
|
||||
function _createUser(userService: UserService, data: CreateUserDto) {
|
||||
return userService.createUser(data);
|
||||
}
|
||||
|
||||
describe('User', () => {
|
||||
let app: INestApplication;
|
||||
|
||||
afterAll(async () => {
|
||||
await clearDb();
|
||||
await app.close();
|
||||
});
|
||||
|
||||
describe('without auth', () => {
|
||||
beforeAll(async () => {
|
||||
const moduleFixture: TestingModule = await Test.createTestingModule({
|
||||
imports: [UserModule, ImmichJwtModule, TypeOrmModule.forRoot(databaseConfig)],
|
||||
}).compile();
|
||||
|
||||
app = moduleFixture.createNestApplication();
|
||||
await app.init();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it('prevents fetching users if not auth', async () => {
|
||||
const { status } = await request(app.getHttpServer()).get('/user');
|
||||
expect(status).toEqual(401);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with auth', () => {
|
||||
let userService: UserService;
|
||||
let authUser: User;
|
||||
|
||||
beforeAll(async () => {
|
||||
const builder = Test.createTestingModule({
|
||||
imports: [UserModule, TypeOrmModule.forRoot(databaseConfig)],
|
||||
});
|
||||
const moduleFixture: TestingModule = await authCustom(builder, () => authUser).compile();
|
||||
|
||||
app = moduleFixture.createNestApplication();
|
||||
userService = app.get(UserService);
|
||||
await app.init();
|
||||
});
|
||||
|
||||
describe('with users in DB', () => {
|
||||
const authUserEmail = 'auth-user@test.com';
|
||||
const userOneEmail = 'one@test.com';
|
||||
const userTwoEmail = 'two@test.com';
|
||||
|
||||
beforeAll(async () => {
|
||||
await Promise.allSettled([
|
||||
_createUser(userService, {
|
||||
firstName: 'auth-user',
|
||||
lastName: 'test',
|
||||
email: authUserEmail,
|
||||
password: '1234',
|
||||
}).then((user) => (authUser = user)),
|
||||
_createUser(userService, {
|
||||
firstName: 'one',
|
||||
lastName: 'test',
|
||||
email: userOneEmail,
|
||||
password: '1234',
|
||||
}),
|
||||
_createUser(userService, {
|
||||
firstName: 'two',
|
||||
lastName: 'test',
|
||||
email: userTwoEmail,
|
||||
password: '1234',
|
||||
}),
|
||||
]);
|
||||
});
|
||||
|
||||
it('fetches the user collection excluding the auth user', async () => {
|
||||
const { status, body } = await request(app.getHttpServer()).get('/user');
|
||||
expect(status).toEqual(200);
|
||||
expect(body).toHaveLength(2);
|
||||
expect(body).toEqual(
|
||||
expect.arrayContaining([
|
||||
{
|
||||
email: userOneEmail,
|
||||
firstName: 'one',
|
||||
lastName: 'test',
|
||||
id: expect.anything(),
|
||||
createdAt: expect.anything(),
|
||||
isAdmin: false,
|
||||
isFirstLoggedIn: true,
|
||||
profileImagePath: '',
|
||||
},
|
||||
{
|
||||
email: userTwoEmail,
|
||||
firstName: 'two',
|
||||
lastName: 'test',
|
||||
id: expect.anything(),
|
||||
createdAt: expect.anything(),
|
||||
isAdmin: false,
|
||||
isFirstLoggedIn: true,
|
||||
profileImagePath: '',
|
||||
},
|
||||
]),
|
||||
);
|
||||
expect(body).toEqual(expect.not.arrayContaining([expect.objectContaining({ email: authUserEmail })]));
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
17
server/apps/immich/tsconfig.app.json
Normal file
17
server/apps/immich/tsconfig.app.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"declaration": false,
|
||||
"outDir": "../../dist/apps/immich"
|
||||
},
|
||||
"include": [
|
||||
"src/**/*",
|
||||
"../../libs/**/*"
|
||||
],
|
||||
"exclude": [
|
||||
"node_modules",
|
||||
"dist",
|
||||
"test",
|
||||
"**/*spec.ts"
|
||||
]
|
||||
}
|
18
server/apps/microservices/src/main.ts
Normal file
18
server/apps/microservices/src/main.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Logger } from '@nestjs/common';
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { MicroservicesModule } from './microservices.module';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(MicroservicesModule);
|
||||
|
||||
await app.listen(3000, () => {
|
||||
if (process.env.NODE_ENV == 'development') {
|
||||
Logger.log('Running Immich Microservices in DEVELOPMENT environment', 'ImmichMicroservice');
|
||||
}
|
||||
|
||||
if (process.env.NODE_ENV == 'production') {
|
||||
Logger.log('Running Immich Microservices in PRODUCTION environment', 'ImmichMicroservice');
|
||||
}
|
||||
});
|
||||
}
|
||||
bootstrap();
|
70
server/apps/microservices/src/microservices.module.ts
Normal file
70
server/apps/microservices/src/microservices.module.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { BullModule } from '@nestjs/bull';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { DatabaseModule } from '@app/database';
|
||||
import { AssetEntity } from '@app/database/entities/asset.entity';
|
||||
import { ExifEntity } from '@app/database/entities/exif.entity';
|
||||
import { SmartInfoEntity } from '@app/database/entities/smart-info.entity';
|
||||
import { UserEntity } from '@app/database/entities/user.entity';
|
||||
import { MicroservicesService } from './microservices.service';
|
||||
import { AssetUploadedProcessor } from './processors/asset-uploaded.processor';
|
||||
import { ThumbnailGeneratorProcessor } from './processors/thumbnail.processor';
|
||||
import { MetadataExtractionProcessor } from './processors/metadata-extraction.processor';
|
||||
import { VideoTranscodeProcessor } from './processors/video-transcode.processor';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
DatabaseModule,
|
||||
TypeOrmModule.forFeature([UserEntity, ExifEntity, AssetEntity, SmartInfoEntity]),
|
||||
BullModule.forRootAsync({
|
||||
useFactory: async () => ({
|
||||
redis: {
|
||||
host: process.env.REDIS_HOSTNAME || 'immich_redis',
|
||||
port: 6379,
|
||||
},
|
||||
}),
|
||||
}),
|
||||
BullModule.registerQueue({
|
||||
name: 'thumbnail-generator-queue',
|
||||
defaultJobOptions: {
|
||||
attempts: 3,
|
||||
removeOnComplete: true,
|
||||
removeOnFail: false,
|
||||
},
|
||||
}),
|
||||
BullModule.registerQueue({
|
||||
name: 'asset-uploaded-queue',
|
||||
defaultJobOptions: {
|
||||
attempts: 3,
|
||||
removeOnComplete: true,
|
||||
removeOnFail: false,
|
||||
},
|
||||
}),
|
||||
BullModule.registerQueue({
|
||||
name: 'metadata-extraction-queue',
|
||||
defaultJobOptions: {
|
||||
attempts: 3,
|
||||
removeOnComplete: true,
|
||||
removeOnFail: false,
|
||||
},
|
||||
}),
|
||||
BullModule.registerQueue({
|
||||
name: 'video-conversion-queue',
|
||||
defaultJobOptions: {
|
||||
attempts: 3,
|
||||
removeOnComplete: true,
|
||||
removeOnFail: false,
|
||||
},
|
||||
}),
|
||||
],
|
||||
controllers: [],
|
||||
providers: [
|
||||
MicroservicesService,
|
||||
AssetUploadedProcessor,
|
||||
ThumbnailGeneratorProcessor,
|
||||
MetadataExtractionProcessor,
|
||||
VideoTranscodeProcessor,
|
||||
],
|
||||
exports: [],
|
||||
})
|
||||
export class MicroservicesModule {}
|
8
server/apps/microservices/src/microservices.service.ts
Normal file
8
server/apps/microservices/src/microservices.service.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class MicroservicesService {
|
||||
getHello(): string {
|
||||
return 'Hello World 123!';
|
||||
}
|
||||
}
|
@@ -0,0 +1,67 @@
|
||||
import { InjectQueue, OnQueueActive, OnQueueCompleted, OnQueueWaiting, Process, Processor } from '@nestjs/bull';
|
||||
import { Job, Queue } from 'bull';
|
||||
import { AssetEntity, AssetType } from '@app/database/entities/asset.entity';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { randomUUID } from 'crypto';
|
||||
|
||||
@Processor('asset-uploaded-queue')
|
||||
export class AssetUploadedProcessor {
|
||||
constructor(
|
||||
@InjectQueue('thumbnail-generator-queue')
|
||||
private thumbnailGeneratorQueue: Queue,
|
||||
|
||||
@InjectQueue('metadata-extraction-queue')
|
||||
private metadataExtractionQueue: Queue,
|
||||
|
||||
@InjectQueue('video-conversion-queue')
|
||||
private videoConversionQueue: Queue,
|
||||
|
||||
@InjectRepository(AssetEntity)
|
||||
private assetRepository: Repository<AssetEntity>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Post processing uploaded asset to perform the following function if missing
|
||||
* 1. Generate JPEG Thumbnail
|
||||
* 2. Generate Webp Thumbnail <-> if JPEG thumbnail exist
|
||||
* 3. EXIF extractor
|
||||
* 4. Reverse Geocoding
|
||||
*
|
||||
* @param job asset-uploaded
|
||||
*/
|
||||
@Process('asset-uploaded')
|
||||
async processUploadedVideo(job: Job) {
|
||||
const {
|
||||
asset,
|
||||
fileName,
|
||||
fileSize,
|
||||
hasThumbnail,
|
||||
}: { asset: AssetEntity; fileName: string; fileSize: number; hasThumbnail: boolean } = job.data;
|
||||
|
||||
if (hasThumbnail) {
|
||||
// The jobs below depends on the existence of jpeg thumbnail
|
||||
await this.thumbnailGeneratorQueue.add('generate-webp-thumbnail', { asset }, { jobId: randomUUID() });
|
||||
await this.metadataExtractionQueue.add('tag-image', { asset }, { jobId: randomUUID() });
|
||||
await this.metadataExtractionQueue.add('detect-object', { asset }, { jobId: randomUUID() });
|
||||
} else {
|
||||
// Generate Thumbnail -> Then generate webp, tag image and detect object
|
||||
}
|
||||
|
||||
// Video Conversion
|
||||
if (asset.type == AssetType.VIDEO) {
|
||||
await this.videoConversionQueue.add('mp4-conversion', { asset }, { jobId: randomUUID() });
|
||||
} else {
|
||||
// Extract Metadata/Exif for Images - Currently the library cannot extract EXIF for video yet
|
||||
await this.metadataExtractionQueue.add(
|
||||
'exif-extraction',
|
||||
{
|
||||
asset,
|
||||
fileName,
|
||||
fileSize,
|
||||
},
|
||||
{ jobId: randomUUID() },
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,131 @@
|
||||
import { Process, Processor } from '@nestjs/bull';
|
||||
import { Job } from 'bull';
|
||||
import { AssetEntity } from '@app/database/entities/asset.entity';
|
||||
import { Repository } from 'typeorm/repository/Repository';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { ExifEntity } from '@app/database/entities/exif.entity';
|
||||
import exifr from 'exifr';
|
||||
import mapboxGeocoding, { GeocodeService } from '@mapbox/mapbox-sdk/services/geocoding';
|
||||
import { MapiResponse } from '@mapbox/mapbox-sdk/lib/classes/mapi-response';
|
||||
import { readFile } from 'fs/promises';
|
||||
import { Logger } from '@nestjs/common';
|
||||
import axios from 'axios';
|
||||
import { SmartInfoEntity } from '@app/database/entities/smart-info.entity';
|
||||
|
||||
@Processor('metadata-extraction-queue')
|
||||
export class MetadataExtractionProcessor {
|
||||
private geocodingClient: GeocodeService;
|
||||
|
||||
constructor(
|
||||
@InjectRepository(AssetEntity)
|
||||
private assetRepository: Repository<AssetEntity>,
|
||||
|
||||
@InjectRepository(ExifEntity)
|
||||
private exifRepository: Repository<ExifEntity>,
|
||||
|
||||
@InjectRepository(SmartInfoEntity)
|
||||
private smartInfoRepository: Repository<SmartInfoEntity>,
|
||||
) {
|
||||
if (process.env.ENABLE_MAPBOX) {
|
||||
this.geocodingClient = mapboxGeocoding({
|
||||
accessToken: process.env.MAPBOX_KEY,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Process('exif-extraction')
|
||||
async extractExifInfo(job: Job) {
|
||||
try {
|
||||
const { asset, fileName, fileSize }: { asset: AssetEntity; fileName: string; fileSize: number } = job.data;
|
||||
|
||||
const fileBuffer = await readFile(asset.originalPath);
|
||||
|
||||
const exifData = await exifr.parse(fileBuffer);
|
||||
|
||||
const newExif = new ExifEntity();
|
||||
newExif.assetId = asset.id;
|
||||
newExif.make = exifData['Make'] || null;
|
||||
newExif.model = exifData['Model'] || null;
|
||||
newExif.imageName = fileName || null;
|
||||
newExif.exifImageHeight = exifData['ExifImageHeight'] || null;
|
||||
newExif.exifImageWidth = exifData['ExifImageWidth'] || null;
|
||||
newExif.fileSizeInByte = fileSize || null;
|
||||
newExif.orientation = exifData['Orientation'] || null;
|
||||
newExif.dateTimeOriginal = exifData['DateTimeOriginal'] || null;
|
||||
newExif.modifyDate = exifData['ModifyDate'] || null;
|
||||
newExif.lensModel = exifData['LensModel'] || null;
|
||||
newExif.fNumber = exifData['FNumber'] || null;
|
||||
newExif.focalLength = exifData['FocalLength'] || null;
|
||||
newExif.iso = exifData['ISO'] || null;
|
||||
newExif.exposureTime = exifData['ExposureTime'] || null;
|
||||
newExif.latitude = exifData['latitude'] || null;
|
||||
newExif.longitude = exifData['longitude'] || null;
|
||||
|
||||
// Reverse GeoCoding
|
||||
if (process.env.ENABLE_MAPBOX && exifData['longitude'] && exifData['latitude']) {
|
||||
const geoCodeInfo: MapiResponse = await this.geocodingClient
|
||||
.reverseGeocode({
|
||||
query: [exifData['longitude'], exifData['latitude']],
|
||||
types: ['country', 'region', 'place'],
|
||||
})
|
||||
.send();
|
||||
|
||||
const res: [] = geoCodeInfo.body['features'];
|
||||
|
||||
const city = res.filter((geoInfo) => geoInfo['place_type'][0] == 'place')[0]['text'];
|
||||
const state = res.filter((geoInfo) => geoInfo['place_type'][0] == 'region')[0]['text'];
|
||||
const country = res.filter((geoInfo) => geoInfo['place_type'][0] == 'country')[0]['text'];
|
||||
|
||||
newExif.city = city || null;
|
||||
newExif.state = state || null;
|
||||
newExif.country = country || null;
|
||||
}
|
||||
|
||||
await this.exifRepository.save(newExif);
|
||||
} catch (e) {
|
||||
Logger.error(`Error extracting EXIF ${e.toString()}`, 'extractExif');
|
||||
}
|
||||
}
|
||||
|
||||
@Process({ name: 'tag-image', concurrency: 2 })
|
||||
async tagImage(job: Job) {
|
||||
const { asset }: { asset: AssetEntity } = job.data;
|
||||
|
||||
const res = await axios.post('http://immich-machine-learning:3001/image-classifier/tag-image', {
|
||||
thumbnailPath: asset.resizePath,
|
||||
});
|
||||
|
||||
if (res.status == 201 && res.data.length > 0) {
|
||||
const smartInfo = new SmartInfoEntity();
|
||||
smartInfo.assetId = asset.id;
|
||||
smartInfo.tags = [...res.data];
|
||||
|
||||
await this.smartInfoRepository.upsert(smartInfo, {
|
||||
conflictPaths: ['assetId'],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Process({ name: 'detect-object', concurrency: 2 })
|
||||
async detectObject(job: Job) {
|
||||
try {
|
||||
const { asset }: { asset: AssetEntity } = job.data;
|
||||
|
||||
const res = await axios.post('http://immich-machine-learning:3001/object-detection/detect-object', {
|
||||
thumbnailPath: asset.resizePath,
|
||||
});
|
||||
|
||||
if (res.status == 201 && res.data.length > 0) {
|
||||
const smartInfo = new SmartInfoEntity();
|
||||
smartInfo.assetId = asset.id;
|
||||
smartInfo.objects = [...res.data];
|
||||
|
||||
await this.smartInfoRepository.upsert(smartInfo, {
|
||||
conflictPaths: ['assetId'],
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
Logger.error(`Failed to trigger object detection pipe line ${error.toString()}`);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
import { Process, Processor } from '@nestjs/bull';
|
||||
import { Job } from 'bull';
|
||||
import { AssetEntity } from '@app/database/entities/asset.entity';
|
||||
import { Repository } from 'typeorm/repository/Repository';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import sharp from 'sharp';
|
||||
|
||||
@Processor('thumbnail-generator-queue')
|
||||
export class ThumbnailGeneratorProcessor {
|
||||
constructor(
|
||||
@InjectRepository(AssetEntity)
|
||||
private assetRepository: Repository<AssetEntity>,
|
||||
) {}
|
||||
|
||||
@Process('generate-jpeg-thumbnail')
|
||||
async generateJPEGThumbnail(job: Job) {
|
||||
const { asset }: { asset: AssetEntity } = job.data;
|
||||
|
||||
console.log(asset);
|
||||
}
|
||||
|
||||
@Process({ name: 'generate-webp-thumbnail', concurrency: 2 })
|
||||
async generateWepbThumbnail(job: Job) {
|
||||
const { asset }: { asset: AssetEntity } = job.data;
|
||||
|
||||
const webpPath = asset.resizePath.replace('jpeg', 'webp');
|
||||
|
||||
sharp(asset.resizePath)
|
||||
.resize(250)
|
||||
.webp()
|
||||
.toFile(webpPath, (err, info) => {
|
||||
if (!err) {
|
||||
this.assetRepository.update({ id: asset.id }, { webpPath: webpPath });
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@@ -0,0 +1,59 @@
|
||||
import { Process, Processor } from '@nestjs/bull';
|
||||
import { Logger } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Job } from 'bull';
|
||||
import ffmpeg from 'fluent-ffmpeg';
|
||||
import { existsSync, mkdirSync } from 'fs';
|
||||
import { Repository } from 'typeorm';
|
||||
import { AssetEntity } from '../../../../libs/database/src/entities/asset.entity';
|
||||
import { APP_UPLOAD_LOCATION } from '../../../immich/src/constants/upload_location.constant';
|
||||
|
||||
@Processor('video-conversion-queue')
|
||||
export class VideoTranscodeProcessor {
|
||||
constructor(
|
||||
@InjectRepository(AssetEntity)
|
||||
private assetRepository: Repository<AssetEntity>,
|
||||
) {}
|
||||
|
||||
@Process({ name: 'mp4-conversion', concurrency: 1 })
|
||||
async mp4Conversion(job: Job) {
|
||||
const { asset }: { asset: AssetEntity } = job.data;
|
||||
|
||||
if (asset.mimeType != 'video/mp4') {
|
||||
const basePath = APP_UPLOAD_LOCATION;
|
||||
const encodedVideoPath = `${basePath}/${asset.userId}/encoded-video`;
|
||||
|
||||
if (!existsSync(encodedVideoPath)) {
|
||||
mkdirSync(encodedVideoPath, { recursive: true });
|
||||
}
|
||||
|
||||
const savedEncodedPath = encodedVideoPath + '/' + asset.id + '.mp4';
|
||||
|
||||
if (asset.encodedVideoPath == '' || !asset.encodedVideoPath) {
|
||||
// Put the processing into its own async function to prevent the job exist right away
|
||||
await this.runFFMPEGPipeLine(asset, savedEncodedPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async runFFMPEGPipeLine(asset: AssetEntity, savedEncodedPath: string): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
ffmpeg(asset.originalPath)
|
||||
.outputOptions(['-crf 23', '-preset ultrafast', '-vcodec libx264', '-acodec mp3', '-vf scale=1280:-2'])
|
||||
.output(savedEncodedPath)
|
||||
.on('start', () => {
|
||||
Logger.log('Start Converting', 'mp4Conversion');
|
||||
})
|
||||
.on('error', (error, b, c) => {
|
||||
Logger.error(`Cannot Convert Video ${error}`, 'mp4Conversion');
|
||||
reject();
|
||||
})
|
||||
.on('end', async () => {
|
||||
Logger.log(`Converting Success ${asset.id}`, 'mp4Conversion');
|
||||
await this.assetRepository.update({ id: asset.id }, { encodedVideoPath: savedEncodedPath });
|
||||
resolve();
|
||||
})
|
||||
.run();
|
||||
});
|
||||
}
|
||||
}
|
21
server/apps/microservices/test/app.e2e-spec.ts
Normal file
21
server/apps/microservices/test/app.e2e-spec.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import * as request from 'supertest';
|
||||
import { MicroservicesModule } from './../src/microservices.module';
|
||||
|
||||
describe('MicroservicesController (e2e)', () => {
|
||||
let app: INestApplication;
|
||||
|
||||
beforeEach(async () => {
|
||||
const moduleFixture: TestingModule = await Test.createTestingModule({
|
||||
imports: [MicroservicesModule],
|
||||
}).compile();
|
||||
|
||||
app = moduleFixture.createNestApplication();
|
||||
await app.init();
|
||||
});
|
||||
|
||||
it('/ (GET)', () => {
|
||||
return request(app.getHttpServer()).get('/').expect(200).expect('Hello World!');
|
||||
});
|
||||
});
|
9
server/apps/microservices/test/jest-e2e.json
Normal file
9
server/apps/microservices/test/jest-e2e.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"moduleFileExtensions": ["js", "json", "ts"],
|
||||
"rootDir": ".",
|
||||
"testEnvironment": "node",
|
||||
"testRegex": ".e2e-spec.ts$",
|
||||
"transform": {
|
||||
"^.+\\.(t|j)s$": "ts-jest"
|
||||
}
|
||||
}
|
9
server/apps/microservices/tsconfig.app.json
Normal file
9
server/apps/microservices/tsconfig.app.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"declaration": false,
|
||||
"outDir": "../../dist/apps/microservices"
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
|
||||
}
|
Reference in New Issue
Block a user