Skip to content

Commit

Permalink
Fix broken new media uploads in Git Gateway. (#1221)
Browse files Browse the repository at this point in the history
* Use file data instead of inferring path for new uploads.
  • Loading branch information
tech4him1 authored and erquhart committed Apr 4, 2018
1 parent fefd8bb commit 5e040ee
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 45 deletions.
5 changes: 0 additions & 5 deletions src/backends/github/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ export default class API {
});
}

isPrivateRepo() {
return this.request(this.repoURL)
.then(repo => repo.private);
}

requestHeaders(headers = {}) {
const baseHeader = {
"Content-Type": "application/json",
Expand Down
44 changes: 4 additions & 40 deletions src/backends/github/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,49 +106,13 @@ 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;
let url = `https://raw.githubusercontent.com/${repo}/${this.branch}${path}`;

// Assets uploaded to private repos will need valid access tokens.
const isPrivateRepo = await this.api.isPrivateRepo();
if (isPrivateRepo) {
const files = await this.api.listFiles(this.config.get('media_folder'));
const file = files.find(f => (f.sha === mediaFile.sha));
if (file) {
url = file.download_url;
}
}

return { id: mediaFile.sha, name: value, size: fileObj.size, url, path: trimStart(path, '/') };

const { sha, value, size, path, fileObj } = mediaFile;
const url = URL.createObjectURL(fileObj);
return { id: sha, name: value, size: fileObj.size, url, path: trimStart(path, '/') };
}
catch(error) {
console.error(error);
Expand Down

0 comments on commit 5e040ee

Please sign in to comment.