private async Task <string> GetHashForFileAsync(ListedItem fileItem, string nameOfAlg, CancellationToken token, ProgressBar progress, IShellPage associatedInstance) { HashAlgorithmProvider algorithmProvider = HashAlgorithmProvider.OpenAlgorithm(nameOfAlg); StorageFile file = await StorageItemHelpers.ToStorageItem <StorageFile>((fileItem as ShortcutItem)?.TargetPath ?? fileItem.ItemPath, associatedInstance); if (file == null) { return(""); } Stream stream = await FilesystemTasks.Wrap(() => file.OpenStreamForReadAsync()); if (stream == null) { return(""); } var inputStream = stream.AsInputStream(); var str = inputStream.AsStreamForRead(); var cap = (long)(0.5 * str.Length) / 100; uint capacity; if (cap >= uint.MaxValue) { capacity = uint.MaxValue; } else { capacity = Convert.ToUInt32(cap); } Windows.Storage.Streams.Buffer buffer = new Windows.Storage.Streams.Buffer(capacity); var hash = algorithmProvider.CreateHash(); while (!token.IsCancellationRequested) { await inputStream.ReadAsync(buffer, capacity, InputStreamOptions.None); if (buffer.Length > 0) { hash.Append(buffer); } else { break; } if (progress != null) { progress.Value = (double)str.Position / str.Length * 100; } } inputStream.Dispose(); stream.Dispose(); if (token.IsCancellationRequested) { return(""); } return(CryptographicBuffer.EncodeToHexString(hash.GetValueAndReset()).ToLower()); }
public async void DecompressArchiveToChildFolder() { var selectedItem = associatedInstance?.SlimContentPage?.SelectedItem; if (selectedItem == null) { return; } BaseStorageFile archive = await StorageItemHelpers.ToStorageItem <BaseStorageFile>(selectedItem.ItemPath); BaseStorageFolder currentFolder = await StorageItemHelpers.ToStorageItem <BaseStorageFolder>(associatedInstance.FilesystemViewModel.CurrentFolder.ItemPath); BaseStorageFolder destinationFolder = null; if (currentFolder != null) { destinationFolder = await FilesystemTasks.Wrap(() => currentFolder.CreateFolderAsync(Path.GetFileNameWithoutExtension(archive.Path), CreationCollisionOption.OpenIfExists).AsTask()); } if (archive != null && destinationFolder != null) { CancellationTokenSource extractCancellation = new CancellationTokenSource(); PostedStatusBanner banner = App.OngoingTasksViewModel.PostOperationBanner( string.Empty, "ExtractingArchiveText".GetLocalized(), 0, ReturnResult.InProgress, FileOperationType.Extract, extractCancellation); Stopwatch sw = new Stopwatch(); sw.Start(); await ZipHelpers.ExtractArchive(archive, destinationFolder, banner.Progress, extractCancellation.Token); sw.Stop(); banner.Remove(); if (sw.Elapsed.TotalSeconds >= 6) { App.OngoingTasksViewModel.PostBanner( "ExtractingCompleteText".GetLocalized(), "ArchiveExtractionCompletedSuccessfullyText".GetLocalized(), 0, ReturnResult.Success, FileOperationType.Extract); } } }
public async void DecompressArchiveToChildFolder() { StorageFile archive = await StorageItemHelpers.ToStorageItem <StorageFile>(associatedInstance.SlimContentPage.SelectedItem.ItemPath); StorageFolder currentFolder = await StorageItemHelpers.ToStorageItem <StorageFolder>(associatedInstance.FilesystemViewModel.CurrentFolder.ItemPath); StorageFolder destinationFolder = null; if (currentFolder != null) { destinationFolder = await currentFolder.CreateFolderAsync(Path.GetFileNameWithoutExtension(archive.Path), CreationCollisionOption.OpenIfExists); } if (archive != null && destinationFolder != null) { CancellationTokenSource extractCancellation = new CancellationTokenSource(); PostedStatusBanner banner = App.StatusCenterViewModel.PostOperationBanner( string.Empty, "Extracting archive", 0, ReturnResult.InProgress, FileOperationType.Extract, extractCancellation); Stopwatch sw = new Stopwatch(); sw.Start(); await ZipHelpers.ExtractArchive(archive, destinationFolder, banner.Progress, extractCancellation.Token); sw.Stop(); banner.Remove(); if (sw.Elapsed.TotalSeconds >= 6) { App.StatusCenterViewModel.PostBanner( "Extracting complete!", "The archive extraction completed successfully.", 0, ReturnResult.Success, FileOperationType.Extract); } } }
public async void DecompressArchiveHere() { StorageFile archive = await StorageItemHelpers.ToStorageItem <StorageFile>(associatedInstance.SlimContentPage.SelectedItem.ItemPath); StorageFolder currentFolder = await StorageItemHelpers.ToStorageItem <StorageFolder>(associatedInstance.FilesystemViewModel.CurrentFolder.ItemPath); if (archive != null && currentFolder != null) { CancellationTokenSource extractCancellation = new CancellationTokenSource(); PostedStatusBanner banner = App.StatusCenterViewModel.PostOperationBanner( string.Empty, "ExtractingArchiveText".GetLocalized(), 0, ReturnResult.InProgress, FileOperationType.Extract, extractCancellation); Stopwatch sw = new Stopwatch(); sw.Start(); await ZipHelpers.ExtractArchive(archive, currentFolder, banner.Progress, extractCancellation.Token); sw.Stop(); banner.Remove(); if (sw.Elapsed.TotalSeconds >= 6) { App.StatusCenterViewModel.PostBanner( "ExtractingCompleteText".GetLocalized(), "ArchiveExtractionCompletedSuccessfullyText".GetLocalized(), 0, ReturnResult.Success, FileOperationType.Extract); } } }
public virtual void ShareItem(RoutedEventArgs e) { DataTransferManager manager = DataTransferManager.GetForCurrentView(); manager.DataRequested += new TypedEventHandler <DataTransferManager, DataRequestedEventArgs>(Manager_DataRequested); DataTransferManager.ShowShareUI(new ShareUIOptions { Theme = Enum.IsDefined(typeof(ShareUITheme), ThemeHelper.RootTheme.ToString()) ? (ShareUITheme)ThemeHelper.RootTheme : ShareUITheme.Default }); async void Manager_DataRequested(DataTransferManager sender, DataRequestedEventArgs args) { DataRequestDeferral dataRequestDeferral = args.Request.GetDeferral(); List <IStorageItem> items = new List <IStorageItem>(); DataRequest dataRequest = args.Request; /*dataRequest.Data.Properties.Title = "Data Shared From Files"; * dataRequest.Data.Properties.Description = "The items you selected will be shared";*/ foreach (ListedItem item in SlimContentPage.SelectedItems) { if (item.IsShortcutItem) { if (item.IsLinkItem) { dataRequest.Data.Properties.Title = string.Format("ShareDialogTitle".GetLocalized(), items.First().Name); dataRequest.Data.Properties.Description = "ShareDialogSingleItemDescription".GetLocalized(); dataRequest.Data.SetWebLink(new Uri(((ShortcutItem)item).TargetPath)); dataRequestDeferral.Complete(); return; } } else if (item.PrimaryItemAttribute == StorageItemTypes.Folder) { if (await StorageItemHelpers.ToStorageItem <StorageFolder>(item.ItemPath, associatedInstance) is StorageFolder folder) { items.Add(folder); } } else { if (await StorageItemHelpers.ToStorageItem <StorageFile>(item.ItemPath, associatedInstance) is StorageFile file) { items.Add(file); } } } if (items.Count == 1) { dataRequest.Data.Properties.Title = string.Format("ShareDialogTitle".GetLocalized(), items.First().Name); dataRequest.Data.Properties.Description = "ShareDialogSingleItemDescription".GetLocalized(); } else if (items.Count == 0) { dataRequest.FailWithDisplayText("ShareDialogFailMessage".GetLocalized()); dataRequestDeferral.Complete(); return; } else { dataRequest.Data.Properties.Title = string.Format("ShareDialogTitleMultipleItems".GetLocalized(), items.Count, "ItemsCount.Text".GetLocalized()); dataRequest.Data.Properties.Description = "ShareDialogMultipleItemsDescription".GetLocalized(); } dataRequest.Data.SetStorageItems(items); dataRequestDeferral.Complete(); // TODO: Unhook the event somewhere } }
public async void DecompressArchive() { StorageFile archive = await StorageItemHelpers.ToStorageItem <StorageFile>(associatedInstance.SlimContentPage.SelectedItem.ItemPath); if (archive != null) { DecompressArchiveDialog decompressArchiveDialog = new DecompressArchiveDialog(); DecompressArchiveDialogViewModel decompressArchiveViewModel = new DecompressArchiveDialogViewModel(archive); decompressArchiveDialog.ViewModel = decompressArchiveViewModel; ContentDialogResult option = await decompressArchiveDialog.ShowAsync(); if (option == ContentDialogResult.Primary) { // Check if archive still exists if (!StorageItemHelpers.Exists(archive.Path)) { return; } CancellationTokenSource extractCancellation = new CancellationTokenSource(); PostedStatusBanner banner = App.StatusCenterViewModel.PostOperationBanner( string.Empty, "ExtractingArchiveText".GetLocalized(), 0, ReturnResult.InProgress, FileOperationType.Extract, extractCancellation); StorageFolder destinationFolder = decompressArchiveViewModel.DestinationFolder; string destinationFolderPath = decompressArchiveViewModel.DestinationFolderPath; if (destinationFolder == null) { StorageFolder parentFolder = await StorageItemHelpers.ToStorageItem <StorageFolder>(Path.GetDirectoryName(archive.Path)); destinationFolder = await parentFolder.CreateFolderAsync(Path.GetFileName(destinationFolderPath), CreationCollisionOption.GenerateUniqueName); } Stopwatch sw = new Stopwatch(); sw.Start(); await ZipHelpers.ExtractArchive(archive, destinationFolder, banner.Progress, extractCancellation.Token); sw.Stop(); banner.Remove(); if (sw.Elapsed.TotalSeconds >= 6) { App.StatusCenterViewModel.PostBanner( "ExtractingCompleteText".GetLocalized(), "ArchiveExtractionCompletedSuccessfullyText".GetLocalized(), 0, ReturnResult.Success, FileOperationType.Extract); } if (decompressArchiveViewModel.OpenDestinationFolderOnCompletion) { await NavigationHelpers.OpenPath(destinationFolderPath, associatedInstance, FilesystemItemType.Directory); } } } }
private async Task <string> GetHashForFileAsync(ListedItem fileItem, string nameOfAlg, CancellationToken token, IProgress <float> progress, IShellPage associatedInstance) { HashAlgorithmProvider algorithmProvider = HashAlgorithmProvider.OpenAlgorithm(nameOfAlg); BaseStorageFile file = await StorageItemHelpers.ToStorageItem <BaseStorageFile>((fileItem as ShortcutItem)?.TargetPath ?? fileItem.ItemPath, associatedInstance); if (file == null) { return(""); } Stream stream = await FilesystemTasks.Wrap(() => file.OpenStreamForReadAsync()); if (stream == null) { return(""); } uint capacity; var inputStream = stream.AsInputStream(); bool isProgressSupported = false; try { var cap = (long)(0.5 * stream.Length) / 100; if (cap >= uint.MaxValue) { capacity = uint.MaxValue; } else { capacity = Convert.ToUInt32(cap); } isProgressSupported = true; } catch (NotSupportedException) { capacity = 64 * 1024; } Windows.Storage.Streams.Buffer buffer = new Windows.Storage.Streams.Buffer(capacity); var hash = algorithmProvider.CreateHash(); while (!token.IsCancellationRequested) { await inputStream.ReadAsync(buffer, capacity, InputStreamOptions.None); if (buffer.Length > 0) { hash.Append(buffer); } else { break; } if (stream.Length > 0) { progress?.Report(isProgressSupported ? (float)stream.Position / stream.Length * 100.0f : 20); } } inputStream.Dispose(); stream.Dispose(); if (token.IsCancellationRequested) { return(""); } return(CryptographicBuffer.EncodeToHexString(hash.GetValueAndReset()).ToLower()); }