All files / tcb-admin-node/lib/storage index.js

68.49% Statements 50/73
65.91% Branches 29/44
79.17% Functions 19/24
70.59% Lines 48/68
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182  8x 7x 1x   8x 7x     8x 8x 8x 8x 8x 8x   2x 2x         2x           2x     2x       2x               8x   1x 1x           1x 1x             1x     1x         1x 1x           8x   3x 3x           3x 3x 3x 2x             2x         1x 1x                     3x     3x         3x   3x                     8x   1x 1x 1x               1x 1x 1x                                                                   8x  
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const cloudApiRequest_1 = require("../utils/cloudApiRequest");
const httpRequest = require("../utils/httpRequest");
const fs = require("fs");
const request = require("request");
const actionName = require("../constants/actionName");
function uploadFile({ cloudPath, fileContent }, { onResponseReceived }) {
    return __awaiter(this, void 0, void 0, function* () {
        let params = {
            action: "storage.uploadFile",
            path: cloudPath,
            file: fileContent
        };
        return httpRequest({
            config: this.config,
            params,
            method: "post",
            headers: {},
            callback: (response) => {
                onResponseReceived && typeof onResponseReceived === 'function' && onResponseReceived(response);
            }
        }).then((res) => {
            Iif (res.code) {
                return res;
            }
            else {
                return {
                    fileID: res.data.fileID,
                    requestId: res.requestId
                };
            }
        });
    });
}
exports.uploadFile = uploadFile;
function deleteFile({ fileList }) {
    return __awaiter(this, void 0, void 0, function* () {
        Iif (!fileList || !Array.isArray(fileList)) {
            return Promise.reject({
                code: "INVALID_PARAM",
                message: "fileList必须是非空的数组"
            });
        }
        for (let file of fileList) {
            Iif (!file || typeof file != "string") {
                return Promise.reject({
                    code: "INVALID_PARAM",
                    message: "fileList的元素必须是非空的字符串"
                });
            }
        }
        let params = {
            FileIds: fileList
        };
        return cloudApiRequest_1.default({
            config: this.config,
            params,
            action: actionName.DeleteFiles
        }).then((res) => {
            return {
                fileList: res.DeletedFiles.map(({ FileId, Code }) => ({ fileID: FileId, code: Code })),
                requestId: res.RequestId
            };
        });
    });
}
exports.deleteFile = deleteFile;
function getTempFileURL({ fileList }) {
    return __awaiter(this, void 0, void 0, function* () {
        Iif (!fileList || !Array.isArray(fileList)) {
            return {
                code: "INVALID_PARAM",
                message: "fileList必须是非空的数组"
            };
        }
        let file_list = [];
        for (let file of fileList) {
            if (typeof file === 'object') {
                Iif (!file.hasOwnProperty("fileID") ||
                    !file.hasOwnProperty("maxAge")) {
                    return Promise.reject({
                        code: "INVALID_PARAM",
                        message: "fileList的元素必须是包含fileID和maxAge的对象"
                    });
                }
                file_list.push({
                    FileId: file.fileID,
                    TTL: file.maxAge
                });
            }
            else Eif (typeof file === 'string') {
                file_list.push({
                    FileId: file,
                });
            }
            else {
                return Promise.reject({
                    code: "INVALID_PARAM",
                    message: "fileList的元素必须是字符串"
                });
            }
        }
        let params = {
            Files: file_list
        };
        return cloudApiRequest_1.default({
            config: this.config,
            params,
            action: actionName.GetDownloadUrls
        }).then((res) => {
            return {
                fileList: res.DownloadList.map(file => {
                    return {
                        code: file.Code,
                        fileID: file.FileId,
                        tempFileURL: file.DownloadUrl
                    };
                }),
                requestId: res.RequestId
            };
        });
    });
}
exports.getTempFileURL = getTempFileURL;
function downloadFile({ fileID, tempFilePath }) {
    return __awaiter(this, void 0, void 0, function* () {
        let tmpUrl, self = this;
        const tmpUrlRes = yield this.getTempFileURL({
            fileList: [
                {
                    fileID,
                    maxAge: 600
                }
            ]
        });
        const res = tmpUrlRes.fileList[0];
        Eif (res.code != 'SUCCESS') {
            return res;
        }
        tmpUrl = res.tempFileURL;
        let req = request({
            url: tmpUrl,
            encoding: null,
            proxy: self.config.proxy
        });
        return new Promise((resolve, reject) => {
            let fileContent = Buffer.alloc(0);
            req.on('response', function (response) {
                if (response && +response.statusCode === 200) {
                    if (tempFilePath) {
                        response.pipe(fs.createWriteStream(tempFilePath));
                    }
                    else {
                        response.on('data', (data) => {
                            fileContent = Buffer.concat([fileContent, data]);
                        });
                    }
                    response.on('end', () => {
                        resolve({
                            fileContent: tempFilePath ? undefined : fileContent,
                            message: '文件下载完成'
                        });
                    });
                }
                else {
                    reject(response);
                }
            });
        });
    });
}
exports.downloadFile = downloadFile;