-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cba9544
commit 54b0b81
Showing
11 changed files
with
322 additions
and
74 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
packages/backend/scripts/src/modules/rest/attachments/attachment.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { Body, Controller, Delete, Get, Inject, Param, Post, Put, StreamableFile, UploadedFile, UseGuards, UseInterceptors } from '@nestjs/common' | ||
import { REQUEST } from '@nestjs/core' | ||
import { FileInterceptor } from '@nestjs/platform-express' | ||
import { ApiBearerAuth, ApiBody, ApiConsumes, ApiExtraModels, ApiParam, ApiResponse, getSchemaPath } from '@nestjs/swagger' | ||
|
||
import { Attachment, AttachmentAddData, AttachmentREST, AttachmentUpdateData } from 'productboard-common' | ||
|
||
import { AttachmentService } from './attachment.service' | ||
import { canCreateAttachmentOrFail, canDeleteAttachmentOrFail, canFindAttachmentOrFail, canReadAttachmentOrFail, canUpdateAttachmentOrFail } from '../../../functions/permission' | ||
import { AuthorizedRequest } from '../../../request' | ||
import { TokenOptionalGuard } from '../tokens/token.guard' | ||
|
||
@Controller('rest/products/:productId/attachments') | ||
@UseGuards(TokenOptionalGuard) | ||
@ApiBearerAuth() | ||
@ApiExtraModels(AttachmentAddData, AttachmentUpdateData) | ||
export class AttachmentController implements AttachmentREST<string, string, Express.Multer.File> { | ||
constructor( | ||
private readonly service: AttachmentService, | ||
@Inject(REQUEST) | ||
private readonly request: AuthorizedRequest | ||
) {} | ||
|
||
@Get() | ||
@ApiParam({ name: 'productId', type: 'string', required: true }) | ||
@ApiResponse({ type: [Attachment] }) | ||
async findAttachments( | ||
@Param('productId') productId: string | ||
): Promise<Attachment[]> { | ||
await canFindAttachmentOrFail(this.request.user.userId, productId) | ||
return this.service.findAttachments(productId) | ||
} | ||
|
||
@Post() | ||
@UseInterceptors(FileInterceptor('file')) | ||
@ApiParam({ name: 'productId', type: 'string', required: true}) | ||
@ApiConsumes('multipart/form-data') | ||
@ApiBody({ | ||
schema: { | ||
type: 'object', | ||
properties: { | ||
data: { $ref: getSchemaPath(AttachmentAddData) }, | ||
file: { type: 'string', format: 'binary' } | ||
}, | ||
required: ['data', 'file'] | ||
} | ||
}) | ||
@ApiResponse({ type: Attachment }) | ||
async addAttachment( | ||
@Param('productId') productId: string, | ||
@Body('data') data: string, | ||
@UploadedFile() file: Express.Multer.File | ||
): Promise<Attachment> { | ||
await canCreateAttachmentOrFail(this.request.user.userId, productId) | ||
return this.addAttachment(productId, JSON.parse(data), file) | ||
} | ||
|
||
@Get(':attachmentId') | ||
@ApiParam({ name: 'productId', type: 'string', required: true }) | ||
@ApiParam({ name: 'attachmentId', type: 'string', required: true }) | ||
@ApiResponse({ type: Attachment }) | ||
async getAttachment( | ||
@Param('productId') productId: string, | ||
@Param('attachmentId') attachmentId: string | ||
): Promise<Attachment> { | ||
await canReadAttachmentOrFail(this.request.user.userId, productId, attachmentId) | ||
return this.service.getAttachment(productId, attachmentId) | ||
} | ||
|
||
@Get(':attachmentId/file') | ||
@ApiParam({ name: 'productId', type: 'string', required: true }) | ||
@ApiParam({ name: 'attachmentId', type: 'string', required: true }) | ||
@ApiResponse({ type: StreamableFile }) | ||
async getAttachmentFile( | ||
@Param('productId') productId: string, | ||
@Param('attachmentId') attachmentId: string | ||
): Promise<StreamableFile> { | ||
await canReadAttachmentOrFail(this.request.user.userId, productId, attachmentId) | ||
const attachment = await this.service.getAttachment(productId, attachmentId) | ||
return new StreamableFile(await this.service.getAttachmentFile(productId, attachmentId), { | ||
type: attachment.type | ||
}) | ||
} | ||
|
||
@Put(':attachmentId') | ||
@UseInterceptors(FileInterceptor('file')) | ||
@ApiParam({ name: 'productId', type: 'string', required: true }) | ||
@ApiParam({ name: 'attachmentId', type: 'string', required: true }) | ||
@ApiConsumes('multipart/form-data') | ||
@ApiBody({ | ||
schema: { | ||
type: 'object', | ||
properties: { | ||
data: { $ref: getSchemaPath(AttachmentAddData) }, | ||
file: { type: 'string', format: 'binary' } | ||
}, | ||
required: ['data', 'file'] | ||
} | ||
}) | ||
@ApiResponse({ type: Attachment }) | ||
async updateAttachment( | ||
@Param('productId') productId: string, | ||
@Param('attachmentId') attachmentId: string, | ||
@Body('data') data: string, | ||
@UploadedFile() file: Express.Multer.File | ||
): Promise<Attachment> { | ||
await canUpdateAttachmentOrFail(this.request.user.userId, productId, attachmentId) | ||
return this.service.updateAttachment(productId, attachmentId, JSON.parse(data), file) | ||
} | ||
|
||
@Delete(':attachmentId') | ||
@ApiParam({ name: 'productId', type: 'string', required: true }) | ||
@ApiParam({ name: 'attachmentId', type: 'string', required: true }) | ||
@ApiResponse({ type: Attachment }) | ||
async deleteAttachment( | ||
@Param('productId') productId: string, | ||
@Param('attachmentId') attachmentId: string | ||
): Promise<Attachment> { | ||
await canDeleteAttachmentOrFail(this.request.user.userId, productId, attachmentId) | ||
return this.service.deleteAttachment(productId, attachmentId) | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/backend/scripts/src/modules/rest/attachments/attachment.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Module } from '@nestjs/common' | ||
|
||
import { AttachmentController } from './attachment.controller' | ||
import { AttachmentService } from './attachment.service' | ||
|
||
@Module({ | ||
controllers: [AttachmentController], | ||
providers: [AttachmentService] | ||
}) | ||
export class AttachmentModule {} |
96 changes: 96 additions & 0 deletions
96
packages/backend/scripts/src/modules/rest/attachments/attachment.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { ReadStream, createReadStream, existsSync, mkdirSync, writeFileSync } from 'fs' | ||
|
||
import { Inject } from '@nestjs/common' | ||
import { REQUEST } from '@nestjs/core' | ||
|
||
import shortid from 'shortid' | ||
import { IsNull } from 'typeorm' | ||
|
||
import { Attachment, AttachmentAddData, AttachmentREST, AttachmentUpdateData } from 'productboard-common' | ||
import { Database, convertAttachment } from 'productboard-database' | ||
|
||
import { emitProductMessage } from '../../../functions/emit' | ||
import { AuthorizedRequest } from '../../../request' | ||
|
||
export class AttachmentService implements AttachmentREST<AttachmentAddData, AttachmentUpdateData, Express.Multer.File> { | ||
|
||
constructor( | ||
@Inject(REQUEST) | ||
private readonly request: AuthorizedRequest | ||
) { | ||
if (!existsSync('./uploads')) { | ||
mkdirSync('./uploads') | ||
} | ||
} | ||
|
||
async findAttachments(productId: string): Promise<Attachment[]> { | ||
const where = { productId, deleted: IsNull() } | ||
const result: Attachment[] = [] | ||
for (const attachment of await Database.get().attachmentRepository.findBy(where)) | ||
result.push(convertAttachment(attachment)) | ||
return result | ||
} | ||
|
||
async addAttachment(productId: string, data: AttachmentAddData, file: Express.Multer.File): Promise<Attachment> { | ||
const attachmentId = shortid() | ||
// Save file | ||
writeFileSync(`./uploads/${attachmentId}`, file.buffer) | ||
// Add attachment | ||
const created = Date.now() | ||
const updated = created | ||
const userId = this.request.user.userId | ||
const attachment = await Database.get().attachmentRepository.save({ productId, attachmentId, created, updated, userId, ...data }) | ||
// Update product | ||
const product = await Database.get().productRepository.findOneBy({ productId }) | ||
product.updated = attachment.updated | ||
await Database.get().productRepository.save(product) | ||
// Emit changes | ||
emitProductMessage(productId, { type: 'patch', products: [product], attachments: [attachment] }) | ||
// Return attachment | ||
return convertAttachment(attachment) | ||
} | ||
|
||
async getAttachment(productId: string, attachmentId: string): Promise<Attachment> { | ||
const attachment = await Database.get().attachmentRepository.findOneByOrFail({ productId, attachmentId }) | ||
return convertAttachment(attachment) | ||
} | ||
|
||
async getAttachmentFile(_productId: string, attachmentId: string): Promise<ReadStream> { | ||
return createReadStream(`./uploads/${attachmentId}`) | ||
} | ||
|
||
async updateAttachment(productId: string, attachmentId: string, data: AttachmentUpdateData, file?: Express.Multer.File): Promise<Attachment> { | ||
// Save file | ||
writeFileSync(`./uploads/${attachmentId}`, file.buffer) | ||
// Update attachment | ||
const attachment = await Database.get().attachmentRepository.findOneByOrFail({ productId, attachmentId }) | ||
attachment.updated = Date.now() | ||
attachment.type = data.type | ||
await Database.get().attachmentRepository.save(attachment) | ||
// Update product | ||
const product = await Database.get().productRepository.findOneBy({ productId }) | ||
product.updated = attachment.updated | ||
await Database.get().productRepository.save(product) | ||
// Emit changes | ||
emitProductMessage(productId, { type: 'patch', products: [product], attachments: [attachment] }) | ||
// Return attachment | ||
return convertAttachment(attachment) | ||
} | ||
|
||
async deleteAttachment(productId: string, attachmentId: string): Promise<Attachment> { | ||
// Delete attachment | ||
const attachment = await Database.get().attachmentRepository.findOneByOrFail({ productId, attachmentId }) | ||
attachment.deleted = Date.now() | ||
attachment.updated = attachment.deleted | ||
await Database.get().attachmentRepository.save(attachment) | ||
// Update product | ||
const product = await Database.get().productRepository.findOneBy({ productId }) | ||
product.updated = attachment.updated | ||
await Database.get().productRepository.save(product) | ||
// Emit changes | ||
emitProductMessage(productId, { type: 'patch', products: [product], attachments: [attachment] }) | ||
// Return attachment | ||
return convertAttachment(attachment) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters