1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . IO ;
4
+ using System . IO . Compression ;
4
5
using System . Threading . Tasks ;
5
6
using BcfToolkit . Builder . Bcf30 ;
6
7
using BcfToolkit . Utils ;
7
8
using BcfToolkit . Model ;
8
9
using BcfToolkit . Model . Bcf30 ;
9
- using File = System . IO . File ;
10
10
using Version = BcfToolkit . Model . Bcf30 . Version ;
11
11
12
12
namespace BcfToolkit . Converter . Bcf30 ;
@@ -16,8 +16,7 @@ namespace BcfToolkit.Converter.Bcf30;
16
16
/// and back.
17
17
/// </summary>
18
18
public class Converter : IConverter {
19
-
20
- private BcfBuilder _builder = new ( ) ;
19
+ private readonly BcfBuilder _builder = new ( ) ;
21
20
22
21
/// <summary>
23
22
/// Defines the converter function, which must be used for converting the
@@ -33,31 +32,45 @@ public class Converter : IConverter {
33
32
/// Defines the file writer function which must be used for write the BCF
34
33
/// object to the targeted version.
35
34
/// </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
+ } ;
41
52
42
- public async Task BcfZipToJson ( Stream source , string target ) {
53
+ public async Task BcfToJson ( Stream source , string target ) {
43
54
var builder = new BcfBuilder ( ) ;
44
55
var bcf = await builder . BuildFromStream ( source ) ;
45
56
await FileWriter . WriteJson ( bcf , target ) ;
46
57
}
47
58
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 ) ;
51
63
}
52
64
53
- public async Task JsonToBcfZip ( string source , string target ) {
65
+ public async Task JsonToBcf ( string source , string target ) {
54
66
// Project and DocumentInfo are optional
55
67
var extensions =
56
68
await JsonExtensions . ParseObject < Extensions > ( $ "{ source } /extensions.json") ;
57
69
var project =
58
70
await JsonExtensions . ParseObject < ProjectInfo > ( $ "{ source } /project.json") ;
59
71
var documents =
60
- await JsonExtensions . ParseObject < DocumentInfo > ( $ "{ source } /documents.json") ;
72
+ await JsonExtensions . ParseObject < DocumentInfo > (
73
+ $ "{ source } /documents.json") ;
61
74
var markups = await JsonExtensions . ParseMarkups < Markup > ( source ) ;
62
75
63
76
var bcf = new Bcf {
@@ -71,26 +84,32 @@ public async Task JsonToBcfZip(string source, string target) {
71
84
await FileWriter . SerializeAndWriteBcfToFolder ( bcf , target ) ;
72
85
}
73
86
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 ) {
78
96
var converterFn = _converterFn [ targetVersion ] ;
79
97
var convertedBcf = converterFn ( ( Bcf ) bcf ) ;
80
98
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 ) ;
83
102
}
84
103
85
- public Task ToBcfZip ( IBcf bcf , string target ) {
104
+ public Task ToBcf ( IBcf bcf , string target ) {
86
105
return FileWriter . SerializeAndWriteBcfToFolder ( ( Bcf ) bcf , target ) ;
87
106
}
88
107
89
108
public Task ToJson ( IBcf bcf , string target ) {
90
109
return FileWriter . WriteJson ( ( Bcf ) bcf , target ) ;
91
110
}
92
111
93
- public async Task < T > BuildBcfFromStream < T > ( Stream stream ) {
112
+ public async Task < T > BcfFromStream < T > ( Stream stream ) {
94
113
var bcf = await _builder . BuildFromStream ( stream ) ;
95
114
var targetVersion = BcfVersion . TryParse ( typeof ( T ) ) ;
96
115
var converterFn = _converterFn [ targetVersion ] ;
0 commit comments