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
69 changes: 69 additions & 0 deletions Frends.File.Tests/UnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,39 @@ public async Task WriteFileThrow()
Assert.Equal("old content", fileContent);
Assert.Equal($"File already exists: {Path.Combine(TestFileContext.RootPath, "test.txt")}", ex.Message);
}
[Fact]
public async Task WriteFileCreateDirectory()
{
var result = await File.Write(
new WriteInput()
{
Content = "Created with a subdirectory",
Path = Path.Combine(TestFileContext.RootPath, "folder/Subdir/subdir.txt")
},
new WriteOption()
{
CreateTargetDirectories = true
});
var fileContent = System.IO.File.ReadAllText(result.Path);
Console.WriteLine(result.Path);
Assert.Equal("Created with a subdirectory", fileContent);
}

[Fact]
public async Task WriteFileCreateDirectoryThrow()
{
var result = await Assert.ThrowsAsync<DirectoryNotFoundException>(async () => await File.Write(
new WriteInput()
{
Content = "Task should throw",
Path = Path.Combine(TestFileContext.RootPath, "folder/Subdir/subdir.txt")
},
new WriteOption()
{
CreateTargetDirectories = false
}));

}

[Fact]
public async Task WriteFileBytesAppend()
Expand Down Expand Up @@ -430,6 +463,42 @@ public async Task WriteFileBytesThrow()
Assert.Equal(imageBytes, fileContentBytes);
}

[Fact]
public async Task WriteFileBytesCreateDirectory()
{
var imageBytes = System.IO.File.ReadAllBytes(BinaryTestFilePath);
TestFileContext.CreateBinaryFile("test.png", new byte[] { 137, 80, 78, 71, 13, 10, 26, 10 }); // empty png
var result = await File.WriteBytes(
new WriteBytesInput()
{
ContentBytes = imageBytes,
Path = Path.Combine(TestFileContext.RootPath, "folder/Subdir/test.png")
},
new WriteBytesOption()
{
CreateTargetDirectories = true,
});
var fileContentBytes = System.IO.File.ReadAllBytes(result.Path);
Assert.Equal(imageBytes.Length, fileContentBytes.Length);
Assert.Equal(imageBytes, fileContentBytes);
}
[Fact]
public async Task WriteFileBytesCreateDirectoryThrow()
{
var imageBytes = System.IO.File.ReadAllBytes(BinaryTestFilePath);
TestFileContext.CreateBinaryFile("test.png", new byte[] { 137, 80, 78, 71, 13, 10, 26, 10 }); // empty png
var result = await Assert.ThrowsAsync<DirectoryNotFoundException>(async () => await File.WriteBytes(
new WriteBytesInput()
{
ContentBytes = imageBytes,
Path = Path.Combine(TestFileContext.RootPath, "folder/Subdir/test.png")
},
new WriteBytesOption()
{
CreateTargetDirectories = false,
}));
}

[Fact]
public async Task ReadFileContent()
{
Expand Down
14 changes: 14 additions & 0 deletions Frends.File/Definitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@ public class WriteOption
/// How the file write should work if a file with the new name already exists
/// </summary>
public WriteBehaviour WriteBehaviour { get; set; }

/// <summary>
/// If set, will create the target directory if it does not exist,
/// as well as any sub directories.
/// </summary>
[DefaultValue(false)]
public bool CreateTargetDirectories { get; set; }
}

public class WriteBytesOption
Expand All @@ -273,6 +280,13 @@ public class WriteBytesOption
/// How the file write should work if a file with the new name already exists
/// </summary>
public WriteBehaviour WriteBehaviour { get; set; }

/// <summary>
/// If set, will create the target directory if it does not exist,
/// as well as any sub directories.
/// </summary>
[DefaultValue(false)]
public bool CreateTargetDirectories { get; set; }
}

public class WriteResult
Expand Down
17 changes: 17 additions & 0 deletions Frends.File/File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,15 @@ private static async Task<WriteResult> ExecuteWrite(WriteInput input, WriteOptio
var encoding = GetEncoding(options.FileEncoding, options.EnableBom, options.EncodingInString);
var fileMode = GetAndCheckWriteMode(options.WriteBehaviour, input.Path);

if(options.CreateTargetDirectories)
{
var directoryPath = Path.GetDirectoryName(input.Path);
if(!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
}

using (var fileStream = new FileStream(input.Path, fileMode, FileAccess.Write, FileShare.Write, 4096, useAsync: true))
using (var writer = new StreamWriter(fileStream, encoding))
{
Expand All @@ -298,6 +307,14 @@ private static async Task<WriteResult> ExecuteWrite(WriteInput input, WriteOptio

private static async Task<WriteResult> ExecuteWriteBytes(WriteBytesInput input, WriteBytesOption options)
{
if (options.CreateTargetDirectories)
{
var directoryPath = Path.GetDirectoryName(input.Path);
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
}
var bytes = input?.ContentBytes as byte[] ?? throw new ArgumentException("Input.ContentBytes must be a byte array", nameof(input.ContentBytes)); // TODO: Use corrctly typed input once UI support expression default editor for arrays

var fileMode = GetAndCheckWriteMode(options.WriteBehaviour, input.Path);
Expand Down