Skip to content

Commit

Permalink
feature: Add mimetype to knora json response (#305)
Browse files Browse the repository at this point in the history
* Version set to 2.0.0

* Bugfix: explode dumped core on linux

* Added pdf support/mimtype for knora.json
  • Loading branch information
lrosenth authored and subotic committed Oct 2, 2019
1 parent 9fe6c1e commit 81ab98a
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 36 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ include(CheckIncludeFiles)
project(sipi)

string(TIMESTAMP SIPIDATE "%Y-%m-%d %H:%M")
set(SIPI_RELEASE "Sipi Version v1.5.0-SNAPSHOT (build ${SIPIDATE})")
set(SIPI_RELEASE "Sipi Version v2.0.0-SNAPSHOT (build ${SIPIDATE})")

# ------------------------------------------------------------------------------
# Set our path variables
Expand Down
13 changes: 9 additions & 4 deletions config/sipi.init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,14 @@ function pre_flight(prefix,identifier,cookie)

return 'allow', filepath

end
--[[
--[[
if config.prefix_as_path then
filepath = config.imgroot .. '/' .. prefix .. '/' .. identifier
else
filepath = config.imgroot .. '/' .. identifier
end
if server.cookies['sipi-auth'] then
print('preflight: IIIF cookie')
access_info = server.cookies['sipi-auth']
Expand Down Expand Up @@ -346,7 +352,6 @@ end
label = 'Login to SIPI',
}, filepath
end
end
--]]
end
-------------------------------------------------------------------------------
3 changes: 2 additions & 1 deletion include/SipiIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ namespace Sipi {
int width;
int height;
int numpages;
std::string internalmimetype;
std::string origname;
std::string mimetype;
std::string origmimetype;

SipiImgInfo(void) {
success = FAILURE;
Expand Down
11 changes: 0 additions & 11 deletions shttps/Global.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,6 @@ namespace shttps {
}
//-------------------------------------------------------------------------

template<class OutIt>
void explode(std::string const &input, char sep, OutIt output) {
std::istringstream buffer(input);
std::string temp;

while (std::getline(buffer, temp, sep)) {
*output++ = temp;
}
}
//-------------------------------------------------------------------------


inline std::string getFileName(const std::string &s) {
char sep = '/';
Expand Down
7 changes: 4 additions & 3 deletions src/SipiHttpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ namespace Sipi {
// test if we have access to the file
//
if (access(infile.c_str(), R_OK) != 0) { // test, if file exists
throw SipiError(__file__, __LINE__, "Cannot read image file!");
throw SipiError(__file__, __LINE__, "Cannot read image file: " + infile);
}
pre_flight_info["infile"] = infile;
return pre_flight_info;
Expand Down Expand Up @@ -646,8 +646,9 @@ namespace Sipi {
if (info.numpages > 0) {
json_object_set_new(root, "numpages", json_integer(info.numpages));
}
json_object_set_new(root, "internalMimeType", json_string(info.internalmimetype.c_str()));
if (info.success == SipiImgInfo::ALL) {
json_object_set_new(root, "originalMimeType", json_string(info.mimetype.c_str()));
json_object_set_new(root, "originalMimeType", json_string(info.origmimetype.c_str()));
json_object_set_new(root, "originalFilename", json_string(info.origname.c_str()));
}
char *json_str = json_dumps(root, JSON_INDENT(3));
Expand Down Expand Up @@ -879,7 +880,7 @@ namespace Sipi {
(actual_mimetype == "image/jpeg") ||
(actual_mimetype == "image/png") ||
(actual_mimetype == "image/jpx") ||
(actual_mimetype == "image/jp2")) { // no PDF here!! They are servced as blob
(actual_mimetype == "image/jp2")) { // no PDF here!! They are served as blob
conn_obj.setBuffer();
conn_obj.status(Connection::SEE_OTHER);
const std::string host = conn_obj.header("host");
Expand Down
13 changes: 8 additions & 5 deletions src/SipiImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,17 +355,20 @@ namespace Sipi {
std::transform(fext.begin(), fext.end(), _fext.begin(), ::tolower);

SipiImgInfo info;
if ((_fext == "tif") || (_fext == "tiff")) {
std::string mimetype = shttps::Parsing::getFileMimetype(filepath).first;

if ((mimetype == "image/tiff") || (mimetype == "image/x-tiff")) {
info = io[std::string("tif")]->getDim(filepath, pagenum);
} else if ((_fext == "jpg") || (_fext == "jpeg")) {
} else if ((mimetype == "image/jpeg") || (mimetype == "image/pjpeg")) {
info = io[std::string("jpg")]->getDim(filepath, pagenum);
} else if (_fext == "png") {
} else if (mimetype == "image/png") {
info = io[std::string("png")]->getDim(filepath, pagenum);
} else if ((_fext == "jp2") || (_fext == "jpx")) {
} else if ((mimetype == "image/jp2") || (mimetype == "image/jpx")) {
info = io[std::string("jpx")]->getDim(filepath, pagenum);
} else if (_fext == "pdf") {
} else if (mimetype == "application/pdf") {
info = io[std::string("pdf")]->getDim(filepath, pagenum);
}
info.internalmimetype = mimetype;

if (info.success == SipiImgInfo::FAILURE) {
for (auto const &iterator : io) {
Expand Down
2 changes: 1 addition & 1 deletion src/formats/SipiIOJ2k.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ namespace Sipi {
const char *cstr = comment.get_text();
if (strncmp(cstr, "SIPI:", 5) == 0) {
SipiEssentials se(cstr + 5);
info.mimetype = se.mimetype();
info.origmimetype = se.mimetype();
info.origname = se.origname();
info.success = SipiImgInfo::ALL;
break;
Expand Down
2 changes: 1 addition & 1 deletion src/formats/SipiIOTiff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,7 @@ namespace Sipi {
char *emdatastr;
if (1 == TIFFGetField(tif, TIFFTAG_SIPIMETA, &emdatastr)) {
SipiEssentials se(emdatastr);
info.mimetype = se.mimetype();
info.origmimetype = se.mimetype();
info.origname = se.origname();
info.success = SipiImgInfo::ALL;
}
Expand Down
31 changes: 23 additions & 8 deletions src/metadata/SipiEssentials.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,26 +157,41 @@ namespace Sipi {
_icc_profile = base64Encode(icc_profile_p);
}

// for string delimiter
std::vector<std::string> split (std::string s, std::string delimiter) {
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
std::string token;
std::vector<std::string> res;

while ((pos_end = s.find (delimiter, pos_start)) != std::string::npos) {
token = s.substr (pos_start, pos_end - pos_start);
pos_start = pos_end + delim_len;
res.push_back (token);
}

res.push_back (s.substr (pos_start));
return res;
}

void SipiEssentials::parse(const std::string &str) {
std::vector<std::string> result(6);
shttps::explode(str, '|', result.begin());
_origname = *result.begin();
_mimetype = *(result.begin() + 1);
std::string _hash_type_str = *(result.begin() + 2);
std::vector<std::string> result = split(str, "|");
_origname = result[0];
_mimetype = result[1];
std::string _hash_type_str = result[2];
if (_hash_type_str == "none") _hash_type = shttps::HashType::none;
else if (_hash_type_str == "md5") _hash_type = shttps::HashType::md5;
else if (_hash_type_str == "sha1") _hash_type = shttps::HashType::sha1;
else if (_hash_type_str == "sha256") _hash_type = shttps::HashType::sha256;
else if (_hash_type_str == "sha384") _hash_type = shttps::HashType::sha384;
else if (_hash_type_str == "sha512") _hash_type = shttps::HashType::sha512;
else _hash_type = shttps::HashType::none;
_data_chksum = *(result.begin() + 3);
_data_chksum = result[3];

if (result.size() > 5) {
std::string tmp_use_icc = *(result.begin() + 4);
std::string tmp_use_icc = result[4];
_use_icc = (tmp_use_icc == "USE_ICC");
if (_use_icc) {
_icc_profile = *(result.begin() + 5);
_icc_profile = result[5];
}
else {
_icc_profile = std::string();
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/test_02_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ def test_knora_info_validation(self, manager):
"width": 512,
"height": 512,
"originalFilename": "lena512.tif",
"originalMimeType": "image/tiff"
"originalMimeType": "image/tiff",
"internalMimeType": "image/jpx"
}

response_json = manager.post_file("/api/upload", manager.data_dir_path("unit/lena512.tif"), "image/tiff")
Expand Down

0 comments on commit 81ab98a

Please sign in to comment.