Skip to content

Commit

Permalink
Merge pull request #1293 from sillsdev/fix/robust-tagLib-use
Browse files Browse the repository at this point in the history
Fix 4 img metadata methods that could fail due to cloud or scanning interference (#1293)
  • Loading branch information
hatton authored Oct 16, 2023
2 parents 591a664 + ac0efa8 commit 0a5bdea
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased]

### Added

- [SIL.Core] `RobustFile.Open`, `RobustFile.AppendAllText`, `RobustFile.WriteAllLines`, `RobustFile.GetAccessControl`, `RobustIO.EnumerateFilesInDirectory`, `RobustIO.EnumerateDirectoriesInDirectory`, `RobustIO.EnumerateEntriesInDirectory`, `RobustIO.RequireThatDirectoryExists`, `RobustIO.GetFileStream`, `RobustIO.ReadAllTextFromFileWhichMightGetWrittenTo`, and `RobustIO.IsFileLocked` methods
- [SIL.Core.Desktop] `RobustImageIO.GetImageFromFile` method
- [SIL.Windows.Forms] `ImageToolboxControl.ImageChanged` (selected or cropped) and `ImageToolboxControl.MetadataChanged` events
Expand All @@ -43,8 +44,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- [SIL.Core] Make RetryUtility retry for exceptions that are subclasses of the ones listed to try. For example, by default (IOException) it will now retry for FileNotFoundException.
- [SIL.Windows.Forms] Spelling of `CreativeCommonsLicense.IntergovernmentalOrganizationQualifier`
- [SIL.Windows.Forms] Fixed internationalization problem: SettingsProtection.LauncherButtonLabel was used as ID for two different strings.
- [SIL.Windows.Forms] Fix 4 img metadata methods that could fail due to cloud or scanning interference

### Removed

- [SIL.Windows.Forms] ImageGalleryControl.InSomeoneElesesDesignMode (seemingly unused and misspelled)
- [SIL.Windows.Forms] Checkbox for `IntergovernmentalOrganizationQualifier` from `MetadataEditorControl`

Expand All @@ -53,7 +56,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed

- [SIL.Windows.Forms] Make `PalasoImage.FromFile(Robustly)` methods more robust
- [SIL.Windows.Forms] Update dll to `libdl.so.2` to make compatible with Ubuntu 22.x. Affects multiple projects.
- [SIL.Windows.Forms] Update dll to `libdl.so.2` to make compatible with Ubuntu 22.x. Affects multiple projects.
- [SIL.Core] Fixed `BulkObservableList.MoveRange` method when moving a single item forward.

## [12.0.0] - 2023-02-14
Expand Down Expand Up @@ -161,6 +164,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- [SIL.Core.Desktop] Added Testing channel to UpdateSettings.

### Fixed

- [SIL.Archiving] Fixed formatting of DateTimes
- [SIL.Core] Fixed SIL.IO.PathUtilities.DeleteToRecycleBin and .GetDefaultFileManager to work in a flatpak environment.
- [SIL.Lexicon] Fixed crash caused by incorrect processing of keyboard data
Expand All @@ -171,6 +175,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [10.0.0] - 2022-08-04

### Added

- [SIL.Windows.Forms] Added extension method InitializeWithAvailableUILocales.
- [SIL.Windows.Forms] Added LocalizationIncompleteDlg and LocalizationIncompleteViewModel classes
- [SIL.Windows.Forms] Added property SummaryDisplayMember to CheckedComboBox.
Expand All @@ -181,17 +186,20 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- [SIL.Windows.Forms.Scripture] Added event InvalidReferencePasted to VerseControl, which is fired whenever an attempt to paste an invalid scripture reference is made.

### Changed

- [SIL.Windows.Forms.WritingSystems] Moved (internal) extension method InitializeWithAvailableUILocales to SIL.Windows.Forms.
- [SIL.Windows.Forms.WritingSystems] Added additional optional localizationIncompleteViewModel parameter to ToolStripExtensions.InitializeWithAvailableUILocales.
- [SIL.Core] If NotifyUserOfProblem is called with a null exception, it will no longer call UsageReporter.ReportException
- Replace deprecated `Mono.Posix` dependency with `Mono.Unix` (#1186)

### Removed

- Removed the "new" DisplayMember property from CheckedComboBox (which overrode the base class member). I don't believe this is a breaking change.

## [9.0.0] - 2022-06-03

### Added

- [SIL.Core] NamePrefix setting and CleanupTempFolder method added to TempFile
- [SIL.Core] Utility methods to remove XML namespaces
- [SIL.Core.Desktop] Serializable class `UpdateSettings` (settings for getting updates)
Expand Down Expand Up @@ -346,7 +354,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
projects

[Unreleased]: https://github.com/sillsdev/libpalaso/compare/v12.0.1...master

[12.0.1]: https://github.com/sillsdev/libpalaso/compare/v12.0.0...v12.0.1
[12.0.0]: https://github.com/sillsdev/libpalaso/compare/v11.0.1...v12.0.0
[11.0.1]: https://github.com/sillsdev/libpalaso/compare/v11.0.0...v11.0.1
Expand Down
13 changes: 10 additions & 3 deletions SIL.Windows.Forms/ClearShare/Metadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text;
using System.Text.RegularExpressions;
using L10NSharp;
using SIL.Code;
using SIL.Extensions;
using TagLib;
using TagLib.IFD;
Expand Down Expand Up @@ -108,7 +109,9 @@ private static void LoadProperties(string path, Metadata destinationMetadata)
{
try
{
destinationMetadata._originalTaglibMetadata = TagLib.File.Create(path) as TagLib.Image.File;
destinationMetadata._originalTaglibMetadata = RetryUtility.Retry(() =>
TagLib.File.Create(path) as TagLib.Image.File,
memo:$"LoadProperties({path})");
}
catch (TagLib.UnsupportedFormatException)
{
Expand Down Expand Up @@ -412,7 +415,9 @@ public void Write()
/// <summary>Returns if the format of the image file supports metadata</summary>
public bool FileFormatSupportsMetadata(string path)
{
var file = TagLib.File.Create(path) as TagLib.Image.File;
var file = RetryUtility.Retry(() =>
TagLib.File.Create(path) as TagLib.Image.File,
memo:$"FileFormatSupportsMetadata({path})");
return file != null && !file.GetType().FullName.Contains("NoMetadata");
}

Expand Down Expand Up @@ -445,7 +450,9 @@ public void Write(string path, bool copyAllMetaDataFromOriginal = true)
if (!FileFormatSupportsMetadata(path))
throw new NotSupportedException(String.Format("The image file {0} is in a format that does not support metadata.", Path.GetFileName(path)));

var file = TagLib.File.Create(path) as TagLib.Image.File;
var file = RetryUtility.Retry(() =>
TagLib.File.Create(path) as TagLib.Image.File,
memo:$"Write({path})");

file.GetTag(TagTypes.XMP, true); // The Xmp tag, at least, must exist so we can store properties into it.
// This does nothing if the file is not allowed to have PNG tags, that is, if it's not a PNG file.
Expand Down
10 changes: 6 additions & 4 deletions SIL.Windows.Forms/ImageToolbox/PalasoImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,12 @@ private void SaveUpdatedMetadata()
private static string GetCorrectImageExtension(string path)
{
byte[] bytes = new byte[10];
using (var file = File.OpenRead(path))
{
file.Read(bytes, 0, 10);
}
RetryUtility.Retry(() => {
using (var file = File.OpenRead(path))
{
file.Read(bytes, 0, 10);
}
}, memo:$"PalasoImage.GetCorrectImageExtension({path})");
// see http://www.mikekunz.com/image_file_header.html
var bmp = Encoding.ASCII.GetBytes("BM"); // BMP
var gif = Encoding.ASCII.GetBytes("GIF"); // GIF
Expand Down

0 comments on commit 0a5bdea

Please sign in to comment.