-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Feature: Added support for displaying and editing metadata of multiple files #12476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7217c07
Edit metadata of multiple files
hishitetsu 1599b9e
Cancel modifying metadata
hishitetsu d279bd5
Added a comment
hishitetsu 32051a6
Merge branch 'main' into EditMultipleMetadata
yaira2 2bcae40
Update src/Files.App/ViewModels/Properties/BasePropertiesPage.cs
hishitetsu ac4374e
Update src/Files.App/ViewModels/Properties/BasePropertiesPage.cs
hishitetsu 31c92a4
Update LocationHelpers.cs
hishitetsu 7987e6d
Merge branch 'EditMultipleMetadata' of https://github.com/hishitetsu/…
hishitetsu ad3a326
Added comments
hishitetsu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright(c) 2023 Files Community | ||
// Licensed under the MIT License. See the LICENSE. | ||
|
||
using System.Text.Json; | ||
using Windows.Devices.Geolocation; | ||
using Windows.Services.Maps; | ||
using Windows.Storage; | ||
|
||
namespace Files.App.Helpers | ||
{ | ||
public static class LocationHelpers | ||
{ | ||
public static async Task<string> GetAddressFromCoordinatesAsync(double? Lat, double? Lon) | ||
{ | ||
if (!Lat.HasValue || !Lon.HasValue) | ||
return null; | ||
|
||
if (string.IsNullOrEmpty(MapService.ServiceToken)) | ||
{ | ||
try | ||
{ | ||
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(@"ms-appx:///Resources/BingMapsKey.txt")); | ||
var lines = await FileIO.ReadTextAsync(file); | ||
using var obj = JsonDocument.Parse(lines); | ||
MapService.ServiceToken = obj.RootElement.GetProperty("key").GetString(); | ||
} | ||
catch (Exception) | ||
{ | ||
return null; | ||
} | ||
} | ||
|
||
BasicGeoposition location = new BasicGeoposition(); | ||
location.Latitude = Lat.Value; | ||
location.Longitude = Lon.Value; | ||
Geopoint pointToReverseGeocode = new Geopoint(location); | ||
|
||
// Reverse geocode the specified geographic location. | ||
|
||
var result = await MapLocationFinder.FindLocationsAtAsync(pointToReverseGeocode); | ||
return result?.Locations?.FirstOrDefault()?.DisplayName; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
src/Files.App/ViewModels/Properties/Items/CombinedFileProperties.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
// Copyright(c) 2023 Files Community | ||
// Licensed under the MIT License. See the LICENSE. | ||
|
||
using Files.App.Filesystem.StorageItems; | ||
using Microsoft.UI.Dispatching; | ||
|
||
namespace Files.App.ViewModels.Properties | ||
{ | ||
internal class CombinedFileProperties : CombinedProperties, IFileProperties | ||
{ | ||
public CombinedFileProperties( | ||
SelectedItemsPropertiesViewModel viewModel, | ||
CancellationTokenSource tokenSource, | ||
DispatcherQueue coreDispatcher, | ||
List<ListedItem> listedItems, | ||
IShellPage instance) | ||
: base(viewModel, tokenSource, coreDispatcher, listedItems, instance) { } | ||
|
||
public async Task GetSystemFilePropertiesAsync() | ||
{ | ||
var queries = await Task.WhenAll(List.AsParallel().Select(async item => { | ||
BaseStorageFile file = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFileFromPathAsync(item.ItemPath)); | ||
if (file is null) | ||
{ | ||
// Could not access file, can't show any other property | ||
return null; | ||
} | ||
|
||
var list = await FileProperty.RetrieveAndInitializePropertiesAsync(file); | ||
|
||
list.Find(x => x.ID == "address").Value = | ||
await LocationHelpers.GetAddressFromCoordinatesAsync((double?)list.Find( | ||
x => x.Property == "System.GPS.LatitudeDecimal").Value, | ||
(double?)list.Find(x => x.Property == "System.GPS.LongitudeDecimal").Value); | ||
|
||
// Find Encoding Bitrate property and convert it to kbps | ||
var encodingBitrate = list.Find(x => x.Property == "System.Audio.EncodingBitrate"); | ||
if (encodingBitrate?.Value is not null) | ||
{ | ||
var sizes = new string[] { "Bps", "KBps", "MBps", "GBps" }; | ||
var order = (int)Math.Floor(Math.Log((uint)encodingBitrate.Value, 1024)); | ||
var readableSpeed = (uint)encodingBitrate.Value / Math.Pow(1024, order); | ||
encodingBitrate.Value = $"{readableSpeed:0.##} {sizes[order]}"; | ||
} | ||
|
||
return list | ||
.Where(fileProp => !(fileProp.Value is null && fileProp.IsReadOnly)) | ||
.GroupBy(fileProp => fileProp.SectionResource) | ||
.Select(group => new FilePropertySection(group) { Key = group.Key }) | ||
.Where(section => !section.All(fileProp => fileProp.Value is null)); | ||
})); | ||
|
||
if (queries.Any(query => query is null)) | ||
return; | ||
|
||
// Display only the sections that all files have | ||
var keys = queries.Select(query => query!.Select(section => section.Key)).Aggregate((x, y) => x.Intersect(y)); | ||
var sections = queries[0]!.Where(section => keys.Contains(section.Key)).OrderBy(group => group.Priority).ToArray(); | ||
|
||
foreach (var group in sections) | ||
{ | ||
var props = queries.SelectMany(query => query!.First(section => section.Key == group.Key)); | ||
foreach (FileProperty prop in group) | ||
{ | ||
if (props.Where(x => x.Property == prop.Property).Any(x => !Equals(x.Value, prop.Value))) | ||
{ | ||
// Has multiple values | ||
prop.Value = null; | ||
prop.PlaceholderText = "MultipleValues".GetLocalizedResource(); | ||
} | ||
} | ||
} | ||
|
||
ViewModel.PropertySections = new ObservableCollection<FilePropertySection>(sections); | ||
} | ||
|
||
public async Task SyncPropertyChangesAsync() | ||
{ | ||
var files = new List<BaseStorageFile>(); | ||
foreach (var item in List) | ||
{ | ||
BaseStorageFile file = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFileFromPathAsync(item.ItemPath)); | ||
|
||
// Couldn't access the file to save properties | ||
if (file is null) | ||
return; | ||
|
||
files.Add(file); | ||
} | ||
|
||
var failedProperties = ""; | ||
|
||
foreach (var group in ViewModel.PropertySections) | ||
{ | ||
foreach (FileProperty prop in group) | ||
{ | ||
if (!prop.IsReadOnly && prop.Modified) | ||
{ | ||
var newDict = new Dictionary<string, object>(); | ||
newDict.Add(prop.Property, prop.Value); | ||
|
||
foreach (var file in files) | ||
{ | ||
try | ||
{ | ||
if (file.Properties is not null) | ||
{ | ||
await file.Properties.SavePropertiesAsync(newDict); | ||
} | ||
} | ||
catch | ||
{ | ||
failedProperties += $"{file.Name}: {prop.Name}\n"; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (!string.IsNullOrWhiteSpace(failedProperties)) | ||
{ | ||
throw new Exception($"The following properties failed to save: {failedProperties}"); | ||
} | ||
} | ||
|
||
public async Task ClearPropertiesAsync() | ||
{ | ||
var failedProperties = new List<string>(); | ||
var files = new List<BaseStorageFile>(); | ||
foreach (var item in List) | ||
{ | ||
BaseStorageFile file = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFileFromPathAsync(item.ItemPath)); | ||
|
||
if (file is null) | ||
return; | ||
|
||
files.Add(file); | ||
} | ||
|
||
foreach (var group in ViewModel.PropertySections) | ||
{ | ||
foreach (FileProperty prop in group) | ||
{ | ||
if (!prop.IsReadOnly) | ||
{ | ||
var newDict = new Dictionary<string, object>(); | ||
newDict.Add(prop.Property, null); | ||
|
||
foreach (var file in files) | ||
{ | ||
try | ||
{ | ||
if (file.Properties is not null) | ||
{ | ||
await file.Properties.SavePropertiesAsync(newDict); | ||
} | ||
} | ||
catch | ||
{ | ||
failedProperties.Add(prop.Name); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
_ = GetSystemFilePropertiesAsync(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.