Skip to content

Commit

Permalink
Add FileBlob to provide file access in the board
Browse files Browse the repository at this point in the history
  • Loading branch information
MRmlik12 committed May 17, 2024
1 parent ca4e35d commit 0e45b17
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 62 deletions.
33 changes: 0 additions & 33 deletions src/NoteSHR.File/Blob.cs

This file was deleted.

39 changes: 28 additions & 11 deletions src/NoteSHR.File/BoardConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,19 @@ namespace NoteSHR.File;

internal static class BoardConverter
{
public async static Task<BoardScheme> ConvertToScheme(string boardName, List<Note> notes)
private static bool FilterByBlobUrl(PropertyInfo property, object data)
{
var value = property.GetValue(data);

if (value is string str)
{
return str.Contains("blob://");
}

return false;
}

internal static BoardScheme ConvertToScheme(string boardName, List<Note> notes)
{
var scheme = new BoardScheme
{
Expand All @@ -32,20 +44,18 @@ public async static Task<BoardScheme> ConvertToScheme(string boardName, List<Not
{
data = ((IDataPersistence)node.ViewModel).ExportValues();

foreach (var property in data.GetType().GetProperties().Where(x => x.PropertyType == typeof(Stream)))
foreach (var property in data.GetType().GetProperties().Where(x => x.PropertyType == typeof(FileBlob)))
{
Stream? stream = null;
property.GetValue(stream);
var fileBlob = property.GetValue(data) as FileBlob;

if (stream != null)
if (fileBlob != null)
{
var fileId = Guid.NewGuid();
var blob = new Blob(stream, fileId.ToString(), scheme.Id);

Task.Run(async () =>
{
await blob.Write();
property.SetValue(data, fileId);
var fileId = Guid.NewGuid();

var path = await fileBlob.Write( fileId.ToString());
property.SetValue(data, path);
});
}
}
Expand All @@ -65,7 +75,7 @@ public async static Task<BoardScheme> ConvertToScheme(string boardName, List<Not
return scheme;
}

public static Board ConvertBack(BoardScheme scheme)
internal static Board ConvertBack(BoardScheme scheme)
{
var notes = new List<Note>();

Expand Down Expand Up @@ -97,6 +107,13 @@ public static Board ConvertBack(BoardScheme scheme)
var viewModel = (ViewModelBase)Activator.CreateInstance(viewModelType);
if (viewModel is IDataPersistence persistence)
{
foreach (var properties in nodeScheme.Data.GetType().GetProperties().Where(x => FilterByBlobUrl(x, nodeScheme.Data)))
{
var path = properties.GetValue(nodeScheme.Data) as string;
var blob = new FileBlob(scheme.Id, path);
properties.SetValue(nodeScheme.Data, blob);
}

persistence?.ConvertValues(nodeScheme.Data);
}

Expand Down
15 changes: 4 additions & 11 deletions src/NoteSHR.File/BoardExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,15 @@ namespace NoteSHR.File;

public static class BoardExporter
{
private static string GetTemporaryPath()
{
var tempFolder = Path.Combine(Path.GetTempPath(), $"NoteSHR/{Guid.NewGuid()}");
Directory.CreateDirectory(tempFolder);

return tempFolder;
}

public static async Task<string> ExportToFile(List<Note> notes, string name, string path)
{
var boardScheme = await BoardConverter.ConvertToScheme(name, notes);
var boardScheme = BoardConverter.ConvertToScheme(name, notes);
var json = JsonConvert.SerializeObject(boardScheme);

var tempFolder = GetTemporaryPath();
var tempFolder = PathUtils.GetTemporaryPath(boardScheme.Id);
Directory.CreateDirectory(tempFolder);

var boardFile = System.IO.File.Create(PathUtils.GetTemporaryPath(boardScheme.Id));
var boardFile = System.IO.File.Create(Path.Combine(tempFolder, "board.json"));
await boardFile.WriteAsync(Encoding.UTF8.GetBytes(json));
boardFile.Close();

Expand Down
52 changes: 52 additions & 0 deletions src/NoteSHR.File/FileBlob.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using NoteSHR.File.Utils;

namespace NoteSHR.File;

public class FileBlob(Stream? stream = null) : IDisposable, IAsyncDisposable
{
private Guid ProjectId { get; set; }
private Uri Uri { get; }

internal void SetProjectId(Guid projectId)
{
ProjectId = projectId;
}

internal async Task<string> Write(string filename)
{
var internalPath = $"/assets/{filename}";
await using var fileStream = System.IO.File.Create($"{PathUtils.GetTemporaryPath(ProjectId)}/{internalPath}");
await stream!.CopyToAsync(fileStream);

return $"blob://{internalPath}";
}

public Stream Read()
{
return System.IO.File.OpenRead(GetFilePath(Uri.AbsolutePath));
}

private string GetFilePath(string filename)
{
return $"{PathUtils.GetTemporaryPath(ProjectId)}/assets/{filename}";
}

public void Dispose()
{
stream?.Dispose();
}

internal FileBlob(Guid projectId, string blobUri) : this()
{
ProjectId = projectId;
Uri = new Uri(blobUri);
}

public async ValueTask DisposeAsync()
{
if (stream != null)
{
await stream.DisposeAsync();
}
}
}
5 changes: 3 additions & 2 deletions src/NoteSHR/Components/Image/ImageComponentViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using NoteSHR.Core.Helpers;
using NoteSHR.Core.Models;
using NoteSHR.Core.ViewModel;
using NoteSHR.File;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;

Expand Down Expand Up @@ -61,15 +62,15 @@ public ImageComponentViewModel()
[Reactive] public bool ImageSelected { get; set; }

public ReactiveCommand<RoutedEventArgs, Unit> SetImageCommand { get; set; }

public object ExportValues()
{
var stream = new MemoryStream();
Image?.Save(stream);

return new
{
Image = stream
Image = new FileBlob(stream)
};
}
}
1 change: 1 addition & 0 deletions src/NoteSHR/NoteSHR.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@

<ItemGroup>
<ProjectReference Include="..\NoteSHR.Core\NoteSHR.Core.csproj" />
<ProjectReference Include="..\NoteSHR.File\NoteSHR.File.csproj" />
</ItemGroup>
</Project>
8 changes: 4 additions & 4 deletions test/NoteSHR.File.Tests/BoardConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace NoteSHR.File.Tests;
public class BoardConverterTests
{
[Fact]
public async Task ExportBoardToScheme_VerifyProperties()
public void ExportBoardToScheme_VerifyProperties()
{
var notes = new List<Note>
{
Expand All @@ -28,7 +28,7 @@ public async Task ExportBoardToScheme_VerifyProperties()

const string boardName = "Hello";

var scheme = await BoardConverter.ConvertToScheme(boardName, notes);
var scheme = BoardConverter.ConvertToScheme(boardName, notes);

scheme.LastModifiedAt.Should().BeSameDateAs(DateTime.Now);
scheme.Name.Should().Be(boardName);
Expand All @@ -47,12 +47,12 @@ public async Task ExportBoardToScheme_VerifyProperties()
}

[Fact]
public async Task ExportBoardToScheme_ShouldNotesBeEmpty()
public void ExportBoardToScheme_ShouldNotesBeEmpty()
{
var notes = new List<Note>();

const string boardName = "empty";
var scheme = await BoardConverter.ConvertToScheme(boardName, notes);
var scheme = BoardConverter.ConvertToScheme(boardName, notes);

scheme.Name.Should().Be(boardName);
scheme.LastModifiedAt.Should().BeSameDateAs(DateTime.Today);
Expand Down
3 changes: 2 additions & 1 deletion test/NoteSHR.File.Tests/BoardExporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace NoteSHR.File.Tests;

public class BoardExporterTests
{
private const string TestImagePath = "./Assets/image.png";
private const string BoardName = "test-board";

[Fact]
Expand All @@ -34,7 +35,7 @@ public async Task TryExportBoard_OK()
Text = "Prepare meal",
Checked = true
}));

var outputPath = await BoardExporter.ExportToFile(notes, BoardName, ".");

var zip = ZipFile.Open(outputPath, ZipArchiveMode.Read);
Expand Down

0 comments on commit 0e45b17

Please sign in to comment.