OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-present eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 /* eslint-env node */ |
| 19 |
| 20 "use strict"; |
| 21 |
| 22 const fs = require("fs"); |
| 23 const https = require("https"); |
| 24 const path = require("path"); |
| 25 |
| 26 const extractZip = require("extract-zip"); |
| 27 |
| 28 function download(url, destFile) |
| 29 { |
| 30 return new Promise((resolve, reject) => |
| 31 { |
| 32 let cacheDir = path.dirname(destFile); |
| 33 if (!fs.existsSync(cacheDir)) |
| 34 fs.mkdirSync(cacheDir); |
| 35 let tempDest = destFile + "-" + process.pid; |
| 36 let writable = fs.createWriteStream(tempDest); |
| 37 |
| 38 https.get(url, response => |
| 39 { |
| 40 if (response.statusCode != 200) |
| 41 { |
| 42 reject( |
| 43 new Error(`Unexpected server response: ${response.statusCode}`)); |
| 44 response.resume(); |
| 45 return; |
| 46 } |
| 47 |
| 48 response.pipe(writable) |
| 49 .on("error", error => |
| 50 { |
| 51 writable.close(); |
| 52 fs.unlinkSync(tempDest); |
| 53 reject(error); |
| 54 }) |
| 55 .on("close", () => |
| 56 { |
| 57 writable.close(); |
| 58 fs.renameSync(tempDest, destFile); |
| 59 resolve(); |
| 60 }); |
| 61 }).on("error", reject); |
| 62 }); |
| 63 } |
| 64 |
| 65 function unzipArchive(archive, destDir) |
| 66 { |
| 67 return new Promise((resolve, reject) => |
| 68 { |
| 69 extractZip(archive, {dir: destDir}, err => |
| 70 { |
| 71 if (err) |
| 72 reject(err); |
| 73 else |
| 74 resolve(); |
| 75 }); |
| 76 }); |
| 77 } |
| 78 |
| 79 module.exports = { |
| 80 download, |
| 81 unzipArchive |
| 82 }; |
OLD | NEW |