播放:附加的公共/资产目录 [英] Play: additional public / assets directory
问题描述
为了拥有一个干净的目录结构,我想将其他资产文件夹设为公用.我在项目文件夹中创建了一个目录资产",编写了一个与"controller.Assets"几乎相同的"PictureAssets"控制器,在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.
PictureAssets:
PictureAssets:
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
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引用由控制器"操作.资产不再起作用.
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屋!