Skip to content

Commit

Permalink
fix image not shown after upload for git gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
erquhart committed Nov 11, 2017
1 parent e00c396 commit 9c7c0ae
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/backends/github/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,37 @@ export default class GitHub {
return this.api.persistFiles(entry, mediaFiles, options);
}

/**
* Pulls repo info from a `repos` response url property.
*
* Turns this:
* '<api_root>/repo/<username>/<repo>/...'
*
* Into this:
* '<username>/<repo>'
*/
getRepoFromResponseUrl(url) {
return url

// -> '/repo/<username>/<repo>/...'
.slice(this.api_root.length)

// -> [ '', 'repo', '<username>', '<repo>', ... ]
.split('/')

// -> [ '<username>', '<repo>' ]
.slice(2, 4)

// -> '<username>/<repo>'
.join('/');
}

async persistMedia(mediaFile, options = {}) {
try {
const response = await this.api.persistFiles(null, [mediaFile], options);
const repo = this.repo || this.getRepoFromResponseUrl(response.url);
const { value, size, path, fileObj } = mediaFile;
const url = `https://raw.githubusercontent.com/${this.repo}/${this.branch}${path}`;
const url = `https://raw.githubusercontent.com/${repo}/${this.branch}${path}`;
return { id: response.sha, name: value, size: fileObj.size, url, path: trimStart(path, '/') };
}
catch(error) {
Expand Down

0 comments on commit 9c7c0ae

Please sign in to comment.