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

114 lines
2.9 KiB
JavaScript
Raw Normal View History

2020-04-02 19:44:21 +09:00
let data;
2020-09-27 17:48:29 +03:00
const urlResolver = document.createElement('a');
2020-09-26 12:27:14 +03:00
// `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
2020-09-27 17:48:29 +03:00
// https://github.com/github/fetch/pull/92#issuecomment-512187452
2020-09-26 12:27:14 +03:00
async function fetchLocal(url, options) {
2020-09-27 17:48:29 +03:00
urlResolver.href = url;
const requestURL = urlResolver.href;
2020-09-26 12:27:14 +03:00
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest;
xhr.onload = () => {
// `file` protocol has invalid OK status
let status = xhr.status;
2020-09-27 17:48:29 +03:00
if (requestURL.startsWith('file:') && status === 0) {
2020-09-26 12:27:14 +03:00
status = 200;
}
2020-09-27 17:48:29 +03:00
/* eslint-disable-next-line compat/compat */
2020-09-26 12:27:14 +03:00
resolve(new Response(xhr.responseText, {status: status}));
2020-09-27 17:48:29 +03:00
};
2020-09-26 12:27:14 +03:00
xhr.onerror = () => {
reject(new TypeError('Local request failed'));
2020-09-27 17:48:29 +03:00
};
2020-09-26 12:27:14 +03:00
xhr.open('GET', url);
if (options && options.cache) {
xhr.setRequestHeader('Cache-Control', options.cache);
}
xhr.send(null);
});
}
async function getConfig() {
2020-04-02 19:44:21 +09:00
if (data) return Promise.resolve(data);
try {
2020-09-26 12:27:14 +03:00
const response = await fetchLocal('config.json', {
cache: 'no-cache'
});
if (!response.ok) {
throw new Error('network response was not ok');
}
data = await response.json();
2020-05-12 06:28:14 +09:00
return data;
} catch (error) {
console.warn('failed to fetch the web config file:', error);
2020-05-12 06:28:14 +09:00
return getDefaultConfig();
}
2020-05-12 06:28:14 +09:00
}
async function getDefaultConfig() {
try {
2020-09-26 12:27:14 +03:00
const response = await fetchLocal('config.template.json', {
cache: 'no-cache'
});
if (!response.ok) {
throw new Error('network response was not ok');
}
2020-08-09 14:24:59 +02:00
data = await response.json();
2020-04-02 19:50:09 +09:00
return data;
} catch (error) {
console.error('failed to fetch the default web config file:', error);
}
2020-04-02 03:51:22 +09:00
}
export function getMultiServer() {
2020-04-02 03:51:22 +09:00
return getConfig().then(config => {
return config.multiserver;
2020-04-24 13:46:57 +03:00
}).catch(error => {
2020-05-04 12:44:12 +02:00
console.log('cannot get web config:', error);
2020-04-24 13:46:57 +03:00
return false;
2020-04-02 03:51:22 +09:00
});
}
export function getServers() {
return getConfig().then(config => {
return config.servers;
}).catch(error => {
console.log('cannot get web config:', error);
return [];
});
}
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 [];
});
}