1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2025-01-18 04:58:59 +02:00

Fixing backend

This commit is contained in:
Patrik J. Braun 2023-08-30 23:35:42 +02:00
parent 961fcf3dea
commit c510283e51
6 changed files with 49 additions and 78 deletions

View File

@ -16,7 +16,7 @@ import {SearchQueryDTO, SearchQueryTypes,} from '../../common/entities/SearchQue
import {LocationLookupException} from '../exceptions/LocationLookupException';
import {SupportedFormats} from '../../common/SupportedFormats';
import {ServerTime} from './ServerTimingMWs';
import {SortingMethods} from '../../common/entities/SortingMethods';
import {SortByTypes} from '../../common/entities/SortingMethods';
export class GalleryMWs {
@ServerTime('1.db', 'List Directory')
@ -322,7 +322,7 @@ export class GalleryMWs {
);
const photos =
await ObjectManagers.getInstance().SearchManager.getNMedia(query, [SortingMethods.random], 1, true);
await ObjectManagers.getInstance().SearchManager.getNMedia(query, [{method: SortByTypes.Random, ascending: null}], 1, true);
if (!photos || photos.length !== 1) {
return next(new ErrorDTO(ErrorCodes.INPUT_ERROR, 'No photo found'));
}

View File

@ -4,7 +4,7 @@ import {MediaEntity} from './enitites/MediaEntity';
import {DiskMangerWorker} from '../threading/DiskMangerWorker';
import {ObjectManagers} from '../ObjectManagers';
import {DatabaseType} from '../../../common/config/private/PrivateConfig';
import {SortingMethods} from '../../../common/entities/SortingMethods';
import {SortingMethod} from '../../../common/entities/SortingMethods';
import {SQLConnection} from './SQLConnection';
import {SearchQueryDTO, SearchQueryTypes, TextSearch,} from '../../../common/entities/SearchQueryDTO';
import {DirectoryEntity} from './enitites/DirectoryEntity';
@ -14,6 +14,7 @@ import {Utils} from '../../../common/Utils';
import {CoverPhotoDTO} from '../../../common/entities/PhotoDTO';
import {IObjectManager} from './IObjectManager';
import {Logger} from '../../Logger';
import {SearchManager} from './SearchManager';
const LOG_TAG = '[CoverManager]';
@ -25,34 +26,6 @@ export interface CoverPhotoDTOWithID extends CoverPhotoDTO {
export class CoverManager implements IObjectManager {
private static DIRECTORY_SELECT = ['directory.name', 'directory.path'];
private static setSorting<T>(
query: SelectQueryBuilder<T>
): SelectQueryBuilder<T> {
for (const sort of Config.AlbumCover.Sorting) {
switch (sort) {
case SortingMethods.descDate:
query.addOrderBy('media.metadata.creationDate', 'DESC');
break;
case SortingMethods.ascDate:
query.addOrderBy('media.metadata.creationDate', 'ASC');
break;
case SortingMethods.descRating:
query.addOrderBy('media.metadata.rating', 'DESC');
break;
case SortingMethods.ascRating:
query.addOrderBy('media.metadata.rating', 'ASC');
break;
case SortingMethods.descName:
query.addOrderBy('media.name', 'DESC');
break;
case SortingMethods.ascName:
query.addOrderBy('media.name', 'ASC');
break;
}
}
return query;
}
public async resetCovers(): Promise<void> {
const connection = await SQLConnection.getConnection();
@ -119,7 +92,7 @@ export class CoverManager implements IObjectManager {
.innerJoin('media.directory', 'directory')
.select(['media.name', 'media.id', ...CoverManager.DIRECTORY_SELECT])
.where(albumQuery);
CoverManager.setSorting(query);
SearchManager.setSorting(query, Config.AlbumCover.Sorting);
return query;
};
let coverMedia = null;
@ -201,7 +174,7 @@ export class CoverManager implements IObjectManager {
'ASC'
);
CoverManager.setSorting(query);
SearchManager.setSorting(query, Config.AlbumCover.Sorting);
return query;
};

View File

@ -13,9 +13,11 @@ import {
DatePatternFrequency,
DatePatternSearch,
DistanceSearch,
FromDateSearch, MaxPersonCountSearch,
FromDateSearch,
MaxPersonCountSearch,
MaxRatingSearch,
MaxResolutionSearch, MinPersonCountSearch,
MaxResolutionSearch,
MinPersonCountSearch,
MinRatingSearch,
MinResolutionSearch,
OrientationSearch,
@ -34,7 +36,7 @@ import {DatabaseType} from '../../../common/config/private/PrivateConfig';
import {Utils} from '../../../common/Utils';
import {FileEntity} from './enitites/FileEntity';
import {SQL_COLLATE} from './enitites/EntityUtils';
import {SortingMethods} from '../../../common/entities/SortingMethods';
import {GroupSortByTypes, SortByTypes, SortingMethod} from '../../../common/entities/SortingMethods';
export class SearchManager {
private DIRECTORY_SELECT = [
@ -349,43 +351,31 @@ export class SearchManager {
}
private static setSorting<T>(
public static setSorting<T>(
query: SelectQueryBuilder<T>,
sortings: SortingMethods[]
sortings: SortingMethod[]
): SelectQueryBuilder<T> {
if (!sortings || !Array.isArray(sortings)) {
return query;
}
if (sortings.includes(SortingMethods.random) && sortings.length > 1) {
throw new Error('Error during applying sorting: Can\' randomize and also sort the result. Bad input:' + sortings.map(s => SortingMethods[s]).join(', '));
if (sortings.findIndex(s => s.method == SortByTypes.Random) !== -1 && sortings.length > 1) {
throw new Error('Error during applying sorting: Can\' randomize and also sort the result. Bad input:' + sortings.map(s => GroupSortByTypes[s.method]).join(', '));
}
for (const sort of sortings) {
switch (sort) {
case SortingMethods.descDate:
query.addOrderBy('media.metadata.creationDate', 'DESC');
switch (sort.method) {
case SortByTypes.Date:
query.addOrderBy('media.metadata.creationDate', sort.ascending ? 'ASC' : 'DESC');
break;
case SortingMethods.ascDate:
query.addOrderBy('media.metadata.creationDate', 'ASC');
case SortByTypes.Rating:
query.addOrderBy('media.metadata.rating', sort.ascending ? 'ASC' : 'DESC');
break;
case SortingMethods.descRating:
query.addOrderBy('media.metadata.rating', 'DESC');
case SortByTypes.Name:
query.addOrderBy('media.name', sort.ascending ? 'ASC' : 'DESC');
break;
case SortingMethods.ascRating:
query.addOrderBy('media.metadata.rating', 'ASC');
case SortByTypes.PersonCount:
query.addOrderBy('media.metadata.personsLength', sort.ascending ? 'ASC' : 'DESC');
break;
case SortingMethods.descName:
query.addOrderBy('media.name', 'DESC');
break;
case SortingMethods.ascName:
query.addOrderBy('media.name', 'ASC');
break;
case SortingMethods.descPersonCount:
query.addOrderBy('media.metadata.personsLength', 'DESC');
break;
case SortingMethods.ascPersonCount:
query.addOrderBy('media.metadata.personsLength', 'ASC');
break;
case SortingMethods.random:
case SortByTypes.Random:
if (Config.Database.type === DatabaseType.mysql) {
query.groupBy('RAND(), media.id');
} else {
@ -398,7 +388,7 @@ export class SearchManager {
return query;
}
public async getNMedia(query: SearchQueryDTO, sortings: SortingMethods[], take: number, photoOnly = false) {
public async getNMedia(query: SearchQueryDTO, sortings: SortingMethod[], take: number, photoOnly = false) {
const connection = await SQLConnection.getConnection();
const sqlQuery: SelectQueryBuilder<PhotoEntity> = connection
.getRepository(photoOnly ? PhotoEntity : MediaEntity)

View File

@ -18,7 +18,8 @@ import {ProjectPath} from '../../../../../src/backend/ProjectPath';
import * as path from 'path';
import {DiskManager} from '../../../../../src/backend/model/DiskManger';
import {AlbumManager} from '../../../../../src/backend/model/database/AlbumManager';
import {SortingMethods} from '../../../../../src/common/entities/SortingMethods';
import {SortByTypes} from '../../../../../src/common/entities/SortingMethods';
import {ClientSortingConfig} from '../../../../../src/common/config/public/ClientConfig';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const deepEqualInAnyOrder = require('deep-equal-in-any-order');
@ -71,7 +72,7 @@ describe('IndexingManager', (sqlHelper: DBTestHelper) => {
afterEach(async () => {
Config.loadSync();
Config.AlbumCover.Sorting = [SortingMethods.descRating];
Config.AlbumCover.Sorting = [new ClientSortingConfig(SortByTypes.Rating, false)];
await sqlHelper.clearDB();
});
@ -358,7 +359,7 @@ describe('IndexingManager', (sqlHelper: DBTestHelper) => {
const sp2 = TestHelper.getRandomizedPhotoEntry(subDir, 'subPhoto2', 0);
sp2.metadata.rating = 3;
subDir.cover = sp1;
Config.AlbumCover.Sorting = [SortingMethods.descRating];
Config.AlbumCover.Sorting = [new ClientSortingConfig(SortByTypes.Rating, false)];
DirectoryDTOUtils.removeReferences(subDir);
await im.saveToDB(Utils.clone(subDir) as ParentDirectoryDTO);
@ -396,7 +397,7 @@ describe('IndexingManager', (sqlHelper: DBTestHelper) => {
const sp2 = TestHelper.getRandomizedPhotoEntry(subDir, 'subPhoto2', 0);
sp2.metadata.rating = 3;
subDir.cover = sp1;
Config.AlbumCover.Sorting = [SortingMethods.descRating];
Config.AlbumCover.Sorting = [new ClientSortingConfig(SortByTypes.Rating, false)];
DirectoryDTOUtils.removeReferences(subDir);
@ -433,7 +434,7 @@ describe('IndexingManager', (sqlHelper: DBTestHelper) => {
const sp2 = TestHelper.getRandomizedPhotoEntry(subDir, 'subPhoto2', 0);
sp2.metadata.rating = 3;
subDir.cover = sp1;
Config.AlbumCover.Sorting = [SortingMethods.descRating];
Config.AlbumCover.Sorting = [new ClientSortingConfig(SortByTypes.Rating, false)];
DirectoryDTOUtils.removeReferences(parent);
await im.saveToDB(Utils.clone(parent) as ParentDirectoryDTO);
@ -563,7 +564,7 @@ describe('IndexingManager', (sqlHelper: DBTestHelper) => {
const sp2 = TestHelper.getRandomizedPhotoEntry(subDir, 'subPhoto2', 1);
sp2.metadata.rating = 3;
subDir.cover = sp1;
Config.AlbumCover.Sorting = [SortingMethods.descRating];
Config.AlbumCover.Sorting = [new ClientSortingConfig(SortByTypes.Rating, false)];
DirectoryDTOUtils.removeReferences(parent);
const s1 = im.queueForSave(Utils.clone(parent) as ParentDirectoryDTO);

View File

@ -12,10 +12,11 @@ import {VideoDTO} from '../../../../../src/common/entities/VideoDTO';
import {FileDTO} from '../../../../../src/common/entities/FileDTO';
import {CoverManager} from '../../../../../src/backend/model/database/CoverManager';
import {Config} from '../../../../../src/common/config/private/Config';
import {SortingMethods} from '../../../../../src/common/entities/SortingMethods';
import {SortByTypes} from '../../../../../src/common/entities/SortingMethods';
import {Utils} from '../../../../../src/common/Utils';
import {SQLConnection} from '../../../../../src/backend/model/database/SQLConnection';
import {DirectoryEntity} from '../../../../../src/backend/model/database/enitites/DirectoryEntity';
import {ClientSortingConfig} from '../../../../../src/common/config/public/ClientConfig';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const deepEqualInAnyOrder = require('deep-equal-in-any-order');
@ -161,7 +162,8 @@ describe('CoverManager', (sqlHelper: DBTestHelper) => {
afterEach(() => {
Config.AlbumCover.SearchQuery = null;
Config.AlbumCover.Sorting = [SortingMethods.descRating, SortingMethods.descDate];
Config.AlbumCover.Sorting = [new ClientSortingConfig(SortByTypes.Rating, false),
new ClientSortingConfig(SortByTypes.Date, false)];
});
@ -181,13 +183,15 @@ describe('CoverManager', (sqlHelper: DBTestHelper) => {
it('should sort directory cover', async () => {
const pm = new CoverManager();
Config.AlbumCover.Sorting = [SortingMethods.descRating, SortingMethods.descDate];
Config.AlbumCover.Sorting = [new ClientSortingConfig(SortByTypes.Rating, false),
new ClientSortingConfig(SortByTypes.Date, false)];
expect(Utils.clone(await pm.setAndGetCoverForDirectory(subDir))).to.deep.equalInAnyOrder(previewifyMedia(p2));
Config.AlbumCover.Sorting = [SortingMethods.descDate];
Config.AlbumCover.Sorting = [
new ClientSortingConfig(SortByTypes.Date, false)];
expect(Utils.clone(await pm.setAndGetCoverForDirectory(subDir))).to.deep.equalInAnyOrder(previewifyMedia(pFaceLess));
Config.AlbumCover.Sorting = [SortingMethods.descRating];
Config.AlbumCover.Sorting = [new ClientSortingConfig(SortByTypes.Rating, false)];
expect(Utils.clone(await pm.setAndGetCoverForDirectory(dir))).to.deep.equalInAnyOrder(previewifyMedia(p4));
Config.AlbumCover.Sorting = [SortingMethods.descName];
Config.AlbumCover.Sorting = [new ClientSortingConfig(SortByTypes.Name, false)];
expect(Utils.clone(await pm.setAndGetCoverForDirectory(dir))).to.deep.equalInAnyOrder(previewifyMedia(v));
});

View File

@ -37,7 +37,7 @@ import {AutoCompleteItem} from '../../../../../src/common/entities/AutoCompleteI
import {Config} from '../../../../../src/common/config/private/Config';
import {SearchQueryParser} from '../../../../../src/common/SearchQueryParser';
import {FileDTO} from '../../../../../src/common/entities/FileDTO';
import {SortingMethods} from '../../../../../src/common/entities/SortingMethods';
import {SortByTypes} from '../../../../../src/common/entities/SortingMethods';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const deepEqualInAnyOrder = require('deep-equal-in-any-order');
@ -1485,14 +1485,17 @@ describe('SearchManager', (sqlHelper: DBTestHelper) => {
} as TextSearch;
// eslint-disable-next-line
expect(await sm.getNMedia(query, [SortingMethods.random], 1, true)).to.deep.equalInAnyOrder([]);
expect(await sm.getNMedia(query, [{method: SortByTypes.Random, ascending: null}], 1, true)).to.deep.equalInAnyOrder([]);
query = ({
text: 'wookiees',
matchType: TextSearchQueryMatchTypes.exact_match,
type: SearchQueryTypes.keyword
} as TextSearch);
expect(Utils.clone(await sm.getNMedia(query, [SortingMethods.random], 1, true))).to.deep.equalInAnyOrder([searchifyMedia(pFaceLess)]);
expect(Utils.clone(await sm.getNMedia(query, [{
method: SortByTypes.Random,
ascending: null
}], 1, true))).to.deep.equalInAnyOrder([searchifyMedia(pFaceLess)]);
});
});