【问题标题】:Asset mapping outside public folder on Play frameworkPlay框架上公用文件夹外的资产映射
【发布时间】:2015-07-13 23:27:10
【问题描述】:

我们有大量图像需要存储在外部路径中……即在播放应用程序文件夹之外。

我们如何使它可以作为资产播放,以便将其作为网络服务器流式传输?

【问题讨论】:

  • 您不需要以 > 括号开始您的问题。它导致您的整个问题被格式化为摘录/块。
  • 抱歉,这是我的第一个问题,以后会更正。感谢更新

标签: playframework playframework-2.2


【解决方案1】:

您可能已经看过 Play 的 documentation about Assets。除了 Play 的标准资源,您还可以定义自己的资源。

conf/routes 中,您需要为您的外部资产添加一行:

# Static resources (not managed by the Play framework)
GET     /my_external_assets/*file         controllers.ExtAssets.at(file)
# Play's standard assets
GET     /assets/*file                     controllers.Assets.at(path = "/public", file)

然后你必须定义你的资产控制器。一个简单的例子:

public class ExtAssets extends Controller {

    public Result at(String filePath) {
        File file = new File(filePath);
        return ok(file, true);
    }
}

【讨论】:

  • 这样安全吗?当文件路径设置为“../conf/application.conf”时说
  • 它会返回 '../conf/application.conf' 的内容,这可能不是你想要的。我只是举了一个简单的例子。如果你想要额外的限制,你必须自己添加它们。 :)
【解决方案2】:

为了完整起见,我已经多次遇到这个问题,但一直没有足够的答案。

通常 nginx 会面向外部世界和reverse-proxy back into the application server。您不想直接从 Play 提供文件,特别是如果您自己构建可能存在安全风险的路径。让高手来处理静态文件,即nginx。

Play Framework supports sbt-native-packager 的"dist" directory which will attach any files 就在分发 ZIP 包 [1] 中。以下是相关文档:

dist    → Arbitrary files to be included in your projects distribution

对于controlled downloads 等用例,请使用nginx's X-Accel,其中通过响应标头告诉 nginx 要将什么文件发送回客户端。


TLDR: 将 nginx 用于其用途,将 Play 用于其用途。

【讨论】:

【解决方案3】:

一个 hacky 解决方案是在您的 sbt play 公共文件夹中包含一个符号链接,指向您要加载文件的位置。符号链接将打包在程序集中并在运行时起作用。

cd ~/your/playframework/awesome/project/public
ln -s '/var/lib/funky-data-set-for-my-awesome-project/' funky-data

现在你可以在你的路由文件中使用类似的东西:

GET         /funky/assets/*file                controllers.Assets.at(path="/public/funky-data", file)

这样做的缺点是您的 stage/dev/prod 环境必须在同一位置具有您需要的内容。

【讨论】:

    【解决方案4】:

    播放 2.5

    文件:路线

    GET  /external_resources/*file controllers.ImagesController.getImage(path="/home/project_name/external/images", file: String)
    

    文件:图像控制器

    package controllers;
    
    import play.mvc.Controller;
    import play.mvc.Result;
    
    import java.io.File;
    
    /**
     * Created by js on 6/1/17.
     */
    public class ImagesController extends Controller {
    
    public Result getImage(String path, String image) {
        System.out.println(path);
        System.out.println(image);
        image = image.replace("%20", " ");
        File file = new File(path + image);
    
        return ok(file);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-13
      • 1970-01-01
      • 2016-11-06
      • 1970-01-01
      • 1970-01-01
      • 2017-04-02
      • 1970-01-01
      • 2012-10-23
      • 2015-08-08
      相关资源
      最近更新 更多