score:0
Ah, We need to use Streaming HTTP responses to do this.
Here is the documentation.... https://www.playframework.com/documentation/2.6.x/ScalaStream
my controller code will be
package controllers
import akka.stream.scaladsl.{FileIO, Source}
import akka.util.ByteString
import javax.inject.{Inject, Singleton}
import play.api.http.HttpEntity
import play.api.mvc._
@Singleton
class ImageServerController @Inject()(cc: ControllerComponents)(implicit assetsFinder: AssetsFinder)
extends AbstractController(cc) {
def serveImages(imageName:String):Action[AnyContent] = Action {
val file = new java.io.File("/tmp/images/"+imageName)
val path: java.nio.file.Path = file.toPath
val source: Source[ByteString, _] = FileIO.fromPath(path)
Result(
header = ResponseHeader(200, Map.empty),
body = HttpEntity.Streamed(source, None, Some("image/jpeg"))
)
}
}
score:1
Here are two options :
1) After the upload of the file, you move it to a folder (for example /uploads) created in your app directory. Then you serve these images via Apache which is more adapted than play to serve assets.
You need to have Apache as a reverse proxy of your application and configure a url to serve assets instead or redirecting to your application.
2) You also move the file to a folder (/uploads) and then you create a route in you application
GET /img/:name controllers.MyController.serveImage(name: String)
and in your controller :
public Result serveImage(String name) {
String path = "uploads/" + name;
return ok(new File(path));
}
The benefit of 2) is that you can manage authorisations when serving the file depending on the user.
Source: stackoverflow.com
Related Query
- what is the way of accessing app outer folder files to render in template of play framework 2.6.x?
- What is the proper way to format a double in a Play 2 template
- Play framework 2 : What is a proper way to render the contents of parent template?
- Play framework : What is the best way to pass on scala template parameter to Javascript (angular)?
- What is the fastest way to compile Scala files using maven?
- What is the proper way to insert/update a BLOB with Play 2.3?
- What is the correct way to identify if the folder exist on ADLS gen 2 account or not
- What is the correct way to configure actors in Play 2.x?
- IntelliJ: Deploying Play 2.0 app to .war file; what do I put in the manifest?
- What is the best way to get the paths of all the files located in a GCS bucket from Spark in Scala?
- What is the canonical way to do a one-to-many outer join with NULLs and case classes?
- What is the easiest way to use python to read pureconfig config files created in scala?
- play framework - what is the proper way to return list of data?
- What is the easest way return objects as json response from action in play framework (scala)?
- Scala, Play framework, Specs2, Mockito, what is the correct way to verify order of calls?
- What is the proper way of using coffeescript in play framework?
- Scala: what is the best way to append an element to an Array?
- What is the correct way to get a subarray in Scala?
- What is the idiomatic scala way of finding, if a given string contains a given substring?
- What is the purpose of `scala-2.11` folder in IntelliJ IDEA
- What is the proper way to code a read-while loop in Scala?
- What is the fastest way to sum a collection in Scala
- What is the preferred way to implement 'yield' in Scala?
- What is the idiomatic way to pattern match sequence comprehensions?
- What is the best way to perform OAuth2 authentication using akka-http?
- What is the most straightforward way to parse JSON in Scala?
- In Spark, what is the right way to have a static object on all workers?
- What is the Play 2.5.x equivalent to acceptWithActor[String, JsValue]?
- What is the server side comment tag in scala templates in play framework?
- What is the proper way to remove elements from a scala mutable map using a predicate
More Query from same tag
- PlayFramework 2.5: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder"
- Returning <Case Class>.type with Case Class
- Is it possible to publishLocal in development mode with scala-sbt?
- Task serialization issue when using SparkML prediction model
- Inserting iterated value to scala list
- How to map sequence only if a condition applies with scala using an immutable approach?
- Scala - Flink Monitoring API (Upload Jobs)
- Modifying the source code of external libraries in Scala, IntelliJ
- Keep one zero and remove rest from the beginning if there are multiple leading 0
- Why does fold left expect (a -> b -> a) instead of (b -> a -> a)?
- scala.Some cannot be cast to custom object
- Complex Scala Type Inference w/ Lambda Expressions
- How to count a text input and sort by occurence with total in Scala
- How to keep my incoming websocket connection open all the time?
- Cats applicative lift `empty` value into an option
- Why Scala 'String' object's iterator and 'List[Int]' object's iterator are behaving differently here?
- MatchError after sorting a Set
- Scala : Nested getOrElse
- Is there any style guidelines about using implicit parameter with default value in scala?
- Write a parquet file with delta encoded coulmns
- Spring Security @Secured annotation and Scala controller
- Run specific test using gradle in scala test module
- Type inference in generic curried method in Scala
- Feeding values to Iterator "from inside"
- How does HasManyThrough differ from MappedManyToMany?
- View vs LazyList
- Can I get the extend-er class' name in Scala?
- Sum or Product Type?
- Scala format is causing issues when saving my main.js file in play
- Scala pattern match on type parameter