Skip to content

Commit 0a895bd

Browse files
committed
refactor(attachments): reuse share validation
1 parent 54134b9 commit 0a895bd

File tree

3 files changed

+11
-43
lines changed

3 files changed

+11
-43
lines changed

src/attachments/attachments.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { AttachmentsService } from 'omniboxd/attachments/attachments.service';
66
import { MinioModule } from 'omniboxd/minio/minio.module';
77
import { ResourceAttachmentsModule } from 'omniboxd/resource-attachments/resource-attachments.module';
88
import { SharesModule } from 'omniboxd/shares/shares.module';
9-
import { ResourcesModule } from 'omniboxd/resources/resources.module';
9+
import { ShareResourcesModule } from 'omniboxd/share-resources/share-resources.module';
1010

1111
@Module({
1212
exports: [AttachmentsService],
@@ -17,7 +17,7 @@ import { ResourcesModule } from 'omniboxd/resources/resources.module';
1717
MinioModule,
1818
ResourceAttachmentsModule,
1919
SharesModule,
20-
ResourcesModule,
20+
ShareResourcesModule,
2121
],
2222
})
2323
export class AttachmentsModule {}

src/attachments/attachments.service.ts

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
import { SharesService } from 'omniboxd/shares/shares.service';
2323
import { ResourcesService } from 'omniboxd/resources/resources.service';
2424
import * as bcrypt from 'bcrypt';
25+
import { ShareResourcesService } from 'omniboxd/share-resources/share-resources.service';
2526

2627
@Injectable()
2728
export class AttachmentsService {
@@ -32,7 +33,7 @@ export class AttachmentsService {
3233
private readonly permissionsService: PermissionsService,
3334
private readonly resourceAttachmentsService: ResourceAttachmentsService,
3435
private readonly sharesService: SharesService,
35-
private readonly resourcesService: ResourcesService,
36+
private readonly shareResourcesService: ShareResourcesService,
3637
) {}
3738

3839
async checkPermission(
@@ -195,45 +196,12 @@ export class AttachmentsService {
195196
userId: string | undefined,
196197
httpResponse: Response,
197198
) {
198-
const share = await this.sharesService.getShareById(shareId);
199-
if (!share || !share.enabled) {
200-
throw new NotFoundException(`No share found with id ${shareId}`);
201-
}
202-
203-
// Check if share has expired
204-
if (share.expiresAt && share.expiresAt < new Date()) {
205-
throw new NotFoundException(`No share found with id ${shareId}`);
206-
}
207-
208-
if (share.requireLogin && !userId) {
209-
throw new UnauthorizedException('This share requires login');
210-
}
211-
212-
if (share.password) {
213-
if (!password) {
214-
throw new ForbiddenException(`Invalid password for share ${shareId}`);
215-
}
216-
const match = await bcrypt.compare(password, share.password);
217-
if (!match) {
218-
throw new ForbiddenException(`Invalid password for share ${shareId}`);
219-
}
220-
}
221-
222-
const resource = await this.resourcesService.get(resourceId);
223-
if (!resource || resource.namespaceId != share.namespaceId) {
224-
throw new NotFoundException(`No resource found with id ${resourceId}`);
225-
}
226-
227-
if (resource.id !== share.resourceId) {
228-
const parents = await this.resourcesService.getParentResources(
229-
share.namespaceId,
230-
resource.parentId,
231-
);
232-
if (!parents.map((r) => r.id).includes(share.resourceId)) {
233-
throw new NotFoundException(`No resource found with id ${resourceId}`);
234-
}
235-
}
236-
199+
const share = await this.sharesService.getAndValidateShare(
200+
shareId,
201+
password,
202+
userId,
203+
);
204+
await this.shareResourcesService.getAndValidateResource(share, resourceId);
237205
await this.resourceAttachmentsService.getResourceAttachmentOrFail(
238206
share.namespaceId,
239207
resourceId,

src/share-resources/share-resources.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class ShareResourcesService {
5151
return children.map((child) => SharedResourceMetaDto.fromEntity(child));
5252
}
5353

54-
private async getAndValidateResource(
54+
async getAndValidateResource(
5555
share: Share,
5656
resourceId: string,
5757
): Promise<Resource> {

0 commit comments

Comments
 (0)