Fix broken new media uploads in Git Gateway. (#1221)

* Use file data instead of inferring path for new uploads.
This commit is contained in:
Caleb 2018-04-04 12:30:31 -06:00 committed by Shawn Erquhart
parent fefd8bb398
commit 5e040eec62
2 changed files with 4 additions and 45 deletions

View File

@ -30,11 +30,6 @@ export default class API {
}); });
} }
isPrivateRepo() {
return this.request(this.repoURL)
.then(repo => repo.private);
}
requestHeaders(headers = {}) { requestHeaders(headers = {}) {
const baseHeader = { const baseHeader = {
"Content-Type": "application/json", "Content-Type": "application/json",

View File

@ -106,49 +106,13 @@ export default class GitHub {
return this.api.persistFiles(entry, mediaFiles, options); 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 = {}) { async persistMedia(mediaFile, options = {}) {
try { try {
const response = await this.api.persistFiles(null, [mediaFile], options); 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 { sha, value, size, path, fileObj } = mediaFile;
const isPrivateRepo = await this.api.isPrivateRepo(); const url = URL.createObjectURL(fileObj);
if (isPrivateRepo) { return { id: sha, name: value, size: fileObj.size, url, path: trimStart(path, '/') };
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, '/') };
} }
catch(error) { catch(error) {
console.error(error); console.error(error);