Skip to content

Commit

Permalink
add protection against github giving is no file content
Browse files Browse the repository at this point in the history
  • Loading branch information
blurymind committed Apr 8, 2024
1 parent 6b5d99d commit e8d1eb2
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 52 deletions.
14 changes: 8 additions & 6 deletions src/js/classes/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,10 @@ export const data = {
}
};

console.log('OPENING::', { content });
// different depending on file
if (type === FILETYPE.JSON) {
content = JSON.parse(content);
content = JSON.parse(content); // todo this can fail, show error
if (!content) {
return;
}
Expand Down Expand Up @@ -1207,9 +1208,9 @@ export const data = {
const previouslyOpenedGist =
data.lastStorageHost() === 'GIST' ? data.editingName() : '';
gists.get(gists.file).then(gist => {
console.log("GOT", gist)
console.log('GOT', gist);
const gistFiles = gist.body.files;
console.log({gistFiles})
console.log({ gistFiles });
const inputOptions = {};
Object.keys(gistFiles).forEach(key => {
inputOptions[key] = key;
Expand All @@ -1225,11 +1226,12 @@ export const data = {
inputPlaceholder: 'Select a file from the gist',
showCancelButton: true,
}).then(({ value }) => {
console.log("GOT data from gist", {value, gistFiles})
if (value) {
const content = gistFiles[value].content;
console.log({content})
data.openGist(content, value);
const rawUrl = gistFiles[value].raw_url;
gists.getContentOrRaw(content, rawUrl).then(content => {
data.openGist(content, value);
});
}
});
});
Expand Down
54 changes: 34 additions & 20 deletions src/js/classes/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,48 @@ const getCloudStorage = function(type = 'gist', credentials) {
headers: {
Accept: 'application/vnd.github+json',
Authorization: `Bearer ${credentials.token}`,
'X-GitHub-Api-Version': '2022-11-28'
'X-GitHub-Api-Version': '2022-11-28',
},
})
.then(data => data.json())
.then(data => {
console.log('GOT -- ', { data });
return data.json();
})
.then(content => {
console.log('NEW from get::', { content });
return { body: content };
});
},
edit: (gistId, fileName, content) => {
console.log({gistId, fileName, content, credentials})
return fetch(
'https://api.github.com/gists/' + gistId,
{
method: 'POST',
headers: {
Accept: 'application/vnd.github+json',
Authorization: `Bearer ${credentials.token}`,
'X-GitHub-Api-Version': '2022-11-28'
},
body: JSON.stringify({
description: 'upload data from api',
public: false,
files: { [fileName]: { content } }
}),
getContentOrRaw: (content, rawUrl) => {
// sometimes github comes back empty handed for content, but has raw_url
return new Promise((resolve, reject) => {
if (!content && rawUrl) {
fetch(rawUrl)
.then(data => data.text())
.then(rawContent => {
resolve(rawContent);
});
} else {
resolve(content);
}
).then(res => res.json());
}
});
},
edit: (gistId, fileName, content) => {
console.log({ gistId, fileName, content, credentials });
return fetch('https://api.github.com/gists/' + gistId, {
method: 'POST',
headers: {
Accept: 'application/vnd.github+json',
Authorization: `Bearer ${credentials.token}`,
'X-GitHub-Api-Version': '2022-11-28',
},
body: JSON.stringify({
description: 'upload data from api',
public: false,
files: { [fileName]: { content } },
}),
}).then(res => res.json());
},
};
} else if (type === 'github') {
// todo implement
Expand Down
60 changes: 34 additions & 26 deletions src/public/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,33 +273,41 @@ export var Plugins = function(app) {
if (gistFile.language === 'JavaScript') {
console.log({ gistFile });
try {
importModuleWeb(gistFile.content, gistFile.filename).then(
importedPlugin => {
const newPlugin = importedPlugin(pluginApiMethods);
newPlugin.name = newPlugin.name || gistFile.filename;
console.log({ newPlugin }, 'loaded from ', gistFile.raw_url);
if ('dependencies' in newPlugin) {
newPlugin.dependencies.forEach(dependency => {
const scriptEle = document.createElement('script');
scriptEle.setAttribute('src', dependency);
document.body.appendChild(scriptEle);
scriptEle.addEventListener('load', () => {
console.log('File loaded', dependency);
});
app.gists
.getContentOrRaw(gistFile.content, gistFile.raw_url)
.then(content => {
importModuleWeb(content, gistFile.filename).then(
importedPlugin => {
const newPlugin = importedPlugin(pluginApiMethods);
newPlugin.name = newPlugin.name || gistFile.filename;
console.log(
{ newPlugin },
'loaded from ',
gistFile.raw_url
);
if ('dependencies' in newPlugin) {
newPlugin.dependencies.forEach(dependency => {
const scriptEle = document.createElement('script');
scriptEle.setAttribute('src', dependency);
document.body.appendChild(scriptEle);
scriptEle.addEventListener('load', () => {
console.log('File loaded', dependency);
});

scriptEle.addEventListener('error', ev => {
console.log('Error on loading file', ev);
});
});
}
registerPlugin(newPlugin);
}
);
} catch (e) {
console.error(gistFile.filename, 'Plugin failed to load', e);
scriptEle.addEventListener('error', ev => {
console.log('Error on loading file', ev);
});
});
}
registerPlugin(newPlugin);
}
);
});
} catch (e) {
console.error(gistFile.filename, 'Plugin failed to load', e);
}
}
}
});
});
});
}
}
};

0 comments on commit e8d1eb2

Please sign in to comment.