mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-12-05 03:04:35 +00:00
Moved temporary files into a folder of their own below the data directory (#1245)
This commit is contained in:
1 parent
ddfc632b63
commit
12d3e1ec6c
52 files changed
+1219
-396
No files matched your search
@@ -1,5 +1,4 @@
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
|
||||
using AIStudio.Chat;
|
||||
using AIStudio.Tools.PluginSystem;
|
||||
@@ -34,29 +33,21 @@ public static class PandocExport
|
||||
if (!format.UsesPandoc)
|
||||
throw new ArgumentOutOfRangeException(nameof(format), format, "Pandoc cannot write this format.");
|
||||
|
||||
var tempMarkdownFilePath = string.Empty;
|
||||
try
|
||||
{
|
||||
var tempMarkdownFile = Guid.NewGuid().ToString();
|
||||
tempMarkdownFilePath = Path.Combine(Path.GetTempPath(), tempMarkdownFile);
|
||||
|
||||
// Write text content to a temporary file. Pandoc expects UTF-8 without a byte order
|
||||
// mark; a mark would end up as a stray character at the start of the document:
|
||||
await File.WriteAllTextAsync(tempMarkdownFilePath, markdownText, new UTF8Encoding(false), token);
|
||||
|
||||
// Call Pandoc to create the document:
|
||||
// Call Pandoc to create the document. It reads the text from its standard input, so the text needs no temporary file:
|
||||
var pandocBuilder = PandocProcessBuilder
|
||||
.Create()
|
||||
.UseStandaloneMode()
|
||||
.WithInputFormat("gfm+emoji+tex_math_dollars")
|
||||
.WithOutputFormat(format.PandocOutputFormat)
|
||||
.WithOutputFile(targetFilePath)
|
||||
.WithInputFile(tempMarkdownFilePath);
|
||||
.WithStandardInput();
|
||||
|
||||
//
|
||||
// The document is named after the file it is written to. Set as metadata, the name
|
||||
// reaches the page as a string which Pandoc escapes; only a file named true or false
|
||||
// is read as a switch and keeps the temporary name.
|
||||
// is read as a switch, and the page gets the title Pandoc falls back to.
|
||||
//
|
||||
if (format.NeedsPageTitle)
|
||||
pandocBuilder.AddArgument("-M").AddArgument($"pagetitle={Path.GetFileNameWithoutExtension(targetFilePath)}");
|
||||
@@ -74,6 +65,16 @@ public static class PandocExport
|
||||
var outputTask = process.StandardOutput.ReadToEndAsync(token);
|
||||
var errorTask = process.StandardError.ReadToEndAsync(token);
|
||||
|
||||
try
|
||||
{
|
||||
await using var input = process.StandardInput;
|
||||
await input.WriteAsync(markdownText.AsMemory(), token);
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
// Pandoc stopped reading before the end of the text, e.g., over an invalid argument. Its exit code and error output below tell why.
|
||||
}
|
||||
|
||||
// Wait for the process to exit AND for streams to be fully read:
|
||||
await process.WaitForExitAsync(token);
|
||||
await outputTask;
|
||||
@@ -93,21 +94,6 @@ public static class PandocExport
|
||||
LOGGER.LogError(ex, "Error during {ExportFormat} conversion.", format);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Try to remove the temp file:
|
||||
if (!string.IsNullOrWhiteSpace(tempMarkdownFilePath))
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Delete(tempMarkdownFilePath);
|
||||
}
|
||||
catch
|
||||
{
|
||||
LOGGER.LogWarning("Was not able to delete the temporary file '{TempFilePath}'.", tempMarkdownFilePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in new issue
Block a user