Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@Get()
async findAll(): Promise {
return this.profileService.findAll();
}
@ApiOperation({ summary: 'Find Profile by id. Admins only' })
@ApiTags('Admin')
@Roles(RolesEnum.ADMIN)
@Get(':id')
async findById(@Param('id', new ParseUUIDPipe({ version: '4' })) id: string): Promise {
console.log('in findById', id);
return this.profileService.findOne(id);
}
@ApiOperation({ summary: 'Create new Profile.' })
@ApiConsumes('multipart/form-data')
// TODO @ApiImplicitFile({ name: 'file', required: true, description: 'Profile Picture' })
// @ApiBody({
// description: 'Profile Picture',
// type: CreateProfileDto,
// })
@UseInterceptors(
FileInterceptor('file', {
fileFilter: (req, file, cb) => {
if (ALLOWED_MIME_TYPES.indexOf(file.mimetype) === -1) {
return cb(
new BadRequestException(
`Error! Incorrect mimetype '${file.mimetype}'. Correct: ${ALLOWED_MIME_TYPES.join(', ')}`,
),
false,
);
}
},
limits: {
fileSize: 1024000,
},
}),
)
@Post()
async create(@Body() entity: CreateProfileDto, @UploadedFile() file, @CurrentUser() user: User): Promise {
if (user.profileId) {
throw new BadRequestException('user already has profile. if you want to change, use UPDATE');
}
return this.profileService.create(entity, file, user);
}
@ApiOperation({ summary: 'Update an existing record' })
@ApiConsumes('multipart/form-data')
// TODO @ApiImplicitFile({ name: 'file', required: false, description: 'Profile Picture' })
@UseInterceptors(
FileInterceptor('file', {
fileFilter: (req, file, cb) => {
if (ALLOWED_MIME_TYPES.indexOf(file.mimetype) === -1) {
return cb(
new BadRequestException(
`Error! Incorrect mimetype '${file.mimetype}'. Correct: ${ALLOWED_MIME_TYPES.join(', ')}`,
),
false,
);
}
cb(null, true);
},
limits: {
fileSize: 1024000,
@Req() req: AnyAuthRequest,
): Promise {
const { identifier } = req;
logger.log(`createChunksUploadTask: ${r(query)}`);
return UploaderHelper.createChunksUploadTask({ ...query, identifier });
}
/**
* 流式上传 chunk
* @param filename
* @param chunk
* @param req
*/
@ApiBearerAuth()
@ApiOperation({ summary: 'Stream upload chunked file' })
@ApiConsumes('multipart/form-data')
@ApiQuery({
name: 'chunk',
required: false,
description: 'chunked upload file index',
})
@UseGuards(AnyAuthGuard, OperationTokenGuard)
@Post('chunks-stream')
async streamChunkedUploader(
@Query('filename') filename: string,
@Query('chunk') chunk: number,
@Req() req: AnyAuthRequest & OperationTokenRequest,
): Promise {
assert(!isBlank(filename), 'filename needed');
assert(!isBlank(chunk), 'chunk needed');
// save uploaded file to temp dir
@Req() req: AnyAuthRequest & OperationTokenRequest,
): Promise {
return this.uploaderService.mergeChunks(req.token, filename);
}
/**
* 直接上传文件
* @param bucket
* @param prefix
* @param local 在某些可能需要同一个 server 执行任务时可能需要
* @param req
* @param files
*/
@ApiBearerAuth()
@ApiOperation({ summary: 'Upload files' })
@ApiConsumes('multipart/form-data')
@ApiQuery({
name: 'local',
enum: ['1'],
required: false,
description: 'force use local storage',
})
@UseGuards(AnyAuthGuard)
@Post()
@UseInterceptors(
// new FastifyFileInterceptor('files'),
FilesInterceptor('files', configLoader.loadNumericConfig(ConfigKeys.UPLOADER_MAX_COUNT, 3), fileInterceptorOptions),
)
async uploader(
@Query('bucket') bucket = '',
@Query('prefix') prefix = '',
@Query('local') local: string, // 是否使用本地存储
import {JwtDto} from '../auth/dto/jwt.dto';
import {MediaUploadDto} from './dto/media-upload.dto';
@ApiUseTags('media')
@ApiBearerAuth()
@UseGuards(AuthGuard('jwt'))
@Controller('media')
export class MediaController {
private logger = new AppLogger(MediaController.name);
constructor(@Inject(AWS_CON_TOKEN) private readonly awsConnection) {}
@Post('upload')
@UseInterceptors(FileInterceptor('media'))
@ApiConsumes('multipart/form-data')
@ApiImplicitFile({ name: 'media', required: true, description: 'Any media file' })
@ApiResponse({ status: 200, description: 'OK', type: MediaUploadDto })
public uploadFile(@UploadedFile() file) {
return file;
}
@MessagePattern({ cmd: MEDIA_CMD_DELETE })
public async onMediaDelete(homeMedia: HomeMediaEntity): Promise {
const key = new URL(homeMedia.url).pathname.substring(1);
this.logger.debug(`[onMediaDelete] Going to remove key ${key} from bucket ${config.aws.s3.bucket_name}`);
const s3 = new S3();
s3.deleteObject({
Bucket: config.aws.s3.bucket_name,
Key: key
}).promise().then(() => {
this.logger.debug(`[onMediaDelete] item with key: ${key} removed from bucket`);
});
const { token } = req;
return this.uploaderService.uploadChunks(token, filename, tempFile, chunk);
}
/**
* 基于 operation-token 上传 chunk
* @param filename
* @param chunk
* @param req
* @param file
*/
@ApiBearerAuth()
@ApiOperation({ summary: 'Chunked upload file' })
@ApiConsumes('multipart/form-data')
@ApiQuery({
name: 'chunk',
required: false,
description: 'chunked upload file index',
})
@UseGuards(AnyAuthGuard, OperationTokenGuard)
@Post('chunks')
@UseInterceptors(FileInterceptor('file', fileInterceptorOptions))
async chunkedUploader(
@Query('filename') filename: string,
@Query('chunk') chunk: number,
@Req() req: AnyAuthRequest & OperationTokenRequest,
@UploadedFile() file,
): Promise {
assert(!isBlank(filename), 'filename needed');
assert(!isBlank(chunk), 'chunk needed');
import { Api } from '../../common/aspects/decorator';
import { resolve } from 'path';
import { UploadActionType, StorageType } from '../../common/aspects/enum';
import { ImportService } from '../../common/services/import.service';
import { Logger } from '../../common/lib/logger';
import { Qiniu } from '../../common/lib/qiniu';
@Api('storage')
@ApiUseTags('storage')
@ApiBearerAuth()
@UseGuards(AuthGuard())
export class StorageController {
constructor(private readonly importService: ImportService) { }
@Post()
@ApiConsumes('multipart/form-data')
@ApiImplicitFile({ name: 'file', required: true })
async upload(@Req() req, @Res() res) {
const files = req.raw.files;
if (Object.keys(files).length == 0) {
throw new BadRequestException('没有上传任何文件');
}
const file = files.file;
const action = req.raw.body.action || UploadActionType.UPLOAD;
switch (action) {
case UploadActionType.IMPORT:
const target = req.raw.body.target;
if (!target) throw new BadRequestException('参数 target 缺失');