Skip to content
50 changes: 45 additions & 5 deletions src/Files.App/Utils/Cloud/Detector/LucidLinkCloudDetector.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// Copyright (c) 2024 Files Community
// Licensed under the MIT License. See the LICENSE.

using Files.App.Utils.Cloud;
using System.IO;
using System.Text.Json;
using Windows.Storage;

namespace Files.App.Utils.Cloud
Expand All @@ -13,9 +11,21 @@ namespace Files.App.Utils.Cloud
/// </summary>
public sealed class LucidLinkCloudDetector : AbstractCloudDetector
{
private readonly string iconPath = Path.Combine(Environment.GetEnvironmentVariable("ProgramFiles"), "Lucid", "resources", "Logo.ico");

protected override async IAsyncEnumerable<ICloudProvider> GetProviders()
{
string jsonPath = Path.Combine(Environment.GetEnvironmentVariable("UserProfile"), ".lucid", "app.json");
string jsonPath = Path.Combine(Constants.UserEnvironmentPaths.HomePath, ".lucid", "app.json");

// Lucid Link v3 and above
if (!File.Exists(jsonPath))
{
string volumePath = Path.Combine(Constants.UserEnvironmentPaths.SystemDrivePath, "Volumes");
await foreach (var provider in GetLucidLinkV3Providers(volumePath))
{
yield return provider;
}
}

var configFile = await StorageFile.GetFileFromPathAsync(jsonPath);
using var jsonFile = JsonDocument.Parse(await FileIO.ReadTextAsync(configFile));
Expand All @@ -31,7 +41,6 @@ protected override async IAsyncEnumerable<ICloudProvider> GetProviders()
string path = Path.Combine($@"{Constants.UserEnvironmentPaths.SystemDrivePath}\Volumes", orgNameFilespaceName[1], orgNameFilespaceName[0]);
string filespaceName = orgNameFilespaceName[0];

string iconPath = Path.Combine(Environment.GetEnvironmentVariable("ProgramFiles"), "Lucid", "resources", "Logo.ico");
StorageFile iconFile = await FilesystemTasks.Wrap(() => StorageFile.GetFileFromPathAsync(iconPath).AsTask());

yield return new CloudProvider(CloudProviders.LucidLink)
Expand All @@ -43,5 +52,36 @@ protected override async IAsyncEnumerable<ICloudProvider> GetProviders()
}
}
}

private async IAsyncEnumerable<ICloudProvider> GetLucidLinkV3Providers(string volumePath)
{
if (Directory.Exists(volumePath))
{
foreach (string directory in Directory.GetDirectories(volumePath))
{
if (IsSymlink(directory))
{
string[] orgNameFilespaceName = directory.Split("\\");
string path = Path.Combine($@"{Constants.UserEnvironmentPaths.SystemDrivePath}\Volumes", orgNameFilespaceName[orgNameFilespaceName.Length - 1], orgNameFilespaceName[orgNameFilespaceName.Length - 2]);
string filespaceName = orgNameFilespaceName[orgNameFilespaceName.Length - 2];

StorageFile iconFile = await FilesystemTasks.Wrap(() => StorageFile.GetFileFromPathAsync(iconPath).AsTask());

yield return new CloudProvider(CloudProviders.LucidLink)
{
Name = $"Lucid Link ({filespaceName})",
SyncFolder = path,
IconData = iconFile is not null ? await iconFile.ToByteArrayAsync() : null,
};
}
}
}
}

private static bool IsSymlink(string path)
{
var fileInfo = new FileInfo(path);
return fileInfo.Attributes.HasFlag(System.IO.FileAttributes.ReparsePoint);
}
}
}
}