2021-01-16 23:59:59 +08:00
|
|
|
import {GPSMetadata} from '../../../common/entities/PhotoDTO';
|
2021-04-06 17:32:31 +08:00
|
|
|
import * as NodeGeocoder from 'node-geocoder';
|
|
|
|
import {LocationLookupException} from '../../exceptions/LocationLookupException';
|
2021-04-18 21:48:35 +08:00
|
|
|
import {LRU} from '../../../common/Utils';
|
2021-08-08 00:09:46 +08:00
|
|
|
import {IObjectManager} from './interfaces/IObjectManager';
|
|
|
|
import {ParentDirectoryDTO} from '../../../common/entities/DirectoryDTO';
|
2021-01-16 23:59:59 +08:00
|
|
|
|
2021-08-08 00:09:46 +08:00
|
|
|
export class LocationManager implements IObjectManager {
|
|
|
|
// onNewDataVersion only need for TypeScript, otherwise the interface is not implemented.
|
|
|
|
readonly onNewDataVersion: (changedDir?: ParentDirectoryDTO) => Promise<void>;
|
2021-04-06 17:32:31 +08:00
|
|
|
readonly geocoder: NodeGeocoder.Geocoder;
|
2021-04-18 21:48:35 +08:00
|
|
|
cache = new LRU<GPSMetadata>(100);
|
2021-04-06 17:32:31 +08:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.geocoder = NodeGeocoder({provider: 'openstreetmap'});
|
|
|
|
}
|
2021-01-16 23:59:59 +08:00
|
|
|
|
|
|
|
async getGPSData(text: string): Promise<GPSMetadata> {
|
2021-04-06 17:32:31 +08:00
|
|
|
if (!this.cache.get(text)) {
|
|
|
|
|
|
|
|
const ret = await this.geocoder.geocode(text);
|
|
|
|
if (ret.length < 1) {
|
|
|
|
throw new LocationLookupException('Cannot find location:' + text, text);
|
|
|
|
}
|
|
|
|
this.cache.set(text, {
|
|
|
|
latitude: ret[0].latitude,
|
|
|
|
longitude: ret[0].longitude
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.cache.get(text);
|
2021-01-16 23:59:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|