Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AppService.js « services « scripts « ngax « webroot « Server « Duplicati - github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 211fe1290a85f0a3503376cce55ca4c9fc3929e9 (plain)
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
backupApp.service('AppService', function($http, $cookies, $q, DialogService, appConfig) {
    this.apiurl = '/api/v1';
    this.proxy_url = null;

    var self = this;

    var dummyconfig = function(method, options, data, targeturl) { };
    this.proxy_config = dummyconfig;

    var setupConfig = function (method, options, data, targeturl) {
        options = options || {};
        options.method = options.method || method;
        options.responseType = options.responseType || 'json';
        options.xsrfHeaderName ='X-XSRF-Token';
        options.xsrfCookieName = 'xsrf-token';
        options.headers = options.headers || {};

        if ((method == "POST" || method == "PATCH" || method == "PUT") && options.headers['Content-Type'] == null && data != null && typeof(data) != typeof('')) {
            options.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
            options.transformRequest = function(obj) {
                var str = [];
                for(var p in obj) {
                    var arg = obj[p] == null ? '' : encodeURIComponent(obj[p]);
                    str.push(encodeURIComponent(p) + "=" + arg);
                }
                return str.join("&");
            };
        }

        // Disable cache in IE
        if (method == "GET" || method == "HEAD") {
            options.headers['Cache-Control'] = 'no-cache';
            options.headers['Pragma'] = 'no-cache';
        }
  
        if (self.proxy_config != null)
            self.proxy_config(method, options, data, targeturl);

        return options;
    };
        
    var installResponseHook = function(promise) {        
        var deferred = $q.defer();
        
        promise.then(function successCallback(response) {
            deferred.resolve(response);
        }, function errorCallback(response) {                            
            if (response.status == 401){
                DialogService.dismissAll();
                DialogService.accept('Not logged in', function () {
                    window.location = appConfig.login_url;
                });
                return;
            }
            deferred.reject(response);    
        });
        
        return deferred.promise;
    };

    this.get = function(url, options) {        
        var rurl = this.apiurl + url;
        
        return installResponseHook($http.get(this.proxy_url == null ? rurl : this.proxy_url, setupConfig('GET', options, null, rurl)));
    };
    
    this.patch = function(url, data, options) {
        var rurl = this.apiurl + url;
        return installResponseHook($http.patch(this.proxy_url == null ? rurl : this.proxy_url, data, setupConfig('PATCH', options, data, rurl)));
    };

    this.post = function(url, data, options) {
        var rurl = this.apiurl + url;
        return installResponseHook($http.post(this.proxy_url == null ? rurl : this.proxy_url, data, setupConfig('POST', options, data, rurl)));
    };

    this.put = function(url, data, options) {
        var rurl = this.apiurl + url;
        return installResponseHook($http.put(this.proxy_url == null ? rurl : this.proxy_url, data, setupConfig('PUT', options, data, rurl)));
    };

    this.delete = function(url, options) {
        var rurl = this.apiurl + url;
        return installResponseHook($http.delete(this.proxy_url == null ? rurl : this.proxy_url, setupConfig('DELETE', options, null, rurl)));
    };


    this.get_export_url = function(backupid, passphrase) {
        var rurl = this.apiurl + '/backup/' + backupid + '/export?x-xsrf-token=' + encodeURIComponent($cookies.get('xsrf-token'));
        if ((passphrase || '').trim().length > 0)
            rurl += '&passphrase=' + encodeURIComponent(passphrase);

        if (this.proxy_url != null)
            return this.proxy_url + '?x-proxy-path=' + encodeURIComponent(rurl);
        return rurl;
    };

    this.get_import_url = function(passphrase) {
        var rurl = this.apiurl + '/backups/import?x-xsrf-token=' + encodeURIComponent($cookies.get('xsrf-token'));
        if ((passphrase || '').trim().length > 0)
            rurl += '&passphrase=' + encodeURIComponent(passphrase);

        if (this.proxy_url != null)
            return this.proxy_url + '?x-proxy-path=' + encodeURIComponent(rurl);
        return rurl;
    };

    this.get_bugreport_url = function(reportid) {
        var rurl = this.apiurl + '/bugreport/' + reportid + '?x-xsrf-token=' + encodeURIComponent($cookies.get('xsrf-token'));

        if (this.proxy_url != null)
            return this.proxy_url + '?x-proxy-path=' + encodeURIComponent(rurl);

        return rurl;
    };
});