Skip to content

Commit

Permalink
Fix large files failing to load. (decaporg#1224)
Browse files Browse the repository at this point in the history
  • Loading branch information
tech4him1 authored and brianlmacdonald committed May 23, 2018
1 parent f8eb9ba commit 3fdad90
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/backends/github/API.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import LocalForage from "Lib/LocalForage";
import { Base64 } from "js-base64";
import { uniq, initial, last, get, find } from "lodash";
import { uniq, initial, last, get, find, hasIn } from "lodash";
import { filterPromises, resolvePromiseProperties } from "Lib/promiseHelper";
import AssetProxy from "ValueObjects/AssetProxy";
import { SIMPLE, EDITORIAL_WORKFLOW, status } from "Constants/publishModes";
Expand Down Expand Up @@ -156,18 +156,33 @@ export default class API {
}

readFile(path, sha, branch = this.branch) {
const cache = sha ? LocalForage.getItem(`gh.${ sha }`) : Promise.resolve(null);
return cache.then((cached) => {
if (cached) { return cached; }

if (sha) {
return this.getBlob(sha);
} else {
return this.request(`${ this.repoURL }/contents/${ path }`, {
headers: { Accept: "application/vnd.github.VERSION.raw" },
params: { ref: branch },
cache: "no-store",
}).then((result) => {
if (sha) {
LocalForage.setItem(`gh.${ sha }`, result);
}).catch(error => {
if (hasIn(error, 'message.errors') && find(error.message.errors, { code: "too_large" })) {
const dir = path.split('/').slice(0, -1).join('/');
return this.listFiles(dir)
.then(files => files.find(file => file.path === path))
.then(file => this.getBlob(file.sha));
}
throw error;
});
}
}

getBlob(sha) {
return LocalForage.getItem(`gh.${sha}`).then(cached => {
if (cached) { return cached; }

return this.request(`${this.repoURL}/git/blobs/${sha}`, {
headers: { Accept: "application/vnd.github.VERSION.raw" },
}).then(result => {
LocalForage.setItem(`gh.${sha}`, result);
return result;
});
});
Expand Down

0 comments on commit 3fdad90

Please sign in to comment.