Skip to content

armgit / Filee

Public Imported from GitHub
0

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

fix: confine 3D model resources

Stamina97bab9e834986
diff --git a/src/Filee.Engines/Models3D/ObjFormat.cs b/src/Filee.Engines/Models3D/ObjFormat.cs
index 1ef01220..8a051a6a 100644
--- a/src/Filee.Engines/Models3D/ObjFormat.cs
+++ b/src/Filee.Engines/Models3D/ObjFormat.cs
@@ -187,7 +187,7 @@ internal static class ObjFormat
return text.Split([' ', '\t'], StringSplitOptions.RemoveEmptyEntries).Select(n => Resolve(directory, n)).OfType<string>();
}
- /// <summary>Finds a file referenced from an OBJ / MTL: relative to <paramref name="directory"/>, else by name only.</summary>
+ /// <summary>Finds a file referenced from a 3D model inside <paramref name="directory"/>, else by name only.</summary>
internal static string? Resolve(string directory, string reference)
{
reference = reference.Trim().Trim('"').Replace('\\', '/');
@@ -195,10 +195,12 @@ internal static class ObjFormat
return null;
try
{
- var candidate = Path.GetFullPath(Path.Combine(directory, reference));
- if (File.Exists(candidate))
+ var root = Path.TrimEndingDirectorySeparator(Path.GetFullPath(directory)) + Path.DirectorySeparatorChar;
+ var comparison = OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
+ var candidate = Path.GetFullPath(Path.Combine(root, reference));
+ if (candidate.StartsWith(root, comparison) && File.Exists(candidate))
return candidate;
- candidate = Path.Combine(directory, Path.GetFileName(reference));
+ candidate = Path.Combine(root, Path.GetFileName(reference));
return File.Exists(candidate) ? candidate : null;
}
catch (Exception ex) when (ex is ArgumentException or NotSupportedException or PathTooLongException)
diff --git a/tests/Filee.Engines.Tests/ModelTests.cs b/tests/Filee.Engines.Tests/ModelTests.cs
index 48539f1b..164e2564 100644
--- a/tests/Filee.Engines.Tests/ModelTests.cs
+++ b/tests/Filee.Engines.Tests/ModelTests.cs
@@ -372,6 +372,49 @@ public class ModelTests(EngineFixture fx) : IClassFixture<EngineFixture>
Assert.NotNull(glb.LogicalMaterials.Single(m => m.Name == "wood").FindChannel("BaseColor")!.Value.Texture);
}
+ [Fact]
+ public void Obj_textures_stay_beside_the_model()
+ {
+ var root = fx.NewFolder();
+ var source = Directory.CreateDirectory(Path.Combine(root, "source")).FullName;
+ var outside = Directory.CreateDirectory(Path.Combine(root, "outside")).FullName;
+ var allowed = Path.Combine(source, "allowed.png");
+ var secret = Path.Combine(outside, "secret.png");
+ File.WriteAllBytes(allowed, ModelBuilders.Checker());
+ File.WriteAllBytes(secret, ModelBuilders.Checker());
+ File.WriteAllText(Path.Combine(source, "materials.mtl"), $$"""
+ newmtl allowed
+ map_Kd allowed.png
+ newmtl parent
+ map_Kd ../outside/secret.png
+ newmtl absolute
+ map_Kd {{secret.Replace('\\', '/')}}
+ """);
+ File.WriteAllText(Path.Combine(source, "drawing.obj"), """
+ mtllib materials.mtl
+ v 0 0 0
+ v 1 0 0
+ v 0 1 0
+ vt 0 0
+ vt 1 0
+ vt 0 1
+ o allowed
+ usemtl allowed
+ f 1/1 2/2 3/3
+ o parent
+ usemtl parent
+ f 1/1 2/2 3/3
+ o absolute
+ usemtl absolute
+ f 1/1 2/2 3/3
+ """);
+
+ var model = ModelBuilders.Load(Path.Combine(source, "drawing.obj"), "obj");
+ Assert.Equal(ModelBuilders.Checker(), model.MaterialOf(model.Parts.Single(p => p.Name == "allowed"))!.BaseColorTexture!.Data);
+ Assert.Null(model.MaterialOf(model.Parts.Single(p => p.Name == "parent"))!.BaseColorTexture);
+ Assert.Null(model.MaterialOf(model.Parts.Single(p => p.Name == "absolute"))!.BaseColorTexture);
+ }
+
[Fact]
public void Ply_ascii_with_quads_and_colours_and_big_endian_binary_are_read()
{