aboutsummaryrefslogtreecommitdiffstats
path: root/src/media-servers.js
blob: e2220d7f1e7c2c554f4f6feaf518b39bc2765c90 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Upload destinations are a device preference, separate from wallet sync storage.
const KEY = 'btc-wallet-media-servers';
export function parseMediaServers(text) {
  const entries = String(text).trim().split(/\s+/).filter(Boolean);
  if (!entries.length) throw new Error('invalid');
  const servers = [...new Set(entries.map((entry) => {
    let url;
    try { url = new URL(entry); } catch { throw new Error('invalid'); }
    if (url.protocol !== 'https:' || url.username || url.password || url.search || url.hash || !/^\/*$/.test(url.pathname)) throw new Error('invalid');
    return url.origin;
  }))];
  if (servers.length > 6) throw new Error('invalid');
  return servers;
}
export function customMediaServers() {
  try {
    const saved = JSON.parse(localStorage.getItem(KEY));
    return Array.isArray(saved) ? parseMediaServers(saved.join('\n')) : null;
  } catch { return null; }
}
export const mediaServers = (defaults) => customMediaServers() || defaults;
export function saveMediaServers(text) {
  const servers = parseMediaServers(text);
  localStorage.setItem(KEY, JSON.stringify(servers));
  return servers;
}
export function resetMediaServers() { localStorage.removeItem(KEY); }