mirror of
https://github.com/immich-app/immich.git
synced 2025-12-24 09:14:58 +03:00
* Added read-only flag for assets, endpoint to trigger file import vs upload * updated fixtures with new property * if upload is 'read-only', ensure there is no existing asset at the designated originalPath * added test for file import as well as detecting existing image at read-only destination location * Added storage service test for a case where it should not move read-only assets * upload doesn't need the read-only flag available, just importing * default isReadOnly on import endpoint to true * formatting fixes * create-asset dto needs isReadOnly, so set it to false by default on create, updated api generation * updated code to reflect changes in MR * fixed read stream promise return type * new index for originalPath, check for existing path on import, reglardless of user, to prevent duplicates * refactor: import asset * chore: open api * chore: tests * Added externalPath support for individual users, updated UI to allow this to be set by admin * added missing var for externalPath in ui * chore: open api * fix: compilation issues * fix: server test * built api, fixed user-response dto to include externalPath * reverted accidental commit * bad commit of duplicate externalPath in user response dto * fixed tests to include externalPath on expected result * fix: unit tests * centralized supported filetypes, perform file type checking of asset and sidecar during file import process * centralized supported filetype check method to keep regex DRY * fixed typo * combined migrations into one * update api * Removed externalPath from shared-link code, added column to admin user page whether external paths / import is enabled or not * update mimetype * Fixed detect correct mimetype * revert asset-upload config * reverted domain.constant * refactor * fix mime-type issue * fix format --------- Co-authored-by: Jason Rasmussen <jrasm91@gmail.com> Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
161 lines
5.3 KiB
TypeScript
161 lines
5.3 KiB
TypeScript
import { UserEntity } from '@app/infra/entities';
|
|
import {
|
|
BadRequestException,
|
|
ForbiddenException,
|
|
InternalServerErrorException,
|
|
Logger,
|
|
NotFoundException,
|
|
} from '@nestjs/common';
|
|
import { constants, createReadStream, ReadStream } from 'fs';
|
|
import fs from 'fs/promises';
|
|
import { AuthUserDto } from '../auth';
|
|
import { ICryptoRepository } from '../crypto';
|
|
import { CreateAdminDto, CreateUserDto, CreateUserOAuthDto } from './dto/create-user.dto';
|
|
import { IUserRepository, UserListFilter } from './user.repository';
|
|
|
|
const SALT_ROUNDS = 10;
|
|
|
|
export class UserCore {
|
|
constructor(private userRepository: IUserRepository, private cryptoRepository: ICryptoRepository) {}
|
|
|
|
async updateUser(authUser: AuthUserDto, id: string, dto: Partial<UserEntity>): Promise<UserEntity> {
|
|
if (!authUser.isAdmin && authUser.id !== id) {
|
|
throw new ForbiddenException('You are not allowed to update this user');
|
|
}
|
|
|
|
if (!authUser.isAdmin) {
|
|
// Users can never update the isAdmin property.
|
|
delete dto.isAdmin;
|
|
delete dto.storageLabel;
|
|
delete dto.externalPath;
|
|
} else if (dto.isAdmin && authUser.id !== id) {
|
|
// Admin cannot create another admin.
|
|
throw new BadRequestException('The server already has an admin');
|
|
}
|
|
|
|
if (dto.email) {
|
|
const duplicate = await this.userRepository.getByEmail(dto.email);
|
|
if (duplicate && duplicate.id !== id) {
|
|
throw new BadRequestException('Email already in use by another account');
|
|
}
|
|
}
|
|
|
|
if (dto.storageLabel) {
|
|
const duplicate = await this.userRepository.getByStorageLabel(dto.storageLabel);
|
|
if (duplicate && duplicate.id !== id) {
|
|
throw new BadRequestException('Storage label already in use by another account');
|
|
}
|
|
}
|
|
|
|
try {
|
|
if (dto.password) {
|
|
dto.password = await this.cryptoRepository.hashBcrypt(dto.password, SALT_ROUNDS);
|
|
}
|
|
|
|
if (dto.storageLabel === '') {
|
|
dto.storageLabel = null;
|
|
}
|
|
|
|
if (dto.externalPath === '') {
|
|
dto.externalPath = null;
|
|
}
|
|
|
|
return this.userRepository.update(id, dto);
|
|
} catch (e) {
|
|
Logger.error(e, 'Failed to update user info');
|
|
throw new InternalServerErrorException('Failed to update user info');
|
|
}
|
|
}
|
|
|
|
async createUser(createUserDto: CreateUserDto | CreateAdminDto | CreateUserOAuthDto): Promise<UserEntity> {
|
|
const user = await this.userRepository.getByEmail(createUserDto.email);
|
|
if (user) {
|
|
throw new BadRequestException('User exists');
|
|
}
|
|
|
|
if (!(createUserDto as CreateAdminDto).isAdmin) {
|
|
const localAdmin = await this.userRepository.getAdmin();
|
|
if (!localAdmin) {
|
|
throw new BadRequestException('The first registered account must the administrator.');
|
|
}
|
|
}
|
|
|
|
try {
|
|
const payload: Partial<UserEntity> = { ...createUserDto };
|
|
if (payload.password) {
|
|
payload.password = await this.cryptoRepository.hashBcrypt(payload.password, SALT_ROUNDS);
|
|
}
|
|
return this.userRepository.create(payload);
|
|
} catch (e) {
|
|
Logger.error(e, 'Create new user');
|
|
throw new InternalServerErrorException('Failed to register new user');
|
|
}
|
|
}
|
|
|
|
async get(userId: string, withDeleted?: boolean): Promise<UserEntity | null> {
|
|
return this.userRepository.get(userId, withDeleted);
|
|
}
|
|
|
|
async getAdmin(): Promise<UserEntity | null> {
|
|
return this.userRepository.getAdmin();
|
|
}
|
|
|
|
async getByEmail(email: string, withPassword?: boolean): Promise<UserEntity | null> {
|
|
return this.userRepository.getByEmail(email, withPassword);
|
|
}
|
|
|
|
async getByOAuthId(oauthId: string): Promise<UserEntity | null> {
|
|
return this.userRepository.getByOAuthId(oauthId);
|
|
}
|
|
|
|
async getUserProfileImage(user: UserEntity): Promise<ReadStream> {
|
|
if (!user.profileImagePath) {
|
|
throw new NotFoundException('User does not have a profile image');
|
|
}
|
|
await fs.access(user.profileImagePath, constants.R_OK | constants.W_OK);
|
|
return createReadStream(user.profileImagePath);
|
|
}
|
|
|
|
async getList(filter?: UserListFilter): Promise<UserEntity[]> {
|
|
return this.userRepository.getList(filter);
|
|
}
|
|
|
|
async createProfileImage(authUser: AuthUserDto, filePath: string): Promise<UserEntity> {
|
|
try {
|
|
return this.userRepository.update(authUser.id, { profileImagePath: filePath });
|
|
} catch (e) {
|
|
Logger.error(e, 'Create User Profile Image');
|
|
throw new InternalServerErrorException('Failed to create new user profile image');
|
|
}
|
|
}
|
|
|
|
async restoreUser(authUser: AuthUserDto, userToRestore: UserEntity): Promise<UserEntity> {
|
|
if (!authUser.isAdmin) {
|
|
throw new ForbiddenException('Unauthorized');
|
|
}
|
|
try {
|
|
return this.userRepository.restore(userToRestore);
|
|
} catch (e) {
|
|
Logger.error(e, 'Failed to restore deleted user');
|
|
throw new InternalServerErrorException('Failed to restore deleted user');
|
|
}
|
|
}
|
|
|
|
async deleteUser(authUser: AuthUserDto, userToDelete: UserEntity): Promise<UserEntity> {
|
|
if (!authUser.isAdmin) {
|
|
throw new ForbiddenException('Unauthorized');
|
|
}
|
|
|
|
if (userToDelete.isAdmin) {
|
|
throw new ForbiddenException('Cannot delete admin user');
|
|
}
|
|
|
|
try {
|
|
return this.userRepository.delete(userToDelete);
|
|
} catch (e) {
|
|
Logger.error(e, 'Failed to delete user');
|
|
throw new InternalServerErrorException('Failed to delete user');
|
|
}
|
|
}
|
|
}
|