-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- re-structure admin and user controller - stream / view / download file for user and admin - fix some authorization in admin endpoint
- Loading branch information
Showing
14 changed files
with
227 additions
and
35 deletions.
There are no files selected for viewing
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,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { FileController } from './file.controller'; | ||
|
||
describe('FileController', () => { | ||
let controller: FileController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [FileController], | ||
}).compile(); | ||
|
||
controller = module.get<FileController>(FileController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
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
2 changes: 1 addition & 1 deletion
2
src/file/controller/file.controller.spec.ts → ...e/controller/user/file.controller.spec.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
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,79 @@ | ||
import { | ||
Controller, | ||
Get, | ||
Post, | ||
Param, | ||
Delete, | ||
UseInterceptors, | ||
UploadedFiles, | ||
UseGuards, | ||
Request, | ||
Query, | ||
Res, | ||
StreamableFile, | ||
} from '@nestjs/common'; | ||
import { FilesInterceptor } from '@nestjs/platform-express'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { PageOptionsDto } from 'src/common/dto/page-options.dto'; | ||
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'; | ||
import { ApiPaginatedResponse } from 'src/common/decorator/api-paginated-response.decoratos'; | ||
import { FileService } from 'src/file/service/file.service'; | ||
import { FileResponseDto } from 'src/file/dto/file-response.dto'; | ||
import { FileMineResponseDto } from 'src/file/dto/file-mine-response.dto'; | ||
import { ApiSuccessResponse } from 'src/common/decorator/api-success-response'; | ||
import { Stream } from 'stream'; | ||
import { Response } from 'express'; | ||
|
||
@UseGuards(AuthGuard('jwt')) | ||
@ApiTags('File') | ||
@ApiBearerAuth('Bearer') | ||
@Controller('file') | ||
export class FileController { | ||
constructor(private readonly fileService: FileService) {} | ||
|
||
@ApiSuccessResponse(FileResponseDto, 'Successfully upload file') | ||
@Post() | ||
@UseInterceptors(FilesInterceptor('files')) | ||
async upload( | ||
@UploadedFiles() files: Array<Express.Multer.File>, | ||
@Request() request, | ||
) { | ||
return await this.fileService.upload(files, request.user.id); | ||
} | ||
|
||
@ApiPaginatedResponse(FileMineResponseDto) | ||
@Get() | ||
mine(@Query() pageOptionsDto: PageOptionsDto, @Request() request) { | ||
return this.fileService.findAll(pageOptionsDto, request.user.id); | ||
} | ||
|
||
@ApiSuccessResponse(FileMineResponseDto, 'Successfully received file detail') | ||
@Get(':id') | ||
mineDetail(@Param('id') id: string, @Request() request) { | ||
return this.fileService.mineDetail(+id, request.user.id); | ||
} | ||
|
||
@Delete(':id') | ||
mineRemove(@Param('id') id: string, @Request() request) { | ||
return this.fileService.mineRemove(+id, request.user.id); | ||
} | ||
|
||
@Get('stream/:internalCid') | ||
async stream( | ||
@Param('internalCid') internalCid: string, | ||
@Res({ passthrough: true }) res: Response, | ||
@Request() request, | ||
): Promise<StreamableFile> { | ||
const fileMetaData = await this.fileService.mineStream( | ||
internalCid, | ||
request.user.id, | ||
); | ||
const file = Stream.Readable.from(fileMetaData.buffer); | ||
|
||
res.set({ | ||
'Content-Type': fileMetaData.mimeType, | ||
}); | ||
|
||
return new StreamableFile(file); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { FileMineResponseDto } from './file-mine-response.dto'; | ||
import { FileModel } from '../model/files.model'; | ||
|
||
export class FileViewResponseDto extends FileMineResponseDto { | ||
@ApiProperty() | ||
buffer: Buffer; | ||
|
||
constructor(file: FileModel, buffer: Buffer) { | ||
super(file); | ||
this.buffer = buffer; | ||
} | ||
} |
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
File renamed without changes.
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
2 changes: 1 addition & 1 deletion
2
src/user/controller/users.controller.spec.ts → ...controller/admin/users.controller.spec.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
Oops, something went wrong.