diff --git a/Converters/FileNameToCanConvertCommandVisible.cs b/Converters/FileNameToCanConvertCommandVisible.cs new file mode 100644 index 0000000..4bae62b --- /dev/null +++ b/Converters/FileNameToCanConvertCommandVisible.cs @@ -0,0 +1,31 @@ +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Data; +using System; +using System.Linq; + +namespace SimpleList.Converters +{ + public class FileNameToCanConvertCommandVisible : IValueConverter + { + public static readonly FileNameToCanConvertCommandVisible Instance = new(); + public object Convert(object value, Type targetType, object parameter, string language) + { + string[] allowedExtensions = { ".csv", ".doc", ".docx", ".odp", ".ods", ".odt", ".pot", ".potm", ".potx", ".pps", ".ppsx", ".ppsxm", ".ppt", ".pptm", ".pptx", ".rtf", ".xls", ".xlsx" }; + if (value is string fileName) + { + string fileExtension = System.IO.Path.GetExtension(fileName).ToLower(); + if (allowedExtensions.Contains(fileExtension)) + { + return Visibility.Visible; + } + } + return Visibility.Collapsed; + } + + + public object ConvertBack(object value, Type targetType, object parameter, string language) + { + throw new NotImplementedException(); + } + } +} diff --git a/Services/OneDrive.cs b/Services/OneDrive.cs index 2c90c18..09d8e39 100644 --- a/Services/OneDrive.cs +++ b/Services/OneDrive.cs @@ -1,5 +1,6 @@ using CommunityToolkit.Authentication; using CommunityToolkit.Graph.Extensions; +using Downloader; using Microsoft.Graph; using System; using System.Collections.Generic; @@ -110,6 +111,19 @@ public async Task GetStorageInfo() return drive.Quota; } + public async Task ConvertFileFormat(string itemId, StorageFile file, string format = "pdf") + { + GraphServiceClient graphClient = _provider.GetClient(); + List queryOptions = new() + { + new QueryOption("format", format) + }; + Stream result = await graphClient.Me.Drive.Items[itemId].Content.Request(queryOptions).GetAsync(); + using var fileStream = System.IO.File.Create(file.Path); + result.Seek(0, SeekOrigin.Begin); + await result.CopyToAsync(fileStream); + } + public async void Login() { try diff --git a/SimpleList.csproj b/SimpleList.csproj index 06e35ee..6bfaf93 100644 --- a/SimpleList.csproj +++ b/SimpleList.csproj @@ -52,6 +52,7 @@ + @@ -102,6 +103,9 @@ True \ + + MSBuild:Compile + diff --git a/ViewModels/ConvertFileFormatViewModel.cs b/ViewModels/ConvertFileFormatViewModel.cs new file mode 100644 index 0000000..b4a3f0e --- /dev/null +++ b/ViewModels/ConvertFileFormatViewModel.cs @@ -0,0 +1,59 @@ +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.DependencyInjection; +using SimpleList.Services; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Windows.Storage.Pickers; +using Windows.Storage; +using WinRT.Interop; +using Microsoft.UI.Xaml; +using CommunityToolkit.Mvvm.Input; +using System.IO; +using System.Linq; + +namespace SimpleList.ViewModels +{ + public class ConvertFileFormatViewModel : ObservableObject + { + public ConvertFileFormatViewModel(FileViewModel file) + { + _file = file; + ConvertFileCommand = new AsyncRelayCommand(ConvertFileFormat); + } + + public async Task ConvertFileFormat() + { + Window _downloadPathSelectWindow = new(); + IntPtr hwnd = WindowNative.GetWindowHandle(_downloadPathSelectWindow); + FileSavePicker savePicker = new() + { + SuggestedStartLocation = PickerLocationId.Downloads + }; + savePicker.FileTypeChoices.Add("PDF Document", new List() { ".pdf" }); + savePicker.SuggestedFileName = Path.GetFileNameWithoutExtension(_file.Name); + InitializeWithWindow.Initialize(savePicker, hwnd); + StorageFile file = await savePicker.PickSaveFileAsync(); + SavedFilePath = file?.Path; + + string fileExtension = Path.GetExtension(_file.Name).ToLower(); + + if (allowedExtensions.Contains(fileExtension)) + { + await drive.ConvertFileFormat(_file.Id, file); + } + } + + private readonly FileViewModel _file; + private readonly OneDrive drive = Ioc.Default.GetService(); + private static readonly string[] allowedExtensions = { ".csv", ".doc", ".docx", ".odp", ".ods", ".odt", ".pot", ".potm", ".potx", ".pps", ".ppsx", ".ppsxm", ".ppt", ".pptm", ".pptx", ".rtf", ".xls", ".xlsx" }; + private string _selectedFormat = "pdf"; + private string _savedFilePath; + + public IEnumerable TargetFormats => new List { "pdf" }; + public string SelectedFormat { get => _selectedFormat; set => SetProperty(ref _selectedFormat, value); } + public string SavedFilePath { get => _savedFilePath; set => SetProperty(ref _savedFilePath, value); } + public string FormattedExtensions => string.Join(", ", allowedExtensions.Select(ext => ext.TrimStart('.'))); + public AsyncRelayCommand ConvertFileCommand { get; } + } +} diff --git a/Views/ConvertFileFormatView.xaml b/Views/ConvertFileFormatView.xaml new file mode 100644 index 0000000..b270b0f --- /dev/null +++ b/Views/ConvertFileFormatView.xaml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + +