Skip to content

Commit e4e84b5

Browse files
committed
Simplified IConverter API. Added support for predefined output streams.
1 parent d6609e5 commit e4e84b5

File tree

11 files changed

+214
-180
lines changed

11 files changed

+214
-180
lines changed

src/bcf-toolkit/Converter/Bcf21/Converter.cs

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.IO.Compression;
45
using System.Threading.Tasks;
56
using BcfToolkit.Builder.Bcf21;
67
using BcfToolkit.Utils;
@@ -15,7 +16,6 @@ namespace BcfToolkit.Converter.Bcf21;
1516
/// JSON, and BCFzip.
1617
/// </summary>
1718
public class Converter : IConverter {
18-
1919
private BcfBuilder _builder = new();
2020

2121
/// <summary>
@@ -32,29 +32,42 @@ public class Converter : IConverter {
3232
/// Defines the file writer function which must be used for write the BCF
3333
/// object to the targeted version.
3434
/// </summary>
35-
private readonly Dictionary<BcfVersionEnum, Func<IBcf, bool, Task<Stream>>> _writerFn =
36-
new() {
37-
[BcfVersionEnum.Bcf21] = FileWriter.SerializeAndWriteBcf,
38-
[BcfVersionEnum.Bcf30] = Bcf30.FileWriter.SerializeAndWriteBcf
39-
};
35+
private readonly Dictionary<BcfVersionEnum, Func<IBcf, Task<Stream>>>
36+
_writerFn =
37+
new() {
38+
[BcfVersionEnum.Bcf21] = FileWriter.SerializeAndWriteBcf,
39+
[BcfVersionEnum.Bcf30] = Bcf30.FileWriter.SerializeAndWriteBcf
40+
};
4041

41-
public async Task BcfZipToJson(Stream source, string targetPath) {
42+
/// <summary>
43+
/// Defines the stream writer function which must be used for write the BCF
44+
/// object to the targeted version.
45+
/// </summary>
46+
private readonly Dictionary<BcfVersionEnum, Action<IBcf, ZipArchive>>
47+
_streamWriterFn =
48+
new() {
49+
[BcfVersionEnum.Bcf21] = FileWriter.SerializeAndWriteBcfToStream,
50+
[BcfVersionEnum.Bcf30] = Bcf30.FileWriter.SerializeAndWriteBcfToStream
51+
};
52+
53+
public async Task BcfToJson(Stream source, string targetPath) {
4254
var bcf = await _builder.BuildFromStream(source);
4355
await FileWriter.WriteBcfToJson(bcf, targetPath);
4456
}
4557

46-
public async Task BcfZipToJson(string sourcePath, string targetPath) {
58+
public async Task BcfToJson(string sourcePath, string targetPath) {
4759
try {
4860
await using var fileStream =
4961
new FileStream(sourcePath, FileMode.Open, FileAccess.Read);
50-
await BcfZipToJson(fileStream, targetPath);
62+
await BcfToJson(fileStream, targetPath);
5163
}
5264
catch (Exception ex) {
53-
throw new ArgumentException($"Source path is not readable. {ex.Message}", ex);
65+
throw new ArgumentException($"Source path is not readable. {ex.Message}",
66+
ex);
5467
}
5568
}
5669

57-
public async Task JsonToBcfZip(string source, string target) {
70+
public async Task JsonToBcf(string source, string target) {
5871
// Project is optional
5972
var projectPath = $"{source}/project.json";
6073
var project = Path.Exists(projectPath)
@@ -72,26 +85,32 @@ public async Task JsonToBcfZip(string source, string target) {
7285
await FileWriter.SerializeAndWriteBcfToFolder(bcf, target);
7386
}
7487

75-
public async Task<Stream> ToBcfStream(
76-
IBcf bcf,
77-
BcfVersionEnum targetVersion,
78-
bool writeTmpFolder) {
88+
public async Task<Stream> ToBcf(IBcf bcf, BcfVersionEnum targetVersion) {
7989
var converterFn = _converterFn[targetVersion];
8090
var convertedBcf = converterFn((Bcf)bcf);
8191

8292
var writerFn = _writerFn[targetVersion];
83-
return await writerFn(convertedBcf, writeTmpFolder);
93+
return await writerFn(convertedBcf);
94+
}
95+
96+
public void ToBcf(IBcf bcf, BcfVersionEnum targetVersion, Stream stream) {
97+
var converterFn = _converterFn[targetVersion];
98+
var convertedBcf = converterFn((Bcf)bcf);
99+
100+
var writerFn = _streamWriterFn[targetVersion];
101+
var zip = new ZipArchive(stream, ZipArchiveMode.Create, true);
102+
writerFn(convertedBcf, zip);
84103
}
85104

86-
public Task ToBcfZip(IBcf bcf, string target) {
105+
public Task ToBcf(IBcf bcf, string target) {
87106
return FileWriter.SerializeAndWriteBcfToFolder((Bcf)bcf, target);
88107
}
89108

90109
public Task ToJson(IBcf bcf, string target) {
91110
return FileWriter.WriteBcfToJson((Bcf)bcf, target);
92111
}
93112

94-
public async Task<T> BuildBcfFromStream<T>(Stream stream) {
113+
public async Task<T> BcfFromStream<T>(Stream stream) {
95114
var bcf = await _builder.BuildFromStream(stream);
96115
var targetVersion = BcfVersion.TryParse(typeof(T));
97116
var converterFn = _converterFn[targetVersion];

src/bcf-toolkit/Converter/Bcf21/FileWriter.cs

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -57,48 +57,30 @@ public static Task WriteBcfToJson(Bcf bcf, string target) {
5757
/// WARNING: Disposing the stream is the responsibility of the user!
5858
/// </summary>
5959
/// <param name="bcf">The `BCF` object that should be written.</param>
60-
/// <param name="writeToTmp">Should the archive be saved in the tmp folder.</param>
6160
/// <returns>It returns a stream of the archive.</returns>
62-
public static async Task<Stream> SerializeAndWriteBcf(
63-
IBcf bcf,
64-
bool writeToTmp) {
65-
66-
if (writeToTmp) {
67-
var workingDir = Directory.GetCurrentDirectory();
68-
var tmpBcfTargetPath = workingDir + $"/{Guid.NewGuid()}.bcfzip";
69-
var tmpFolder = await SerializeAndWriteBcfToFolder(bcf, tmpBcfTargetPath, false);
70-
var fileStream = new FileStream(tmpBcfTargetPath, FileMode.Open, FileAccess.Read);
71-
72-
Directory.Delete(tmpFolder, true);
73-
File.Delete(tmpBcfTargetPath);
74-
75-
return fileStream;
76-
}
77-
78-
// Memory stream for the zip archive
79-
var ms = new MemoryStream();
80-
var zip = new ZipArchive(ms, ZipArchiveMode.Create, true);
81-
82-
var zipStream = await SerializeAndWriteBcfToStream(bcf, ms, zip);
83-
zip.Dispose();
84-
return zipStream;
61+
public static async Task<Stream> SerializeAndWriteBcf(IBcf bcf) {
62+
var workingDir = Directory.GetCurrentDirectory();
63+
var tmpBcfTargetPath = workingDir + $"/{Guid.NewGuid()}.bcfzip";
64+
var tmpFolder =
65+
await SerializeAndWriteBcfToFolder(bcf, tmpBcfTargetPath, false);
66+
var fileStream =
67+
new FileStream(tmpBcfTargetPath, FileMode.Open, FileAccess.Read);
68+
69+
Directory.Delete(tmpFolder, true);
70+
File.Delete(tmpBcfTargetPath);
71+
72+
return fileStream;
8573
}
8674

8775
/// <summary>
8876
/// The method serializes the BCF content to xml from the given object,
89-
/// creates a zip entry from a memory stream and return a stream of the
90-
/// archive.
91-
///
92-
/// WARNING: Disposing the stream is the responsibility of the user!
77+
/// creates a zip entry from to the specified stream.
9378
/// </summary>
9479
/// <param name="bcf">The `Bcf` object that should be written.</param>
95-
/// <param name="stream">The memory stream for the zip archive.</param>
9680
/// <param name="zip">The zip archive which the object is written in.</param>
9781
/// <returns>Generated stream from bcf zip.</returns>
9882
/// <exception cref="ApplicationException"></exception>
99-
public static Task<Stream> SerializeAndWriteBcfToStream(
100-
IBcf bcf,
101-
MemoryStream stream,
83+
public static void SerializeAndWriteBcfToStream(IBcf bcf,
10284
ZipArchive zip) {
10385
var bcfObject = (Bcf)bcf;
10486

@@ -116,7 +98,8 @@ public static Task<Stream> SerializeAndWriteBcfToStream(
11698

11799
zip.SerializeAndCreateEntry($"{topicFolder}/markup.bcf", markup);
118100

119-
var visInfo = (VisualizationInfo)markup.GetFirstViewPoint()?.GetVisualizationInfo()!;
101+
var visInfo =
102+
(VisualizationInfo)markup.GetFirstViewPoint()?.GetVisualizationInfo()!;
120103
zip.SerializeAndCreateEntry($"{topicFolder}/viewpoint.bcf", visInfo);
121104

122105
var snapshotFileName = markup.GetFirstViewPoint()?.Snapshot;
@@ -129,8 +112,6 @@ public static Task<Stream> SerializeAndWriteBcfToStream(
129112
}
130113

131114
zip.SerializeAndCreateEntry("project.bcfp", bcfObject.Project);
132-
133-
return Task.FromResult<Stream>(stream);
134115
}
135116

136117
/// <summary>
@@ -152,7 +133,8 @@ public static async Task<string> SerializeAndWriteBcfToFolder(
152133
$"Target folder not found ${targetFolder}");
153134

154135
// Creating a tmp folder for the intermediate files.
155-
var tmpFolder = $"{targetFolder}/tmp{Path.GetFileNameWithoutExtension(target)}";
136+
var tmpFolder =
137+
$"{targetFolder}/tmp{Path.GetFileNameWithoutExtension(target)}";
156138
if (Directory.Exists(tmpFolder)) Directory.Delete(tmpFolder, true);
157139
Directory.CreateDirectory(tmpFolder);
158140

src/bcf-toolkit/Converter/Bcf30/Converter.cs

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.IO.Compression;
45
using System.Threading.Tasks;
56
using BcfToolkit.Builder.Bcf30;
67
using BcfToolkit.Utils;
78
using BcfToolkit.Model;
89
using BcfToolkit.Model.Bcf30;
9-
using File = System.IO.File;
1010
using Version = BcfToolkit.Model.Bcf30.Version;
1111

1212
namespace BcfToolkit.Converter.Bcf30;
@@ -16,8 +16,7 @@ namespace BcfToolkit.Converter.Bcf30;
1616
/// and back.
1717
/// </summary>
1818
public class Converter : IConverter {
19-
20-
private BcfBuilder _builder = new();
19+
private readonly BcfBuilder _builder = new();
2120

2221
/// <summary>
2322
/// Defines the converter function, which must be used for converting the
@@ -33,31 +32,45 @@ public class Converter : IConverter {
3332
/// Defines the file writer function which must be used for write the BCF
3433
/// object to the targeted version.
3534
/// </summary>
36-
private readonly Dictionary<BcfVersionEnum, Func<IBcf, bool, Task<Stream>>> _writerFn =
37-
new() {
38-
[BcfVersionEnum.Bcf21] = Bcf21.FileWriter.SerializeAndWriteBcf,
39-
[BcfVersionEnum.Bcf30] = FileWriter.SerializeAndWriteBcf
40-
};
35+
private readonly Dictionary<BcfVersionEnum, Func<IBcf, Task<Stream>>>
36+
_fileWriterFn =
37+
new() {
38+
[BcfVersionEnum.Bcf21] = Bcf21.FileWriter.SerializeAndWriteBcf,
39+
[BcfVersionEnum.Bcf30] = FileWriter.SerializeAndWriteBcf
40+
};
41+
42+
/// <summary>
43+
/// Defines the stream writer function which must be used for write the BCF
44+
/// object to the targeted version.
45+
/// </summary>
46+
private readonly Dictionary<BcfVersionEnum, Action<IBcf, ZipArchive>>
47+
_streamWriterFn =
48+
new() {
49+
[BcfVersionEnum.Bcf21] = Bcf21.FileWriter.SerializeAndWriteBcfToStream,
50+
[BcfVersionEnum.Bcf30] = FileWriter.SerializeAndWriteBcfToStream
51+
};
4152

42-
public async Task BcfZipToJson(Stream source, string target) {
53+
public async Task BcfToJson(Stream source, string target) {
4354
var builder = new BcfBuilder();
4455
var bcf = await builder.BuildFromStream(source);
4556
await FileWriter.WriteJson(bcf, target);
4657
}
4758

48-
public async Task BcfZipToJson(string source, string target) {
49-
await using var fileStream = new FileStream(source, FileMode.Open, FileAccess.Read);
50-
await BcfZipToJson(fileStream, target);
59+
public async Task BcfToJson(string source, string target) {
60+
await using var fileStream =
61+
new FileStream(source, FileMode.Open, FileAccess.Read);
62+
await BcfToJson(fileStream, target);
5163
}
5264

53-
public async Task JsonToBcfZip(string source, string target) {
65+
public async Task JsonToBcf(string source, string target) {
5466
// Project and DocumentInfo are optional
5567
var extensions =
5668
await JsonExtensions.ParseObject<Extensions>($"{source}/extensions.json");
5769
var project =
5870
await JsonExtensions.ParseObject<ProjectInfo>($"{source}/project.json");
5971
var documents =
60-
await JsonExtensions.ParseObject<DocumentInfo>($"{source}/documents.json");
72+
await JsonExtensions.ParseObject<DocumentInfo>(
73+
$"{source}/documents.json");
6174
var markups = await JsonExtensions.ParseMarkups<Markup>(source);
6275

6376
var bcf = new Bcf {
@@ -71,26 +84,32 @@ public async Task JsonToBcfZip(string source, string target) {
7184
await FileWriter.SerializeAndWriteBcfToFolder(bcf, target);
7285
}
7386

74-
public async Task<Stream> ToBcfStream(
75-
IBcf bcf,
76-
BcfVersionEnum targetVersion,
77-
bool writeToTmp) {
87+
public async Task<Stream> ToBcf(IBcf bcf, BcfVersionEnum targetVersion) {
88+
var converterFn = _converterFn[targetVersion];
89+
var convertedBcf = converterFn((Bcf)bcf);
90+
91+
var writerFn = _fileWriterFn[targetVersion];
92+
return await writerFn(convertedBcf);
93+
}
94+
95+
public void ToBcf(IBcf bcf, BcfVersionEnum targetVersion, Stream stream) {
7896
var converterFn = _converterFn[targetVersion];
7997
var convertedBcf = converterFn((Bcf)bcf);
8098

81-
var writerFn = _writerFn[targetVersion];
82-
return await writerFn(convertedBcf, writeToTmp);
99+
var writerFn = _streamWriterFn[targetVersion];
100+
var zip = new ZipArchive(stream, ZipArchiveMode.Create, true);
101+
writerFn(convertedBcf, zip);
83102
}
84103

85-
public Task ToBcfZip(IBcf bcf, string target) {
104+
public Task ToBcf(IBcf bcf, string target) {
86105
return FileWriter.SerializeAndWriteBcfToFolder((Bcf)bcf, target);
87106
}
88107

89108
public Task ToJson(IBcf bcf, string target) {
90109
return FileWriter.WriteJson((Bcf)bcf, target);
91110
}
92111

93-
public async Task<T> BuildBcfFromStream<T>(Stream stream) {
112+
public async Task<T> BcfFromStream<T>(Stream stream) {
94113
var bcf = await _builder.BuildFromStream(stream);
95114
var targetVersion = BcfVersion.TryParse(typeof(T));
96115
var converterFn = _converterFn[targetVersion];

0 commit comments

Comments
 (0)