UNPKG

voxa-cli

Version:
123 lines 5.56 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /* * Copyright (c) 2018 Rain Agency <contact@rain.agency> * Author: Rain Agency <contact@rain.agency> * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ const crypto = require("crypto"); const fs = require("fs-extra"); const googleapis_1 = require("googleapis"); const path_1 = __importDefault(require("path")); function downloadDirs(dirs, assetsRoot, key) { return __awaiter(this, void 0, void 0, function* () { const jwtClient = new googleapis_1.google.auth.JWT(key.client_email, undefined, key.private_key, [ "https://www.googleapis.com/auth/drive.metadata.readonly", "https://www.googleapis.com/auth/drive" ], key.email); const drive = googleapis_1.google.drive({ version: "v3", auth: jwtClient }); for (const dir of dirs) { const reply = yield resInFolder(drive, dir); const files = reply.data.files; if (files) { yield Promise.map(files, file => downloadResource(drive, file, assetsRoot)); } } }); } exports.downloadDirs = downloadDirs; function downloadResource(driveService, fileResource, rootPath) { return __awaiter(this, void 0, void 0, function* () { if (fileResource.mimeType === "application/vnd.google-apps.folder") { return downloadDirectoryResource(driveService, fileResource, rootPath); } // tslint:disable-next-line const getOptions = { fileId: fileResource.id, alt: "media" }; const destPath = path_1.default.join(rootPath, fileResource.name); let md5 = ""; if (yield fs.pathExists(destPath)) { md5 = yield fileMd5(destPath); } if (md5 === fileResource.md5Checksum) { return; } const writer = fs.createWriteStream(destPath); const result = (yield driveService.files.get(getOptions, { responseType: "stream" })); result.data.pipe(writer); return new Promise((resolve, reject) => { writer.on("finish", resolve); writer.on("error", reject); }); }); } function downloadDirectoryResource(driveService, fileResource, rootPath) { return __awaiter(this, void 0, void 0, function* () { const newRoot = path_1.default.join(rootPath, fileResource.name); yield fs.mkdirp(newRoot); const reply = yield resInFolder(driveService, fileResource.id); const files = reply.data.files; if (files) { yield Promise.map(files, file => downloadResource(driveService, file, newRoot)); } }); } exports.downloadDirectoryResource = downloadDirectoryResource; function resInFolder(driveService, folderId) { return __awaiter(this, void 0, void 0, function* () { // tslint:disable-next-line const listOptions = { q: `'${folderId}' in parents`, fields: "files(md5Checksum, name, mimeType, id)" }; const fileList = yield driveService.files.list(listOptions); return fileList; }); } function fileMd5(filepath) { return __awaiter(this, void 0, void 0, function* () { const fd = fs.createReadStream(filepath); const hash = crypto.createHash("md5"); hash.setEncoding("hex"); // read all file and pipe it (write it) to the hash object fd.pipe(hash); return new Promise((resolve, reject) => { hash.on("finish", () => { resolve(hash.read()); }); hash.on("error", reject); }); }); } exports.fileMd5 = fileMd5; //# sourceMappingURL=Drive.js.map