21
21
22
22
#include " DataFile_p.h"
23
23
#include " Signature.h"
24
- #include " util/DateTime.h"
25
24
#include " util/File.h"
26
25
#include " util/log.h"
27
- #include " util/ZipSerialize.h"
28
26
29
27
#include < algorithm>
30
- #include < array>
31
28
#include < ctime>
32
29
#include < fstream>
33
30
#include < map>
@@ -48,29 +45,24 @@ class ASiContainer::Private
48
45
map<string, ZipSerialize::Properties> properties;
49
46
};
50
47
51
- const string ASiContainer::MIMETYPE_ASIC_E = " application/vnd.etsi.asic-e+zip" ;
52
- const string ASiContainer::MIMETYPE_ASIC_S = " application/vnd.etsi.asic-s+zip" ;
53
- // https://signa.mitsoft.lt/static/signa-web/webResources/docs/ADOC_specification_approved20090907_EN.pdf
54
- const string ASiContainer::MIMETYPE_ADOC = " application/vnd.lt.archyvai.adoc-2008" ;
55
-
56
48
/* *
57
- * Initialize BDOC container .
49
+ * Initialize Container .
58
50
*/
59
- ASiContainer::ASiContainer (const string & mimetype)
51
+ ASiContainer::ASiContainer (string_view mimetype)
60
52
: d(make_unique<Private>())
61
53
{
62
54
d->mimetype = mimetype;
63
55
}
64
56
65
57
/* *
66
- * Loads ASi Container from a file.
58
+ * Loads Container from a file.
67
59
*
68
60
* @param path name of the container file.
69
61
* @param mimetypeRequired flag indicating if the mimetype must be present and checked.
70
62
* @param supported supported mimetypes.
71
63
* @return returns zip serializer for the container.
72
64
*/
73
- unique_ptr<ZipSerialize> ASiContainer::load (const string &path, bool mimetypeRequired, const set<string > &supported)
65
+ unique_ptr<ZipSerialize> ASiContainer::load (const string &path, bool mimetypeRequired, const set<string_view > &supported)
74
66
{
75
67
DEBUG (" ASiContainer::ASiContainer(path = '%s')" , path.c_str ());
76
68
auto z = make_unique<ZipSerialize>(d->path = path, false );
@@ -79,17 +71,17 @@ unique_ptr<ZipSerialize> ASiContainer::load(const string &path, bool mimetypeReq
79
71
if (list.empty ())
80
72
THROW (" Failed to parse container" );
81
73
74
+ // ETSI TS 102 918: mimetype has to be the first in the archive
82
75
if (mimetypeRequired && list.front () != " mimetype" )
83
76
THROW (" required mimetype not found" );
84
77
85
- // ETSI TS 102 918: mimetype has to be the first in the archive
86
78
if (list.front () == " mimetype" )
87
79
{
88
80
d->mimetype = readMimetype (*z);
89
- DEBUG (" mimetype = '%s'" , d->mimetype .c_str ());
90
81
if (supported.find (d->mimetype ) == supported.cend ())
91
82
THROW (" Incorrect mimetype '%s'" , d->mimetype .c_str ());
92
83
}
84
+ DEBUG (" mimetype = '%s'" , d->mimetype .c_str ());
93
85
94
86
for (const string &file: list)
95
87
d->properties [file] = z->properties (file);
@@ -167,24 +159,20 @@ void ASiContainer::addDataFile(const string &path, const string &mediaType)
167
159
{
168
160
string fileName = File::fileName (path);
169
161
addDataFileChecks (fileName, mediaType);
170
- if (!File::fileExists (path))
171
- THROW (" Document file '%s' does not exist." , path.c_str ());
172
-
173
- ZipSerialize::Properties prop { appInfo (), File::modifiedTime (path), File::fileSize (path) };
174
- bool useTempFile = prop.size > MAX_MEM_FILE;
175
- zproperty (fileName, std::move (prop));
176
- unique_ptr<istream> is;
177
- if (useTempFile)
178
- {
179
- is = make_unique<ifstream>(File::encodeName (path), ifstream::binary);
180
- }
181
- else
162
+ auto size = File::fileSize (path);
163
+ if (size == 0 )
164
+ THROW (" Document file '%s' does not exist or is empty." , path.c_str ());
165
+
166
+ unique_ptr<istream> is = make_unique<ifstream>(File::encodeName (path), ifstream::binary);
167
+ if (!*is)
168
+ THROW (" Failed to open file for reading: %s." , path.c_str ());
169
+ if (size <= MAX_MEM_FILE)
182
170
{
183
171
auto data = make_unique<stringstream>();
184
- if (ifstream file{File::encodeName (path), ifstream::binary})
185
- *data << file.rdbuf ();
172
+ *data << is->rdbuf ();
186
173
is = std::move (data);
187
174
}
175
+ d->properties [fileName] = { appInfo (), File::modifiedTime (path), size };
188
176
addDataFilePrivate (std::move (is), std::move (fileName), mediaType);
189
177
}
190
178
@@ -270,18 +258,13 @@ string ASiContainer::zpath() const
270
258
return d->path ;
271
259
}
272
260
273
- ZipSerialize::Properties ASiContainer::zproperty (const string &file) const
261
+ const ZipSerialize::Properties& ASiContainer::zproperty (const string &file) const
274
262
{
275
263
if (auto i = d->properties .find (file); i != d->properties .cend ())
276
264
return i->second ;
277
265
return d->properties [file] = { appInfo (), time (nullptr ), 0 };
278
266
}
279
267
280
- void ASiContainer::zproperty (const string &file, ZipSerialize::Properties &&prop)
281
- {
282
- d->properties [file] = std::move (prop);
283
- }
284
-
285
268
/* *
286
269
* Reads and parses container mimetype. Checks that the mimetype is supported.
287
270
*
@@ -294,9 +277,9 @@ string ASiContainer::readMimetype(const ZipSerialize &z)
294
277
DEBUG (" ASiContainer::readMimetype()" );
295
278
stringstream is;
296
279
z.extract (" mimetype" , is);
297
- string text;
298
- is >> text ;
299
- if (!is )
280
+ string text = is. str () ;
281
+ text. erase (text. find_last_not_of ( " \n\r\f\t\v " ) + 1 ) ;
282
+ if (text. empty () )
300
283
THROW (" Failed to read mimetype." );
301
284
// Contains UTF-16 BOM
302
285
if (text.find (" \xFF\xEF " ) == 0 || text.find (" \xEF\xFF " ) == 0 )
0 commit comments