Play Framework:在PRODUCTION模式下处理动态创建的文件(图像) [英] Play Framework: Handling dynamic created files (images) in PRODUCTION mode

查看:106
本文介绍了Play Framework:在PRODUCTION模式下处理动态创建的文件(图像)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试允许用户将照片上传到服务器,然后在生产(非开发)中查看它们(所有用户都可以查看所有照片)。在开发模式中,一切都很简单 - 我可以将文件上传到公共文件夹,然后从那里读取,在生产模式我无法访问公众文件夹不再(因为这种方法适用于静态访问而非动态)。

I'm trying to allow users to upload photos to the server and then view them (all users can view all photos) in production (NOT development). While in development mode everything is simple - I can upload the files to the public folder and then read then from there, in production mode I don't have access to the public folder anymore (as this approach is for static accesses and not dynamic).

所以,我有2个问题:


  1. 上传:目前我无法理解如何将上传的照片保存到特定文件夹,而不使用绝对路径指向我希望保存照片的位置。

  1. Upload: currently I can't understand how to save the uploaded photos to a specific folder, without using an absolute path to the location where I want the photos to be saved.

以下是上传代码(与本指南类似 - https://www.playframework.com/documentation/2.4.x/ScalaFileUpload ):

Here is the code for upload (similarly to this guide - https://www.playframework.com/documentation/2.4.x/ScalaFileUpload):

def uploadPhoto = Action(parse.multipartFormData) { request =>
  import play.api.mvc.MultipartFormData
  import play.api.libs.Files.TemporaryFile
  import java.io.File
  import java.nio.file.Path
  import java.nio.file.Paths

  try {
    val multipartForm: MultipartFormData[TemporaryFile] = request.body

    val pathDev: Path = Paths.get("./public/img");
    val pathProduction: Path = Paths.get("/...<[full path]>.../public/img");
    val imgPath =
      if (Files.exists(pathDev)) { pathDev.toString() } 
      else { pathProduction.toString() }

    val newFile = img.get.ref.moveTo(new File(s"$imgPath/$imgName"))
    // [HERE I save the path to my DB]
    Ok(Json.obj("imgName" -> imgName))

  } catch {
    case e: Exception =>
      BadRequest("unknown error")
  }
}




  1. 我不清楚如何将上传的图片提供给用户以便查看它们。
    我想动态更改img html标签中的scr以显示相关图像,如下所示:$(#img)。attr(src,'assets / img / 1.jpg');

  1. It is unclear to me how to serve the uploaded images back to users that want to see them. I want to dynamically change the scr in the img html tag to show the relevant image, like so: $("#img").attr("src",'assets/img/1.jpg');

但是由于公共文件夹不可用,图像不存在(至少在我将暂存项目之前并重新运行它 - https://www.playframework.com/documentation/2.4。 x /资产)。

But as the public folder is not available, the images are "not there" (at least until I will "stage" the project and re-run it - https://www.playframework.com/documentation/2.4.x/Assets).

我尝试了以下方法( https://www.playframework.com/documentation/2.4.x/ScalaStream ):
我已将以下行添加到我的conf / routes文件中:
GET / img /:filename controllers.MyController.getPhoto(filename)

I tried the following approach (https://www.playframework.com/documentation/2.4.x/ScalaStream): I have added the following line to my conf/routes file: GET /img/:filename controllers.MyController.getPhoto(filename)

并在控制器中定义了以下函数:
def getPhoto(filename :String)= Action {
Ok.sendFile(new java.io.File(./ img /) + filename))
}

and defined the following function in the controller: def getPhoto(filename: String) = Action { Ok.sendFile(new java.io.File("./img/" + filename)) }

但是浏览器正在下载文件而不是显示它...

But the browser is downloading the file instead of showing it...

这些是相关的:
处理游戏中动态创建的文件2
如何提供上传的文件Play!2使用Scala?

任何帮助都将非常合适。

Any assistance will be very appropriated.

推荐答案

以下是我如何解决这个问题

Here's how I fix this

ISSUE 1

对于上传文件路径,在游戏中你可以在conf / application.conf文件中定义配置,你可以使用不同的文件进行生产模式,使用-Dconfig.file = / path /到/ / /。

For upload file path, in play you can define configurations in conf/application.conf file, and you can use different file for production mode, using -Dconfig.file=/path/to/the/file.

所以我定义了一个名为myapp.image.base的属性,在调试模式下只需将其设置为,并在生产模式下(我创建)一个名为conf / application.prod.conf的文件,我给它一个绝对路径。
所以在我的代码中,我总是使用以下命令来获取文件路径(它是用Java编写的,但你应该在Scala中找到类似的方法来读取配置)

So I defined an attribuite called myapp.image.base, in debug mode just set it to "", and in production mode (I created a file called conf/application.prod.conf) , I put an absolute path to it. So in my code, I always use the following command for file path (it's in Java, but you should find a similar way in Scala for reading configuration)

Play.application().configuration().getString("myapp.image.base")+"img" 

问题2

用于提供图像
您需要创建一个路由器。
首先在你的路线文件中添加如下内容:

For serving image You need to create a router. First in your routes file, add something like this:

GET /user/images/:name controllers.Application.imageAt(name:String)

并在动作中写一个简单的文件阅读器imageAt返回文件流。我的样本再次使用Java,但您应该使用Scala将其归档

And write a simple file reader in action imageAt which return the file in stream. Again my sample is in Java but you should archive the same using Scala

    File imageFile = new File(ReportFileHelper.getImagePath());
    if (imageFile.exists()) {
    //resource type such as image+png, image+jpg
        String resourceType = "image+"+imageName.substring(imageName.length()-3);
        return ok(new FileInputStream(imageFile)).as(resourceType);
    } else {
        return notFound(imageFile.getAbsoluteFile());
    }

之后,图片可以从url / user / images /

After that, the images is reachable from url /user/images/

这篇关于Play Framework:在PRODUCTION模式下处理动态创建的文件(图像)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆