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.

142 lines
4.4 KiB

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