播放:附加公共/资产目录 [英] Play: additional public / assets directory

查看:13
本文介绍了播放:附加公共/资产目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了有一个干净的目录结构,我想公开一个额外的资产文件夹.我在我的项目文件夹中创建了一个目录assets",编写了一个与controller.Assets"几乎相同的PictureAssets"控制器,将assets"添加到 build.sbt 中的 playAssetsDirectories 并尝试遵循一些路由条目而没有成功.

In order to have a clean directory structure, I would like to make an additional assets folder public. I've created a directory 'assets' in my project folder, wrote an 'PictureAssets' controller which is nearly identical to 'controller.Assets', added 'assets' to the playAssetsDirectories in the build.sbt and tried following some route entries without success.

图片资源:

package controllers

import play.api.mvc.Action
import play.api.mvc.AnyContent

object PictureAssets extends AssetsBuilder {
   def create : Action[AnyContent]  =  Action {
      Ok(views.html.fileUploadForm())
   }
}

build.sbt

playAssetsDirectories <+= baseDirectory / "assets"

路线

# GET     /file                 controllers.PictureAssets.at(path="/assets", file="MM1.png")
GET     /datei/*file            controllers.PictureAssets.at(path="/assets", file)
# GET       /datei/*file            controllers.Assets.at(path="/assets", file)

如果我尝试访问 URL,则要么不显示任何内容,要么显示错误图像 http:9000//localhost/datei/MM1.png 无法显示,因为它包含错误"或处理的 css 引用'controller.Assets' 不再起作用.

If I try to access the URL, either nothing is displayed, or the error 'The image http:9000//localhost/datei/MM1.png cannot be displayed because it contains errors' is displayed or the css references to handled by the 'controller.Assets' don't work any more.

我错过了什么?

推荐答案

我认为问题来自于 at 使用的方法是 Assets 之前使用的默认方法代码>.

I think the issue comes from the fact that the at method used is the default one used previously by Assets.

我在去年的某个时候遇到了同样的问题,我想提供将存储在外部文件夹中的图像,该文件夹位于磁盘上的某个位置,这是我的编码方式:

I ran into the same issue at some point last year, where I wanted to serve images that would be stored in a external folder, a folder that is somewhere on disk, and here is how I coded this:

我创建了一个名为 Photos 的简单控制器,其中包含一个 Action:

I created a simple controller called Photos, that contained one Action:

object Photos extends Controller {

  val AbsolutePath = """^(/|[a-zA-Z]:\).*""".r

  /**
   * Generates an `Action` that serves a static resource from an external folder
   *
   * @param absoluteRootPath the root folder for searching the static resource files.
   * @param file the file part extracted from the URL
   */
  def at(rootPath: String, file: String): Action[AnyContent] = Action { request =>
    val fileToServe = rootPath match {
      case AbsolutePath(_) => new File(rootPath, file)
      case _ => new File(Play.application.getFile(rootPath), file)
    }

    if (fileToServe.exists) {
      Ok.sendFile(fileToServe, inline = true)
    } else {
      Logger.error("Photos controller failed to serve photo: " + file)
      NotFound
    }
  }

}

然后,在我的路线中,我定义了以下内容:

Then, in my routes, I defined the following:

GET /photos/*file   controllers.Photos.at(path="/absolute/path/to/photos",file)

这对我来说效果很好.希望这会有所帮助.

This worked just fine for me. Hope this helps.

PS:这是对帮助提供 js 和 css 文件的普通 Assets 控制器的补充.

PS: This was in addition to the normal Assets controller that helped serving js and css files.

这篇关于播放:附加公共/资产目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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