加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 播放:其他公共/资源目录

发布时间:2020-12-16 09:28:44 所属栏目:安全 来源:网络整理
导读:为了拥有一个干净的目录结构,我想公开一个额外的资产文件夹.我在我的项目文件夹中创建了一个目录’assets’,编写了一个’PictureAssets’控制器,它几乎与’controller.Assets’相同,将’资产’添加到build.sbt中的playAssetsDirectories并尝试跟踪一些路由条
为了拥有一个干净的目录结构,我想公开一个额外的资产文件夹.我在我的项目文件夹中创建了一个目录’assets’,编写了一个’PictureAssets’控制器,它几乎与’controller.Assets’相同,将’资产’添加到build.sbt中的playAssetsDirectories并尝试跟踪一些路由条目成功.

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

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’不再起作用了.

我错过了什么?

解决方法

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

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

我创建了一个名为Photos的简单控制器,其中包含一个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
    }
  }

}

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

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

这对我来说很好.希望这可以帮助.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读