1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2024-12-25 02:04:15 +02:00
pigallery2/test/TestHelper.ts

404 lines
12 KiB
TypeScript
Raw Normal View History

import {
CameraMetadataEntity,
2019-01-13 18:38:39 +02:00
GPSMetadataEntity,
MediaDimensionEntity,
PositionMetaDataEntity
2023-01-06 00:11:58 +02:00
} from '../src/backend/model/database/enitites/MediaEntity';
import {PhotoEntity, PhotoMetadataEntity} from '../src/backend/model/database/enitites/PhotoEntity';
import {DirectoryEntity} from '../src/backend/model/database/enitites/DirectoryEntity';
import {VideoEntity, VideoMetadataEntity} from '../src/backend/model/database/enitites/VideoEntity';
import {MediaDimension, MediaDTO} from '../src/common/entities/MediaDTO';
import {
CameraMetadata,
FaceRegion,
GPSMetadata,
PhotoDTO,
PhotoMetadata,
PositionMetaData,
PreviewPhotoDTO
} from '../src/common/entities/PhotoDTO';
import {DirectoryBaseDTO, DirectoryPathDTO} from '../src/common/entities/DirectoryDTO';
import {FileDTO} from '../src/common/entities/FileDTO';
import {DiskMangerWorker} from '../src/backend/model/threading/DiskMangerWorker';
export class TestHelper {
static creationCounter = 0;
2021-06-27 19:33:37 +02:00
public static getDirectoryEntry(parent: DirectoryBaseDTO = null, name = 'wars dir'): DirectoryEntity {
const dir = new DirectoryEntity();
dir.name = name;
dir.path = DiskMangerWorker.pathFromParent({path: '', name: '.'});
dir.mediaCount = 0;
dir.directories = [];
dir.metaFile = [];
dir.media = [];
dir.lastModified = 1656069687773;
dir.lastScanned = 1656069687773;
// dir.parent = null;
if (parent !== null) {
dir.path = DiskMangerWorker.pathFromParent(parent);
parent.directories.push(dir);
}
return dir;
}
public static getPhotoEntry(dir: DirectoryPathDTO): PhotoEntity {
const sd = new MediaDimensionEntity();
sd.height = 400;
sd.width = 200;
const gps = new GPSMetadataEntity();
gps.latitude = 1;
gps.longitude = 1;
const pd = new PositionMetaDataEntity();
pd.city = 'New York';
pd.country = 'Alderan';
pd.state = 'Kamino';
pd.GPSData = gps;
const cd = new CameraMetadataEntity();
cd.ISO = 100;
cd.model = '60D';
2018-12-18 01:05:12 +02:00
cd.make = 'Canon';
cd.fStop = 1;
cd.exposure = 1;
cd.focalLength = 1;
cd.lens = 'Lens';
const m = new PhotoMetadataEntity();
m.caption = null;
m.keywords = ['apple'];
m.cameraData = cd;
m.positionData = pd;
m.size = sd;
m.creationDate = 1656069387772;
m.fileSize = 123456789;
// m.rating = 0; no rating by default
// TODO: remove when typeorm is fixed
m.duration = null;
m.bitRate = null;
const d = new PhotoEntity();
d.name = 'test media.jpg';
d.directory = (dir as any);
if ((dir as DirectoryBaseDTO).media) {
(dir as DirectoryBaseDTO).media.push(d);
(dir as DirectoryBaseDTO).mediaCount++;
}
d.metadata = m;
return d;
}
public static getVideoEntry(dir: DirectoryPathDTO): VideoEntity {
2018-11-24 15:51:47 +02:00
const sd = new MediaDimensionEntity();
sd.height = 200;
sd.width = 300;
2018-11-24 15:51:47 +02:00
const m = new VideoMetadataEntity();
m.caption = null;
2018-11-24 15:51:47 +02:00
m.keywords = null;
m.rating = null;
2018-11-24 15:51:47 +02:00
m.size = sd;
m.creationDate = 1656069387771;
2018-11-24 15:51:47 +02:00
m.fileSize = 123456789;
m.duration = 10000;
m.bitRate = 4000;
const d = new VideoEntity();
d.name = 'test video.mp4';
d.directory = (dir as any);
if ((dir as DirectoryBaseDTO).media) {
(dir as DirectoryBaseDTO).media.push(d);
(dir as DirectoryBaseDTO).mediaCount++;
}
2018-11-24 15:51:47 +02:00
d.metadata = m;
return d;
}
public static getGPXEntry(dir: DirectoryPathDTO): FileDTO {
const d: FileDTO = {
id: null,
name: 'saturdayRun.gpx',
directory: dir
};
if ((dir as DirectoryBaseDTO).metaFile) {
(dir as DirectoryBaseDTO).metaFile.push(d);
}
return d;
}
2021-06-27 19:33:37 +02:00
public static getVideoEntry1(dir: DirectoryBaseDTO): VideoEntity {
const p = TestHelper.getVideoEntry(dir);
p.name = 'swVideo.mp4';
return p;
}
public static getPhotoEntry1(dir: DirectoryPathDTO): PhotoEntity {
const p = TestHelper.getPhotoEntry(dir);
2018-12-05 20:15:24 +02:00
p.metadata.caption = 'Han Solo\'s dice';
p.metadata.keywords = ['Boba Fett', 'star wars', 'Anakin', 'death star'];
p.metadata.positionData.city = 'Mos Eisley';
p.metadata.positionData.country = 'Tatooine';
p.name = 'sw1.jpg';
p.metadata.positionData.GPSData.latitude = 10;
p.metadata.positionData.GPSData.longitude = 10;
p.metadata.creationDate = 1656069387772 - 1000;
p.metadata.rating = 1;
p.metadata.size.height = 1000;
p.metadata.size.width = 1000;
2019-01-27 01:03:40 +02:00
p.metadata.faces = [{
2019-03-10 22:59:45 +02:00
box: {height: 10, width: 10, left: 10, top: 10},
2019-01-27 01:03:40 +02:00
name: 'Boba Fett'
} as FaceRegion, {
box: {height: 10, width: 10, left: 102, top: 102},
2019-01-27 01:03:40 +02:00
name: 'Luke Skywalker'
} as FaceRegion, {
box: {height: 10, width: 10, left: 103, top: 103},
2019-01-27 01:03:40 +02:00
name: 'Han Solo'
} as FaceRegion, {
box: {height: 10, width: 10, left: 104, top: 104},
2019-01-27 01:03:40 +02:00
name: 'Unkle Ben'
} as FaceRegion, {
box: {height: 10, width: 10, left: 105, top: 105},
name: 'Arvíztűrő Tükörfúrógép'
} as FaceRegion, {
2021-01-17 00:37:14 +02:00
box: {height: 10, width: 10, left: 201, top: 201},
name: 'R2-D2'
} as FaceRegion] as any[];
return p;
}
public static getPhotoEntry2(dir: DirectoryPathDTO): PhotoEntity {
const p = TestHelper.getPhotoEntry(dir);
p.metadata.caption = 'Light saber';
p.metadata.keywords = ['Padmé Amidala', 'star wars', 'Natalie Portman', 'death star', 'wookiee'];
p.metadata.positionData.city = 'Derem City';
p.metadata.positionData.state = 'Research City';
p.metadata.positionData.country = 'Kamino';
p.name = 'sw2.jpg';
p.metadata.positionData.GPSData.latitude = -10;
p.metadata.positionData.GPSData.longitude = -10;
p.metadata.creationDate = 1656069387772 - 2000;
p.metadata.rating = 2;
p.metadata.size.height = 2000;
p.metadata.size.width = 1000;
p.metadata.faces = [{
2019-03-14 23:29:09 +02:00
box: {height: 10, width: 10, left: 10, top: 10},
2019-01-27 01:03:40 +02:00
name: 'Padmé Amidala'
} as FaceRegion, {
2019-03-14 23:29:09 +02:00
box: {height: 10, width: 10, left: 101, top: 101},
2019-01-27 01:03:40 +02:00
name: 'Anakin Skywalker'
} as FaceRegion, {
2019-03-14 23:29:09 +02:00
box: {height: 10, width: 10, left: 101, top: 101},
name: 'Obivan Kenobi'
} as FaceRegion, {
2021-01-17 00:37:14 +02:00
box: {height: 10, width: 10, left: 201, top: 201},
name: 'R2-D2'
} as FaceRegion] as any[];
return p;
}
public static getPhotoEntry3(dir: DirectoryPathDTO): PhotoEntity {
const p = TestHelper.getPhotoEntry(dir);
p.metadata.caption = 'Amber stone';
p.metadata.keywords = ['star wars', 'wookiees'];
p.metadata.positionData.city = 'Castilon';
p.metadata.positionData.state = 'Devaron';
p.metadata.positionData.country = 'Ajan Kloss';
p.name = 'sw3.jpg';
p.metadata.positionData.GPSData.latitude = 10;
p.metadata.positionData.GPSData.longitude = 15;
p.metadata.creationDate = 1656069387772 - 3000;
p.metadata.rating = 3;
p.metadata.size.height = 1000;
p.metadata.size.width = 2000;
p.metadata.faces = [{
box: {height: 10, width: 10, left: 10, top: 10},
name: 'Kylo Ren'
} as FaceRegion, {
box: {height: 10, width: 10, left: 101, top: 101},
name: 'Leia Organa'
} as FaceRegion, {
box: {height: 10, width: 10, left: 103, top: 103},
name: 'Han Solo'
} as FaceRegion] as any[];
return p;
}
public static getPhotoEntry4(dir: DirectoryPathDTO): PhotoEntity {
const p = TestHelper.getPhotoEntry(dir);
p.metadata.caption = 'Millennium falcon';
p.metadata.keywords = ['star wars', 'ewoks'];
p.metadata.positionData.city = 'Tipoca City';
p.metadata.positionData.state = 'Exegol';
p.metadata.positionData.country = 'Jedha';
p.name = 'sw4.jpg';
p.metadata.positionData.GPSData.latitude = 15;
p.metadata.positionData.GPSData.longitude = 10;
p.metadata.creationDate = 1656069387772 - 4000;
p.metadata.size.height = 3000;
p.metadata.size.width = 2000;
2021-01-17 00:37:14 +02:00
p.metadata.faces = [{
box: {height: 10, width: 10, left: 10, top: 10},
name: 'Kylo Ren'
} as FaceRegion, {
box: {height: 10, width: 10, left: 101, top: 101},
name: 'Anakin Skywalker'
} as FaceRegion, {
box: {height: 10, width: 10, left: 101, top: 101},
2019-01-27 01:03:40 +02:00
name: 'Obivan Kenobi'
} as FaceRegion, {
2021-01-17 00:37:14 +02:00
box: {height: 10, width: 10, left: 201, top: 201},
name: 'R2-D2'
} as FaceRegion] as any[];
2021-01-17 00:37:14 +02:00
return p;
}
2021-06-27 19:33:37 +02:00
public static getRandomizedDirectoryEntry(parent: DirectoryBaseDTO = null, forceStr: string = null): DirectoryBaseDTO<MediaDTO> {
2021-06-27 19:33:37 +02:00
const dir: DirectoryBaseDTO = {
2018-12-18 01:05:12 +02:00
id: null,
name: DiskMangerWorker.dirName(forceStr || Math.random().toString(36).substring(7)),
path: DiskMangerWorker.pathFromParent({path: '', name: '.'}),
mediaCount: 0,
2018-12-18 01:05:12 +02:00
directories: [],
metaFile: [],
preview: null,
validPreview: false,
2018-12-18 01:05:12 +02:00
media: [],
lastModified: Date.now(),
lastScanned: null,
parent
2018-12-18 01:05:12 +02:00
};
if (parent !== null) {
dir.path = DiskMangerWorker.pathFromParent(parent);
parent.directories.push(dir);
}
return dir;
}
2021-06-27 19:33:37 +02:00
public static getRandomizedGPXEntry(dir: DirectoryBaseDTO, forceStr: string = null): FileDTO {
2018-12-18 01:05:12 +02:00
const d: FileDTO = {
id: null,
name: forceStr + '_' + Math.random().toString(36).substring(7) + '.gpx',
directory: dir
};
2018-11-26 01:26:29 +02:00
dir.metaFile.push(d);
return d;
}
public static getRandomizedFace(media: PhotoDTO, forceStr: string = null): FaceRegion {
const rndStr = (): string => {
return forceStr + '_' + Math.random().toString(36).substring(7);
};
const rndInt = (max = 5000): number => {
2019-01-13 18:38:39 +02:00
return Math.floor(Math.random() * max);
};
const f: FaceRegion = {
name: rndStr() + '.jpg',
box: {
2019-03-10 21:57:27 +02:00
left: rndInt(),
top: rndInt(),
2019-01-13 18:38:39 +02:00
width: rndInt(),
height: rndInt()
}
};
media.metadata.faces = (media.metadata.faces || []);
media.metadata.faces.push(f);
return f;
}
public static getRandomizedPhotoEntry(dir: DirectoryBaseDTO, forceStr: string = null, faces = 2): PhotoDTO {
2019-01-13 18:38:39 +02:00
const rndStr = (): string => {
2019-01-13 18:38:39 +02:00
return forceStr + '_' + Math.random().toString(36).substring(7);
};
const rndInt = (max = 5000): number => {
return Math.floor(Math.random() * max);
};
2018-12-18 01:05:12 +02:00
const sd: MediaDimension = {
height: rndInt(),
width: rndInt(),
};
2018-12-18 01:05:12 +02:00
const gps: GPSMetadata = {
latitude: rndInt(1000),
longitude: rndInt(1000)
};
const pd: PositionMetaData = {
city: rndStr(),
country: rndStr(),
state: rndStr(),
GPSData: gps
};
const cd: CameraMetadata = {
ISO: rndInt(500),
model: rndStr(),
make: rndStr(),
fStop: rndInt(10),
exposure: rndInt(10),
focalLength: rndInt(10),
lens: rndStr()
};
const m: PhotoMetadata = {
keywords: [rndStr(), rndStr()],
cameraData: cd,
positionData: pd,
size: sd,
creationDate: Date.now() + ++TestHelper.creationCounter,
2018-12-18 01:05:12 +02:00
fileSize: rndInt(10000),
caption: rndStr(),
rating: rndInt(5) as any,
2018-12-18 01:05:12 +02:00
};
const p: PhotoDTO = {
2018-12-18 01:05:12 +02:00
id: null,
name: rndStr() + '.jpg',
directory: dir,
metadata: m
2018-12-18 01:05:12 +02:00
};
2019-01-13 18:38:39 +02:00
for (let i = 0; i < faces; i++) {
this.getRandomizedFace(p, 'Person ' + i);
}
dir.media.push(p);
TestHelper.updatePreview(dir);
return p;
}
2021-06-27 19:33:37 +02:00
static updatePreview(dir: DirectoryBaseDTO): void {
if (dir.media.length > 0) {
dir.preview = dir.media.sort((a, b): number => b.metadata.creationDate - a.metadata.creationDate)[0];
} else {
const filtered = dir.directories.filter((d): PreviewPhotoDTO => d.preview).map((d): PreviewPhotoDTO => d.preview);
if (filtered.length > 0) {
dir.preview = filtered.sort((a, b): number => b.metadata.creationDate - a.metadata.creationDate)[0];
}
}
if (dir.parent) {
TestHelper.updatePreview(dir.parent);
2019-01-13 18:38:39 +02:00
}
}
}