|
@@ -1,4 +1,12 @@
|
|
-import { ForbiddenException, Inject, Injectable, Logger } from '@nestjs/common';
|
|
|
|
|
|
+import {
|
|
|
|
+ BadRequestException,
|
|
|
|
+ ForbiddenException,
|
|
|
|
+ Inject,
|
|
|
|
+ Injectable,
|
|
|
|
+ Logger,
|
|
|
|
+ UnauthorizedException,
|
|
|
|
+} from '@nestjs/common';
|
|
|
|
+import { UserService } from '@app/domain';
|
|
import { AuthUserDto } from '../../decorators/auth-user.decorator';
|
|
import { AuthUserDto } from '../../decorators/auth-user.decorator';
|
|
import { EditSharedLinkDto } from './dto/edit-shared-link.dto';
|
|
import { EditSharedLinkDto } from './dto/edit-shared-link.dto';
|
|
import { mapSharedLinkToResponseDto, SharedLinkResponseDto } from './response-dto/shared-link-response.dto';
|
|
import { mapSharedLinkToResponseDto, SharedLinkResponseDto } from './response-dto/shared-link-response.dto';
|
|
@@ -13,9 +21,31 @@ export class ShareService {
|
|
constructor(
|
|
constructor(
|
|
@Inject(ISharedLinkRepository)
|
|
@Inject(ISharedLinkRepository)
|
|
sharedLinkRepository: ISharedLinkRepository,
|
|
sharedLinkRepository: ISharedLinkRepository,
|
|
|
|
+ private userService: UserService,
|
|
) {
|
|
) {
|
|
this.shareCore = new ShareCore(sharedLinkRepository);
|
|
this.shareCore = new ShareCore(sharedLinkRepository);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ async validate(key: string): Promise<AuthUserDto> {
|
|
|
|
+ const link = await this.shareCore.getSharedLinkByKey(key);
|
|
|
|
+ if (link) {
|
|
|
|
+ if (!link.expiresAt || new Date(link.expiresAt) > new Date()) {
|
|
|
|
+ const user = await this.userService.getUserById(link.userId).catch(() => null);
|
|
|
|
+ if (user) {
|
|
|
|
+ return {
|
|
|
|
+ id: user.id,
|
|
|
|
+ email: user.email,
|
|
|
|
+ isAdmin: user.isAdmin,
|
|
|
|
+ isPublicUser: true,
|
|
|
|
+ sharedLinkId: link.id,
|
|
|
|
+ isAllowUpload: link.allowUpload,
|
|
|
|
+ };
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ throw new UnauthorizedException();
|
|
|
|
+ }
|
|
|
|
+
|
|
async getAll(authUser: AuthUserDto): Promise<SharedLinkResponseDto[]> {
|
|
async getAll(authUser: AuthUserDto): Promise<SharedLinkResponseDto[]> {
|
|
const links = await this.shareCore.getSharedLinks(authUser.id);
|
|
const links = await this.shareCore.getSharedLinks(authUser.id);
|
|
return links.map(mapSharedLinkToResponseDto);
|
|
return links.map(mapSharedLinkToResponseDto);
|
|
@@ -26,13 +56,14 @@ export class ShareService {
|
|
throw new ForbiddenException();
|
|
throw new ForbiddenException();
|
|
}
|
|
}
|
|
|
|
|
|
- const link = await this.shareCore.getSharedLinkById(authUser.sharedLinkId);
|
|
|
|
-
|
|
|
|
- return mapSharedLinkToResponseDto(link);
|
|
|
|
|
|
+ return this.getById(authUser.sharedLinkId);
|
|
}
|
|
}
|
|
|
|
|
|
async getById(id: string): Promise<SharedLinkResponseDto> {
|
|
async getById(id: string): Promise<SharedLinkResponseDto> {
|
|
const link = await this.shareCore.getSharedLinkById(id);
|
|
const link = await this.shareCore.getSharedLinkById(id);
|
|
|
|
+ if (!link) {
|
|
|
|
+ throw new BadRequestException('Shared link not found');
|
|
|
|
+ }
|
|
return mapSharedLinkToResponseDto(link);
|
|
return mapSharedLinkToResponseDto(link);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -43,12 +74,14 @@ export class ShareService {
|
|
|
|
|
|
async getByKey(key: string): Promise<SharedLinkResponseDto> {
|
|
async getByKey(key: string): Promise<SharedLinkResponseDto> {
|
|
const link = await this.shareCore.getSharedLinkByKey(key);
|
|
const link = await this.shareCore.getSharedLinkByKey(key);
|
|
|
|
+ if (!link) {
|
|
|
|
+ throw new BadRequestException('Shared link not found');
|
|
|
|
+ }
|
|
return mapSharedLinkToResponseDto(link);
|
|
return mapSharedLinkToResponseDto(link);
|
|
}
|
|
}
|
|
|
|
|
|
async edit(id: string, authUser: AuthUserDto, dto: EditSharedLinkDto) {
|
|
async edit(id: string, authUser: AuthUserDto, dto: EditSharedLinkDto) {
|
|
const link = await this.shareCore.updateSharedLink(id, authUser.id, dto);
|
|
const link = await this.shareCore.updateSharedLink(id, authUser.id, dto);
|
|
-
|
|
|
|
return mapSharedLinkToResponseDto(link);
|
|
return mapSharedLinkToResponseDto(link);
|
|
}
|
|
}
|
|
}
|
|
}
|