cleaned up image file repository
This commit is contained in:
parent
de893f7c40
commit
ba512c2e5b
|
@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';
|
|||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { ImageFileType } from 'picsur-shared/dist/dto/image-file-types.enum';
|
||||
import { AsyncFailable, Fail } from 'picsur-shared/dist/types';
|
||||
import { In, LessThan, Repository } from 'typeorm';
|
||||
import { LessThan, Repository } from 'typeorm';
|
||||
import { EImageDerivativeBackend } from '../../models/entities/image-derivative.entity';
|
||||
import { EImageFileBackend } from '../../models/entities/image-file.entity';
|
||||
|
||||
|
@ -57,37 +57,24 @@ export class ImageFileDBService {
|
|||
}
|
||||
}
|
||||
|
||||
public async getFileMime(
|
||||
imageId: string,
|
||||
type: ImageFileType,
|
||||
): AsyncFailable<string> {
|
||||
try {
|
||||
const found = await this.imageFileRepo.findOne({
|
||||
where: { image_id: imageId, type },
|
||||
select: ['mime'],
|
||||
});
|
||||
|
||||
if (!found) return Fail('Image not found');
|
||||
return found.mime;
|
||||
} catch (e) {
|
||||
return Fail(e);
|
||||
}
|
||||
}
|
||||
|
||||
// This is useful because you dont have to pull the whole image file
|
||||
public async getFileMimes(
|
||||
imageId: string,
|
||||
types: ImageFileType[],
|
||||
): AsyncFailable<{ [key: string]: string | undefined }> {
|
||||
): AsyncFailable<{ [key in ImageFileType]?: string }> {
|
||||
try {
|
||||
const found = await this.imageFileRepo.find({
|
||||
where: { image_id: imageId, type: In(types) },
|
||||
where: { image_id: imageId },
|
||||
select: ['type', 'mime'],
|
||||
});
|
||||
|
||||
if (!found) return Fail('Image not found');
|
||||
return Object.fromEntries(
|
||||
types.map((type) => [type, found.find((f) => f.type === type)?.mime]),
|
||||
);
|
||||
|
||||
const result: { [key in ImageFileType]?: string } = {};
|
||||
for (const file of found) {
|
||||
result[file.type] = file.mime;
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (e) {
|
||||
return Fail(e);
|
||||
}
|
||||
|
@ -113,6 +100,7 @@ export class ImageFileDBService {
|
|||
}
|
||||
}
|
||||
|
||||
// Returns null when derivative is not found
|
||||
public async getDerivative(
|
||||
imageId: string,
|
||||
key: string,
|
||||
|
@ -123,6 +111,7 @@ export class ImageFileDBService {
|
|||
});
|
||||
if (!derivative) return null;
|
||||
|
||||
// Ensure read time updated to within 1 day precision
|
||||
const yesterday = new Date(Date.now() - A_DAY_IN_SECONDS * 1000);
|
||||
if (derivative.last_read > yesterday) {
|
||||
derivative.last_read = new Date();
|
||||
|
@ -135,23 +124,6 @@ export class ImageFileDBService {
|
|||
}
|
||||
}
|
||||
|
||||
public async getDerivativeMime(
|
||||
imageId: string,
|
||||
key: string,
|
||||
): AsyncFailable<string> {
|
||||
try {
|
||||
const found = await this.imageDerivativeRepo.findOne({
|
||||
where: { image_id: imageId, key },
|
||||
select: ['mime'],
|
||||
});
|
||||
|
||||
if (!found) return Fail('Image not found');
|
||||
return found.mime;
|
||||
} catch (e) {
|
||||
return Fail(e);
|
||||
}
|
||||
}
|
||||
|
||||
public async cleanupDerivatives(
|
||||
olderThanSeconds: number,
|
||||
): AsyncFailable<number> {
|
||||
|
|
|
@ -168,13 +168,14 @@ export class ImageManagerService {
|
|||
}
|
||||
|
||||
public async getMasterMime(imageId: string): AsyncFailable<FullMime> {
|
||||
const mime = await this.imageFilesService.getFileMime(
|
||||
imageId,
|
||||
ImageFileType.MASTER,
|
||||
const mime = await this.imageFilesService.getFileMimes(
|
||||
imageId
|
||||
);
|
||||
if (HasFailed(mime)) return mime;
|
||||
|
||||
return ParseMime(mime);
|
||||
if (mime.master === undefined) return Fail('No master file');
|
||||
|
||||
return ParseMime(mime.master);
|
||||
}
|
||||
|
||||
public async getOriginal(imageId: string): AsyncFailable<EImageFileBackend> {
|
||||
|
@ -182,23 +183,21 @@ export class ImageManagerService {
|
|||
}
|
||||
|
||||
public async getOriginalMime(imageId: string): AsyncFailable<FullMime> {
|
||||
const mime = await this.imageFilesService.getFileMime(
|
||||
imageId,
|
||||
ImageFileType.ORIGINAL,
|
||||
const mime = await this.imageFilesService.getFileMimes(
|
||||
imageId
|
||||
);
|
||||
if (HasFailed(mime)) return mime;
|
||||
|
||||
return ParseMime(mime);
|
||||
if (mime.original === undefined) return Fail('No original file');
|
||||
|
||||
return ParseMime(mime.original);
|
||||
}
|
||||
|
||||
public async getAllFileMimes(imageId: string): AsyncFailable<{
|
||||
public async getFileMimes(imageId: string): AsyncFailable<{
|
||||
[ImageFileType.MASTER]: string;
|
||||
[ImageFileType.ORIGINAL]: string | undefined;
|
||||
}> {
|
||||
const result = await this.imageFilesService.getFileMimes(imageId, [
|
||||
ImageFileType.MASTER,
|
||||
ImageFileType.ORIGINAL,
|
||||
]);
|
||||
const result = await this.imageFilesService.getFileMimes(imageId);
|
||||
if (HasFailed(result)) return result;
|
||||
|
||||
if (result[ImageFileType.MASTER] === undefined) {
|
||||
|
|
|
@ -6,12 +6,12 @@ import {
|
|||
Logger,
|
||||
NotFoundException,
|
||||
Query,
|
||||
Res,
|
||||
Res
|
||||
} from '@nestjs/common';
|
||||
import type { FastifyReply } from 'fastify';
|
||||
import {
|
||||
ImageMetaResponse,
|
||||
ImageRequestParams,
|
||||
ImageRequestParams
|
||||
} from 'picsur-shared/dist/dto/api/image.dto';
|
||||
import { HasFailed } from 'picsur-shared/dist/types';
|
||||
import { UsersService } from '../../collections/user-db/user-db.service';
|
||||
|
@ -97,7 +97,7 @@ export class ImageController {
|
|||
}
|
||||
|
||||
const [fileMimes, imageUser] = await Promise.all([
|
||||
this.imagesService.getAllFileMimes(id),
|
||||
this.imagesService.getFileMimes(id),
|
||||
this.userService.findOne(image.user_id),
|
||||
]);
|
||||
if (HasFailed(fileMimes)) {
|
||||
|
|
Loading…
Reference in a new issue