Fix large files failing to load. (#1224)
This commit is contained in:
parent
b8fa934a45
commit
55a24a75c1
@ -1,6 +1,6 @@
|
|||||||
import LocalForage from "Lib/LocalForage";
|
import LocalForage from "Lib/LocalForage";
|
||||||
import { Base64 } from "js-base64";
|
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 { filterPromises, resolvePromiseProperties } from "Lib/promiseHelper";
|
||||||
import AssetProxy from "ValueObjects/AssetProxy";
|
import AssetProxy from "ValueObjects/AssetProxy";
|
||||||
import { SIMPLE, EDITORIAL_WORKFLOW, status } from "Constants/publishModes";
|
import { SIMPLE, EDITORIAL_WORKFLOW, status } from "Constants/publishModes";
|
||||||
@ -156,18 +156,33 @@ export default class API {
|
|||||||
}
|
}
|
||||||
|
|
||||||
readFile(path, sha, branch = this.branch) {
|
readFile(path, sha, branch = this.branch) {
|
||||||
const cache = sha ? LocalForage.getItem(`gh.${ sha }`) : Promise.resolve(null);
|
if (sha) {
|
||||||
return cache.then((cached) => {
|
return this.getBlob(sha);
|
||||||
if (cached) { return cached; }
|
} else {
|
||||||
|
|
||||||
return this.request(`${ this.repoURL }/contents/${ path }`, {
|
return this.request(`${ this.repoURL }/contents/${ path }`, {
|
||||||
headers: { Accept: "application/vnd.github.VERSION.raw" },
|
headers: { Accept: "application/vnd.github.VERSION.raw" },
|
||||||
params: { ref: branch },
|
params: { ref: branch },
|
||||||
cache: "no-store",
|
cache: "no-store",
|
||||||
}).then((result) => {
|
}).catch(error => {
|
||||||
if (sha) {
|
if (hasIn(error, 'message.errors') && find(error.message.errors, { code: "too_large" })) {
|
||||||
LocalForage.setItem(`gh.${ sha }`, result);
|
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;
|
return result;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user