diff --git a/config.ts b/config.ts index 80b35b1..f349d03 100644 --- a/config.ts +++ b/config.ts @@ -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/" } diff --git a/libs/api-implementation/samples/src/lib/api-implementation-samples.module.ts b/libs/api-implementation/samples/src/lib/api-implementation-samples.module.ts index 41b191b..6d1cc39 100644 --- a/libs/api-implementation/samples/src/lib/api-implementation-samples.module.ts +++ b/libs/api-implementation/samples/src/lib/api-implementation-samples.module.ts @@ -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 {} diff --git a/libs/api-implementation/samples/src/lib/samples.controller.ts b/libs/api-implementation/samples/src/lib/samples.controller.ts index f9639ba..8f9c207 100644 --- a/libs/api-implementation/samples/src/lib/samples.controller.ts +++ b/libs/api-implementation/samples/src/lib/samples.controller.ts @@ -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 } } diff --git a/libs/api-implementation/samples/src/lib/services/samples.service.ts b/libs/api-implementation/samples/src/lib/services/samples.service.ts new file mode 100644 index 0000000..1007e69 --- /dev/null +++ b/libs/api-implementation/samples/src/lib/services/samples.service.ts @@ -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; + } + } +} diff --git a/libs/frontend/samples/src/lib/components/dialog/dialog.component.spec.ts b/libs/frontend/samples/src/lib/components/dialog/dialog.component.spec.ts deleted file mode 100644 index a6bce8d..0000000 --- a/libs/frontend/samples/src/lib/components/dialog/dialog.component.spec.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { async, ComponentFixture, TestBed } from '@angular/core/testing'; - -import { DialogComponent } from './dialog.component'; - -describe('DialogComponent', () => { - let component: DialogComponent; - let fixture: ComponentFixture; - - beforeEach(async(() => { - TestBed.configureTestingModule({ - declarations: [ DialogComponent ] - }) - .compileComponents(); - })); - - beforeEach(() => { - fixture = TestBed.createComponent(DialogComponent); - component = fixture.componentInstance; - fixture.detectChanges(); - }); - - it('should create', () => { - expect(component).toBeTruthy(); - }); -}); diff --git a/libs/frontend/samples/src/lib/containers/upload/upload.component.spec.ts b/libs/frontend/samples/src/lib/containers/upload/upload.component.spec.ts deleted file mode 100644 index 291982e..0000000 --- a/libs/frontend/samples/src/lib/containers/upload/upload.component.spec.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { async, ComponentFixture, TestBed } from '@angular/core/testing'; - -import { UploadComponent } from './upload.component'; - -describe('UploadComponent', () => { - let component: UploadComponent; - let fixture: ComponentFixture; - - beforeEach(async(() => { - TestBed.configureTestingModule({ - declarations: [ UploadComponent ] - }) - .compileComponents(); - })); - - beforeEach(() => { - fixture = TestBed.createComponent(UploadComponent); - component = fixture.componentInstance; - fixture.detectChanges(); - }); - - it('should create', () => { - expect(component).toBeTruthy(); - }); -}); diff --git a/libs/frontend/samples/src/lib/containers/upload/upload.component.ts b/libs/frontend/samples/src/lib/containers/upload/upload.component.ts index a4f19bf..f181e14 100644 --- a/libs/frontend/samples/src/lib/containers/upload/upload.component.ts +++ b/libs/frontend/samples/src/lib/containers/upload/upload.component.ts @@ -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%', }); diff --git a/libs/frontend/samples/src/lib/frontend-samples.module.spec.ts b/libs/frontend/samples/src/lib/frontend-samples.module.spec.ts deleted file mode 100644 index 405b473..0000000 --- a/libs/frontend/samples/src/lib/frontend-samples.module.spec.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { async, TestBed } from '@angular/core/testing'; -import { FrontendSamplesModule } from './frontend-samples.module'; - -describe('FrontendSamplesModule', () => { - beforeEach(async(() => { - TestBed.configureTestingModule({ - imports: [FrontendSamplesModule], - }).compileComponents(); - })); - - it('should create', () => { - expect(FrontendSamplesModule).toBeDefined(); - }); -}); diff --git a/libs/frontend/samples/src/lib/services/upload.service.ts b/libs/frontend/samples/src/lib/services/upload.service.ts index bb32fb3..3f9ebdd 100644 --- a/libs/frontend/samples/src/lib/services/upload.service.ts +++ b/libs/frontend/samples/src/lib/services/upload.service.ts @@ -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) {} @@ -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(); // 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