-
Notifications
You must be signed in to change notification settings - Fork 0
/
RunTimeContext.cs
67 lines (58 loc) · 2.41 KB
/
RunTimeContext.cs
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
using System;
using System.IO;
using AdysTech.CredentialManager;
using Microsoft.Office.Interop.Outlook;
using ownCloud.Outlook.Enums;
using RestSharp;
using RestSharp.Authenticators;
using WebDav;
namespace ownCloud.Outlook
{
/// <summary>
/// A singleton that contains user info
/// </summary>
public class RuntimeContext
{
private static RuntimeContext _instance;
private static IWebDavClient _webDavClient;
// TODO after changing config or credentials recreate refresh client
private static IWebDavClient WebDavClient
{
get
{
if (_webDavClient != null)
{
return _webDavClient;
}
var config = ConfigManager.Read();
var credential = CredentialManager.GetCredentials(Constants.AddInName);
var parameters = new WebDavClientParams
{
BaseAddress = new Uri(config.Server),
Credentials = credential,
};
_webDavClient = new WebDavClient(parameters);
return _webDavClient;
}
}
public static RuntimeContext Instance => _instance ?? (_instance = new RuntimeContext());
public string UploadAttachment(Attachment attachment)
{
var credential = CredentialManager.GetCredentials(Constants.AddInName);
var config = ConfigManager.Read();
WebDavClient.PutFile(string.Concat($"remote.php/dav/files/{credential.UserName}/", attachment.FileName), File.OpenRead(attachment.GetTemporaryFilePath())).Wait();
var client = new RestClient($"{config.Server}/ocs/v1.php/apps/files_sharing/api/v1")
{
Authenticator = new HttpBasicAuthenticator(credential.UserName, credential.Password)
};
var request = new RestRequest("shares")
.AddParameter("path", "/" + attachment.FileName)
.AddParameter("shareType", (int) ShareType.Public)
.AddParameter("permissions", (int) PermissionType.Read)
.AddParameter("password", 123)
.AddParameter("name", attachment.FileName);
var response = client.Post<SharedItem>(request);
return response.IsSuccessful ? response.Data.Url : $"Could't upload file: {response.ErrorMessage}";
}
}
}