LEFT | RIGHT |
(no file at all) | |
| 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 "use strict"; |
| 19 |
| 20 const fs = require("fs"); |
| 21 const https = require("https"); |
| 22 const path = require("path"); |
| 23 |
| 24 const extractZip = require("extract-zip"); |
| 25 |
| 26 function download(url, destFile) |
| 27 { |
| 28 return new Promise((resolve, reject) => |
| 29 { |
| 30 let cacheDir = path.dirname(destFile); |
| 31 if (!fs.existsSync(cacheDir)) |
| 32 fs.mkdirSync(cacheDir); |
| 33 let tempDest = destFile + "-" + process.pid; |
| 34 let writable = fs.createWriteStream(tempDest); |
| 35 |
| 36 https.get(url, response => |
| 37 { |
| 38 if (response.statusCode != 200) |
| 39 { |
| 40 reject( |
| 41 new Error(`Unexpected server response: ${response.statusCode}`)); |
| 42 response.resume(); |
| 43 return; |
| 44 } |
| 45 |
| 46 response.pipe(writable) |
| 47 .on("error", error => |
| 48 { |
| 49 writable.close(); |
| 50 fs.unlinkSync(tempDest); |
| 51 reject(error); |
| 52 }) |
| 53 .on("close", () => |
| 54 { |
| 55 writable.close(); |
| 56 fs.renameSync(tempDest, destFile); |
| 57 resolve(); |
| 58 }); |
| 59 }).on("error", reject); |
| 60 }); |
| 61 } |
| 62 |
| 63 function unzipArchive(archive, destDir) |
| 64 { |
| 65 return new Promise((resolve, reject) => |
| 66 { |
| 67 extractZip(archive, {dir: destDir}, err => |
| 68 { |
| 69 if (err) |
| 70 reject(err); |
| 71 else |
| 72 resolve(); |
| 73 }); |
| 74 }); |
| 75 } |
| 76 |
| 77 module.exports = { |
| 78 download, |
| 79 unzipArchive |
| 80 }; |
LEFT | RIGHT |