Skip to content

Commit

Permalink
fix(sipi): Try to fix MIME type consistency check.
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Geer committed Nov 28, 2019
1 parent 7e98f1e commit a1a37cc
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 21 deletions.
2 changes: 1 addition & 1 deletion sipi/scripts/convert_from_file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ local check
success, check = fullImg:mimetype_consistency(submitted_mimetype.mimetype, originalFilename)

if not success then
send_error(500, "fullImg:mimetype_consistency() failed: " .. check)
send_error(500, "convert_from_file.lua: fullImg:mimetype_consistency() failed: " .. check)
return
end

Expand Down
34 changes: 17 additions & 17 deletions sipi/scripts/convert_from_path.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@ end

local mime_type = mime_info["mimetype"]

-- check that the submitted mimetype is the same as the real mimetype of the file

local submitted_mimetype
success, submitted_mimetype = server.parse_mimetype(originalMimeType)

if not success then
send_error(400, "Couldn't parse mimetype: " .. originalMimeType)
return
end

if (mime_type ~= submitted_mimetype.mimetype) then
send_error(400, MIMETYPES_INCONSISTENCY)
return
end

-- handle the file depending on its media type (image, text file)
local file_info = get_file_info(originalFilename, mime_type)

Expand Down Expand Up @@ -125,10 +140,10 @@ if media_type == IMAGE then
end

local check
success, check = fullImg:mimetype_consistency(originalMimeType, originalFilename)
success, check = fullImg:mimetype_consistency(submitted_mimetype.mimetype, originalFilename)

if not success then
send_error(500, "fullImg:mimetype_consistency() failed: " .. check)
send_error(500, "convert_from_path.lua: fullImg:mimetype_consistency() failed: " .. check)
return
end

Expand Down Expand Up @@ -205,21 +220,6 @@ elseif media_type == TEXT then
return
end

-- check that the submitted mimetype is the same as the real mimetype of the file

local submitted_mimetype
success, submitted_mimetype = server.parse_mimetype(originalMimeType)

if not success then
send_error(400, "Couldn't parse mimetype: " .. originalMimeType)
return
end

if (mime_type ~= submitted_mimetype.mimetype) then
send_error(400, MIMETYPES_INCONSISTENCY)
return
end

local filename = baseName .. "." .. file_info["extension"]
local filePath = projectFileDir .. filename

Expand Down
2 changes: 1 addition & 1 deletion sipi/scripts/make_thumbnail.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ for imgindex, imgparam in pairs(server.uploads) do
local check
success, check = thumbImg:mimetype_consistency(submitted_mimetype.mimetype, filename)
if not success then
send_error(500, "thumbImg:mimetype_consistency() failed: " .. check)
send_error(500, "make_thumbnail.lua: thumbImg:mimetype_consistency() failed: " .. check)
return
end

Expand Down
2 changes: 1 addition & 1 deletion sipi/scripts/upload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ for file_index, file_params in pairs(server.uploads) do
success, check = uploaded_image:mimetype_consistency(mime_type, original_filename)

if not success then
send_error(500, "uploaded_image:mimetype_consistency() failed: " .. check)
send_error(500, "upload.lua: uploaded_image:mimetype_consistency() failed: " .. check)
return
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class KnoraSipiIntegrationV1ITSpec extends ITKnoraLiveSpec(KnoraSipiIntegrationV
private val password = "test"
private val pathToChlaus = "_test_data/test_route/images/Chlaus.jpg"
private val pathToMarbles = "_test_data/test_route/images/marbles.tif"
private val pathToMarblesWithWrongExtension = "_test_data/test_route/images/marbles_with_wrong_extension.jpg"
private val pathToXSLTransformation = "_test_data/test_route/texts/letterToHtml.xsl"
private val pathToMappingWithXSLT = "_test_data/test_route/texts/mappingForLetterWithXSLTransformation.xml"
private val firstPageIri = new MutableTestIri
Expand Down Expand Up @@ -264,6 +265,30 @@ class KnoraSipiIntegrationV1ITSpec extends ITKnoraLiveSpec(KnoraSipiIntegrationV
checkResponseOK(knoraPutRequest)
}

"reject an 'incunabula:page' with binary data if the file extension is incorrect" in {
// The image to be uploaded.
val fileToSend = new File(pathToMarblesWithWrongExtension)
assert(fileToSend.exists(), s"File $pathToMarblesWithWrongExtension does not exist")

// A multipart/form-data request containing the image.
val formData = Multipart.FormData(
Multipart.FormData.BodyPart(
"file",
HttpEntity.fromPath(MediaTypes.`image/tiff`, fileToSend.toPath),
Map("filename" -> fileToSend.getName)
)
)

// Send the image in a PUT request to the Knora API server.
val knoraPutRequest = Put(baseApiUrl + "/v1/filevalue/" + URLEncoder.encode(firstPageIri.get, "UTF-8"), formData) ~> addCredentials(BasicHttpCredentials(username, password))

val exception = intercept[AssertionException] {
checkResponseOK(knoraPutRequest)
}

assert(exception.getMessage.contains("MIME type and/or file extension are inconsistent"))
}

"create an 'incunabula:page' with parameters" in {
// The image to be uploaded.
val fileToSend = new File(pathToChlaus)
Expand Down Expand Up @@ -808,7 +833,6 @@ class KnoraSipiIntegrationV1ITSpec extends ITKnoraLiveSpec(KnoraSipiIntegrationV
xmlDiff.hasDifferences should be(false)

}

}
}

Expand Down

0 comments on commit a1a37cc

Please sign in to comment.