diff options
author | 2021-08-15 08:35:57 +0000 | |
---|---|---|
committer | 2021-08-15 14:05:57 +0530 | |
commit | ab213dd0a1e51699aae492f8b546e4a311fcb97d (patch) | |
tree | 2dbd59ee85db4899f78f660432190a2df356e8e8 /src/helpers/url-helpers.ts | |
parent | fix: Fixed the 'Changelog' menu item to point to the correct branch (diff) | |
download | ferdium-app-ab213dd0a1e51699aae492f8b546e4a311fcb97d.tar.gz ferdium-app-ab213dd0a1e51699aae492f8b546e4a311fcb97d.tar.zst ferdium-app-ab213dd0a1e51699aae492f8b546e4a311fcb97d.zip |
Initial plumbing and conversion of a simple javascript to typescript (#1790)
* initial conversion of a simple script
* Moved some of the 'gulp' and related npm modules from being runtime dependencies to development dependencies.
Diffstat (limited to 'src/helpers/url-helpers.ts')
-rw-r--r-- | src/helpers/url-helpers.ts | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/helpers/url-helpers.ts b/src/helpers/url-helpers.ts new file mode 100644 index 000000000..23e8fab29 --- /dev/null +++ b/src/helpers/url-helpers.ts | |||
@@ -0,0 +1,36 @@ | |||
1 | // This is taken from: https://benjamin-altpeter.de/shell-openexternal-dangers/ | ||
2 | |||
3 | import { URL } from 'url'; | ||
4 | import { ensureDirSync } from 'fs-extra'; | ||
5 | import { shell } from 'electron'; | ||
6 | |||
7 | import { ALLOWED_PROTOCOLS } from '../config'; | ||
8 | |||
9 | const debug = require('debug')('Ferdi:Helpers:url'); | ||
10 | |||
11 | export function isValidExternalURL(url: string) { | ||
12 | let parsedUrl: URL; | ||
13 | try { | ||
14 | parsedUrl = new URL(url); | ||
15 | } catch (_) { | ||
16 | return false; | ||
17 | } | ||
18 | |||
19 | const isAllowed = ALLOWED_PROTOCOLS.includes(parsedUrl.protocol); | ||
20 | |||
21 | debug('protocol check is', isAllowed, 'for:', url); | ||
22 | |||
23 | return isAllowed; | ||
24 | } | ||
25 | |||
26 | export async function openPath(folderName: string) { | ||
27 | ensureDirSync(folderName); | ||
28 | shell.openPath(folderName); | ||
29 | } | ||
30 | |||
31 | // TODO: Need to verify and fix/remove the skipping logic. Ideally, we should never skip this check | ||
32 | export function openExternalUrl(url: string, skipValidityCheck: boolean = false) { | ||
33 | if (skipValidityCheck || isValidExternalURL(url)) { | ||
34 | shell.openExternal(url); | ||
35 | } | ||
36 | } | ||