forked from HighEncryption/SyncPro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleDriveAdapter.cs
More file actions
242 lines (195 loc) · 8.08 KB
/
Copy pathGoogleDriveAdapter.cs
File metadata and controls
242 lines (195 loc) · 8.08 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
namespace SyncPro.Adapters.GoogleDrive
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using SyncPro.Adapters.GoogleDrive.DataModel;
using SyncPro.Configuration;
using SyncPro.Data;
using SyncPro.OAuth;
using SyncPro.Runtime;
using Item = SyncPro.Adapters.GoogleDrive.DataModel.Item;
public class GoogleDriveAdapter : AdapterBase
{
public static readonly Guid TargetTypeId = Guid.Parse("64a4cd7e-540a-47e8-bd91-bb7b84efdbb3");
private GoogleDriveClient googleDriveClient;
public User UserProfile { get; private set; }
public GoogleDriveAdapter(SyncRelationship relationship)
: base(relationship, new GoogleDriveAdapterConfiguration())
{
}
public GoogleDriveAdapter(SyncRelationship relationship, GoogleDriveAdapterConfiguration configuration)
: base(relationship, configuration)
{
}
#region Saved Properties
public TokenResponse CurrentToken { get; set; }
public string TargetItemId { get; set; }
#endregion
public override Guid GetTargetTypeId()
{
return TargetTypeId;
}
public override async Task<SyncEntry> CreateRootEntry()
{
Item rootItem = await this.googleDriveClient.GetItemById(this.TargetItemId).ConfigureAwait(false);
return this.CreateEntry(rootItem, null);
}
public override async Task<IAdapterItem> GetRootFolder()
{
Item rootItem = await this.googleDriveClient.GetItemById(this.TargetItemId).ConfigureAwait(false);
return new GoogleDriveAdapterItem(rootItem, null, this);
}
public override Task CreateItemAsync(SyncEntry entry)
{
throw new NotImplementedException();
}
public override Stream GetReadStreamForEntry(SyncEntry entry)
{
// Get the adapter entry for the parent entry
var adapterEntry = entry.AdapterEntries.First(e => e.AdapterId == this.Configuration.Id);
return new GoogleFileDownloadStream(this.googleDriveClient, adapterEntry.AdapterEntryId);
}
public override Stream GetWriteStreamForEntry(SyncEntry entry, long length)
{
throw new NotImplementedException();
}
public override void UpdateItem(EntryUpdateInfo updateInfo, SyncEntryChangedFlags changeFlags)
{
throw new NotImplementedException();
}
public override void DeleteItem(SyncEntry entry)
{
throw new NotImplementedException();
}
public override IEnumerable<IAdapterItem> GetAdapterItems(IAdapterItem folder)
{
if (folder == null)
{
Item root = this.googleDriveClient.GetItemById("root").Result;
return new List<IAdapterItem>()
{
new GoogleDriveAdapterItem(root, null, this)
};
}
GoogleDriveAdapterItem adapterItem = folder as GoogleDriveAdapterItem;
Pre.Assert(adapterItem != null, "adapterItem != null");
var items = this.googleDriveClient.GetChildItems(adapterItem).Result;
IEnumerable<GoogleDriveAdapterItem> adapterItems = items.Select(i => new GoogleDriveAdapterItem(i, folder, this));
return adapterItems;
}
public override bool IsEntryUpdated(SyncEntry childEntry, IAdapterItem adapterItem, out EntryUpdateResult result)
{
throw new NotImplementedException();
}
public override SyncEntry CreateSyncEntryForAdapterItem(IAdapterItem item, SyncEntry parentEntry)
{
GoogleDriveAdapterItem adapterItem = item as GoogleDriveAdapterItem;
Pre.Assert(adapterItem != null, "adapterItem != null");
Pre.Assert(adapterItem.Item != null, "adapterItem.Item != null");
return this.CreateEntry(adapterItem.Item, parentEntry);
}
public override byte[] GetItemHash(HashType hashType, IAdapterItem adapterItem)
{
if (hashType == HashType.MD5)
{
return adapterItem.Md5Hash;
}
return null;
}
public override async Task<byte[]> GetItemThumbnail(string itemId, string relativePath)
{
return await Task.FromResult<byte[]>(null);
}
public override void FinalizeItemWrite(Stream stream, EntryUpdateInfo updateInfo)
{
throw new NotImplementedException();
}
public async Task SignIn(AuthenticationResult authenticationResult, string codeVerifier)
{
// Use the authentication result to get an access token
this.CurrentToken = await GoogleDriveClient.GetAccessToken(authenticationResult, codeVerifier).ConfigureAwait(false);
// Create the OneDrive client, set the token refresh callback to save new tokens.
await this.InitializeClient().ConfigureAwait(false);
}
public async Task InitializeClient()
{
this.googleDriveClient = new GoogleDriveClient(this.CurrentToken);
this.googleDriveClient.TokenRefreshed += (s, e) =>
{
//this.PersistedConfiguration.CurrentToken = accessToken;
this.CurrentToken = e.NewToken;
this.SaveConfiguration();
};
// Get the user profile.
this.UserProfile = await this.googleDriveClient.GetUserInformation().ConfigureAwait(false);
}
private SyncEntry CreateEntry(Item item, SyncEntry parent)
{
SyncEntry entry = new SyncEntry
{
CreationDateTimeUtc = item.CreatedTime.ToUniversalTime(),
ModifiedDateTimeUtc = item.ModifiedTime,
Name = item.Name,
AdapterEntries = new List<SyncEntryAdapterData>()
};
if (parent != null)
{
entry.ParentEntry = parent;
entry.ParentId = parent.Id;
}
entry.AdapterEntries.Add(new SyncEntryAdapterData()
{
AdapterId = this.Configuration.Id,
SyncEntry = entry,
AdapterEntryId = item.Id
});
if (item.IsFolder)
{
entry.Type = SyncEntryType.Directory;
}
else
{
entry.Type = SyncEntryType.File;
if (this.Relationship.EncryptionMode == EncryptionMode.None ||
this.Relationship.EncryptionMode == EncryptionMode.Encrypt)
{
entry.OriginalSize = item.Size;
entry.OriginalMd5Hash = HexToBytes(item.Md5Checksum);
}
else
{
entry.EncryptedSize = item.Size;
entry.EncryptedMd5Hash = HexToBytes(item.Md5Checksum);
}
}
// TODO: FIX THIS
//if (this.Relationship.Configuration.SyncTimestamps)
//{
// entry.CreationDateTimeUtc = info.CreationTimeUtc;
// entry.ModifiedDateTimeUtc = info.LastWriteTimeUtc;
//}
entry.EntryLastUpdatedDateTimeUtc = DateTime.UtcNow;
return entry;
}
internal static byte[] ItemIdToUniqueId(string itemId)
{
Pre.ThrowIfStringNullOrWhiteSpace(itemId, nameof(itemId));
return Encoding.ASCII.GetBytes(itemId);
}
internal static string UniqueIdToItemId(byte[] uniqueId)
{
Pre.ThrowIfArgumentNull(uniqueId, nameof(uniqueId));
return Encoding.ASCII.GetString(uniqueId);
}
}
public class GoogleDriveAdapterConfiguration : AdapterConfiguration
{
public override Guid AdapterTypeId => GoogleDriveAdapter.TargetTypeId;
public override bool DirectoriesAreUniqueEntities => true;
}
}