comparison Core/FileStorage/StorageAccessor.cpp @ 1549:e5e975e9b738

refactoring and simplification of StorageAccessor
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 17 Aug 2015 10:47:04 +0200
parents 6e7e5ed91c2d
children 2c7d5eb588e6
comparison
equal deleted inserted replaced
1548:e9325f3ac496 1549:e5e975e9b738
31 31
32 32
33 #include "../PrecompiledHeaders.h" 33 #include "../PrecompiledHeaders.h"
34 #include "StorageAccessor.h" 34 #include "StorageAccessor.h"
35 35
36 #include "../Compression/ZlibCompressor.h"
37 #include "../OrthancException.h"
38 #include "../HttpServer/HttpStreamTranscoder.h"
39
36 namespace Orthanc 40 namespace Orthanc
37 { 41 {
38 FileInfo StorageAccessor::Write(const std::vector<uint8_t>& content, 42 FileInfo StorageAccessor::Write(const void* data,
39 FileContentType type) 43 size_t size,
44 FileContentType type,
45 CompressionType compression,
46 bool storeMd5)
40 { 47 {
41 if (content.size() == 0) 48 std::string uuid = Toolbox::GenerateUuid();
49
50 std::string md5;
51
52 if (storeMd5)
42 { 53 {
43 return WriteInternal(NULL, 0, type); 54 Toolbox::ComputeMD5(md5, data, size);
44 } 55 }
45 else 56
57 switch (compression)
46 { 58 {
47 return WriteInternal(&content[0], content.size(), type); 59 case CompressionType_None:
60 {
61 area_.Create(uuid, data, size, type);
62 return FileInfo(uuid, type, size, md5);
63 }
64
65 case CompressionType_ZlibWithSize:
66 {
67 ZlibCompressor zlib;
68
69 std::string compressed;
70 zlib.Compress(compressed, data, size);
71
72 std::string compressedMD5;
73
74 if (storeMd5)
75 {
76 Toolbox::ComputeMD5(compressedMD5, compressed);
77 }
78
79 if (compressed.size() > 0)
80 {
81 area_.Create(uuid, &compressed[0], compressed.size(), type);
82 }
83 else
84 {
85 area_.Create(uuid, NULL, 0, type);
86 }
87
88 return FileInfo(uuid, type, size, md5,
89 CompressionType_ZlibWithSize, compressed.size(), compressedMD5);
90 }
91
92 default:
93 throw OrthancException(ErrorCode_NotImplemented);
48 } 94 }
49 } 95 }
50 96
51 FileInfo StorageAccessor::Write(const std::string& content, 97
52 FileContentType type) 98 void StorageAccessor::Read(std::string& content,
99 const FileInfo& info)
53 { 100 {
54 if (content.size() == 0) 101 switch (info.GetCompressionType())
55 { 102 {
56 return WriteInternal(NULL, 0, type); 103 case CompressionType_None:
104 {
105 area_.Read(content, info.GetUuid(), info.GetContentType());
106 break;
107 }
108
109 case CompressionType_ZlibWithSize:
110 {
111 ZlibCompressor zlib;
112
113 std::string compressed;
114 area_.Read(compressed, info.GetUuid(), info.GetContentType());
115 IBufferCompressor::Uncompress(content, zlib, compressed);
116 break;
117 }
118
119 default:
120 {
121 throw OrthancException(ErrorCode_NotImplemented);
122 }
57 } 123 }
58 else 124
59 { 125 // TODO Check the validity of the uncompressed MD5?
60 return WriteInternal(&content[0], content.size(), type); 126 }
61 } 127
128
129 void StorageAccessor::SetupSender(BufferHttpSender& sender,
130 const FileInfo& info)
131 {
132 Read(sender.GetBuffer(), info);
133 sender.SetContentType(GetMimeType(info.GetContentType()));
134 sender.SetContentFilename(info.GetUuid() + std::string(GetFileExtension(info.GetContentType())));
135 }
136
137
138 void StorageAccessor::AnswerFile(HttpOutput& output,
139 const FileInfo& info)
140 {
141 BufferHttpSender sender;
142 SetupSender(sender, info);
143
144 HttpStreamTranscoder transcoder(sender, info.GetCompressionType());
145 output.Answer(transcoder);
146 }
147
148
149 void StorageAccessor::AnswerFile(RestApiOutput& output,
150 const FileInfo& info)
151 {
152 BufferHttpSender sender;
153 SetupSender(sender, info);
154
155 HttpStreamTranscoder transcoder(sender, info.GetCompressionType());
156 output.AnswerStream(transcoder);
62 } 157 }
63 } 158 }