-
Notifications
You must be signed in to change notification settings - Fork 128
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
Adding play 3.0 support. Fixes #564 #565
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Copyright 2012-2013 Stephane Godbillon (@sgodbillon) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package play.modules.reactivemongo | ||
|
||
import scala.concurrent.{ ExecutionContext, Future } | ||
|
||
import play.api.http.{ HttpChunk, HttpEntity } | ||
import play.api.mvc.{ BodyParser, MultipartFormData, ResponseHeader, Result } | ||
|
||
import reactivemongo.api.bson.{ BSONDocument, BSONValue } | ||
import reactivemongo.api.bson.collection.BSONSerializationPack | ||
|
||
import akka.stream.Materializer | ||
|
||
object MongoController { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was no need for that, how is it related with support for Play 3? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The MongoController Trait references akka dependencies in the signature of functions and needed to be changed to pekko. |
||
type GridFS = reactivemongo.api.gridfs.GridFS[BSONSerializationPack.type] | ||
|
||
type GridFSBodyParser[T <: BSONValue] = BodyParser[ | ||
MultipartFormData[reactivemongo.api.gridfs.ReadFile[T, BSONDocument]] | ||
] | ||
|
||
type FileToSave[T <: BSONValue] = | ||
reactivemongo.api.gridfs.FileToSave[T, BSONDocument] | ||
|
||
/** `Content-Disposition: attachment` */ | ||
private[reactivemongo] val CONTENT_DISPOSITION_ATTACHMENT = "attachment" | ||
|
||
/** `Content-Disposition: inline` */ | ||
private[reactivemongo] val CONTENT_DISPOSITION_INLINE = "inline" | ||
|
||
} | ||
|
||
/** A mixin for controllers that will provide MongoDB actions. */ | ||
trait MongoController extends PlaySupport.Controller { | ||
self: ReactiveMongoComponents => | ||
|
||
import reactivemongo.api.Cursor | ||
import reactivemongo.akkastream.GridFSStreams | ||
import MongoController._ | ||
|
||
/** | ||
* Returns the current MongoConnection instance | ||
* (the connection pool manager). | ||
*/ | ||
protected final def connection = reactiveMongoApi.connection | ||
|
||
/** Returns the default database (as specified in `application.conf`). */ | ||
protected final def database = reactiveMongoApi.database | ||
|
||
/** | ||
* Returns a future Result that serves the first matched file, | ||
* or a `NotFound` result. | ||
*/ | ||
protected final def serve[Id <: BSONValue]( | ||
gfs: GridFS | ||
)(foundFile: Cursor[gfs.ReadFile[Id]], | ||
dispositionMode: String = CONTENT_DISPOSITION_ATTACHMENT | ||
)(implicit | ||
materializer: Materializer | ||
): Future[Result] = { | ||
implicit def ec: ExecutionContext = materializer.executionContext | ||
|
||
foundFile.headOption.collect { case Some(file) => file }.map { file => | ||
def filename = file.filename.getOrElse("file.bin") | ||
def contentType = file.contentType.getOrElse("application/octet-stream") | ||
|
||
def chunks = GridFSStreams(gfs).source(file).map(HttpChunk.Chunk(_)) | ||
|
||
Result( | ||
header = ResponseHeader(OK), | ||
body = HttpEntity.Chunked(chunks, Some(contentType)) | ||
).as(contentType) | ||
.withHeaders( | ||
CONTENT_LENGTH -> file.length.toString, | ||
CONTENT_DISPOSITION -> (s"""$dispositionMode; filename="$filename"; filename*="UTF-8''""" + java.net.URLEncoder | ||
.encode(filename, "UTF-8") | ||
.replace("+", "%20") + '"') | ||
) | ||
|
||
}.recover { case _ => NotFound } | ||
} | ||
|
||
protected final def gridFSBodyParser( | ||
gfs: Future[GridFS] | ||
)(implicit | ||
materializer: Materializer | ||
): GridFSBodyParser[BSONValue] = { | ||
implicit def ec: ExecutionContext = materializer.executionContext | ||
import play.api.libs.streams.Accumulator | ||
|
||
parse.multipartFormData { (in: Any) => | ||
in match { | ||
case PlaySupport.FileInfo(partName, filename, contentType) => | ||
Accumulator.flatten(gfs.map { gridFS => | ||
val fileRef = gridFS.fileToSave( // see Api.scala | ||
filename = Some(filename), | ||
contentType = contentType | ||
) | ||
|
||
val sink = GridFSStreams(gridFS).sinkWithMD5(fileRef) | ||
|
||
Accumulator(sink).map { ref => | ||
MultipartFormData.FilePart(partName, filename, contentType, ref) | ||
} | ||
}) | ||
|
||
case info => | ||
sys.error(s"Unsupported: $info") | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't think so
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you specify your comment ?