diff --git a/src/backends/github/implementation.js b/src/backends/github/implementation.js index 1f82655d..bba53bda 100644 --- a/src/backends/github/implementation.js +++ b/src/backends/github/implementation.js @@ -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: + * '/repo///...' + * + * Into this: + * '/' + */ + getRepoFromResponseUrl(url) { + return url + + // -> '/repo///...' + .slice(this.api_root.length) + + // -> [ '', 'repo', '', '', ... ] + .split('/') + + // -> [ '', '' ] + .slice(2, 4) + + // -> '/' + .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) {