ns2-front-module-common
Version: 
NS2 common module
136 lines • 4.7 kB
JavaScript
import { Injectable } from "@angular/core";
import { HttpService } from "./http.service";
import { CurrentUserService } from "./current-user.service";
var StorageService = (function () {
    function StorageService(userService, http) {
        this.userService = userService;
        this.http = http;
    }
    StorageService.prototype.setStorageEndPoint = function (url) {
        this.storageEndPoint = url;
    };
    /**
     * Удаление файла со стораджа
     *
     * @param guid
     * @returns {Promise<T>}
     */
    StorageService.prototype.deleteFile = function (guid) {
        var _this = this;
        return new Promise(function (resolve, reject) {
            var url = _this.storageEndPoint + 'files/' + guid;
            _this.http.delete(url).subscribe(function (data) {
                resolve();
            }, function (err) {
                reject(err);
            });
        });
    };
    /**
     * Post file to storage
     *
     * @param file
     * @returns {Promise<T>}
     */
    StorageService.prototype.postFile = function (file) {
        var _this = this;
        return new Promise(function (resolve, reject) {
            var url = _this.storageEndPoint + 'files';
            var formData = new FormData();
            var xhr = new XMLHttpRequest();
            formData.append('data', file, file.name);
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    try {
                        if (xhr.status === 200) {
                            resolve(JSON.parse(xhr.response));
                        }
                        else {
                            var error = JSON.parse(xhr.response);
                            reject(error && error.data ? error.data : error);
                        }
                    }
                    catch (err) {
                        reject(err);
                    }
                }
            };
            xhr.open('POST', url, true);
            xhr.send(formData);
        });
    };
    /**
     * Заменить вложение
     *
     * @param guid
     * @param file
     * @returns {Promise<T>}
     */
    StorageService.prototype.changeAttachment = function (guid, file) {
        var _this = this;
        return new Promise(function (resolve, reject) {
            var url = _this.storageEndPoint + 'files/' + guid + '/change';
            var formData = new FormData();
            var xhr = new XMLHttpRequest();
            formData.append('data', file, file.name);
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    try {
                        if (xhr.status === 200) {
                            resolve(JSON.parse(xhr.response));
                        }
                        else {
                            var error = JSON.parse(xhr.response);
                            reject(error && error.data ? error.data : error);
                        }
                    }
                    catch (err) {
                        reject(err);
                    }
                }
            };
            xhr.open('POST', url, true);
            xhr.setRequestHeader(CurrentUserService.USER_TOKEN_NAME, _this.userService.getToken());
            xhr.send(formData);
        });
    };
    /**
     * Возвращает безопасный стиль
     * для масштабированной картинки
     *
     * @param {string} imageGuid
     * @param {number} width
     * @param {number} height
     * @param {string} mode
     * @param {number} quality
     * @returns {String}
     */
    StorageService.prototype.getScaledImageUrl = function (imageGuid, width, height, mode, quality) {
        if (mode === void 0) { mode = 'outbound'; }
        if (quality === void 0) { quality = 95; }
        var url = this.storageEndPoint + 'images/scale/' + imageGuid + '/' + width + '/' + height;
        url += mode ? '/' + mode : '';
        url += quality ? '/' + quality : '';
        return url;
    };
    /**
     * Возвращает путь до картинки на storage
     *
     * @param {string} imageGuid
     * @returns {string}
     */
    StorageService.prototype.getImageUrl = function (imageGuid) {
        return this.storageEndPoint + 'files/' + imageGuid;
    };
    return StorageService;
}());
export { StorageService };
StorageService.decorators = [
    { type: Injectable },
];
/** @nocollapse */
StorageService.ctorParameters = function () { return [
    { type: CurrentUserService, },
    { type: HttpService, },
]; };
//# sourceMappingURL=storage.service.js.map