本文整理汇总了Scala中play.api.libs.Files类的典型用法代码示例。如果您正苦于以下问题:Scala Files类的具体用法?Scala Files怎么用?Scala Files使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Files类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: UploadZipRecipeForm
//设置package包名称以及导入依赖的类
package requests
import models.Password
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
import play.api.libs.Files
import play.api.mvc.{MultipartFormData, Request}
case class UploadZipRecipeForm(
version: String,
password: String,
file: MultipartFormData.FilePart[Files.TemporaryFile]
) {
def passwordRecord = {
Password(0L, version, MyBCrypt.createHash(password))
}
}
object UploadZipRecipeForm {
def fromReq(req: Request[MultipartFormData[Files.TemporaryFile]]): Option[UploadZipRecipeForm] = {
val data = req.body.dataParts
for {
version <- data.get("ver").flatMap(_.headOption)
password <- data.get("password").flatMap(_.headOption)
file <- req.body.file("zipFile")
} yield new UploadZipRecipeForm(version, password, file)
}
}
object MyBCrypt {
val bcrypt = new BCryptPasswordEncoder(12)
def createHash(password: String) = bcrypt.encode(password)
def authenticate(password: String, hash: String): Boolean =
bcrypt.matches(password, hash)
}
示例2: CameraRelayControllerTest
//设置package包名称以及导入依赖的类
package controllers
import com.intersection.camerarelay.UploadManager
import org.mockito.Matchers._
import org.scalatest.concurrent.{Futures, ScalaFutures}
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{BeforeAndAfter, FunSpec, Matchers}
import play.api.libs.Files
import play.api.mvc.{Request, Result}
import play.api.test.FakeRequest
import play.api.test.Helpers._
import org.mockito.Mockito._
import scala.concurrent.Future
class CameraRelayControllerTest extends FunSpec with Matchers with MockitoSugar with BeforeAndAfter with Futures with ScalaFutures {
var controller : CameraRelayController = _
var uploadManager : UploadManager = _
before {
uploadManager = mock[UploadManager]
controller = new CameraRelayController(uploadManager)
}
describe(classOf[CameraRelayControllerTest].getName) {
it("Rejects requests without a valid siteId header : X-Site-Id") {
val request: Request[Files.TemporaryFile] = FakeRequest().withHeaders(("Content-Type"->"application/octet-stream"), ("X-Link-Time"->"2016-12-12")).withBody(Files.TemporaryFile())
val result = controller.uploadVideoChunk().apply(request)
status(result) should equal(400)
}
it("Rejects requests without a valid Link-Time header : X-Link-Time") {
val request: Request[Files.TemporaryFile] = FakeRequest().withHeaders(("Content-Type"->"application/octet-stream"), ("X-Site-Id"->"mn-1234")).withBody(Files.TemporaryFile())
val result: Future[Result] = controller.uploadVideoChunk().apply(request)
status(result) should equal(400)
}
it("Accepts a fully formed request") {
import scala.concurrent.ExecutionContext.Implicits.global
when(uploadManager.handleVideoChunk(any[String],any[String],any[Files.TemporaryFile])).thenReturn(Future(()))
val request: Request[Files.TemporaryFile] = FakeRequest().withHeaders(("Content-Type"->"application/octet-stream"), ("X-Site-Id"->"mn-1234"), ("X-Link-Time"->"2016-12-12")).withBody(Files.TemporaryFile())
val result: Future[Result] = controller.uploadVideoChunk().apply(request)
status(result) should equal(202)
}
}
}
示例3: ExtractFile
//设置package包名称以及导入依赖的类
package models
import java.io.File
import com.github.pochi.runner.scripts.{ScriptRunner, ScriptRunnerBuilder}
import play.api.libs.Files
import play.api.mvc.{MultipartFormData, Request}
import services.Extracter
class ExtractFile(request: Request[MultipartFormData[Files.TemporaryFile]], kindOfBirthmark: Birthmark) {
val result = request.body.file("file").map { file =>
val filename = file.filename
val contentType = file.contentType
file.ref.moveTo(new File("files", filename))
// filename, kindOfBirthmark
// new Extracter().getExtractFile("files/" + file.filename, kindOfBirthmark.birthmark)
def getExtractFile: String ={
val builder: ScriptRunnerBuilder = new ScriptRunnerBuilder
val runner: ScriptRunner = builder.build
val arg: Array[String] = Array("./extract.js", "files/" + file.filename, kindOfBirthmark.birthmark)
runner.runsScript(arg)
file.filename + ".csv"
}
getExtractFile
}
val extractFile = result.get
println(result.get)
}
示例4: ExtractFile
//设置package包名称以及导入依赖的类
package models
import java.io.File
import com.github.pochi.runner.scripts.{ScriptRunner, ScriptRunnerBuilder}
import play.api.libs.Files
import play.api.mvc.{MultipartFormData, Request}
import services.Extracter
class ExtractFile(request: Request[MultipartFormData[Files.TemporaryFile]], kindOfBirthmark: Birthmark) {
val result = request.body.file("file").map { file =>
val filename = file.filename
val contentType = file.contentType
file.ref.moveTo(new File("files", filename))
// filename, kindOfBirthmark
// new Extracter().getExtractFile("files/" + file.filename, kindOfBirthmark.birthmark)
def getExtractFile: String ={
val builder: ScriptRunnerBuilder = new ScriptRunnerBuilder
val runner: ScriptRunner = builder.build
val arg: Array[String] = Array("./extract.js", "files/" + file.filename, kindOfBirthmark.birthmark)
runner.runsScript(arg)
file.filename + ".csv"
}
getExtractFile
}
val extractFile = result.get
}
示例5: iPostC
//设置package包名称以及导入依赖的类
package controllers
import javax.inject._
import play.api.Environment
import play.api.cache.CacheApi
import play.api.db.slick.DatabaseConfigProvider
import play.api.i18n.MessagesApi
import play.api.libs.Files
import play.api.mvc._
import services.iPostService
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
@Singleton
class iPostC @Inject()(dbConfigProvider: DatabaseConfigProvider, env: Environment, cache: CacheApi, messagesApi: MessagesApi)
extends iPostService(dbConfigProvider, env, cache, messagesApi: MessagesApi) {
def index = Action.async { implicit req: Request[_] =>
getIndexViewDto.flatMap { v =>
Future successful Ok(views.html.iPostC.index(v))
}
}
def reserve: Action[MultipartFormData[Files.TemporaryFile]] = Action.async(parse.multipartFormData) { implicit req: Request[_] =>
savePost.flatMap { _ =>
Future successful Redirect(routes.iPostC.index().url)
}
}
def post = Action.async { implicit req: Request[_] =>
postToInstagram.flatMap { _ =>
Future successful Ok("")
}
}
}
示例6: FacebookC
//设置package包名称以及导入依赖的类
package controllers
import javax.inject.{Inject, Singleton}
import play.api.Environment
import play.api.cache.CacheApi
import play.api.db.slick.DatabaseConfigProvider
import play.api.i18n.MessagesApi
import play.api.libs.Files
import play.api.mvc.{Action, MultipartFormData, Request}
import services.FacebookService
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
@Singleton
class FacebookC @Inject()(dbConfigProvider: DatabaseConfigProvider, env: Environment, cache: CacheApi, messagesApi: MessagesApi)
extends FacebookService(dbConfigProvider, env, cache, messagesApi) {
def auth = Action.async { implicit req: Request[_] =>
Future successful Redirect(AUTH_URL(req, env))
}
def callback(code: String) = Action.async { implicit req: Request[_] =>
super.callback(code).flatMap {
case None => Future successful Redirect(routes.LoginC.login().url)
case Some(account) => Future successful Redirect(routes.MyPageC.index().url).withSession(account.session)
}
}
def remove = Action.async { implicit req: Request[_] =>
removeToken.flatMap {
case false => Future successful Redirect(routes.LoginC.login().url)
case true => Future successful Redirect(routes.MyPageC.index().url)
}
}
def post: Action[MultipartFormData[Files.TemporaryFile]] = Action.async(parse.multipartFormData) { implicit req: Request[_] =>
postMessage.flatMap {
case Right(bool) =>
if (bool) Future successful Redirect(routes.SplashC.index().url)
else Future successful Redirect(routes.LoginC.login().url)
case Left(_) => Future successful Redirect(routes.FacebookC.auth().url)
}
}
}
示例7: UploadServiceSpec
//设置package包名称以及导入依赖的类
package services
import scala.concurrent.Future
import org.specs2.mock.Mockito
import org.specs2.mutable._
import org.specs2.runner._
import controllers.Application
import play.api.mvc.Controller
import play.api.mvc.Results
import play.api.test._
import play.api.test.Helpers._
import org.junit.runner.RunWith
import play.api.mvc.Result
import play.api.mvc.Request
import play.api.mvc.MultipartFormData
import play.api.libs.Files.TemporaryFile
import play.api.mvc.MultipartFormData.BadPart
import play.api.mvc.MultipartFormData.MissingFilePart
import play.api.mvc.MultipartFormData.FilePart
import play.api.libs.Files
class UploadServiceSpec extends Specification with Mockito {
"UploadService" should {
"uploadFile returns (File uploaded)" in new WithApplication {
val files = Seq[FilePart[TemporaryFile]](FilePart("file", "UploadServiceSpec.scala", None, TemporaryFile("file", "spec")))
val multipartBody = MultipartFormData(Map[String, Seq[String]](), files, Seq[BadPart](), Seq[MissingFilePart]())
val fakeRequest = FakeRequest[MultipartFormData[Files.TemporaryFile]]("POST", "/", FakeHeaders(), multipartBody)
val success = UploadService.uploadFile(fakeRequest)
success must beEqualTo("File(UploadServiceSpec.scala) uploaded")
}
"uploadFile returns (Missing file)" in new WithApplication {
val files = Seq[FilePart[TemporaryFile]]()
val multipartBody = MultipartFormData(Map[String, Seq[String]](), files, Seq[BadPart](), Seq[MissingFilePart]())
val fakeRequest = FakeRequest[MultipartFormData[Files.TemporaryFile]]("POST", "/", FakeHeaders(), multipartBody)
val success = UploadService.uploadFile(fakeRequest)
success must beEqualTo("Missing file")
}
}
}
示例8: WiredApiService
//设置package包名称以及导入依赖的类
package services
import java.time.OffsetDateTime
import play.api.libs.Files
import play.api.mvc.MultipartFormData
import shared.models.WiredApiModel.ApiResult
import shared.models.slick.default._
import shared.services.WiredApi
import shared.utils.Implicits
import shared.utils.LoremIpsum
class WiredApiService(user: User,
services: Services,
files: Seq[MultipartFormData.FilePart[Files.TemporaryFile]])
extends WiredApi
with Implicits {
override def ping(): ApiResult[String] = "pong".asResult
override def now(): ApiResult[OffsetDateTime] = OffsetDateTime.now.asResult
override def createLoremIpsum(): ApiResult[List[String]] = {
Thread.sleep(2000)
LoremIpsum.paragraphs(15).asResult
}
}
示例9: fop
//设置package包名称以及导入依赖的类
package bmlogic.common.files
import java.io.File
import play.api.libs.Files
import java.io.FileInputStream
import bmutil.errorcode.ErrorCode
import play.api.mvc.MultipartFormData
import play.api.libs.Files.TemporaryFile
import play.api.libs.json.Json
import play.api.libs.json.Json._
import play.api.libs.json.JsValue
object fop {
def uploadFile(data : MultipartFormData[TemporaryFile]) : JsValue = {
data.file("upload").map { x =>
Files.TemporaryFile(x.ref.file).moveTo(new File("upload/" + x.filename), true)
Json.toJson(Map("status" -> toJson("ok"),
"file_name" -> toJson(x.filename),
"result" -> toJson("success")))
}.getOrElse {
ErrorCode.errorToJson("post image error")
}
}
def downloadFile(name : String) : Array[Byte] = {
val file = new File("upload/" + name)
val reVal : Array[Byte] = new Array[Byte](file.length.intValue)
new FileInputStream(file).read(reVal)
reVal
}
}