Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux har noe bug med png og type 0, som då kan settes til 5 #74

Merged
merged 2 commits into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AttachmentToStorableFormatConverter(private val imageConversionService: Im
return if (Format.PDF == detectedType) {
input
} else {
imageConversionService.convert(input)
imageConversionService.convert(input, detectedType)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import java.util.*
enum class Format(var mimeType: String) {

PDF("application/pdf"),
PNG("image/jpeg"),
JPG("image/png");
PNG("image/png"),
JPEG("image/jpeg");

companion object {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ class ImageConversionService {

private data class ImageSize(val width: Float, val height: Float)

fun convert(input: ByteArray): ByteArray {
fun convert(input: ByteArray, detectedType: Format): ByteArray {
return PDDocument().use { document ->
val imageStream = ByteArrayInputStream(input)
val page = PDPage(PDRectangle.A4)
document.addPage(page)
val image = toPortait(ImageIO.read(imageStream))
val image = toPortait(ImageIO.read(imageStream), detectedType)

val quality = 1.0f

Expand All @@ -40,14 +40,14 @@ class ImageConversionService {
}
}

private fun toPortait(image: BufferedImage): BufferedImage {
private fun toPortait(image: BufferedImage, detectedType: Format): BufferedImage {
if (image.height >= image.width) {
return image
}
val width: Int = image.width
val height: Int = image.height

val dest = BufferedImage(height, width, image.type)
val dest = BufferedImage(height, width, getType(image, detectedType))

val graphics2D = dest.createGraphics()
graphics2D.translate((height - width) / 2, (height - width) / 2)
Expand All @@ -56,6 +56,17 @@ class ImageConversionService {
return dest
}

/**
* En feil i ubuntu slik att hvis det er PNG og type 0 så skal den bli 5
*/
private fun getType(image: BufferedImage, detectedType: Format): Int {
return if(image.type == BufferedImage.TYPE_CUSTOM && detectedType == Format.PNG) {
BufferedImage.TYPE_3BYTE_BGR
} else {
image.type
}
}

private fun scale(image: PDImageXObject, page: PDPage): ImageSize {
var width = image.width.toFloat()
var height = image.height.toFloat()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package no.nav.familie.dokument.virusscan

import no.nav.familie.http.health.AbstractHealthIndicator
import org.springframework.context.annotation.Profile
import org.springframework.stereotype.Component

@Component
@Profile("!local")
class VirusScanHealthIndicator(client: VirusScanClient) : AbstractHealthIndicator(client, "virusscan")
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class AttachmentConverterTest {

@Before
fun setUp() {
every { imageConversionService.convert(any()) } returns convertedDummy
every { imageConversionService.convert(any(), any()) } returns convertedDummy
}

@Test(expected = RuntimeException::class)
Expand All @@ -30,7 +30,7 @@ class AttachmentConverterTest {
val pdfVedlegg = toByteArray("dummy/pdf_dummy.pdf")
val storable = converter.toStorageFormat(pdfVedlegg)
assertThat(storable).isEqualTo(pdfVedlegg)
verify(exactly = 0) { imageConversionService.convert(any()) }
verify(exactly = 0) { imageConversionService.convert(any(), any()) }
}

@Test
Expand All @@ -46,6 +46,6 @@ class AttachmentConverterTest {
converted = converter.toStorageFormat(vedlegg)
assertThat(converted).isEqualTo(convertedDummy)

verify(exactly = 2) {imageConversionService.convert(any())}
verify(exactly = 2) {imageConversionService.convert(any(), any())}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,26 @@ internal class ImageConversionServiceTest {

@Test
internal fun `convert jpg`() {
val pdfBytes = imageConversionService.convert(toByteArray("dummy/jpg_dummy.jpg"))
val pdfBytes = imageConversionService.convert(toByteArray("dummy/jpg_dummy.jpg"), Format.JPEG)
File("./jpg_pdf.pdf").writeBytes(pdfBytes)
}

@Test
internal fun `convert png`() {
val pdfBytes = imageConversionService.convert(toByteArray("dummy/png_dummy.png"))
val pdfBytes = imageConversionService.convert(toByteArray("dummy/png_dummy.png"), Format.PNG)
File("./png_pdf.pdf").writeBytes(pdfBytes)
}

@Test
internal fun `convert png - rotert`() {
val pdfBytes = imageConversionService.convert(toByteArray("dummy/png_dummy_rotated.png"), Format.PNG)
File("./png_rotated_pdf.pdf").writeBytes(pdfBytes)
}

@Test
internal fun `convert png av type 0`() {
val pdfBytes = imageConversionService.convert(toByteArray("dummy/png_type_0.png"), Format.PNG)
File("./png_type0_pdf.pdf").writeBytes(pdfBytes)
}

}
Binary file added src/test/resources/dummy/png_dummy_rotated.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/test/resources/dummy/png_type_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.