"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StorageWebservice = void 0;
var bfast_1 = require("bfast");
var StorageWebservice = (function () {
    function StorageWebservice() {
    }
    StorageWebservice.prototype.handleGetFile = function (securityController, restController, databaseController, storageController, authController, databaseAdapter, filesAdapter, options) {
        return [
            function (request, _, next) {
                request.body.applicationId = request.params.appId;
                request.body.ruleId = 'files.read';
                next();
            },
            function (rq, rs, n) { return restController
                .verifyApplicationId(rq, rs, n, options); },
            function (rq, rs, n) { return restController
                .verifyToken(rq, rs, n, securityController, options); },
            function (rq, rs, n) { return restController
                .filePolicy(rq, rs, n, databaseController, securityController, databaseAdapter, authController, options); },
            function (rq, rs, n) { return restController
                .getFile(rq, rs, n, storageController, databaseAdapter, filesAdapter, options); }
        ];
    };
    StorageWebservice.prototype.handleUploadFile = function (restController, securityController, databaseController, authController, storageController, databaseAdapter, filesAdapter, options) {
        return [
            function (request, response, next) {
                request.body.applicationId = request.params.appId;
                request.body.ruleId = 'files.save';
                next();
            },
            function (rq, rs, n) { return restController
                .verifyApplicationId(rq, rs, n, options); },
            function (rq, rs, n) { return restController
                .verifyToken(rq, rs, n, securityController, options); },
            function (rq, rs, n) { return restController
                .filePolicy(rq, rs, n, databaseController, securityController, databaseAdapter, authController, options); },
            function (rq, rs, n) { return restController
                .multipartForm(rq, rs, n, storageController, securityController, databaseAdapter, filesAdapter, options); }
        ];
    };
    StorageWebservice.prototype.handleGetThumbnail = function (restController, securityController, databaseController, authController, storageController, databaseAdapter, filesAdapter, options) {
        return [
            function (request, _, next) {
                request.body.applicationId = request.params.appId;
                request.body.ruleId = 'files.read';
                next();
            },
            function (rq, rs, n) { return restController.verifyApplicationId(rq, rs, n, options); },
            function (rq, rs, n) { return restController.verifyToken(rq, rs, n, securityController, options); },
            function (rq, rs, n) { return restController
                .filePolicy(rq, rs, n, databaseController, securityController, databaseAdapter, authController, options); },
            function (rq, rs, n) { return restController.getThumbnail(rq, rs, n, storageController, databaseAdapter, filesAdapter, options); }
        ];
    };
    StorageWebservice.prototype.handleListFiles = function (restController, securityController, databaseController, authController, storageController, databaseAdapter, filesAdapter, options) {
        return [
            function (request, _, next) {
                request.body.applicationId = request.params.appId;
                request.body.ruleId = 'files.list';
                next();
            },
            function (rq, rs, n) { return restController.verifyApplicationId(rq, rs, n, options); },
            function (rq, rs, n) { return restController.verifyToken(rq, rs, n, securityController, options); },
            function (rq, rs, n) { return restController
                .filePolicy(rq, rs, n, databaseController, securityController, databaseAdapter, authController, options); },
            function (rq, rs, n) { return restController.getAllFiles(rq, rs, n, storageController, databaseAdapter, filesAdapter, options); }
        ];
    };
    StorageWebservice.prototype.getUploadFileV2 = function (prefix) {
        if (prefix === void 0) { prefix = '/'; }
        return bfast_1.functions().onGetHttpRequest(prefix + "storage/:appId", function (request, response) {
            response.writeHead(200, { 'content-type': 'text/html' });
            response.end("\n                    <h2>With Node.js <code>\"http\"</code> module</h2>\n                    <form action=\"" + prefix + "storage/" + request.params.appId + "\" enctype=\"multipart/form-data\" method=\"post\">\n<!--                      <div>Text field title: <input type=\"text\" name=\"file\" /></div>-->\n                      <div>File: <input type=\"file\" name=\"multipleFiles\" multiple=\"multiple\" /></div>\n                      <input type=\"submit\" value=\"Upload\" />\n                    </form>\n                 ");
        });
    };
    StorageWebservice.prototype.getFileStorageV1 = function (prefix, restController, securityController, databaseController, authController, storageController, databaseAdapter, filesAdapter, options) {
        if (prefix === void 0) { prefix = '/'; }
        return bfast_1.functions().onHttpRequest(prefix + "files/:appId/:filename", this.handleGetFile(securityController, restController, databaseController, storageController, authController, databaseAdapter, filesAdapter, options));
    };
    StorageWebservice.prototype.getFileFromStorage = function (prefix, restController, securityController, databaseController, authController, storageController, databaseAdapter, filesAdapter, options) {
        if (prefix === void 0) { prefix = '/'; }
        return bfast_1.functions().onHttpRequest(prefix + "storage/:appId/file/:filename", this.handleGetFile(securityController, restController, databaseController, storageController, authController, databaseAdapter, filesAdapter, options));
    };
    StorageWebservice.prototype.getFileFromStorageV2 = function (prefix, restController, securityController, databaseController, authController, storageController, databaseAdapter, filesAdapter, options) {
        if (prefix === void 0) { prefix = '/'; }
        return bfast_1.functions().onHttpRequest(prefix + "v2/storage/:appId/file/:filename", this.handleGetFile(securityController, restController, databaseController, storageController, authController, databaseAdapter, filesAdapter, options));
    };
    StorageWebservice.prototype.geThumbnailFromStorage = function (prefix, restController, securityController, databaseController, authController, storageController, databaseAdapter, filesAdapter, options) {
        if (prefix === void 0) { prefix = '/'; }
        return bfast_1.functions().onGetHttpRequest(prefix + "storage/:appId/file/:filename/thumbnail", this.handleGetThumbnail(restController, securityController, databaseController, authController, storageController, databaseAdapter, filesAdapter, options));
    };
    StorageWebservice.prototype.geThumbnailFromStorageV2 = function (prefix, restController, securityController, databaseController, authController, storageController, databaseAdapter, filesAdapter, options) {
        if (prefix === void 0) { prefix = '/'; }
        return bfast_1.functions().onGetHttpRequest(prefix + "v2/storage/:appId/file/:filename/thumbnail", this.handleGetThumbnail(restController, securityController, databaseController, authController, storageController, databaseAdapter, filesAdapter, options));
    };
    StorageWebservice.prototype.uploadMultiPartFile = function (prefix, restController, securityController, databaseController, authController, storageController, databaseAdapter, filesAdapter, options) {
        if (prefix === void 0) { prefix = '/'; }
        return bfast_1.functions().onPostHttpRequest(prefix + "storage/:appId", this.handleUploadFile(restController, securityController, databaseController, authController, storageController, databaseAdapter, filesAdapter, options));
    };
    StorageWebservice.prototype.uploadMultiPartFileV2 = function (prefix, restController, securityController, databaseController, authController, storageController, databaseAdapter, filesAdapter, options) {
        if (prefix === void 0) { prefix = '/'; }
        return bfast_1.functions().onPostHttpRequest(prefix + "v2/storage/:appId", this.handleUploadFile(restController, securityController, databaseController, authController, storageController, databaseAdapter, filesAdapter, options));
    };
    StorageWebservice.prototype.getFilesFromStorage = function (prefix, restController, securityController, databaseController, authController, storageController, databaseAdapter, filesAdapter, options) {
        if (prefix === void 0) { prefix = '/'; }
        return bfast_1.functions().onGetHttpRequest(prefix + "storage/:appId/list", this.handleListFiles(restController, securityController, databaseController, authController, storageController, databaseAdapter, filesAdapter, options));
    };
    StorageWebservice.prototype.getFilesFromStorageV2 = function (prefix, restController, securityController, databaseController, authController, storageController, databaseAdapter, filesAdapter, options) {
        if (prefix === void 0) { prefix = '/'; }
        return bfast_1.functions().onGetHttpRequest(prefix + "v2/storage/:appId/list", this.handleListFiles(restController, securityController, databaseController, authController, storageController, databaseAdapter, filesAdapter, options));
    };
    return StorageWebservice;
}());
exports.StorageWebservice = StorageWebservice;
