nextcloud-node-client
Version:
Nextcloud client API for node.js TypeScript applications
95 lines (94 loc) • 4.57 kB
JavaScript
;
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 });
// tslint:disable-next-line:no-var-requires
const debug = require("debug").debug("DownloadFilesCommand");
const client_1 = require("../client");
const util_1 = __importDefault(require("util"));
const fs_1 = __importDefault(require("fs"));
const command_1 = __importDefault(require("./command"));
/**
* Command to download the contents of a folder from nextcloud to local file system recursively
*/
class DownloadFolderCommand extends command_1.default {
/**
* @param {Client} client the client
* @param {DownloadFolderCommandOptions} options constructor options
*/
constructor(client, options) {
super(client);
this.sourceFolder = options.sourceFolder;
this.getTargetFileNameBeforeDownload = options.getTargetFileNameBeforeDownload;
this.filterFile = options.filterFile;
this.bytesDownloaded = 0;
}
/**
* execute the command
* @async
* @returns {Promise<void>}
*/
onExecute() {
return __awaiter(this, void 0, void 0, function* () {
this.status = client_1.CommandStatus.running;
const writeFile = util_1.default.promisify(fs_1.default.writeFile);
const mkdir = util_1.default.promisify(fs_1.default.mkdir);
try {
// determine all files to download
// it is assumed that this command will use 20% of the time
const command = new client_1.GetFilesRecursivelyCommand(this.client, { sourceFolder: this.sourceFolder, filterFile: this.filterFile });
command.execute();
// check the processing status as long as the command is running
while (command.isFinished() !== true) {
// wait a bit
this.percentCompleted = command.getPercentCompleted() / 5; // 20%
yield (() => __awaiter(this, void 0, void 0, function* () { return new Promise(resolve => setTimeout(resolve, 100)); }))();
}
this.resultMetaData.messages.concat(command.getResultMetaData().messages);
this.resultMetaData.errors.concat(command.getResultMetaData().errors);
const files = command.getFiles();
let bytesToDownload = 0;
for (const file of files) {
bytesToDownload += file.size;
}
for (const file of files) {
const targetFileName = this.getTargetFileNameBeforeDownload({ sourceFileName: file.name, targetFileName: "." + file.name });
const content = yield file.getContent();
const path = targetFileName.substring(0, targetFileName.lastIndexOf("/"));
yield mkdir(path, { recursive: true });
yield writeFile(targetFileName, content);
this.bytesDownloaded += file.size;
this.percentCompleted = (this.bytesDownloaded / bytesToDownload * 80) + 20;
}
this.resultMetaData.messages.push(files.length + " files downloaded");
}
catch (e) {
debug(e.message);
this.resultMetaData.errors.push(e.message);
}
this.percentCompleted = 100;
if (this.resultMetaData.errors.length > 0) {
this.status = client_1.CommandStatus.failed;
}
else {
this.status = client_1.CommandStatus.success;
}
return;
});
}
;
getBytesDownloaded() {
return this.bytesDownloaded;
}
}
exports.default = DownloadFolderCommand;