Skip to content

Commit d5b92d5

Browse files
committed
Fixes writing satellite images extension, fixes vpenades#217
1 parent 2c34b81 commit d5b92d5

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

src/SharpGLTF.Core/Memory/MemoryImage.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,23 @@ public string MimeType
307307
throw new InvalidOperationException("Image format not recognized.");
308308
}
309309
}
310+
311+
/// <summary>
312+
/// If the given path ends with an image extension, it removes the extension.
313+
/// </summary>
314+
/// <param name="path">A file path</param>
315+
/// <returns>a trimmed path if it had an image extension, or the original path.</returns>
316+
public static string TrimImageExtension(string path)
317+
{
318+
if (path == null) return null;
319+
320+
foreach (var ext in new string[] { ".jpg",".jpeg",".png",".dds",".webp",".ktx2" })
321+
{
322+
if (path.EndsWith(ext, StringComparison.OrdinalIgnoreCase)) return path.Substring(0, path.Length - ext.Length);
323+
}
324+
325+
return path;
326+
}
310327

311328
#endregion
312329

src/SharpGLTF.Core/Schema2/gltf.Images.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,13 @@ internal void _WriteToSatellite(WriteContext writer, string satelliteUri)
218218
Memory.MemoryImage._Verify(imimg, nameof(imimg));
219219

220220
if (string.IsNullOrWhiteSpace(AlternateWriteFileName))
221-
{
222-
satelliteUri = System.IO.Path.ChangeExtension(satelliteUri, imimg.FileExtension);
221+
{
222+
// at this point satelliteUri should not have an image extension,
223+
// but it's better to ensure the path is sanitized.
224+
satelliteUri = Memory.MemoryImage.TrimImageExtension(satelliteUri);
225+
satelliteUri += "." + imimg.FileExtension;
223226
}
224-
else
227+
else // using a custom user-provided alternate file path
225228
{
226229
satelliteUri = AlternateWriteFileName;
227230
if (satelliteUri.EndsWith(".*"))

tests/SharpGLTF.Core.Tests/Schema2/LoadAndSave/RegressionTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,22 @@ public void LoadMinMaxBoundsOnByteAccessor()
112112
var gltf = ModelRoot.Load(ResourceInfo.From("cube-integer-min-max-bounds.gltf"));
113113
Assert.That(gltf, Is.Not.Null);
114114
}
115+
116+
[Test]
117+
public void SaveToPathWithExtraDots()
118+
{
119+
// https://github.com/vpenades/SharpGLTF/issues/217
120+
121+
var path1 = TestFiles.GetSampleModelsPaths().First(item => item.EndsWith("BoxTextured.gltf"));
122+
123+
var model = ModelRoot.Load(path1);
124+
125+
126+
var attachmentPath = AttachmentInfo.From("BoxTextured.xyz.gltf").WriteObject(f => model.Save(f));
127+
128+
var texturePath = attachmentPath.Directory.GetFiles("BoxTextured.xyz.png").FirstOrDefault();
129+
130+
Assert.That(texturePath.Name, Is.EqualTo("BoxTextured.xyz.png"));
131+
}
115132
}
116133
}

0 commit comments

Comments
 (0)