Skip to content

Commit

Permalink
[Feat] Saving recordings
Browse files Browse the repository at this point in the history
Added an endpoint for saving recordings in S3.
Referencing #20.
  • Loading branch information
angel-penchev committed Nov 8, 2021
1 parent 8015b46 commit 8209081
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
36 changes: 35 additions & 1 deletion server/src/recordings/controllers/recordings.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import { Controller, Get, Param, Req, UseGuards } from '@nestjs/common';
import {
BadRequestException,
Body,
Controller,
Get,
HttpCode,
HttpStatus,
Param,
Put,
Req,
UseGuards,
} from '@nestjs/common';
import {
ApiAcceptedResponse,
ApiCookieAuth,
ApiForbiddenResponse,
ApiOkResponse,
} from '@nestjs/swagger';
import { Request } from 'express';
import { AuthSessionGuard } from 'src/auth/guards/auth.session.guard';
import RecordingsError from '../errors/recordings.error';
import { RecordingsErrorCode } from '../errors/recordings.error.code';
import { RecordingsService } from '../services/recordings.service';

@Controller('/recordings')
Expand All @@ -27,4 +41,24 @@ export class RecordingsController {
? await this.recordingsService.listRecordings(req.user['username'])
: await this.recordingsService.getRecording(req.user['username'], path);
}

@Put(':path(*)')
@HttpCode(HttpStatus.ACCEPTED)
@UseGuards(AuthSessionGuard)
@ApiCookieAuth()
@ApiAcceptedResponse({ description: 'Recording stored.' })
@ApiForbiddenResponse({ description: 'No user logon.' })
async putObject(@Param('path') path: string, @Body() body: any[]) {
try {
await this.recordingsService.saveRecording(path, body);
return 'Accepted';
} catch (ex) {
if (
ex instanceof RecordingsError &&
ex.message === RecordingsErrorCode.INVALID_FILE_PATH
) {
throw new BadRequestException(ex.message);
}
}
}
}
14 changes: 14 additions & 0 deletions server/src/recordings/services/recordings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,18 @@ export class RecordingsService {

return JSON.parse(queryResponse.Body.toString());
}

async saveRecording(path: string, body: any[]) {
if (path.endsWith('/')) {
throw new RecordingsError(RecordingsErrorCode.INVALID_FILE_PATH);
}

await this.s3
.upload({
Bucket: configObject.aws.codeSnippetsS3Bucket,
Key: path,
Body: body,
})
.promise();
}
}

0 comments on commit 8209081

Please sign in to comment.