Skip to content

Commit

Permalink
fix(vercel): fix too_large error when file > 1M
Browse files Browse the repository at this point in the history
  • Loading branch information
lizheming committed Feb 12, 2021
1 parent b138ab5 commit 77b6452
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion packages/server/src/service/storage/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Github {
this.repo = repo;
}

// content api can only get file < 1MB
async get(filename) {
const resp = await request({
uri: 'https://api.github.com/repos/' + path.join(this.repo, 'contents', filename),
Expand All @@ -24,14 +25,50 @@ class Github {
'User-Agent': 'Waline'
},
json: true
}).catch(e => {
const isTooLarge = e.message.includes('"too_large"');
if(!isTooLarge) {
throw e;
}
return this.getLargeFile(filename);
});

return {
data: Buffer.from(resp.content, 'base64').toString('utf-8'),
sha: resp.sha
};
}

// blob api can get file larger than 1MB
async getLargeFile(filename) {
const {tree} = await request({
uri: 'https://api.github.com/repos/' + path.join(this.repo, 'git/trees/HEAD') + '?recursive=1',
headers: {
Accept: 'application/vnd.github.v3+json',
Authorization: 'token ' + this.token,
'User-Agent': 'Waline'
},
json: true
});

const file = tree.find(({path}) => path === filename);
if(!file) {
const error = new Error('NOT FOUND');
error.statusCode = 404;
throw error;
}

return request({
uri: file.url,
headers: {
Accept: 'application/vnd.github.v3+json',
Authorization: 'token ' + this.token,
'User-Agent': 'Waline'
},
json: true
});
}

async set(filename, content, {sha}) {
return request({
uri: 'https://api.github.com/repos/' + path.join(this.repo, 'contents', filename),
Expand Down

0 comments on commit 77b6452

Please sign in to comment.