1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/scripts/settings/webSettings.js
2020-09-26 12:27:14 +03:00

96 lines
2.4 KiB
JavaScript

let data;
// `fetch` with `file:` support
// Recent browsers seem to support `file` protocol under some conditions.
// Based on https://github.com/github/fetch/pull/92#issuecomment-174730593
async function fetchLocal(url, options) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest;
xhr.onload = () => {
// `file` protocol has invalid OK status
let status = xhr.status;
if (xhr.responseURL.startsWith('file:') && status === 0) {
status = 200;
}
resolve(new Response(xhr.responseText, {status: status}));
}
xhr.onerror = () => {
reject(new TypeError('Local request failed'));
}
xhr.open('GET', url);
if (options && options.cache) {
xhr.setRequestHeader('Cache-Control', options.cache);
}
xhr.send(null);
});
}
async function getConfig() {
if (data) return Promise.resolve(data);
try {
const response = await fetchLocal('config.json', {
cache: 'no-cache'
});
if (!response.ok) {
throw new Error('network response was not ok');
}
data = await response.json();
return data;
} catch (error) {
console.warn('failed to fetch the web config file:', error);
return getDefaultConfig();
}
}
async function getDefaultConfig() {
try {
const response = await fetchLocal('config.template.json', {
cache: 'no-cache'
});
if (!response.ok) {
throw new Error('network response was not ok');
}
data = await response.json();
return data;
} catch (error) {
console.error('failed to fetch the default web config file:', error);
}
}
export function getMultiServer() {
return getConfig().then(config => {
return config.multiserver;
}).catch(error => {
console.log('cannot get web config:', error);
return false;
});
}
export function getThemes() {
return getConfig().then(config => {
return config.themes;
}).catch(error => {
console.log('cannot get web config:', error);
return [];
});
}
export function getPlugins() {
return getConfig().then(config => {
return config.plugins;
}).catch(error => {
console.log('cannot get web config:', error);
return [];
});
}