如何在Play Framework 2.X Scala应用程序中列出公共目录中的所有文件?
这是我的控制器如何在Play Framework 2.X Scala应用程序中列出公共目录中的所有文件?,scala,heroku,playframework,playframework-2.0,Scala,Heroku,Playframework,Playframework 2.0,这是我的控制器 class Proguard extends Controller { val proguardFolder = "/public/proguards/" val proguardFolderFix = "/public/proguards" val proguardSuffix = "proguard-" val proguardExtension = ".pro" val title = "# Created by https://www.proguar
class Proguard extends Controller {
val proguardFolder = "/public/proguards/"
val proguardFolderFix = "/public/proguards"
val proguardSuffix = "proguard-"
val proguardExtension = ".pro"
val title = "# Created by https://www.proguard.io/api/%s\n\n%s"
def proguard(libraryName: String) = Action {
val libraries = libraryName.split(',')
val availableLibs = listInDir(proguardFolderFix)
val result = availableLibs.filter(libraries.contains).map(readFile).mkString
Ok(title.format(libraryName, result))
}
def list() = Action {
Ok(Json.toJson(listInDir(proguardFolder)))
}
private def listInDir(filePath: String): List[String] = {
getListOfFiles(Play.getFile(filePath)).map(_.getName.replace(proguardExtension, "").replace(proguardSuffix, ""))
}
def getListOfFiles(dir: File): List[File] = {
dir.listFiles.toList
}
def readFile(string: String): String = {
val source = scala.io.Source.fromFile(Play.getFile(s"$proguardFolder$proguardSuffix$string$proguardExtension"))
val lines = try source.mkString finally source.close()
lines
}
}
它在调试模式下运行完全正常,但在Heroku的生产中,dir.listFiles.
给了我NPE
我尝试了不同的方法,但似乎唯一的解决方案是将文件移动到s3或数据库。您可以通过运行
Heroku run bash
并使用ls
查找来检查Heroku上是否存在目录吗?您是如何填充公共目录的?你上传到start
ed应用程序的任何内容都不会上传到那里。只有在您启动应用程序之前存在的资源才会被列出。@codefinger看起来像是,heroku@0fnt上存在文件夹和文件在生成之前,所有文件都在我的公用文件夹中,并随公用文件夹一起上载。您能否通过运行heroku run bash
并使用ls
查找来检查heroku上是否存在目录?您如何填充公用目录?你上传到start
ed应用程序的任何内容都不会上传到那里。只有在您启动应用程序之前存在的资源才会被列出。@codefinger看起来像是“是”,heroku@0fnt上存在文件夹和文件所有文件在生成之前都在我的公用文件夹中,并随它一起上载。