fix(github-backend): load media URLs via API (#2817)

This commit is contained in:
Erez Rokah 2019-10-28 22:00:42 +02:00 committed by Shawn Erquhart
parent 2841ff9ffe
commit eaeaf44832
2 changed files with 14 additions and 16 deletions

View File

@ -293,9 +293,15 @@ export default class API {
return text;
}
async getMediaDisplayURL(sha) {
async getMediaDisplayURL(sha, path) {
const response = await this.fetchBlob(sha, this.repoURL);
const blob = await response.blob();
let blob;
if (path.match(/.svg$/)) {
const svg = await response.text();
blob = new Blob([svg], { type: 'image/svg+xml' });
} else {
blob = await response.blob();
}
return URL.createObjectURL(blob);
}

View File

@ -274,25 +274,17 @@ export default class GitHub {
getMedia() {
return this.api.listFiles(this.config.get('media_folder')).then(files =>
files.map(({ sha, name, size, download_url, path }) => {
if (download_url) {
const url = new URL(download_url);
if (url.pathname.match(/.svg$/)) {
url.search += (url.search.slice(1) === '' ? '?' : '&') + 'sanitize=true';
}
// if 'displayURL' is a string it will be loaded as is
return { id: sha, name, size, displayURL: url.href, path };
} else {
// if 'displayURL' is not a string it will be loaded using getMediaDisplayURL
return { id: sha, name, size, displayURL: { sha }, path };
}
files.map(({ sha, name, size, path }) => {
// load media using getMediaDisplayURL to avoid token expiration with GitHub raw content urls
// for private repositories
return { id: sha, name, size, displayURL: { sha, path }, path };
}),
);
}
async getMediaDisplayURL(displayURL) {
const { sha } = displayURL;
const mediaURL = await this.api.getMediaDisplayURL(sha);
const { sha, path } = displayURL;
const mediaURL = await this.api.getMediaDisplayURL(sha, path);
return mediaURL;
}