Skip to content

Commit

Permalink
Fix thumbs for new uploads to private repos
Browse files Browse the repository at this point in the history
  • Loading branch information
Quicksaver committed Mar 16, 2018
1 parent a2d4267 commit d9b6cd1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/backends/github/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ export default class API {
});
}

isPrivateRepo() {
return this.request(this.repoURL)
.then(repo => repo.private)
.catch(error => {
console.error("Problem fetching repo data from GitHub");
throw error;
});
}

requestHeaders(headers = {}) {
const baseHeader = {
"Content-Type": "application/json",
Expand Down
17 changes: 15 additions & 2 deletions src/backends/github/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,21 @@ export default class GitHub {
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/${repo}/${this.branch}${path}`;
return { id: response.sha, name: value, size: fileObj.size, url, path: trimStart(path, '/') };
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'));
for (let file of files) {
if (file.sha === mediaFile.sha) {
url = file.download_url;
break;
}
}
}

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

0 comments on commit d9b6cd1

Please sign in to comment.