@@ -7,10 +7,10 @@ import {
77 NotFoundException ,
88} from '@nestjs/common' ;
99import { Response } from 'express' ;
10- import { MinioService } from 'omnibox-backend/resources/ minio/minio.service' ;
10+ import { MinioService } from 'omnibox-backend/minio/minio.service' ;
1111import { PermissionsService } from 'omnibox-backend/permissions/permissions.service' ;
1212import { ResourcePermission } from 'omnibox-backend/permissions/resource-permission.enum' ;
13- import { objectStreamResponse } from 'omnibox-backend/resources /utils' ;
13+ import { objectStreamResponse } from 'omnibox-backend/minio /utils' ;
1414
1515@Injectable ( )
1616export class AttachmentsService {
@@ -38,12 +38,16 @@ export class AttachmentsService {
3838 }
3939 }
4040
41+ minioPath ( attachmentId : string ) : string {
42+ return `attachments/${ attachmentId } ` ;
43+ }
44+
4145 async checkAttachment (
4246 namespaceId : string ,
4347 resourceId : string ,
4448 attachmentId : string ,
4549 ) {
46- const info = await this . minioService . info ( attachmentId ) ;
50+ const info = await this . minioService . info ( this . minioPath ( attachmentId ) ) ;
4751 if (
4852 info . metadata . namespaceId === namespaceId ||
4953 info . metadata . resourceId === resourceId
@@ -53,6 +57,27 @@ export class AttachmentsService {
5357 throw new NotFoundException ( attachmentId ) ;
5458 }
5559
60+ async uploadAttachment (
61+ namespaceId : string ,
62+ resourceId : string ,
63+ userId : string ,
64+ filename : string ,
65+ buffer : Buffer ,
66+ mimetype : string ,
67+ ) {
68+ await this . checkPermission (
69+ namespaceId ,
70+ resourceId ,
71+ userId ,
72+ ResourcePermission . CAN_EDIT ,
73+ ) ;
74+ const { id } = await this . minioService . put ( filename , buffer , mimetype , {
75+ metadata : { namespaceId, resourceId, userId } ,
76+ folder : 'attachments' ,
77+ } ) ;
78+ return id ;
79+ }
80+
5681 async uploadAttachments (
5782 namespaceId : string ,
5883 resourceId : string ,
@@ -72,13 +97,13 @@ export class AttachmentsService {
7297 try {
7398 const filename : string = encodeFileName ( file . originalname ) ;
7499 file . originalname = filename ;
75- const { id } = await this . minioService . put (
100+ const id = await this . uploadAttachment (
101+ namespaceId ,
102+ resourceId ,
103+ userId ,
76104 filename ,
77105 file . buffer ,
78106 file . mimetype ,
79- {
80- metadata : { namespaceId, resourceId, userId } ,
81- } ,
82107 ) ;
83108 uploaded . push ( {
84109 name : filename ,
@@ -111,7 +136,9 @@ export class AttachmentsService {
111136 resourceId ,
112137 attachmentId ,
113138 ) ;
114- const stream = await this . minioService . getObject ( attachmentId ) ;
139+ const stream = await this . minioService . getObject (
140+ this . minioPath ( attachmentId ) ,
141+ ) ;
115142 return objectStreamResponse ( { stream, ...info } , httpResponse ) ;
116143 }
117144
@@ -128,7 +155,7 @@ export class AttachmentsService {
128155 ResourcePermission . CAN_EDIT ,
129156 ) ;
130157 await this . checkAttachment ( namespaceId , resourceId , attachmentId ) ;
131- await this . minioService . removeObject ( attachmentId ) ;
158+ await this . minioService . removeObject ( this . minioPath ( attachmentId ) ) ;
132159 return { id : attachmentId , success : true } ;
133160 }
134161
@@ -137,7 +164,9 @@ export class AttachmentsService {
137164 userId : string ,
138165 httpResponse : Response ,
139166 ) {
140- const objectResponse = await this . minioService . get ( attachmentId ) ;
167+ const objectResponse = await this . minioService . get (
168+ this . minioPath ( attachmentId ) ,
169+ ) ;
141170 const { namespaceId, resourceId } = objectResponse . metadata ;
142171
143172 await this . checkPermission (
0 commit comments