mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-25 11:16:31 -05:00
PrivateAPI: Add media controller
Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
parent
25e310b7eb
commit
1835cb7499
3 changed files with 158 additions and 1 deletions
86
src/api/private/media/media.controller.spec.ts
Normal file
86
src/api/private/media/media.controller.spec.ts
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
|
import { MediaController } from './media.controller';
|
||||||
|
import { LoggerModule } from '../../../logger/logger.module';
|
||||||
|
import { ConfigModule } from '@nestjs/config';
|
||||||
|
import mediaConfigMock from '../../../config/mock/media.config.mock';
|
||||||
|
import appConfigMock from '../../../config/mock/app.config.mock';
|
||||||
|
import authConfigMock from '../../../config/mock/auth.config.mock';
|
||||||
|
import customizationConfigMock from '../../../config/mock/customization.config.mock';
|
||||||
|
import externalConfigMock from '../../../config/mock/external-services.config.mock';
|
||||||
|
import { MediaModule } from '../../../media/media.module';
|
||||||
|
import { NotesModule } from '../../../notes/notes.module';
|
||||||
|
import { getRepositoryToken } from '@nestjs/typeorm';
|
||||||
|
import { AuthorColor } from '../../../notes/author-color.entity';
|
||||||
|
import { Authorship } from '../../../revisions/authorship.entity';
|
||||||
|
import { AuthToken } from '../../../auth/auth-token.entity';
|
||||||
|
import { Identity } from '../../../users/identity.entity';
|
||||||
|
import { MediaUpload } from '../../../media/media-upload.entity';
|
||||||
|
import { Note } from '../../../notes/note.entity';
|
||||||
|
import { Revision } from '../../../revisions/revision.entity';
|
||||||
|
import { User } from '../../../users/user.entity';
|
||||||
|
import { Tag } from '../../../notes/tag.entity';
|
||||||
|
import { NoteGroupPermission } from '../../../permissions/note-group-permission.entity';
|
||||||
|
import { NoteUserPermission } from '../../../permissions/note-user-permission.entity';
|
||||||
|
import { Group } from '../../../groups/group.entity';
|
||||||
|
|
||||||
|
describe('MediaController', () => {
|
||||||
|
let controller: MediaController;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
|
imports: [
|
||||||
|
MediaModule,
|
||||||
|
NotesModule,
|
||||||
|
LoggerModule,
|
||||||
|
ConfigModule.forRoot({
|
||||||
|
isGlobal: true,
|
||||||
|
load: [
|
||||||
|
appConfigMock,
|
||||||
|
mediaConfigMock,
|
||||||
|
authConfigMock,
|
||||||
|
customizationConfigMock,
|
||||||
|
externalConfigMock,
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
controllers: [MediaController],
|
||||||
|
})
|
||||||
|
.overrideProvider(getRepositoryToken(AuthorColor))
|
||||||
|
.useValue({})
|
||||||
|
.overrideProvider(getRepositoryToken(Authorship))
|
||||||
|
.useValue({})
|
||||||
|
.overrideProvider(getRepositoryToken(AuthToken))
|
||||||
|
.useValue({})
|
||||||
|
.overrideProvider(getRepositoryToken(Identity))
|
||||||
|
.useValue({})
|
||||||
|
.overrideProvider(getRepositoryToken(MediaUpload))
|
||||||
|
.useValue({})
|
||||||
|
.overrideProvider(getRepositoryToken(Note))
|
||||||
|
.useValue({})
|
||||||
|
.overrideProvider(getRepositoryToken(Revision))
|
||||||
|
.useValue({})
|
||||||
|
.overrideProvider(getRepositoryToken(User))
|
||||||
|
.useValue({})
|
||||||
|
.overrideProvider(getRepositoryToken(Tag))
|
||||||
|
.useValue({})
|
||||||
|
.overrideProvider(getRepositoryToken(NoteGroupPermission))
|
||||||
|
.useValue({})
|
||||||
|
.overrideProvider(getRepositoryToken(NoteUserPermission))
|
||||||
|
.useValue({})
|
||||||
|
.overrideProvider(getRepositoryToken(Group))
|
||||||
|
.useValue({})
|
||||||
|
.compile();
|
||||||
|
|
||||||
|
controller = module.get<MediaController>(MediaController);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be defined', () => {
|
||||||
|
expect(controller).toBeDefined();
|
||||||
|
});
|
||||||
|
});
|
69
src/api/private/media/media.controller.ts
Normal file
69
src/api/private/media/media.controller.ts
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
*/
|
||||||
|
|
||||||
|
import {
|
||||||
|
BadRequestException,
|
||||||
|
Controller,
|
||||||
|
Headers,
|
||||||
|
HttpCode,
|
||||||
|
InternalServerErrorException,
|
||||||
|
Post,
|
||||||
|
UploadedFile,
|
||||||
|
UseInterceptors,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
import { ConsoleLoggerService } from '../../../logger/console-logger.service';
|
||||||
|
import { MediaService } from '../../../media/media.service';
|
||||||
|
import { MulterFile } from '../../../media/multer-file.interface';
|
||||||
|
import { MediaUploadUrlDto } from '../../../media/media-upload-url.dto';
|
||||||
|
import {
|
||||||
|
ClientError,
|
||||||
|
MediaBackendError,
|
||||||
|
NotInDBError,
|
||||||
|
} from '../../../errors/errors';
|
||||||
|
import { FileInterceptor } from '@nestjs/platform-express';
|
||||||
|
|
||||||
|
@Controller('media')
|
||||||
|
export class MediaController {
|
||||||
|
constructor(
|
||||||
|
private readonly logger: ConsoleLoggerService,
|
||||||
|
private mediaService: MediaService,
|
||||||
|
) {
|
||||||
|
this.logger.setContext(MediaController.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post()
|
||||||
|
@UseInterceptors(FileInterceptor('file'))
|
||||||
|
@HttpCode(201)
|
||||||
|
async uploadMedia(
|
||||||
|
@UploadedFile() file: MulterFile,
|
||||||
|
@Headers('HedgeDoc-Note') noteId: string,
|
||||||
|
): Promise<MediaUploadUrlDto> {
|
||||||
|
// ToDo: Get real userName
|
||||||
|
const username = 'hardcoded';
|
||||||
|
this.logger.debug(
|
||||||
|
`Recieved filename '${file.originalname}' for note '${noteId}' from user '${username}'`,
|
||||||
|
'uploadImage',
|
||||||
|
);
|
||||||
|
try {
|
||||||
|
const url = await this.mediaService.saveFile(
|
||||||
|
file.buffer,
|
||||||
|
username,
|
||||||
|
noteId,
|
||||||
|
);
|
||||||
|
return this.mediaService.toMediaUploadUrlDto(url);
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof ClientError || e instanceof NotInDBError) {
|
||||||
|
throw new BadRequestException(e.message);
|
||||||
|
}
|
||||||
|
if (e instanceof MediaBackendError) {
|
||||||
|
throw new InternalServerErrorException(
|
||||||
|
'There was an error in the media backend',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -14,9 +14,10 @@ import { FrontendConfigModule } from '../../frontend-config/frontend-config.modu
|
||||||
import { HistoryController } from './me/history/history.controller';
|
import { HistoryController } from './me/history/history.controller';
|
||||||
import { HistoryModule } from '../../history/history.module';
|
import { HistoryModule } from '../../history/history.module';
|
||||||
import { NotesModule } from '../../notes/notes.module';
|
import { NotesModule } from '../../notes/notes.module';
|
||||||
|
import { MediaModule } from '../../media/media.module';
|
||||||
|
import { MediaController } from './media/media.controller';
|
||||||
import { NotesController } from './notes/notes.controller';
|
import { NotesController } from './notes/notes.controller';
|
||||||
import { PermissionsModule } from '../../permissions/permissions.module';
|
import { PermissionsModule } from '../../permissions/permissions.module';
|
||||||
import { MediaModule } from '../../media/media.module';
|
|
||||||
import { RevisionsModule } from '../../revisions/revisions.module';
|
import { RevisionsModule } from '../../revisions/revisions.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
|
@ -34,6 +35,7 @@ import { RevisionsModule } from '../../revisions/revisions.module';
|
||||||
controllers: [
|
controllers: [
|
||||||
TokensController,
|
TokensController,
|
||||||
ConfigController,
|
ConfigController,
|
||||||
|
MediaController,
|
||||||
HistoryController,
|
HistoryController,
|
||||||
NotesController,
|
NotesController,
|
||||||
],
|
],
|
||||||
|
|
Loading…
Reference in a new issue