2019-01-14 00:38:39 +08:00
|
|
|
import {IPersonManager} from '../interfaces/IPersonManager';
|
|
|
|
import {SQLConnection} from './SQLConnection';
|
2019-02-05 06:46:27 +08:00
|
|
|
import {PersonEntry} from './enitites/PersonEntry';
|
|
|
|
import {PhotoDTO} from '../../../common/entities/PhotoDTO';
|
2019-02-15 07:25:55 +08:00
|
|
|
import {MediaEntity} from './enitites/MediaEntity';
|
|
|
|
import {FaceRegionEntry} from './enitites/FaceRegionEntry';
|
2019-03-04 04:17:42 +08:00
|
|
|
import {PersonDTO} from '../../../common/entities/PersonDTO';
|
2019-03-11 03:57:27 +08:00
|
|
|
import {Utils} from '../../../common/Utils';
|
2019-01-14 00:38:39 +08:00
|
|
|
|
|
|
|
const LOG_TAG = '[PersonManager]';
|
|
|
|
|
|
|
|
export class PersonManager implements IPersonManager {
|
2019-03-11 03:57:27 +08:00
|
|
|
samplePhotos: { [key: string]: PhotoDTO } = {};
|
2019-01-14 00:38:39 +08:00
|
|
|
persons: PersonEntry[] = [];
|
|
|
|
|
2019-03-04 04:17:42 +08:00
|
|
|
async updatePerson(name: string, partialPerson: PersonDTO): Promise<PersonEntry> {
|
|
|
|
const connection = await SQLConnection.getConnection();
|
|
|
|
const repository = connection.getRepository(PersonEntry);
|
|
|
|
const person = await repository.createQueryBuilder('person')
|
|
|
|
.limit(1)
|
|
|
|
.where('person.name LIKE :name COLLATE utf8_general_ci', {name: name}).getOne();
|
|
|
|
|
|
|
|
|
|
|
|
if (typeof partialPerson.name !== 'undefined') {
|
|
|
|
person.name = partialPerson.name;
|
|
|
|
}
|
|
|
|
if (typeof partialPerson.isFavourite !== 'undefined') {
|
|
|
|
person.isFavourite = partialPerson.isFavourite;
|
|
|
|
}
|
|
|
|
await repository.save(person);
|
|
|
|
|
|
|
|
await this.loadAll();
|
|
|
|
|
|
|
|
return person;
|
|
|
|
}
|
|
|
|
|
2019-02-15 07:25:55 +08:00
|
|
|
async getSamplePhoto(name: string): Promise<PhotoDTO> {
|
2019-03-11 03:57:27 +08:00
|
|
|
if (!this.samplePhotos[name]) {
|
|
|
|
const connection = await SQLConnection.getConnection();
|
|
|
|
const rawAndEntities = await connection.getRepository(MediaEntity).createQueryBuilder('media')
|
|
|
|
.limit(1)
|
|
|
|
.leftJoinAndSelect('media.directory', 'directory')
|
|
|
|
.leftJoinAndSelect('media.metadata.faces', 'faces')
|
|
|
|
.leftJoin('faces.person', 'person')
|
|
|
|
.where('person.name LIKE :name COLLATE utf8_general_ci', {name: name}).getRawAndEntities();
|
|
|
|
|
|
|
|
if (rawAndEntities.entities.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const media: PhotoDTO = Utils.clone(rawAndEntities.entities[0]);
|
2019-02-15 07:25:55 +08:00
|
|
|
|
2019-03-11 03:57:27 +08:00
|
|
|
media.metadata.faces = [FaceRegionEntry.fromRawToDTO(rawAndEntities.raw[0])];
|
2019-02-15 07:25:55 +08:00
|
|
|
|
2019-03-11 03:57:27 +08:00
|
|
|
this.samplePhotos[name] = media;
|
|
|
|
}
|
2019-02-15 07:25:55 +08:00
|
|
|
|
2019-07-15 01:31:34 +08:00
|
|
|
return this.samplePhotos[name];
|
2019-02-15 07:25:55 +08:00
|
|
|
}
|
|
|
|
|
2019-02-05 06:46:27 +08:00
|
|
|
async loadAll(): Promise<void> {
|
|
|
|
const connection = await SQLConnection.getConnection();
|
|
|
|
const personRepository = connection.getRepository(PersonEntry);
|
|
|
|
this.persons = await personRepository.find();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-02-15 07:25:55 +08:00
|
|
|
async getAll(): Promise<PersonEntry[]> {
|
|
|
|
await this.loadAll();
|
|
|
|
return this.persons;
|
|
|
|
}
|
|
|
|
|
2019-02-05 06:46:27 +08:00
|
|
|
|
2019-01-14 00:38:39 +08:00
|
|
|
async get(name: string): Promise<PersonEntry> {
|
|
|
|
|
|
|
|
let person = this.persons.find(p => p.name === name);
|
|
|
|
if (!person) {
|
|
|
|
const connection = await SQLConnection.getConnection();
|
|
|
|
const personRepository = connection.getRepository(PersonEntry);
|
|
|
|
person = await personRepository.findOne({name: name});
|
|
|
|
if (!person) {
|
|
|
|
person = await personRepository.save(<PersonEntry>{name: name});
|
|
|
|
}
|
|
|
|
this.persons.push(person);
|
|
|
|
}
|
|
|
|
return person;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async saveAll(names: string[]): Promise<void> {
|
|
|
|
const toSave: { name: string }[] = [];
|
|
|
|
const connection = await SQLConnection.getConnection();
|
|
|
|
const personRepository = connection.getRepository(PersonEntry);
|
2019-02-05 06:46:27 +08:00
|
|
|
await this.loadAll();
|
2019-01-14 00:38:39 +08:00
|
|
|
|
|
|
|
for (let i = 0; i < names.length; i++) {
|
|
|
|
|
|
|
|
const person = this.persons.find(p => p.name === names[i]);
|
|
|
|
if (!person) {
|
|
|
|
toSave.push({name: names[i]});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (toSave.length > 0) {
|
|
|
|
for (let i = 0; i < toSave.length / 200; i++) {
|
|
|
|
await personRepository.insert(toSave.slice(i * 200, (i + 1) * 200));
|
|
|
|
}
|
2019-02-05 06:46:27 +08:00
|
|
|
this.persons = await personRepository.find();
|
2019-01-14 00:38:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-03-11 03:57:27 +08:00
|
|
|
|
|
|
|
public async onGalleryIndexUpdate() {
|
|
|
|
await this.updateCounts();
|
|
|
|
this.samplePhotos = {};
|
|
|
|
}
|
|
|
|
|
2019-02-15 07:25:55 +08:00
|
|
|
public async updateCounts() {
|
|
|
|
const connection = await SQLConnection.getConnection();
|
|
|
|
await connection.query('update person_entry set count = ' +
|
|
|
|
' (select COUNT(1) from face_region_entry where face_region_entry.personId = person_entry.id)');
|
2019-02-16 02:26:01 +08:00
|
|
|
|
|
|
|
// remove persons without photo
|
|
|
|
await connection.getRepository(PersonEntry)
|
|
|
|
.createQueryBuilder()
|
|
|
|
.where('count = 0')
|
|
|
|
.delete()
|
|
|
|
.execute();
|
2019-02-15 07:25:55 +08:00
|
|
|
}
|
|
|
|
|
2019-01-14 00:38:39 +08:00
|
|
|
}
|