refactor(github-backend): move getMediaDisplayURL from implementation to api (#2654)

This commit is contained in:
Erez Rokah 2019-09-09 22:42:10 +03:00 committed by Shawn Erquhart
parent 56ee8441ef
commit 0baf651f33
3 changed files with 27 additions and 16 deletions

View File

@ -276,10 +276,28 @@ export default class API {
} }
} }
retrieveBlob(sha, repoURL) { fetchBlob(sha, repoURL) {
return this.request(`${repoURL}/git/blobs/${sha}`, { return this.request(
`${repoURL}/git/blobs/${sha}`,
{
headers: { Accept: 'application/vnd.github.VERSION.raw' }, headers: { Accept: 'application/vnd.github.VERSION.raw' },
}); },
response => response,
);
}
async fetchBlobContent(sha, repoURL) {
const response = await this.fetchBlob(sha, repoURL);
const text = await response.text();
return text;
}
async getMediaDisplayURL(sha) {
const response = await this.fetchBlob(sha, this.repoURL);
const blob = await response.blob();
return URL.createObjectURL(blob);
} }
getBlob(sha, { repoURL = this.repoURL } = {}) { getBlob(sha, { repoURL = this.repoURL } = {}) {
@ -288,7 +306,7 @@ export default class API {
return cached; return cached;
} }
return this.retrieveBlob(sha, repoURL).then(result => { return this.fetchBlobContent(sha, repoURL).then(result => {
localForage.setItem(`gh.${sha}`, result); localForage.setItem(`gh.${sha}`, result);
return result; return result;
}); });

View File

@ -156,7 +156,7 @@ export default class GraphQLAPI extends API {
} }
} }
async retrieveBlob(sha, repoURL) { async fetchBlobContent(sha, repoURL) {
const { owner, name } = this.getOwnerAndNameFromRepoUrl(repoURL); const { owner, name } = this.getOwnerAndNameFromRepoUrl(repoURL);
const { is_null, is_binary, text } = await this.retrieveBlobObject( const { is_null, is_binary, text } = await this.retrieveBlobObject(
owner, owner,
@ -170,7 +170,7 @@ export default class GraphQLAPI extends API {
} else if (!is_binary) { } else if (!is_binary) {
return text; return text;
} else { } else {
return super.retrieveBlob(sha, repoURL); return super.fetchBlobContent(sha, repoURL);
} }
} }

View File

@ -276,15 +276,8 @@ export default class GitHub {
async getMediaDisplayURL(displayURL) { async getMediaDisplayURL(displayURL) {
const { sha } = displayURL; const { sha } = displayURL;
const blob = await this.api.request( const mediaURL = await this.api.getMediaDisplayURL(sha);
`${this.api.repoURL}/git/blobs/${sha}`, return mediaURL;
{
headers: { Accept: 'application/vnd.github.VERSION.raw' },
},
response => response.blob(),
);
return URL.createObjectURL(blob);
} }
persistEntry(entry, mediaFiles = [], options = {}) { persistEntry(entry, mediaFiles = [], options = {}) {