tesses-cms/Tesses.CMS.Avalonia/Tesses.CMS.Avalonia.Desktop/DesktopPlatform.cs

95 lines
2.9 KiB
C#

using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Platform.Storage;
using LibVLCSharp.Avalonia;
using LibVLCSharp.Shared;
using Newtonsoft.Json;
using Tesses.VirtualFilesystem;
using Tesses.VirtualFilesystem.Extensions;
using Tesses.VirtualFilesystem.Filesystems;
namespace Tesses.CMS.Avalonia.Desktop;
internal class DesktopPlatform : IPlatform
{
public DesktopPlatform()
{
if(rootfs.FileExists(Special.LocalAppData / "tesses_cms_app.json"))
{
var conf=JsonConvert.DeserializeObject<Configuration>(rootfs.ReadAllText(Special.LocalAppData/"tesses_cms_app.json"));
if(conf != null)
_conf = conf;
var path=UnixPath.FromLocal(_conf.DownloadPath);
virtualFilesystem = rootfs.GetSubdirFilesystem(path);
}
}
LocalFileSystem rootfs=new LocalFileSystem();
Configuration _conf = new Configuration();
public Configuration Configuration => _conf;
public bool PlatformUsesNormalPathsForDownload => true;
IVirtualFilesystem? virtualFilesystem;
public IVirtualFilesystem? DownloadFilesystem => virtualFilesystem;
public async Task BrowseForDownloadDirectoryAsync()
{
var sp=App.Window?.StorageProvider;
if(sp != null)
{
var res=await sp.OpenFolderPickerAsync(new global::Avalonia.Platform.Storage.FolderPickerOpenOptions{Title="Browse for a downloads folder"});
if(res.Count > 0)
{
var first=res.First();
string? path=first.TryGetLocalPath();
if(!string.IsNullOrWhiteSpace(path))
{
_conf.DownloadPath = path;
await WriteConfigurationAsync();
}
}
}
}
public async Task ReadConfigurationAsync()
{
if(await rootfs.FileExistsAsync(Special.LocalAppData / "tesses_cms_app.json"))
{
var conf=JsonConvert.DeserializeObject<Configuration>(await rootfs.ReadAllTextAsync(Special.LocalAppData/"tesses_cms_app.json"));
if(conf != null)
_conf = conf;
}
}
public async Task WriteConfigurationAsync()
{
await rootfs.WriteAllTextAsync(Special.LocalAppData / "tesses_cms_app.json",JsonConvert.SerializeObject(_conf));
var path=UnixPath.FromLocal(_conf.DownloadPath);
virtualFilesystem = rootfs.GetSubdirFilesystem(path);
}
public Control CreatePlayer()
{
return new VideoPlayer();
}
public void SetMediaPlayer(Control control, MediaPlayer? mediaPlayer)
{
var ctrl = control as VideoPlayer;
if(ctrl != null)
ctrl.MediaPlayer = mediaPlayer;
}
public MediaPlayer? GetMediaPlayer(Control control)
{
var ctrl = control as VideoPlayer;
if(ctrl != null)
return ctrl.MediaPlayer;
return null;
}
}