Skip to content

Commit

Permalink
OneDrive: added workaround for CORS issue with file downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
lostmsu committed Sep 10, 2023
1 parent 9afb96c commit fb762cd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
13 changes: 8 additions & 5 deletions js/my.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ export class Games {
static games = {};
}
export async function getGames(pc, list) {
const response = await OneDrive.makeRequest(`special/approot:/PCs/${pc}.games.json:/content`);
const items = await response.json();
if (items.hasOwnProperty('error')) {
if (items.error.code !== 'itemNotFound')
console.warn('Failed to fetch game from ' + pc, items.error);
let json = null;
try {
json = await OneDrive.download(`special/approot:/PCs/${pc}.games.json`);
} catch (e) {
console.warn('Failed to fetch game list from ' + pc, e);
return;
}
if (json === null)
return;
const items = await json.json();
for (const item of items) {
item.pc = pc;
const exe = GameID.tryGetExe(item.Uri);
Expand Down
18 changes: 16 additions & 2 deletions js/onedrive.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ export async function makeRequest(url, options) {
}
}

export async function download(url) {
// workaround for https://github.com/microsoftgraph/msgraph-sdk-javascript/issues/388
if (url.endsWith(':/content'))
throw new RangeError();
const response = await makeRequest(url + '?select=id,@microsoft.graph.downloadUrl');
if (response.status === 404)
return null;
if (!response.ok)
throw new Error('Failed to download ' + url + ': ' + response.status + ': ' + response.statusText);
const item = await response.json();
const realUrl = item['@microsoft.graph.downloadUrl'];
return await fetch(realUrl);
}

export async function login() {
const msalConfig = {
auth: {
Expand Down Expand Up @@ -82,7 +96,7 @@ export async function login() {
throw err;
}
}

localStorage.removeItem('onedrive-login-failures');

accessToken = tokenResponse.accessToken;
Expand All @@ -96,7 +110,7 @@ export async function ensureBorgTag() {
if (exists.status === 404 || (await exists.json()).size !== 73) {
const response = await makeRequest('special/approot:/' + clientID + ':/content', {
method: 'PUT',
headers: { 'Content-Type': 'text/plain' },
headers: {'Content-Type': 'text/plain'},
body: `${crypto.randomUUID()}+${crypto.randomUUID()}`
});

Expand Down

0 comments on commit fb762cd

Please sign in to comment.