-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add media type detection when linking a file (#5191)
Co-authored-by: Simon Dumas <[email protected]>
- Loading branch information
Showing
27 changed files
with
503 additions
and
244 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...rc/main/scala/ch/epfl/bluebrain/nexus/delta/plugins/storage/files/MediaTypeDetector.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package ch.epfl.bluebrain.nexus.delta.plugins.storage.files | ||
|
||
import akka.http.scaladsl.model.{ContentType, HttpCharsets, MediaType, MediaTypes} | ||
import ch.epfl.bluebrain.nexus.delta.kernel.http.MediaTypeDetectorConfig | ||
import ch.epfl.bluebrain.nexus.delta.kernel.utils.FileUtils | ||
|
||
import scala.util.Try | ||
|
||
/** | ||
* Allows to detect a content type from incoming files from their extensions when the client has not provided one | ||
* | ||
* @param config | ||
* the config with a mapping from the extension to the content type | ||
*/ | ||
final class MediaTypeDetector(config: MediaTypeDetectorConfig) { | ||
|
||
def apply(filename: String, provided: Option[ContentType], fallback: Option[ContentType]): Option[ContentType] = { | ||
val extensionOpt = FileUtils.extension(filename) | ||
|
||
def detectFromConfig = for { | ||
extension <- extensionOpt | ||
customMediaType <- config.find(extension) | ||
} yield contentType(customMediaType) | ||
|
||
def detectAkkaFromExtension = extensionOpt.flatMap { e => | ||
Try(MediaTypes.forExtension(e)).map(contentType).toOption | ||
} | ||
|
||
provided | ||
.orElse(detectFromConfig) | ||
.orElse(detectAkkaFromExtension) | ||
.orElse(fallback) | ||
} | ||
|
||
private def contentType(mediaType: MediaType) = ContentType(mediaType, () => HttpCharsets.`UTF-8`) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.