Skip to content

Commit 97e4156

Browse files
committed
fixes from 0x5bfa's code review (squashed)
1 parent 798b281 commit 97e4156

File tree

1 file changed

+63
-90
lines changed

1 file changed

+63
-90
lines changed

src/Files.App/Utils/Cloud/Detector/GoogleDriveCloudDetector.cs

Lines changed: 63 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@ public sealed class GoogleDriveCloudDetector : AbstractCloudDetector
1717
{
1818
private static readonly ILogger _logger = Ioc.Default.GetRequiredService<ILogger<App>>();
1919

20-
private const string _googleDriveRegKeyName = @"Software\Google\DriveFS";
21-
22-
private const string _googleDriveRegValName = "PerAccountPreferences";
23-
24-
private const string _googleDriveRegValPropName = "value";
25-
26-
private const string _googleDriveRegValPropPropName = "mount_point_path";
20+
private const string _googleDriveRegKeyName = @"Software\Google\DriveFS";
21+
private const string _googleDriveRegValName = "PerAccountPreferences";
22+
private const string _googleDriveRegValPropName = "value";
23+
private const string _googleDriveRegValPropPropName = "mount_point_path";
2724

2825
protected override async IAsyncEnumerable<ICloudProvider> GetProviders()
2926
{
@@ -76,7 +73,7 @@ await FilesystemTasks.Wrap(() => StorageFile.GetFileFromPathAsync(Path.Combine(a
7673

7774
#if DEBUG
7875
Debug.WriteLine("YIELD RETURNING from GoogleDriveCloudDetector#GetProviders (roots): ");
79-
Debug.WriteLine("name=" + $"Google Drive ({title}); path=" + path);
76+
Debug.WriteLine($"name=Google Drive ({title}); path={path}");
8077
#endif
8178

8279
yield return new CloudProvider(CloudProviders.GoogleDrive)
@@ -97,7 +94,7 @@ await FilesystemTasks.Wrap(() => StorageFile.GetFileFromPathAsync(Path.Combine(a
9794

9895
if (!AddMyDriveToPathAndValidate(ref path))
9996
{
100-
_logger.LogWarning("Validation failed for " + path + " (media)");
97+
_logger.LogWarning($"Validation failed for {path} (media)");
10198
continue;
10299
}
103100

@@ -106,11 +103,11 @@ await FilesystemTasks.Wrap(() => StorageFile.GetFileFromPathAsync(Path.Combine(a
106103

107104
App.AppModel.GoogleDrivePath = path;
108105

109-
StorageFile? iconFile = await GetGoogleDriveIconFileAsync();
106+
var iconFile = await GetGoogleDriveIconFileAsync();
110107

111108
#if DEBUG
112109
Debug.WriteLine("YIELD RETURNING from GoogleDriveCloudDetector#GetProviders (media): ");
113-
Debug.WriteLine("name=" + title + "; path=" + path);
110+
Debug.WriteLine($"name={title}; path={path}");
114111
#endif
115112

116113
yield return new CloudProvider(CloudProviders.GoogleDrive)
@@ -124,16 +121,15 @@ await FilesystemTasks.Wrap(() => StorageFile.GetFileFromPathAsync(Path.Combine(a
124121
#if DEBUG
125122
await Inspect(database, "SELECT * FROM roots", "root_preferences db, roots table");
126123
await Inspect(database, "SELECT * FROM media", "root_preferences db, media table");
127-
await Inspect(database, "SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY 1",
128-
"root_preferences db, all tables");
124+
await Inspect(database, "SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY 1", "root_preferences db, all tables");
129125
#endif
130126

131127
await foreach (var provider in GetGoogleDriveProvidersFromRegistryAsync())
132128
{
133129

134130
#if DEBUG
135131
Debug.WriteLine("YIELD RETURNING from GoogleDriveCloudDetector#GetProviders (registry): ");
136-
Debug.WriteLine("name=" + provider.Name + "; path=" + provider.SyncFolder);
132+
Debug.WriteLine($"name={provider.Name}; path={provider.SyncFolder}");
137133
#endif
138134

139135
yield return provider;
@@ -142,81 +138,65 @@ await Inspect(database, "SELECT name FROM sqlite_master WHERE type = 'table' ORD
142138

143139
private async Task Inspect(SqliteConnection database, string sqlCommand, string contentsOf)
144140
{
145-
await using var cmdTablesAll =
146-
new SqliteCommand(sqlCommand, database);
141+
await using var cmdTablesAll = new SqliteCommand(sqlCommand, database);
147142
var reader = await cmdTablesAll.ExecuteReaderAsync();
148143
var colNamesList = Enumerable.Range(0, reader.FieldCount).Select(j => reader.GetName(j)).ToList();
149-
var i = 0;
150-
Debug.WriteLine("BEGIN LOGGING of " + contentsOf + " contents");
151-
while (reader.Read())
144+
145+
#if DEBUG
146+
Debug.WriteLine($"BEGIN LOGGING of {contentsOf} contents");
147+
#endif
148+
149+
for (int index = 0; reader.Read() is not false; index++)
152150
{
153151
var colVals = new object[reader.FieldCount];
154152
reader.GetValues(colVals);
155-
colVals.Select((val, j) => $"row {i}: column {j}: {colNamesList[j]}: {val}").ToList()
156-
.ForEach(s => Debug.WriteLine(s));
157-
i++;
153+
154+
colVals.Select((val, j) => $"row {index}: column {j}: {colNamesList[j]}: {val}")
155+
.ToList().ForEach(s => Debug.WriteLine(s));
158156
}
159-
Debug.WriteLine("END LOGGING of " + contentsOf + " contents");
160-
}
161157

162-
private ILogger GetAltLogger(string filename)
163-
{
164-
var altLogPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, filename);
165-
File.Delete(altLogPath);
166-
var factory = new LoggerFactory().AddFile(altLogPath);
167-
return factory.CreateLogger<App>();
158+
#if DEBUG
159+
Debug.WriteLine($"END LOGGING of {contentsOf} contents");
160+
#endif
168161
}
169162

170163
private JsonDocument? GetGoogleDriveRegValJson()
171-
{
172-
// This will be null if the key name is not found.
173-
using var googleDriveRegKey = Registry.CurrentUser.OpenSubKey(_googleDriveRegKeyName);
174-
175-
if (googleDriveRegKey is null)
176-
{
177-
_logger.LogWarning(
178-
"Google Drive registry key for key name `"
179-
+ _googleDriveRegKeyName
180-
+ "' not found."
181-
);
182-
return null;
183-
}
184-
185-
var googleDriveRegVal = googleDriveRegKey.GetValue(_googleDriveRegValName);
186-
187-
if (googleDriveRegVal is null)
188-
{
189-
_logger.LogWarning(
190-
"Google Drive registry value for value name `"
191-
+ _googleDriveRegValName
192-
+ "' not found."
193-
);
194-
return null;
195-
}
196-
197-
JsonDocument? googleDriveRegValueJson = null;
198-
try
199-
{
200-
googleDriveRegValueJson = JsonDocument.Parse(googleDriveRegVal.ToString() ?? "");
201-
}
202-
catch (JsonException je)
203-
{
204-
_logger.LogWarning(
205-
je,
206-
"Google Drive registry value for value name `"
207-
+ _googleDriveRegValName
208-
+ "' could not be parsed as a JsonDocument."
209-
);
210-
}
211-
212-
return googleDriveRegValueJson;
213-
}
164+
{
165+
// This will be null if the key name is not found.
166+
using var googleDriveRegKey = Registry.CurrentUser.OpenSubKey(_googleDriveRegKeyName);
167+
168+
if (googleDriveRegKey is null)
169+
{
170+
_logger.LogWarning($"Google Drive registry key for key name '{_googleDriveRegKeyName}' not found.");
171+
return null;
172+
}
173+
174+
var googleDriveRegVal = googleDriveRegKey.GetValue(_googleDriveRegValName);
175+
176+
if (googleDriveRegVal is null)
177+
{
178+
_logger.LogWarning($"Google Drive registry value for value name '{_googleDriveRegValName}' not found.");
179+
return null;
180+
}
181+
182+
JsonDocument? googleDriveRegValueJson = null;
183+
try
184+
{
185+
googleDriveRegValueJson = JsonDocument.Parse(googleDriveRegVal.ToString() ?? "");
186+
}
187+
catch (JsonException je)
188+
{
189+
_logger.LogWarning(je, $"Google Drive registry value for value name '{_googleDriveRegValName}' could not be parsed as a JsonDocument.");
190+
}
191+
192+
return googleDriveRegValueJson;
193+
}
214194

215195
private async IAsyncEnumerable<ICloudProvider> GetGoogleDriveProvidersFromRegistryAsync()
216196
{
217-
var googleDriveRegValJson = GetGoogleDriveRegValJson();
197+
var googleDriveRegValJson = GetGoogleDriveRegValJson();
218198

219-
if (googleDriveRegValJson is null)
199+
if (googleDriveRegValJson is null)
220200
yield break;
221201

222202
var googleDriveRegValJsonProperty = googleDriveRegValJson
@@ -227,11 +207,7 @@ private async IAsyncEnumerable<ICloudProvider> GetGoogleDriveProvidersFromRegist
227207
// error if you try to call EnumerateArray on its Value.
228208
if (googleDriveRegValJsonProperty.Value.ValueKind == JsonValueKind.Undefined)
229209
{
230-
_logger.LogWarning(
231-
"Root element of Google Drive registry value for value name `"
232-
+ _googleDriveRegValName
233-
+ "' was empty."
234-
);
210+
_logger.LogWarning($"Root element of Google Drive registry value for value name '{_googleDriveRegValName}' was empty.");
235211
yield break;
236212
}
237213

@@ -245,8 +221,7 @@ private async IAsyncEnumerable<ICloudProvider> GetGoogleDriveProvidersFromRegist
245221
if (!item.TryGetProperty(_googleDriveRegValPropName, out var googleDriveRegValProp))
246222
continue;
247223

248-
if (!googleDriveRegValProp.TryGetProperty(_googleDriveRegValPropPropName,
249-
out var googleDriveRegValPropProp))
224+
if (!googleDriveRegValProp.TryGetProperty(_googleDriveRegValPropPropName, out var googleDriveRegValPropProp))
250225
continue;
251226

252227
var path = googleDriveRegValPropProp.GetString();
@@ -255,7 +230,7 @@ private async IAsyncEnumerable<ICloudProvider> GetGoogleDriveProvidersFromRegist
255230

256231
if (!AddMyDriveToPathAndValidate(ref path))
257232
{
258-
_logger.LogWarning("Validation failed for " + path + " (media)");
233+
_logger.LogWarning($"Validation failed for {path} (media)");
259234
continue;
260235
}
261236

@@ -298,7 +273,7 @@ private bool AddMyDriveToPathAndValidate(ref string path)
298273
}
299274
catch (ArgumentException e)
300275
{
301-
_logger.LogWarning(e, "Could not resolve drive letter `" + path + "' to a valid drive.");
276+
_logger.LogWarning(e, $"Could not resolve drive letter '{path}' to a valid drive.");
302277
return false;
303278
}
304279

@@ -310,11 +285,9 @@ private bool AddMyDriveToPathAndValidate(ref string path)
310285
// TODO: Avoid to use Vanara (#15000)
311286
using var shellFolderBase = ShellFolderExtensions.GetShellItemFromPathOrPIDL(path) as ShellFolder;
312287
var shellFolderBaseFirst = Environment.ExpandEnvironmentVariables((
313-
shellFolderBase?.FirstOrDefault(si =>
314-
si.Name?.Equals("My Drive") ??
315-
false) as ShellLink)
316-
?.TargetPath ??
317-
"");
288+
shellFolderBase?.FirstOrDefault(si =>
289+
si.Name?.Equals("My Drive") ?? false) as ShellLink)?.TargetPath
290+
?? string.Empty);
318291

319292
#if DEBUG
320293
Debug.WriteLine("INVALID PATHS LOGGER");
@@ -330,7 +303,7 @@ private bool AddMyDriveToPathAndValidate(ref string path)
330303
path = Path.Combine(path, "My Drive");
331304
if (Directory.Exists(path))
332305
return true;
333-
_logger.LogWarning("Invalid Google Drive mount point path: " + path);
306+
_logger.LogWarning($"Invalid Google Drive mount point path: {path}");
334307
return false;
335308
}
336309
}

0 commit comments

Comments
 (0)