UNPKG

nestjs-basic-ftp

Version:
118 lines 4.26 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FtpService = void 0; const common_1 = require("@nestjs/common"); const basic_ftp_1 = require("basic-ftp"); const injection_token_1 = require("./application/injection.token"); let FtpService = class FtpService { constructor(_options) { this._options = _options; this.logger = new common_1.Logger('NestJs-Basic-FTP', { timestamp: true }); this.logger.log('Starting module', 'FtpService initialized'); this._ftpClient = new basic_ftp_1.Client(); this._ftpClient.ftp.log = (message) => { if (this._options.verbose) { this.logger.debug(message); } }; } async list(path) { try { await this.access(); return await this._ftpClient.list(path); } catch (err) { this._ftpClient.close(); throw err; } finally { this._ftpClient.close(); } } async downloadTo(destination, fromRemotePath, startAt) { try { await this.access(); return await this._ftpClient.downloadTo(destination, fromRemotePath, startAt); } finally { this._ftpClient.close(); } } async downloadToDir(localDirPath, remoteDirPath) { try { await this.access(); return await this._ftpClient.downloadToDir(localDirPath, remoteDirPath); } finally { this._ftpClient.close(); } } async uploadFrom(source, toRemotePath, options) { try { await this.access(); return await this._ftpClient.uploadFrom(source, toRemotePath, options); } finally { this._ftpClient.close(); } } async remove(path, ignoreErrorCodes) { try { await this.access(); return await this._ftpClient.remove(path, ignoreErrorCodes); } finally { this._ftpClient.close(); } } async size(path) { try { await this.access(); return await this._ftpClient.size(path); } finally { this._ftpClient.close(); } } trackProgress(fileListing) { if (fileListing) { this._ftpClient.trackProgress((info) => { const currentFile = fileListing.find((file) => file.name === info.name); if (currentFile) { this.logger.debug(`${Math.floor((info.bytesOverall * 100) / currentFile.size)} % of ${info.name} file.`); } }); } else { this._ftpClient.trackProgress((info) => { this.logger.debug(info); }); } } async access() { await this._ftpClient.access(this._options); if (this._options.availableListCommands) { this._ftpClient.availableListCommands = this._options.availableListCommands; } } }; FtpService = __decorate([ (0, common_1.Injectable)(), __param(0, (0, common_1.Inject)(injection_token_1.InjectionToken.FTP_CONFIG)), __metadata("design:paramtypes", [Object]) ], FtpService); exports.FtpService = FtpService; //# sourceMappingURL=ftp.service.js.map