-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathDropBoxCloudDetector.cs
More file actions
100 lines (89 loc) · 2.9 KB
/
Copy pathDropBoxCloudDetector.cs
File metadata and controls
100 lines (89 loc) · 2.9 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
// Copyright (c) Files Community
// Licensed under the MIT License.
using Microsoft.Extensions.Logging;
using System.IO;
using Windows.Storage;
namespace Files.App.Utils.Cloud
{
/// <summary>
/// Provides an utility for Drop Box Cloud detection.
/// </summary>
public sealed class DropBoxCloudDetector : AbstractCloudDetector
{
protected override async IAsyncEnumerable<ICloudProvider> GetProviders()
{
string? infoJsonPath = null;
// First, try website version
string websiteJsonPath = Path.Combine(UserDataPaths.GetDefault().LocalAppData, @"Dropbox\info.json");
if (File.Exists(websiteJsonPath))
{
infoJsonPath = websiteJsonPath;
App.Logger.LogInformation("Dropbox: Found website version at {Path}", LogPathHelper.RedactUserName(websiteJsonPath));
}
else
{
// Fallback: Check Store versions
string packagesPath = Path.Combine(UserDataPaths.GetDefault().LocalAppData, "Packages");
if (Directory.Exists(packagesPath))
{
var dropboxPackages = Directory.GetDirectories(packagesPath, "DropboxInc.Dropbox_*");
string? newestInfoJsonPath = null;
DateTime newestTimestamp = DateTime.MinValue;
foreach (var packageDir in dropboxPackages)
{
string storeJsonPath = Path.Combine(packageDir, @"LocalCache\Local\Dropbox\info.json");
if (File.Exists(storeJsonPath))
{
var lastWriteTime = File.GetLastWriteTime(storeJsonPath);
if (lastWriteTime > newestTimestamp)
{
newestTimestamp = lastWriteTime;
newestInfoJsonPath = storeJsonPath;
}
}
}
if (newestInfoJsonPath is not null)
{
infoJsonPath = newestInfoJsonPath;
App.Logger.LogInformation("Dropbox: Found Store version at {Path} (last modified: {Timestamp})", LogPathHelper.RedactUserName(newestInfoJsonPath), newestTimestamp);
}
}
}
if (infoJsonPath is null)
yield break;
// Parse info.json and yield providers
await foreach (var provider in ParseInfoJson(infoJsonPath))
{
yield return provider;
}
}
private async IAsyncEnumerable<ICloudProvider> ParseInfoJson(string jsonPath)
{
var configFile = await StorageFile.GetFileFromPathAsync(jsonPath);
using var jsonDoc = JsonDocument.Parse(await FileIO.ReadTextAsync(configFile));
var jsonElem = jsonDoc.RootElement;
if (jsonElem.TryGetProperty("personal", out JsonElement inner))
{
if (inner.GetProperty("path").GetString() is { Length: > 0 } dropBoxPath)
{
yield return new CloudProvider(CloudProviders.DropBox)
{
Name = "Dropbox",
SyncFolder = dropBoxPath,
};
}
}
if (jsonElem.TryGetProperty("business", out JsonElement innerBusiness))
{
if (innerBusiness.GetProperty("path").GetString() is { Length: > 0 } dropBoxPath)
{
yield return new CloudProvider(CloudProviders.DropBox)
{
Name = "Dropbox Business",
SyncFolder = dropBoxPath,
};
}
}
}
}
}