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

HubiCBackend.cs « HubiC « Backend « Library « Duplicati - github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ea42996ca18c886b0dcafa4c7335a52f64d8e771 (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
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
183
184
185
186
187
188
189
190
191
192
193
194
//  Copyright (C) 2015, The Duplicati Team
//  http://www.duplicati.com, info@duplicati.com
//
//  This library is free software; you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as
//  published by the Free Software Foundation; either version 2.1 of the
//  License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful, but
//  WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using Duplicati.Library.Interface;
using Duplicati.Library;
using Duplicati.Library.Backend.OpenStack;
using System.Collections.Generic;

namespace Duplicati.Library.Backend.HubiC
{
    public class HubiCBackend : IBackend, IStreamingBackend
    {
        private const string AUTHID_OPTION = "authid";
        private const string HUBIC_API_URL = "https://api.hubic.com/1.0/";
        private const string HUBIC_API_CREDENTIAL_URL = HUBIC_API_URL + "account/credentials";

        private OpenStackStorage m_openstack;

        private class HubiCAuthResponse
        {
            public string token { get; set; }
            public string endpoint { get; set; }
            public DateTime? expires { get; set;}
        }

        private class OpenStackHelper : OpenStackStorage
        {
            private OAuthHelper m_helper;
            private HubiCAuthResponse m_token;

            public OpenStackHelper(string authid, string url)
                : base(url, MockOptions())
            {
                m_helper = new OAuthHelper(authid, "hubic") { AutoAuthHeader = true };
            }

            private static Dictionary<string, string> MockOptions()
            {
                var res = new Dictionary<string, string>();
                res["openstack-authuri"] = "invalid://dont-use";
                res["openstack-apikey"] = "invalid";
                res["auth-username"] = "invalid";

                return res;
            }

            private HubiCAuthResponse AuthToken
            {
                get
                {
                    if (m_token == null || (m_token.expires != null && (m_token.expires.Value - DateTime.UtcNow).TotalSeconds < 30))
                        m_token = m_helper.ReadJSONResponse<HubiCAuthResponse>(HUBIC_API_CREDENTIAL_URL);
                    return m_token;
                }
            }

            protected override string AccessToken
            {
                get { return AuthToken.token; }
            }

            protected override string SimpleStorageEndPoint
            {
                get { return AuthToken.endpoint; }
            }
        }

        public HubiCBackend()
        {
        }

        public HubiCBackend(string url, Dictionary<string, string> options)
        {
            string authid = null;
            if (options.ContainsKey(AUTHID_OPTION))
                authid = options[AUTHID_OPTION];

            m_openstack = new OpenStackHelper(authid, url);
        }

        #region IStreamingBackend implementation

        public void Put(string remotename, System.IO.Stream stream)
        {
            m_openstack.Put(remotename, stream);
        }

        public void Get(string remotename, System.IO.Stream stream)
        {
            m_openstack.Get(remotename, stream);
        }

        #endregion

        #region IBackend implementation

        public IEnumerable<IFileEntry> List()
        {
            return m_openstack.List();
        }

        public void Put(string remotename, string filename)
        {
            m_openstack.Put(remotename, filename);
        }

        public void Get(string remotename, string filename)
        {
            m_openstack.Get(remotename, filename);
        }

        public void Delete(string remotename)
        {
            m_openstack.Delete(remotename);
        }

        public void Test()
        {
            m_openstack.Test();
        }

        public void CreateFolder()
        {
            m_openstack.CreateFolder();
        }

        public string DisplayName
        {
            get
            {
                return Strings.HubiC.DisplayName;
            }
        }

        public string ProtocolKey
        {
            get
            {
                return "hubic";
            }
        }

        public System.Collections.Generic.IList<ICommandLineArgument> SupportedCommands
        {
            get {
                return new List<ICommandLineArgument>(new ICommandLineArgument[] {
                    new CommandLineArgument(AUTHID_OPTION, CommandLineArgument.ArgumentType.Password, Strings.HubiC.AuthidShort, Strings.HubiC.AuthidLong(OAuthHelper.OAUTH_LOGIN_URL("hubic"))),
                });
            }
        }

        public string Description
        {
            get
            {
                return Strings.HubiC.Description;
            }
        }

        public string DNSName
        {
            get { return null; }
        }

        #endregion

        #region IDisposable implementation

        public void Dispose()
        {
            if (m_openstack != null)
            {
                m_openstack.Dispose();
                m_openstack = null;
            }
        }

        #endregion
    }
}