Skip to content

Commit

Permalink
Remove temporary patch for png and tiff files (#5108)
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 19, 2024
1 parent 2f475a4 commit 136919c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ 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 @@ -73,7 +71,7 @@ class FileProcessor private (
val newMediaType = patchMediaType(attrs.filename, attrs.mediaType)
val newAttrs = e.attributes.copy(mediaType = newMediaType)
val customMetadata = Some(getCustomMetadata(newAttrs))
val fct = forceMediaType(attrs.mediaType, e.instant, newMediaType)
val fct = forceMediaType(attrs.mediaType, newMediaType)
fileCopier.copyFile(e.project, newAttrs, fct).flatMap {
case FileCopySuccess(newPath) =>
val linkRequest = FileLinkRequest(newPath, newMediaType, customMetadata)
Expand All @@ -87,7 +85,7 @@ class FileProcessor private (
val newMediaType = patchMediaType(attrs.filename, attrs.mediaType)
val newAttrs = e.attributes.copy(mediaType = newMediaType)
val customMetadata = Some(getCustomMetadata(newAttrs))
val fct = forceMediaType(attrs.mediaType, e.instant, newMediaType)
val fct = forceMediaType(attrs.mediaType, newMediaType)
fileCopier.copyFile(e.project, newAttrs, fct).flatMap {
case FileCopySuccess(newPath) =>
val linkRequest = FileLinkRequest(newPath, newMediaType, customMetadata)
Expand Down Expand Up @@ -139,21 +137,10 @@ 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)
}
): Boolean = originalMediaType != newMediaType

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,8 +4,6 @@ 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 @@ -31,41 +29,18 @@ class FileProcessorSuite extends NexusSuite {
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
test("Patching media type for a media type that does not change") {
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)),
FileProcessor.forceMediaType(
Some(ContentTypes.`application/json`),
Some(ContentTypes.`application/json`)
),
false
)
}
Expand Down

0 comments on commit 136919c

Please sign in to comment.