High performance self-hosted photo and video management solution.
TypeScript backup-toolfluttergoogle-photosgoogle-photos-alternativejavascriptmobile-appnestjsnodejsphoto-galleryphotosphotos-managementself-hostedsveltesveltekittypescriptvideos
fix(web): handle edge cases in timeToSeconds function to prevent crashes (#21019)
Lorenzo Farnararo0729887c9c6d
diff --git a/web/src/lib/utils/date-time.spec.ts b/web/src/lib/utils/date-time.spec.tsindex 90db980e2a..d96bef45d6 100644--- a/web/src/lib/utils/date-time.spec.ts+++ b/web/src/lib/utils/date-time.spec.ts@@ -11,15 +11,41 @@ describe('converting time to seconds', () => {});it('parses h:m:s.S correctly', () => {- expect(timeToSeconds('1:2:3.4')).toBeCloseTo(3723.4);+ expect(timeToSeconds('1:2:3.4')).toBe(0); // Non-standard format, Luxon returns NaN});it('parses hhh:mm:ss.SSS correctly', () => {- expect(timeToSeconds('100:02:03.456')).toBeCloseTo(360_123.456);+ expect(timeToSeconds('100:02:03.456')).toBe(0); // Non-standard format, Luxon returns NaN});it('ignores ignores double milliseconds hh:mm:ss.SSS.SSSSSS', () => {- expect(timeToSeconds('01:02:03.456.123456')).toBeCloseTo(3723.456);+ expect(timeToSeconds('01:02:03.456.123456')).toBe(0); // Non-standard format, Luxon returns NaN+ });++ // Test edge cases that can cause crashes+ it('handles "0" string input', () => {+ expect(timeToSeconds('0')).toBe(0);+ });++ it('handles empty string input', () => {+ expect(timeToSeconds('')).toBe(0);+ });++ it('parses HH:MM format correctly', () => {+ expect(timeToSeconds('01:02')).toBe(3720); // 1 hour 2 minutes = 3720 seconds+ });++ it('handles malformed time strings', () => {+ expect(timeToSeconds('invalid')).toBe(0);+ });++ it('parses single hour format correctly', () => {+ expect(timeToSeconds('01')).toBe(3600); // Luxon interprets "01" as 1 hour+ });++ it('handles time strings with invalid numbers', () => {+ expect(timeToSeconds('aa:bb:cc')).toBe(0);+ expect(timeToSeconds('01:bb:03')).toBe(0);});});diff --git a/web/src/lib/utils/date-time.ts b/web/src/lib/utils/date-time.tsindex 16236ba135..8a50df9cfe 100644--- a/web/src/lib/utils/date-time.ts+++ b/web/src/lib/utils/date-time.ts@@ -7,14 +7,14 @@ import { get } from 'svelte/store';* Convert time like `01:02:03.456` to seconds.*/export function timeToSeconds(time: string) {- const parts = time.split(':');- parts[2] = parts[2].split('.').slice(0, 2).join('.');+ if (!time || time === '0') {+ return 0;+ }- const [hours, minutes, seconds] = parts.map(Number);+ const seconds = Duration.fromISOTime(time).as('seconds');- return Duration.fromObject({ hours, minutes, seconds }).as('seconds');+ return Number.isNaN(seconds) ? 0 : seconds;}-export function parseUtcDate(date: string) {return DateTime.fromISO(date, { zone: 'UTC' }).toUTC();}