@@ -22,6 +22,7 @@ import {
2222import { SharesService } from 'omniboxd/shares/shares.service' ;
2323import { ResourcesService } from 'omniboxd/resources/resources.service' ;
2424import * as bcrypt from 'bcrypt' ;
25+ import { ShareResourcesService } from 'omniboxd/share-resources/share-resources.service' ;
2526
2627@Injectable ( )
2728export 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 ,
0 commit comments