-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDropBoxFileData.cs
149 lines (133 loc) · 3.98 KB
/
DropBoxFileData.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
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
using DevExpress.ExpressApp.Utils;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;
using DevExpress.Xpo;
using Dropbox.Api;
using Dropbox.Api.Files;
using System;
using System.ComponentModel;
using System.IO;
namespace BIT.XAF.CloudFileData.Dropbox
{
//https://blogs.dropbox.com/developers/2015/06/introducing-a-preview-of-the-new-dropbox-net-sdk-for-api-v2/
[DefaultProperty("FileName")]
public class DropBoxFileData : BaseObject, IFileData, IEmptyCheckable
{
private string _DropboxPath;
private string _Rev;
private string fileName = "";
#if MediumTrust
private int size;
public int Size {
get { return size; }
set { SetPropertyValue("Size", ref size, value); }
}
#else
[Persistent]
private int size;
static string defaultFolder = System.Configuration.ConfigurationManager.AppSettings["DefaultFolder"];
public int Size
{
get { return size; }
}
#endif
public DropBoxFileData(Session session) : base(session) { }
private static DropboxClient GetDroboxClient()
{
string AuthKey = System.Configuration.ConfigurationManager.AppSettings["DropboxAccessToken"];
return new DropboxClient(AuthKey);
}
public virtual void LoadFromStream(string fileName, Stream stream)
{
// System.Threading.Tasks.Task task = new System.Threading.Tasks.Task(Upload);
//task.Start();
//task.Wait();
Guard.ArgumentNotNull(stream, "stream");
FileName = fileName;
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
var metada = System.Threading.Tasks.Task.Run(() => Upload(defaultFolder, FileName, bytes));
metada.Wait();
this.size = (int)metada.Result.Size;
this.DropboxPath = metada.Result.PathDisplay;
this.Rev = metada.Result.Rev;
//TODO add the sharing info
}
async System.Threading.Tasks.Task<FileMetadata> Upload(string folder, string file, byte[] bytes)
{
var dbx = GetDroboxClient();
string FinalFileName = "/" + folder + "/" + file;
//string FinalFileName = file;
FileMetadata Metadata = null;
using (MemoryStream mem = new MemoryStream(bytes))
{
//TODO add metadata
Metadata = await dbx.Files.UploadAsync(FinalFileName, WriteMode.Overwrite.Instance, false, DateTime.Now, true, null, mem);
}
return Metadata;
}
[Size(SizeAttribute.DefaultStringMappingFieldSize)]
public string DropboxPath
{
get
{
return _DropboxPath;
}
set
{
SetPropertyValue("DropboxPath", ref _DropboxPath, value);
}
}
[Size(SizeAttribute.DefaultStringMappingFieldSize)]
public string Rev
{
get
{
return _Rev;
}
set
{
SetPropertyValue("Rev", ref _Rev, value);
}
}
public virtual void SaveToStream(Stream stream)
{
var dbx = GetDroboxClient();
var TaskResponse = System.Threading.Tasks.Task.Run(() => dbx.Files.DownloadAsync(string.Format("/{0}/{1}", defaultFolder, this.fileName)));
TaskResponse.Wait();
// byte[] TheFile = await response.GetContentAsByteArrayAsync();
var TheFileTask = System.Threading.Tasks.Task.Run(() => TaskResponse.Result.GetContentAsByteArrayAsync());
TheFileTask.Wait();
var t = System.Threading.Tasks.Task.Factory.FromAsync(stream.BeginWrite, stream.EndWrite, TheFileTask.Result, 0, Size, null);
t.Wait();
stream.Flush();
}
public void Clear()
{
//TODO delete
//Content = null;
var dbx = GetDroboxClient();
var TaskResponse = System.Threading.Tasks.Task.Run(() => dbx.Files.DeleteAsync(string.Format("/{0}/{1}", defaultFolder, this.fileName)));
TaskResponse.Wait();
FileName = String.Empty;
}
public override string ToString()
{
return FileName;
}
[Size(260)]
public string FileName
{
get { return fileName; }
set { SetPropertyValue("FileName", ref fileName, value); }
}
#region IEmptyCheckable Members
[NonPersistent, MemberDesignTimeVisibility(false)]
public bool IsEmpty
{
get { return string.IsNullOrEmpty(FileName); }
}
#endregion
}
}