Skip to content
This repository has been archived by the owner on Jun 16, 2021. It is now read-only.

Commit

Permalink
feat(api): write the file to the bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
zelzhan committed Aug 14, 2020
1 parent cdca6a0 commit cf2e3d1
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 86 deletions.
3 changes: 2 additions & 1 deletion config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export enum AppConfig {
GITHUB_API_LINK = "https://api.github.com/",
CCEXTRACTOR_REPO_NAME = "ccextractor",
CCEXTRACTOR_REPO_OWNER = "zelzhan",
MONGO_CONNECTION_STRING = "mongodb://mongo:27017/test"
MONGO_CONNECTION_STRING = "mongodb://mongo:27017/test",
PATH_TO_MOUNTED_BUCKET = "/path/to/bucket/"
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Module } from '@nestjs/common';

import { SamplesController } from './samples.controller'
import { SamplesService } from './services/samples.service';

@Module({
controllers: [SamplesController],
providers: [],
providers: [SamplesService],
exports: [],
})
export class ApiImplementationSamplesModule {}
17 changes: 8 additions & 9 deletions libs/api-implementation/samples/src/lib/samples.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@ import {
Post,
UseInterceptors,
UploadedFile,
Get,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { SamplesService } from './services/samples.service';

@Controller('samples')
export class SamplesController {
constructor() { }

@Get()
log() {
return "OK"
}
constructor(private service: SamplesService) {}

@Post('upload')
@UseInterceptors(FileInterceptor('file'))
uploadFile(@UploadedFile() file) {
console.log(file);
return 'OK';
try {
this.service.writeToBucket(file);
} catch (error) {
return 'Error while writing the file to the bucket'
}
return 200
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Injectable } from '@nestjs/common';
import { writeFileSync } from 'fs';
import { AppConfig } from '../../../../../../config';
@Injectable()
export class SamplesService {
async writeToBucket(file) {
try {
writeFileSync(
AppConfig.PATH_TO_MOUNTED_BUCKET + file.originalname,
file.buffer
);
} catch (error) {
console.error(`Error occured while writing the file to the bucket`);
console.debug(error.stack);
throw error;
}
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class UploadComponent {
constructor(public dialog: MatDialog, public uploadService: UploadService) {}

public openUploadDialog() {
let dialogRef = this.dialog.open(DialogComponent, {
this.dialog.open(DialogComponent, {
width: '50%',
height: '50%',
});
Expand Down
14 changes: 0 additions & 14 deletions libs/frontend/samples/src/lib/frontend-samples.module.spec.ts

This file was deleted.

10 changes: 0 additions & 10 deletions libs/frontend/samples/src/lib/services/upload.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import {
import { Subject } from 'rxjs';
import { Observable } from 'rxjs';

// const url = 'http://localhost:8000/upload';

@Injectable({providedIn: 'root',})
export class UploadService {
constructor(private http: HttpClient) {}
Expand All @@ -23,18 +21,10 @@ export class UploadService {
const formData: FormData = new FormData();
formData.append('file', file, file.name);

// create a http-post request and pass the form
// tell it to report the upload progress
// const req = new HttpRequest('POST', url, formData, {
// reportProgress: true,
// });

// create a new progress-subject for every file
const progress = new Subject<number>();

// send the http-request and subscribe for progress-updates


this.http.post('api/samples/upload', formData, { reportProgress: true }).subscribe((event : any) => {
if (event.type === HttpEventType.UploadProgress) {
// calculate the progress percentage
Expand Down

0 comments on commit cf2e3d1

Please sign in to comment.