mirror of
https://github.com/Art051/immich.git
synced 2025-08-11 19:29:00 +00:00
fix(server): handle invalid coordinates (#2648)
This commit is contained in:
46
server/apps/microservices/src/utils/coordinates.spec.ts
Normal file
46
server/apps/microservices/src/utils/coordinates.spec.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { describe, it, expect } from '@jest/globals';
|
||||
import { parseLatitude, parseLongitude } from './coordinates';
|
||||
|
||||
describe('parsing latitude from string input', () => {
|
||||
it('returns null for invalid inputs', () => {
|
||||
expect(parseLatitude('')).toBeNull();
|
||||
expect(parseLatitude('NaN')).toBeNull();
|
||||
expect(parseLatitude('Infinity')).toBeNull();
|
||||
expect(parseLatitude('-Infinity')).toBeNull();
|
||||
expect(parseLatitude('90.001')).toBeNull();
|
||||
expect(parseLatitude('-90.000001')).toBeNull();
|
||||
expect(parseLatitude('1000')).toBeNull();
|
||||
expect(parseLatitude('-1000')).toBeNull();
|
||||
});
|
||||
|
||||
it('returns the numeric coordinate for valid inputs', () => {
|
||||
expect(parseLatitude('90')).toBeCloseTo(90);
|
||||
expect(parseLatitude('-90')).toBeCloseTo(-90);
|
||||
expect(parseLatitude('89.999999')).toBeCloseTo(89.999999);
|
||||
expect(parseLatitude('-89.9')).toBeCloseTo(-89.9);
|
||||
expect(parseLatitude('0')).toBeCloseTo(0);
|
||||
expect(parseLatitude('-0.0')).toBeCloseTo(-0.0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('parsing longitude from string input', () => {
|
||||
it('returns null for invalid inputs', () => {
|
||||
expect(parseLongitude('')).toBeNull();
|
||||
expect(parseLongitude('NaN')).toBeNull();
|
||||
expect(parseLongitude('Infinity')).toBeNull();
|
||||
expect(parseLongitude('-Infinity')).toBeNull();
|
||||
expect(parseLongitude('180.001')).toBeNull();
|
||||
expect(parseLongitude('-180.000001')).toBeNull();
|
||||
expect(parseLongitude('1000')).toBeNull();
|
||||
expect(parseLongitude('-1000')).toBeNull();
|
||||
});
|
||||
|
||||
it('returns the numeric coordinate for valid inputs', () => {
|
||||
expect(parseLongitude('180')).toBeCloseTo(180);
|
||||
expect(parseLongitude('-180')).toBeCloseTo(-180);
|
||||
expect(parseLongitude('179.999999')).toBeCloseTo(179.999999);
|
||||
expect(parseLongitude('-179.9')).toBeCloseTo(-179.9);
|
||||
expect(parseLongitude('0')).toBeCloseTo(0);
|
||||
expect(parseLongitude('-0.0')).toBeCloseTo(-0.0);
|
||||
});
|
||||
});
|
||||
17
server/apps/microservices/src/utils/coordinates.ts
Normal file
17
server/apps/microservices/src/utils/coordinates.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
export function parseLatitude(input: string): number | null {
|
||||
const latitude = Number.parseFloat(input);
|
||||
|
||||
if (latitude < -90 || latitude > 90 || Number.isNaN(latitude)) {
|
||||
return null;
|
||||
}
|
||||
return latitude;
|
||||
}
|
||||
|
||||
export function parseLongitude(input: string): number | null {
|
||||
const longitude = Number.parseFloat(input);
|
||||
|
||||
if (longitude < -180 || longitude > 180 || Number.isNaN(longitude)) {
|
||||
return null;
|
||||
}
|
||||
return longitude;
|
||||
}
|
||||
Reference in New Issue
Block a user