-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only download cache for system languages and English (#420)
Co-authored-by: Matthew Peveler <matt.peveler@gmail.com>
- Loading branch information
1 parent
7ad3c5a
commit d3f0309
Showing
9 changed files
with
206 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,44 @@ | ||
'use strict'; | ||
|
||
const unzip = require('node-unzip-2'); | ||
const path = require('path'); | ||
const fs = require('fs-extra'); | ||
const unzip = require('adm-zip'); | ||
const config = require('./config'); | ||
const axios = require('axios'); | ||
|
||
// Downloads the zip file from github and extracts it to folder | ||
exports.download = (path) => { | ||
const url = config.get().repository; | ||
exports.download = (loc, lang) => { | ||
// If the lang is english then keep the url simple, otherwise add language. | ||
const suffix = (lang === 'en' ? '' : '.' + lang); | ||
const url = config.get().repositoryBase + suffix + '.zip'; | ||
const folderName = path.join(loc, 'pages' + suffix); | ||
const REQUEST_TIMEOUT = 10000; | ||
|
||
// Creating the extractor | ||
const extractor = unzip.Extract({ path }); | ||
|
||
let req = axios({ | ||
return axios({ | ||
method: 'get', | ||
url: url, | ||
responseType: 'stream', | ||
headers: { 'User-Agent' : 'tldr-node-client' } | ||
}).then(function (response) { | ||
response.data.pipe(extractor); | ||
}); | ||
headers: { 'User-Agent' : 'tldr-node-client' }, | ||
timeout: REQUEST_TIMEOUT, | ||
}).then((response) => { | ||
return new Promise((resolve, reject) => { | ||
let fileName = path.join(loc, 'download_' + lang + '.zip'); | ||
|
||
return new Promise((resolve, reject) => { | ||
req.catch((err) => { | ||
reject(err); | ||
}); | ||
extractor.on('error', () => { | ||
reject(new Error('Cannot update from ' + url)); | ||
}); | ||
extractor.on('close', () => { | ||
resolve(); | ||
const writer = fs.createWriteStream(fileName); | ||
response.data.pipe(writer); | ||
|
||
writer.on('finish', () => { | ||
writer.end(); | ||
const zip = new unzip(fileName); | ||
|
||
zip.extractAllTo(folderName, true); | ||
fs.unlinkSync(fileName); | ||
resolve(); | ||
}).on('error', (err) => { | ||
reject(err); | ||
}); | ||
}); | ||
}).catch((err) => { | ||
return Promise.reject(err); | ||
}); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.