Skip to content

Commit

Permalink
Add temporary patch for png and tiff files (#5107)
Browse files Browse the repository at this point in the history
Co-authored-by: Simon Dumas <simon.dumas@epfl.ch>
  • Loading branch information
imsdu and Simon Dumas authored Aug 16, 2024
1 parent be5dfab commit 2f475a4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ import ch.epfl.bluebrain.nexus.ship._
import ch.epfl.bluebrain.nexus.ship.acls.AclWiring.alwaysAuthorize
import ch.epfl.bluebrain.nexus.ship.config.InputConfig
import ch.epfl.bluebrain.nexus.ship.files.FileCopier.FileCopyResult.{FileCopySkipped, FileCopySuccess}
import ch.epfl.bluebrain.nexus.ship.files.FileProcessor.{logger, patchMediaType}
import ch.epfl.bluebrain.nexus.ship.files.FileProcessor.{forceMediaType, logger, patchMediaType}
import ch.epfl.bluebrain.nexus.ship.files.FileWiring._
import ch.epfl.bluebrain.nexus.ship.storages.StorageWiring
import io.circe.Decoder

import java.time.Instant

class FileProcessor private (
files: Files,
projectMapper: ProjectMapper,
Expand Down Expand Up @@ -71,7 +73,8 @@ class FileProcessor private (
val newMediaType = patchMediaType(attrs.filename, attrs.mediaType)
val newAttrs = e.attributes.copy(mediaType = newMediaType)
val customMetadata = Some(getCustomMetadata(newAttrs))
fileCopier.copyFile(e.project, newAttrs, attrs.mediaType != newMediaType).flatMap {
val fct = forceMediaType(attrs.mediaType, e.instant, newMediaType)
fileCopier.copyFile(e.project, newAttrs, fct).flatMap {
case FileCopySuccess(newPath) =>
val linkRequest = FileLinkRequest(newPath, newMediaType, customMetadata)
files
Expand All @@ -84,7 +87,8 @@ class FileProcessor private (
val newMediaType = patchMediaType(attrs.filename, attrs.mediaType)
val newAttrs = e.attributes.copy(mediaType = newMediaType)
val customMetadata = Some(getCustomMetadata(newAttrs))
fileCopier.copyFile(e.project, newAttrs, attrs.mediaType != newMediaType).flatMap {
val fct = forceMediaType(attrs.mediaType, e.instant, newMediaType)
fileCopier.copyFile(e.project, newAttrs, fct).flatMap {
case FileCopySuccess(newPath) =>
val linkRequest = FileLinkRequest(newPath, newMediaType, customMetadata)
files
Expand Down Expand Up @@ -135,6 +139,22 @@ object FileProcessor {
.map(ContentType(_, () => HttpCharsets.`UTF-8`))
.orElse(original)

private val pngMediaType = Some("image/png")
private val tiffMediaType = Some("image/tiff")
private val instant = Instant.parse("2021-04-11T19:19:16.579Z")

def forceMediaType(
originalMediaType: Option[ContentType],
eventInstant: Instant,
newMediaType: Option[ContentType]
) = {
val originalMediaTypeAsString = originalMediaType.map(_.toString())
(originalMediaType != newMediaType) ||
eventInstant.isBefore(instant) &&
(originalMediaTypeAsString == pngMediaType ||
originalMediaTypeAsString == tiffMediaType)
}

private val noop = new EventProcessor[FileEvent] {
override def resourceType: EntityType = Files.entityType

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import akka.http.scaladsl.model.{ContentTypes, MediaTypes}
import ch.epfl.bluebrain.nexus.delta.kernel.http.MediaTypeDetectorConfig
import ch.epfl.bluebrain.nexus.testkit.mu.NexusSuite

import java.time.Instant

class FileProcessorSuite extends NexusSuite {

implicit private val mediaTypeDetector: MediaTypeDetectorConfig = MediaTypeDetectorConfig(
Expand All @@ -25,4 +27,47 @@ class FileProcessorSuite extends NexusSuite {
)
}

test("Patching media type for a media type that changes") {
assertEquals(
FileProcessor.forceMediaType(
Some(ContentTypes.`application/octet-stream`),
Instant.now,
Some(ContentTypes.`application/json`)
),
true
)
}

test("Patching media type for a png file older than the given instant") {
val imagePng = MediaTypes.`image/png`.toContentType
assertEquals(
FileProcessor.forceMediaType(Some(imagePng), Instant.parse("2021-04-11T19:19:16.578Z"), Some(imagePng)),
true
)
}

test("Not patching media type for a png file after the given instant") {
val imagePng = MediaTypes.`image/png`.toContentType
assertEquals(
FileProcessor.forceMediaType(Some(imagePng), Instant.parse("2021-04-11T19:19:16.580Z"), Some(imagePng)),
false
)
}

test("Patching media type for a tiff file older than the given instant") {
val imageTiff = MediaTypes.`image/tiff`.toContentType
assertEquals(
FileProcessor.forceMediaType(Some(imageTiff), Instant.parse("2021-04-11T19:19:16.578Z"), Some(imageTiff)),
true
)
}

test("Not patching media type for a fiff file after the given instant") {
val imageTiff = MediaTypes.`image/tiff`.toContentType
assertEquals(
FileProcessor.forceMediaType(Some(imageTiff), Instant.parse("2021-04-11T19:19:16.580Z"), Some(imageTiff)),
false
)
}

}

0 comments on commit 2f475a4

Please sign in to comment.