Skip to content

Commit

Permalink
Merge pull request #1302 from sillsdev/BL-12823-RobustifyImageRelated…
Browse files Browse the repository at this point in the history
…Stuff

Make metadata file I/O more robust with retries
  • Loading branch information
andrew-polk authored Nov 1, 2023
2 parents 854d9c9 + 18233d1 commit ab03a37
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

- [SIL.Windows.Forms.ClearShare] Make Metadata.Write (and a few other methods) more robust
- [SIL.Core.Desktop] Make FileUtils.ReplaceFileWithUserInteractionIfNeeded robust
- [SIL.Core] Make RobustFile.ReplaceByCopyDelete truly robust
- [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.
Expand Down
17 changes: 9 additions & 8 deletions SIL.Windows.Forms/ClearShare/Metadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using L10NSharp;
using SIL.Code;
using SIL.Extensions;
using SIL.IO;
using TagLib;
using TagLib.IFD;
using TagLib.Image;
Expand Down Expand Up @@ -452,7 +453,7 @@ public void Write(string path, bool copyAllMetaDataFromOriginal = true)

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

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 All @@ -465,7 +466,7 @@ public void Write(string path, bool copyAllMetaDataFromOriginal = true)
file.CopyFrom(_originalTaglibMetadata);
}
SaveInImageTag(file.ImageTag);
file.Save();
RetryUtility.Retry(() => file.Save(), memo: $"Metadata.Write({path}) - saving TagLib.Image.File");
//as of right now, we are clean with respect to what is on disk, no need to save.
HasChanges = false;
}
Expand Down Expand Up @@ -707,7 +708,7 @@ public void SaveXmpFile(string path)
{
var tag = new XmpTag();
SaveInImageTag(tag);
File.WriteAllText(path, tag.Render(), Encoding.UTF8);
RobustFile.WriteAllText(path, tag.Render(), Encoding.UTF8);
}

/// <summary>
Expand All @@ -716,10 +717,10 @@ public void SaveXmpFile(string path)
/// <example>LoadXmpFile("c:\dir\metadata.xmp")</example>
public void LoadXmpFile(string path)
{
if(!File.Exists(path))
if(!RobustFile.Exists(path))
throw new FileNotFoundException(path);

var xmp = new XmpTag(File.ReadAllText(path, Encoding.UTF8), null);
var xmp = new XmpTag(RobustFile.ReadAllText(path, Encoding.UTF8), null);
LoadProperties(xmp, this);
}

Expand Down Expand Up @@ -750,7 +751,7 @@ public void LoadFromStoredExemplar(FileCategory category)
/// <param name="category">e.g. "image", "document"</param>
public static bool HaveStoredExemplar(FileCategory category)
{
return File.Exists(GetExemplarPath(category));
return RobustFile.Exists(GetExemplarPath(category));
}

/// <summary>
Expand All @@ -760,8 +761,8 @@ public static bool HaveStoredExemplar(FileCategory category)
public static void DeleteStoredExemplar(FileCategory category)
{
var path = GetExemplarPath(category);
if (File.Exists(path))
File.Delete(path);
if (RobustFile.Exists(path))
RobustFile.Delete(path);
}

/// <summary>
Expand Down

0 comments on commit ab03a37

Please sign in to comment.