refactor(media): add media redirection endpoint

Previous versions of HedgeDoc suffered from the problem
that changing the media backend required manipulation of
the media links in all created notes. We discussed in
#3704 that it's favourable to have an endpoint that
redirects to the image's original URL. When changing the
media backend, the link stays the same but just the
redirect changes.

Signed-off-by: Erik Michelson <github@erik.michelson.eu>
This commit is contained in:
Erik Michelson 2024-03-24 01:02:26 +01:00 committed by Philip Molares
parent 1f19a6fac4
commit 8693edbf6a
16 changed files with 104 additions and 87 deletions

View file

@ -7,14 +7,17 @@ import {
BadRequestException,
Controller,
Delete,
Get,
Param,
Post,
Res,
UploadedFile,
UseGuards,
UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { ApiBody, ApiConsumes, ApiHeader, ApiTags } from '@nestjs/swagger';
import { Response } from 'express';
import { PermissionError } from '../../../errors/errors';
import { SessionGuard } from '../../../identity/session.guard';
@ -101,6 +104,17 @@ export class MediaController {
return await this.mediaService.toMediaUploadDto(upload);
}
@Get(':filename')
@OpenApi(404, 500)
async getMedia(
@Param('filename') filename: string,
@Res() response: Response,
): Promise<void> {
const mediaUpload = await this.mediaService.findUploadByFilename(filename);
const targetUrl = mediaUpload.fileUrl;
response.redirect(targetUrl);
}
@Delete(':filename')
@OpenApi(204, 403, 404, 500)
async deleteMedia(

View file

@ -7,8 +7,10 @@ import {
BadRequestException,
Controller,
Delete,
Get,
Param,
Post,
Res,
UploadedFile,
UseGuards,
UseInterceptors,
@ -21,6 +23,7 @@ import {
ApiSecurity,
ApiTags,
} from '@nestjs/swagger';
import { Response } from 'express';
import { TokenAuthGuard } from '../../../auth/token.strategy';
import { PermissionError } from '../../../errors/errors';
@ -101,6 +104,17 @@ export class MediaController {
return await this.mediaService.toMediaUploadDto(upload);
}
@Get(':filename')
@OpenApi(404, 500)
async getMedia(
@Param('filename') filename: string,
@Res() response: Response,
): Promise<void> {
const mediaUpload = await this.mediaService.findUploadByFilename(filename);
const targetUrl = mediaUpload.fileUrl;
response.redirect(targetUrl);
}
@Delete(':filename')
@OpenApi(204, 403, 404, 500)
async deleteMedia(

View file

@ -1,15 +0,0 @@
/*
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { ApiProperty } from '@nestjs/swagger';
import { IsString } from 'class-validator';
import { BaseDto } from '../utils/base.dto.';
export class MediaUploadUrlDto extends BaseDto {
@IsString()
@ApiProperty()
link: string;
}

View file

@ -12,12 +12,12 @@ import { Username } from '../utils/username';
export class MediaUploadDto extends BaseDto {
/**
* The link to the media file.
* @example "https://example.com/uploads/testfile123.jpg"
* The id of the media file.
* @example "testfile123.jpg"
*/
@IsString()
@ApiProperty()
url: string;
id: string;
/**
* The publicId of the note to which the uploaded file is linked to.

View file

@ -232,7 +232,7 @@ export class MediaService {
async toMediaUploadDto(mediaUpload: MediaUpload): Promise<MediaUploadDto> {
const user = await mediaUpload.user;
return {
url: mediaUpload.fileUrl,
id: mediaUpload.id,
notePublicId: (await mediaUpload.note)?.publicId ?? null,
createdAt: mediaUpload.createdAt,
username: user?.username ?? null,