mirror of
https://github.com/seriousm4x/UpSnap.git
synced 2026-08-04 19:25:13 -04:00
fix: invalid cron will crash the homepage, close #975
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
import LL, { locale } from '$lib/i18n/i18n-svelte';
|
||||
import { backendUrl, permission, pocketbase } from '$lib/stores/pocketbase';
|
||||
import type { Device } from '$lib/types/device';
|
||||
import { cronRegex } from '$lib/types/device';
|
||||
import {
|
||||
faBed,
|
||||
faCircleArrowDown,
|
||||
@@ -141,10 +142,18 @@
|
||||
}
|
||||
|
||||
function getNextCronRelativeTime(expression: string, now: number) {
|
||||
const cron = cronParser.parseExpression(expression);
|
||||
return formatRelative(cron.next().toISOString(), now, {
|
||||
locale: dateFnsLocale
|
||||
});
|
||||
if (!cronRegex.test(expression)) {
|
||||
return 'Invalid cron';
|
||||
}
|
||||
|
||||
try {
|
||||
const cron = cronParser.parseExpression(expression);
|
||||
return formatRelative(cron.next().toISOString(), now, {
|
||||
locale: dateFnsLocale
|
||||
});
|
||||
} catch {
|
||||
return 'Invalid cron';
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
import LL from '$lib/i18n/i18n-svelte';
|
||||
import { permission, pocketbase } from '$lib/stores/pocketbase';
|
||||
import type { Device, Group, Port } from '$lib/types/device';
|
||||
import { cronRegex } from '$lib/types/device';
|
||||
import { faSave, faTrash, faX } from '@fortawesome/free-solid-svg-icons';
|
||||
import { onMount } from 'svelte';
|
||||
import Fa from 'svelte-fa';
|
||||
@@ -392,6 +393,7 @@
|
||||
bind:value={device.wake_cron}
|
||||
disabled={!device.wake_cron_enabled}
|
||||
required={device.wake_cron_enabled}
|
||||
pattern={cronRegex.source}
|
||||
/>
|
||||
</div>
|
||||
<div class="flex flex-col">
|
||||
@@ -584,6 +586,7 @@
|
||||
bind:value={device.shutdown_cron}
|
||||
disabled={!device.shutdown_cron_enabled}
|
||||
required={device.shutdown_cron_enabled}
|
||||
pattern={cronRegex.source}
|
||||
/>
|
||||
</div>
|
||||
<div class="flex flex-col">
|
||||
|
||||
@@ -42,3 +42,7 @@ export type Port = RecordModel & {
|
||||
export type Group = RecordModel & {
|
||||
name: string;
|
||||
};
|
||||
|
||||
export const cronRegex = new RegExp(
|
||||
/(@(annually|yearly|monthly|weekly|daily|hourly))|((((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*) ?){5,7})/
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user