You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
4.0 KiB

  1. package com.sothr.imagetools.engine
  2. import java.io.File
  3. import akka.actor.{Actor, ActorLogging, ActorRef, ActorSystem}
  4. import AppConfig
  5. import com.sothr.imagetools.engine.image.{SimilarImages, ImageFilter, Image}
  6. import com.sothr.imagetools.image.SimilarImages
  7. import com.sothr.imagetools.engine.util.DirectoryFilter
  8. import grizzled.slf4j.Logging
  9. import scala.collection.mutable
  10. /**
  11. * Engine definition
  12. *
  13. * Created by drew on 1/26/14.
  14. */
  15. abstract class Engine extends Logging {
  16. val system = ActorSystem("EngineActorSystem")
  17. val imageFilter:ImageFilter = new ImageFilter()
  18. val imageCache = AppConfig.cacheManager.getCache("images")
  19. def setProcessedListener(listenerType: ActorRef)
  20. def setSimilarityListener(listenerType: ActorRef)
  21. def getAllImageFiles(directoryPath:String, recursive:Boolean=false, recursiveDepth:Int=500):List[File] = {
  22. val fileList = new mutable.MutableList[File]()
  23. if (directoryPath != null && directoryPath != "") {
  24. val directory:File = new File(directoryPath)
  25. val imageFilter = new ImageFilter
  26. if (directory.isDirectory) {
  27. val files = directory.listFiles(imageFilter)
  28. if (files != null) {
  29. fileList ++= files
  30. info(s"Found ${files.length} files that are images in directory: $directoryPath")
  31. if (recursive) {
  32. val directoryFilter = new DirectoryFilter
  33. val directories = directory.listFiles(directoryFilter)
  34. for (directory <- directories) {
  35. fileList ++= getAllImageFiles(directory.getAbsolutePath, recursive, recursiveDepth-1)
  36. }
  37. }
  38. }
  39. }
  40. }
  41. fileList.toList
  42. }
  43. /**
  44. * Get all images for a directory with hashes
  45. */
  46. def getImagesForDirectory(directoryPath:String, recursive:Boolean=false, recursiveDepth:Int=500):List[Image]
  47. /**
  48. * Get all similar images for a directory with hashes
  49. */
  50. def getSimilarImagesForDirectory(directoryPath:String, recursive:Boolean=false, recursiveDepth:Int=500):List[SimilarImages]
  51. }
  52. case class SubmitMessage(message:String)
  53. case class ScannedFileCount(count:Integer, total:Integer, message:String=null)
  54. case class ComparedFileCount(count:Integer,total:Integer, message:String=null)
  55. abstract class EngineListener extends Actor with ActorLogging {
  56. override def receive: Actor.Receive = {
  57. case command:SubmitMessage => handleMessage(command)
  58. case command:ScannedFileCount => handleScannedFileCount(command)
  59. case command:ComparedFileCount => handleComparedFileCount(command)
  60. case _ => log.info("received unknown message")
  61. }
  62. def handleMessage(command:SubmitMessage)
  63. def handleScannedFileCount(command:ScannedFileCount)
  64. def handleComparedFileCount(command:ComparedFileCount)
  65. }
  66. /**
  67. * Actor for logging output information
  68. */
  69. class DefaultLoggingEngineListener extends EngineListener with ActorLogging {
  70. override def handleComparedFileCount(command: ComparedFileCount): Unit = {
  71. if (command.message != null) {
  72. log.info(command.message)
  73. }
  74. log.info("Processed {}/{}",command.count,command.total)
  75. }
  76. override def handleScannedFileCount(command: ScannedFileCount): Unit = {
  77. if (command.message != null) {
  78. log.info(command.message)
  79. }
  80. log.info("Scanned {}/{} For Similarities",command.count,command.total)
  81. }
  82. override def handleMessage(command: SubmitMessage): Unit = {
  83. log.info(command.message)
  84. }
  85. }
  86. /**
  87. * Actor for writing progress out to the commandline
  88. */
  89. class CLIEngineListener extends EngineListener with ActorLogging {
  90. override def handleComparedFileCount(command: ComparedFileCount): Unit = {
  91. if (command.message != null) {
  92. System.out.println(command.message)
  93. }
  94. System.out.println(s"Processed ${command.count}/${command.total}")
  95. }
  96. override def handleScannedFileCount(command: ScannedFileCount): Unit = {
  97. if (command.message != null) {
  98. System.out.println(command.message)
  99. }
  100. System.out.println(s"Scanned ${command.count}/${command.total} For Similarities")
  101. }
  102. override def handleMessage(command: SubmitMessage): Unit = {
  103. System.out.println(command.message)
  104. }
  105. }