Skip to content

Commit e55b9fe

Browse files
authored
Re-add support for MEGA cloud drive (#6076)
1 parent 65ed86e commit e55b9fe

File tree

9 files changed

+98
-29
lines changed

9 files changed

+98
-29
lines changed

Files.Launcher/MessageHandlers/NetworkDrivesHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public async Task ParseArgumentsAsync(NamedPipeServerStream connection, Dictiona
2929
case "GetOneDriveAccounts":
3030
try
3131
{
32-
var oneDriveAccountsKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\OneDrive\Accounts", false);
32+
using var oneDriveAccountsKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\OneDrive\Accounts");
3333

3434
if (oneDriveAccountsKey == null)
3535
{
@@ -61,7 +61,7 @@ public async Task ParseArgumentsAsync(NamedPipeServerStream connection, Dictiona
6161
case "GetSharePointSyncLocationsFromOneDrive":
6262
try
6363
{
64-
using var oneDriveAccountsKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\OneDrive\Accounts", false);
64+
using var oneDriveAccountsKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\OneDrive\Accounts");
6565

6666
if (oneDriveAccountsKey == null)
6767
{
Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,33 @@
11
using Files.Enums;
2+
using System;
23

34
namespace Files.Filesystem.Cloud
45
{
5-
public class CloudProvider
6+
public class CloudProvider : IEquatable<CloudProvider>
67
{
78
public CloudProviders ID { get; set; }
89

910
public string Name { get; set; }
1011

1112
public string SyncFolder { get; set; }
13+
14+
public override int GetHashCode()
15+
{
16+
return $"{ID}|{SyncFolder}".GetHashCode();
17+
}
18+
19+
public override bool Equals(object obj)
20+
{
21+
if (obj is CloudProvider other)
22+
{
23+
return Equals(other);
24+
}
25+
return base.Equals(obj);
26+
}
27+
28+
public bool Equals(CloudProvider other)
29+
{
30+
return other != null && other.ID == ID && other.SyncFolder == SyncFolder;
31+
}
1232
}
1333
}

Files/Filesystem/Cloud/CloudProviderController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public async Task<List<CloudProvider>> DetectInstalledCloudProvidersAsync()
3131

3232
await Task.WhenAll(tasks);
3333

34-
return tasks.SelectMany(o => o.Result).Distinct().ToList();
34+
return tasks.SelectMany(o => o.Result).OrderBy(o => o.ID.ToString()).ThenBy(o => o.Name).Distinct().ToList();
3535
}
3636
}
3737
}

Files/Filesystem/Cloud/Providers/BoxCloudProvider.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ public async Task<IList<CloudProvider>> DetectAsync()
3030
if (!string.IsNullOrEmpty(syncPath))
3131
{
3232
return new[] { new CloudProvider()
33-
{
34-
ID = CloudProviders.Box,
35-
Name = "Box",
36-
SyncFolder = syncPath
37-
}
33+
{
34+
ID = CloudProviders.Box,
35+
Name = "Box",
36+
SyncFolder = syncPath
37+
}
3838
};
3939
}
4040
}

Files/Filesystem/Cloud/Providers/GoogleDriveCloudProvider.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ public async Task<IList<CloudProvider>> DetectAsync()
1616
{
1717
// Google Drive's sync database can be in a couple different locations. Go find it.
1818
string appDataPath = UserDataPaths.GetDefault().LocalAppData;
19-
string dbPath = @"Google\Drive\user_default\sync_config.db";
19+
string dbPath = @"Google\DriveFS\root_preference_sqlite.db";
2020
var configFile = await StorageFile.GetFileFromPathAsync(Path.Combine(appDataPath, dbPath));
2121
await configFile.CopyAsync(ApplicationData.Current.TemporaryFolder, "google_drive.db", NameCollisionOption.ReplaceExisting);
2222
var syncDbPath = Path.Combine(ApplicationData.Current.TemporaryFolder.Path, "google_drive.db");
2323

2424
// Build the connection and sql command
2525
SQLitePCL.Batteries_V2.Init();
2626
using (var con = new SqliteConnection($"Data Source='{syncDbPath}'"))
27-
using (var cmd = new SqliteCommand("select * from data where entry_key='root_config__0'", con)) //local_sync_root_path
27+
using (var cmd = new SqliteCommand("select * from roots", con))
2828
{
2929
// Open the connection and execute the command
3030
con.Open();
@@ -34,7 +34,7 @@ public async Task<IList<CloudProvider>> DetectAsync()
3434
while (reader.Read())
3535
{
3636
// Extract the data from the reader
37-
string path = reader["data_value"]?.ToString();
37+
string path = reader["last_seen_absolute_path"]?.ToString();
3838
if (string.IsNullOrWhiteSpace(path))
3939
{
4040
return Array.Empty<CloudProvider>();
@@ -55,14 +55,8 @@ public async Task<IList<CloudProvider>> DetectAsync()
5555
SyncFolder = path
5656
};
5757

58-
if (!folder.Name.Contains("Google"))
59-
{
60-
googleCloud.Name = $"Google Drive ({folder.Name})";
61-
}
62-
else
63-
{
64-
googleCloud.Name = "Google Drive";
65-
}
58+
string title = reader["title"]?.ToString() ?? folder.Name;
59+
googleCloud.Name = $"Google Drive ({title})";
6660

6761
results.Add(googleCloud);
6862
}
Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System;
1+
using Files.Enums;
2+
using Microsoft.Win32;
3+
using System;
24
using System.Collections.Generic;
35
using System.Threading.Tasks;
46

@@ -8,8 +10,64 @@ public class MegaCloudProvider : ICloudProviderDetector
810
{
911
public async Task<IList<CloudProvider>> DetectAsync()
1012
{
11-
// MEGA not supported anymore
12-
return await Task.FromResult(Array.Empty<CloudProvider>());
13+
try
14+
{
15+
return await Task.Run(() => DetectFromRegistry());
16+
}
17+
catch
18+
{
19+
// Not detected
20+
return Array.Empty<CloudProvider>();
21+
}
22+
}
23+
24+
private IList<CloudProvider> DetectFromRegistry()
25+
{
26+
var results = new List<CloudProvider>();
27+
using var clsidKey = Registry.ClassesRoot.OpenSubKey(@"CLSID");
28+
foreach (var subKeyName in clsidKey.GetSubKeyNames())
29+
{
30+
using var subKey = clsidKey.OpenSubKey(subKeyName);
31+
if ((int?)subKey.GetValue("System.IsPinnedToNameSpaceTree") == 1)
32+
{
33+
using var namespaceKey = Registry.CurrentUser.OpenSubKey($@"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace\{subKeyName}");
34+
var driveType = (string)namespaceKey?.GetValue("");
35+
if (driveType == null)
36+
{
37+
continue;
38+
}
39+
40+
using var bagKey = subKey.OpenSubKey(@"Instance\InitPropertyBag");
41+
var syncedFolder = (string)bagKey?.GetValue("TargetFolderPath");
42+
if (syncedFolder == null)
43+
{
44+
continue;
45+
}
46+
47+
// Also works for OneDrive, Box, Amazon Drive, iCloudDrive, Dropbox
48+
CloudProviders? driveID = driveType switch
49+
{
50+
"MEGA" => CloudProviders.Mega,
51+
_ => null
52+
};
53+
if (driveID == null)
54+
{
55+
continue;
56+
}
57+
58+
results.Add(new CloudProvider()
59+
{
60+
ID = driveID.Value,
61+
Name = driveID switch
62+
{
63+
CloudProviders.Mega => $"MEGA ({System.IO.Path.GetFileName(syncedFolder.TrimEnd('\\'))})",
64+
_ => null
65+
},
66+
SyncFolder = syncedFolder
67+
});
68+
}
69+
}
70+
return results;
1371
}
1472
}
1573
}

Files/Filesystem/Cloud/Providers/OneDriveCloudProvider.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ public async Task<IList<CloudProvider>> DetectAsync()
2626
{
2727
var results = new List<CloudProvider>();
2828
foreach (var key in response.Keys
29-
.Where(k => k != "Count" && k != "RequestID")
30-
.OrderByDescending(o => string.Equals(o, "OneDrive", StringComparison.OrdinalIgnoreCase))
31-
.ThenBy(o => o))
29+
.Where(k => k != "Count" && k != "RequestID"))
3230
{
3331
results.Add(new CloudProvider()
3432
{

Files/Filesystem/Cloud/Providers/OneDriveSharePointCloudProvider.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ public async Task<IList<CloudProvider>> DetectAsync()
2626
{
2727
var results = new List<CloudProvider>();
2828
foreach (var key in response.Keys
29-
.Where(k => k != "Count" && k != "RequestID")
30-
.OrderBy(o => o))
29+
.Where(k => k != "Count" && k != "RequestID"))
3130
{
3231
results.Add(new CloudProvider()
3332
{

Files/ViewModels/ItemViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1430,7 +1430,7 @@ public async Task<int> EnumerateItemsFromStandardFolderAsync(string path, Type s
14301430

14311431
BaseStorageFolder rootFolder = null;
14321432

1433-
if (FolderHelpers.CheckFolderAccessWithWin32(path))
1433+
if (!enumFromStorageFolder && FolderHelpers.CheckFolderAccessWithWin32(path))
14341434
{
14351435
// Will enumerate with FindFirstFileExFromApp, rootFolder only used for Bitlocker
14361436
currentStorageFolder = null;

0 commit comments

Comments
 (0)