All files / src/command uploadFilesCommand.ts

100% Statements 41/41
100% Branches 8/8
100% Functions 3/3
100% Lines 41/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104  1x     1x 1x 1x                             1x                     10x 10x 10x 10x                 10x 10x 10x 10x   10x 10x 1x         10x   74x 74x 74x 73x 73x 30x 30x   43x 43x     1x     74x 74x 74x   74x 2x         1x 1x 1x   10x 8x   2x     10x   10x       70x        
// tslint:disable-next-line:no-var-requires
const debug = require("debug").debug("UploadFilesCommand");
 
import Client, { File } from "../client";
import Command, { CommandStatus } from "./command";
import util from "util";
import fs from "fs";
 
export interface SourceTargetFileNames {
    sourceFileName: string;
    targetFileName: string;
}
 
export interface UploadFilesCommandOptions {
    files: SourceTargetFileNames[];
    processFileAfterUpload?: (file: File) => Promise<void>;
}
 
/**
 * Command to upload a set or files from local file system to nextcloud
 */
export default class UploadFilesCommand extends Command {
    private files: SourceTargetFileNames[];
    private processFileAfterUpload?: (file: File) => Promise<void>;
    private bytesUploaded: number;
 
    /**
     * @param {Client} client the client
     * @param {SourceTargetFileNames[]} files the files to be uploaded
     * @param {(file: File) => void} processAfterUpload callback function to process the uploaded file
     */
    constructor(client: Client, options: UploadFilesCommandOptions) {
        super(client);
        this.files = options.files;
        this.bytesUploaded = 0;
        this.processFileAfterUpload = options.processFileAfterUpload;
    }
 
    /**
     * execute the command
     * @async
     * @returns {Promise<void>}
     */
    protected async onExecute(): Promise<void> {
        this.status = CommandStatus.running;
        try {
            const readfile = util.promisify(fs.readFile);
            let countCompleted = 0;
 
            this.percentCompleted = 0;
            if (this.files.length === 0) {
                this.percentCompleted = 100;
            }
 
            let newFile: File | null;
 
            for (const file of this.files) {
                let data: Buffer;
                newFile = null;
                try {
                    data = await readfile(file.sourceFileName);
                    try {
                        newFile = await this.client.createFile(file.targetFileName, data);
                        this.resultMetaData.messages.push(`${file.targetFileName}`);
                        this.bytesUploaded += data.length;
                    } catch (e) {
                        this.resultMetaData.errors.push(`${file.targetFileName}: ${e.message}`);
                        debug(file.targetFileName, e);
                    }
                } catch (e) {
                    this.resultMetaData.errors.push(`${file.targetFileName}: ${e.message}`);
                }
 
                countCompleted++;
                this.percentCompleted = Math.round(countCompleted / this.files.length * 100);
                debug(" completed:" + this.percentCompleted + "%");
 
                if (newFile && this.processFileAfterUpload) {
                    await this.processFileAfterUpload(newFile);
                }
            }
 
        } catch (e) {
            debug(e.message);
            this.resultMetaData.errors.push(e.message);
            this.percentCompleted = 100;
        }
        if (this.resultMetaData.errors.length > 0) {
            this.status = CommandStatus.failed;
        } else {
            this.status = CommandStatus.success;
        }
 
        debug("execute finished", this.percentCompleted, this.status);
 
        return;
    };
 
    public getBytesUploaded(): number {
        return this.bytesUploaded;
    }
 
}