Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion ChatGPTExport/Assets/ExistingAssetLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@ public void Add(string newFile)

public string? GetMarkdownMediaAsset(AssetRequest assetRequest)
{
// it may already exist in the destination directory from a previous export
var invalidChars = fileSystem.Path.GetInvalidFileNameChars()
.Concat(new[] { '*', '?', fileSystem.Path.DirectorySeparatorChar, fileSystem.Path.AltDirectorySeparatorChar })
.Distinct()
.ToArray();

if (assetRequest.SearchPattern.IndexOfAny(invalidChars) >= 0)
{
return null;
}

// it may already exist in the destination directory from a previous export
var destinationMatches = GetCachedDestinationFiles(assetRequest.SearchPattern).ToList();
if (destinationMatches.Count == 0)
{
Expand Down
47 changes: 47 additions & 0 deletions ChatGTPExportTests/Assets/ExistingAssetLocatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using ChatGPTExport.Assets;
using System.IO.Abstractions.TestingHelpers;

namespace ChatGTPExportTests.Assets
{
public class ExistingAssetLocatorTests
{
[Theory]
[InlineData("image*")]
[InlineData("image?")]
[InlineData("image/")]
[InlineData("image\\")]
[InlineData("invali<d")]
public void GetMarkdownMediaAsset_InvalidPattern_ReturnsNull(string pattern)
{
var fs = new MockFileSystem();
var destPath = fs.Path.Combine(fs.Path.DirectorySeparatorChar.ToString(), "dest");
fs.AddDirectory(destPath);
var destDir = fs.DirectoryInfo.New(destPath);
var locator = new ExistingAssetLocator(fs, destDir);

var request = new AssetRequest(pattern, "role", null, null);
var result = locator.GetMarkdownMediaAsset(request);

Assert.Null(result);
}

[Fact]
public void GetMarkdownMediaAsset_ValidPattern_ReturnsMarkdown()
{
var fs = new MockFileSystem();
var destPath = fs.Path.Combine(fs.Path.DirectorySeparatorChar.ToString(), "dest");
fs.AddDirectory(destPath);
var filePath = fs.Path.Combine(destPath, "image1.png");
fs.AddFile(filePath, new MockFileData("data"));

var destDir = fs.DirectoryInfo.New(destPath);
var locator = new ExistingAssetLocator(fs, destDir);

var request = new AssetRequest("image1", "role", null, null);
var result = locator.GetMarkdownMediaAsset(request);

Assert.NotNull(result);
Assert.Contains("image1.png", result);
}
}
}
Loading