From 2475af1870e3cbf852348aeced1736e6d7eb25cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Miguel?= Date: Fri, 23 Mar 2018 14:51:15 +0000 Subject: [PATCH] Fix thumbs for new uploads to private repos --- src/backends/github/API.js | 5 +++++ src/backends/github/implementation.js | 13 ++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/backends/github/API.js b/src/backends/github/API.js index 0da3f4f6..7bddd27e 100644 --- a/src/backends/github/API.js +++ b/src/backends/github/API.js @@ -30,6 +30,11 @@ export default class API { }); } + isPrivateRepo() { + return this.request(this.repoURL) + .then(repo => repo.private); + } + requestHeaders(headers = {}) { const baseHeader = { "Content-Type": "application/json", diff --git a/src/backends/github/implementation.js b/src/backends/github/implementation.js index dea02251..92a5af59 100644 --- a/src/backends/github/implementation.js +++ b/src/backends/github/implementation.js @@ -136,7 +136,18 @@ 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}`; + 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, '/') }; } catch(error) {