mirror of
https://github.com/CaramelFur/Picsur.git
synced 2026-06-23 09:51:55 +02:00
send back error message in image form
This commit is contained in:
@@ -1,22 +1,22 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import {
|
||||
DecodedSysPref,
|
||||
PrefValueType,
|
||||
PrefValueTypeStrings
|
||||
DecodedSysPref,
|
||||
PrefValueType,
|
||||
PrefValueTypeStrings
|
||||
} from 'picsur-shared/dist/dto/preferences.dto';
|
||||
import { SysPreference } from 'picsur-shared/dist/dto/sys-preferences.enum';
|
||||
import { AsyncFailable, Fail, FT, HasFailed } from 'picsur-shared/dist/types';
|
||||
import { Repository } from 'typeorm';
|
||||
import {
|
||||
ESysPreferenceBackend,
|
||||
ESysPreferenceSchema
|
||||
ESysPreferenceBackend,
|
||||
ESysPreferenceSchema
|
||||
} from '../../database/entities/sys-preference.entity';
|
||||
import {
|
||||
SysPreferenceList,
|
||||
SysPreferenceValueTypes
|
||||
SysPreferenceList,
|
||||
SysPreferenceValueTypes
|
||||
} from '../../models/constants/syspreferences.const';
|
||||
import { MutexFallBack } from '../../models/util/mutex-fallback';
|
||||
import { MutexFallBack } from '../../util/mutex-fallback';
|
||||
import { PreferenceCommonService } from './preference-common.service';
|
||||
import { PreferenceDefaultsService } from './preference-defaults.service';
|
||||
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import {
|
||||
DecodedUsrPref,
|
||||
PrefValueType,
|
||||
PrefValueTypeStrings
|
||||
DecodedUsrPref,
|
||||
PrefValueType,
|
||||
PrefValueTypeStrings
|
||||
} from 'picsur-shared/dist/dto/preferences.dto';
|
||||
import { UsrPreference } from 'picsur-shared/dist/dto/usr-preferences.enum';
|
||||
import { AsyncFailable, Fail, FT, HasFailed } from 'picsur-shared/dist/types';
|
||||
import { Repository } from 'typeorm';
|
||||
import {
|
||||
EUsrPreferenceBackend,
|
||||
EUsrPreferenceSchema
|
||||
EUsrPreferenceBackend,
|
||||
EUsrPreferenceSchema
|
||||
} from '../../database/entities/usr-preference.entity';
|
||||
import {
|
||||
UsrPreferenceList,
|
||||
UsrPreferenceValueTypes
|
||||
UsrPreferenceList,
|
||||
UsrPreferenceValueTypes
|
||||
} from '../../models/constants/usrpreferences.const';
|
||||
import { MutexFallBack } from '../../models/util/mutex-fallback';
|
||||
import { MutexFallBack } from '../../util/mutex-fallback';
|
||||
import { PreferenceCommonService } from './preference-common.service';
|
||||
import { PreferenceDefaultsService } from './preference-defaults.service';
|
||||
|
||||
|
||||
@@ -3,11 +3,11 @@ import { InjectRepository } from '@nestjs/typeorm';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
import { SysPreference } from 'picsur-shared/dist/dto/sys-preferences.enum';
|
||||
import {
|
||||
AsyncFailable,
|
||||
Fail,
|
||||
FT,
|
||||
HasFailed,
|
||||
HasSuccess
|
||||
AsyncFailable,
|
||||
Fail,
|
||||
FT,
|
||||
HasFailed,
|
||||
HasSuccess
|
||||
} from 'picsur-shared/dist/types';
|
||||
import { FindResult } from 'picsur-shared/dist/types/find-result';
|
||||
import { makeUnique } from 'picsur-shared/dist/util/unique';
|
||||
@@ -15,15 +15,15 @@ import { Repository } from 'typeorm';
|
||||
import { EUserBackend } from '../../database/entities/user.entity';
|
||||
import { Permissions } from '../../models/constants/permissions.const';
|
||||
import {
|
||||
DefaultRolesList,
|
||||
SoulBoundRolesList
|
||||
DefaultRolesList,
|
||||
SoulBoundRolesList
|
||||
} from '../../models/constants/roles.const';
|
||||
import {
|
||||
ImmutableUsersList,
|
||||
LockedLoginUsersList,
|
||||
UndeletableUsersList
|
||||
ImmutableUsersList,
|
||||
LockedLoginUsersList,
|
||||
UndeletableUsersList
|
||||
} from '../../models/constants/special-users.const';
|
||||
import { GetCols } from '../../models/util/collection';
|
||||
import { GetCols } from '../../util/collection';
|
||||
import { SysPreferenceService } from '../preference-db/sys-preference-db.service';
|
||||
import { RolesService } from '../role-db/role-db.service';
|
||||
|
||||
|
||||
@@ -1,4 +1,12 @@
|
||||
import { ArgumentsHost, Catch, ExceptionFilter, ForbiddenException, Logger, MethodNotAllowedException, NotFoundException, UnauthorizedException } from '@nestjs/common';
|
||||
import {
|
||||
ArgumentsHost,
|
||||
Catch, ExceptionFilter,
|
||||
ForbiddenException,
|
||||
Logger,
|
||||
MethodNotAllowedException,
|
||||
NotFoundException,
|
||||
UnauthorizedException
|
||||
} from '@nestjs/common';
|
||||
import { FastifyReply, FastifyRequest } from 'fastify';
|
||||
import { ApiErrorResponse } from 'picsur-shared/dist/dto/api/api.dto';
|
||||
import {
|
||||
|
||||
@@ -4,10 +4,10 @@ import { fileTypeFromBuffer, FileTypeResult } from 'file-type';
|
||||
import { ImageRequestParams } from 'picsur-shared/dist/dto/api/image.dto';
|
||||
import { ImageEntryVariant } from 'picsur-shared/dist/dto/image-entry-variant.enum';
|
||||
import {
|
||||
AnimFileType,
|
||||
FileType,
|
||||
ImageFileType,
|
||||
Mime2FileType
|
||||
AnimFileType,
|
||||
FileType,
|
||||
ImageFileType,
|
||||
Mime2FileType
|
||||
} from 'picsur-shared/dist/dto/mimes.dto';
|
||||
import { SysPreference } from 'picsur-shared/dist/dto/sys-preferences.enum';
|
||||
import { UsrPreference } from 'picsur-shared/dist/dto/usr-preferences.enum';
|
||||
@@ -22,7 +22,7 @@ import { UsrPreferenceService } from '../../collections/preference-db/usr-prefer
|
||||
import { EImageDerivativeBackend } from '../../database/entities/image-derivative.entity';
|
||||
import { EImageFileBackend } from '../../database/entities/image-file.entity';
|
||||
import { EImageBackend } from '../../database/entities/image.entity';
|
||||
import { MutexFallBack } from '../../models/util/mutex-fallback';
|
||||
import { MutexFallBack } from '../../util/mutex-fallback';
|
||||
import { ImageConverterService } from './image-converter.service';
|
||||
import { ImageProcessorService } from './image-processor.service';
|
||||
import { WebPInfo } from './webpinfo/webpinfo';
|
||||
|
||||
@@ -6,7 +6,7 @@ import {
|
||||
} from 'picsur-shared/dist/dto/api/image.dto';
|
||||
import { ImageEntryVariant } from 'picsur-shared/dist/dto/image-entry-variant.enum';
|
||||
import { FileType2Mime } from 'picsur-shared/dist/dto/mimes.dto';
|
||||
import { ThrowIfFailed } from 'picsur-shared/dist/types';
|
||||
import { FT, IsFailure, ThrowIfFailed } from 'picsur-shared/dist/types';
|
||||
import { UsersService } from '../../collections/user-db/user-db.service';
|
||||
import { ImageFullIdParam } from '../../decorators/image-id/image-full-id.decorator';
|
||||
import { ImageIdParam } from '../../decorators/image-id/image-id.decorator';
|
||||
@@ -16,6 +16,7 @@ import { ImageManagerService } from '../../managers/image/image.service';
|
||||
import type { ImageFullId } from '../../models/constants/image-full-id.const';
|
||||
import { Permission } from '../../models/constants/permissions.const';
|
||||
import { EUserBackend2EUser } from '../../models/transformers/user.transformer';
|
||||
import { BrandMessageType, GetBrandMessage } from '../../util/branding';
|
||||
|
||||
// This is the only controller with CORS enabled
|
||||
@Controller('i')
|
||||
@@ -53,21 +54,35 @@ export class ImageController {
|
||||
@ImageFullIdParam() fullid: ImageFullId,
|
||||
@Query() params: ImageRequestParams,
|
||||
): Promise<Buffer> {
|
||||
if (fullid.variant === ImageEntryVariant.ORIGINAL) {
|
||||
try {
|
||||
if (fullid.variant === ImageEntryVariant.ORIGINAL) {
|
||||
const image = ThrowIfFailed(
|
||||
await this.imagesService.getOriginal(fullid.id),
|
||||
);
|
||||
|
||||
res.type(ThrowIfFailed(FileType2Mime(image.filetype)));
|
||||
return image.data;
|
||||
}
|
||||
|
||||
const image = ThrowIfFailed(
|
||||
await this.imagesService.getOriginal(fullid.id),
|
||||
await this.imagesService.getConverted(
|
||||
fullid.id,
|
||||
fullid.filetype,
|
||||
params,
|
||||
),
|
||||
);
|
||||
|
||||
res.type(ThrowIfFailed(FileType2Mime(image.filetype)));
|
||||
return image.data;
|
||||
} catch (e) {
|
||||
if (!IsFailure(e) || e.getType() !== FT.NotFound) throw e;
|
||||
|
||||
const message = ThrowIfFailed(
|
||||
await GetBrandMessage(BrandMessageType.NotFound),
|
||||
);
|
||||
res.type(message.type);
|
||||
return message.data;
|
||||
}
|
||||
|
||||
const image = ThrowIfFailed(
|
||||
await this.imagesService.getConverted(fullid.id, fullid.filetype, params),
|
||||
);
|
||||
|
||||
res.type(ThrowIfFailed(FileType2Mime(image.filetype)));
|
||||
return image.data;
|
||||
}
|
||||
|
||||
@Get('meta/:id')
|
||||
|
||||
38
backend/src/util/branding.ts
Normal file
38
backend/src/util/branding.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { readFile } from 'fs/promises';
|
||||
import { resolve } from 'path';
|
||||
import { AsyncFailable, Fail, FT } from 'picsur-shared/dist/types';
|
||||
import { PackageRoot } from '../config/config.static';
|
||||
|
||||
export const BrandingPath = resolve(PackageRoot, '../branding');
|
||||
|
||||
export enum BrandMessageType {
|
||||
NotFound = 'notfound',
|
||||
}
|
||||
|
||||
export interface BrandMessage {
|
||||
type: string;
|
||||
data: Buffer;
|
||||
}
|
||||
|
||||
const BrandMessageCache: Record<string, BrandMessage> = {};
|
||||
|
||||
export async function GetBrandMessage(
|
||||
name: BrandMessageType,
|
||||
): AsyncFailable<BrandMessage> {
|
||||
if (BrandMessageCache[name]) {
|
||||
return BrandMessageCache[name];
|
||||
}
|
||||
|
||||
try {
|
||||
const file = await readFile(
|
||||
resolve(BrandingPath, 'messages', name + '.png'),
|
||||
);
|
||||
BrandMessageCache[name] = {
|
||||
type: 'image/png',
|
||||
data: file,
|
||||
}
|
||||
return BrandMessageCache[name];
|
||||
} catch (e) {
|
||||
return Fail(FT.Internal, e);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user