bitshow-tmp /src/main/scala/proc/MagickRequest.scala

Language Scala Lines 107
MD5 Hash f3b8d2605e4d0540e60b4cd247bf08d0
Repository https://github.com/bmatheny/bitshow-tmp.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
package bitshow.proc

import bitshow.{Item, Converter}
import java.io._
import io.Source
import java.lang.StringBuffer

object MagickRequest {
  import scala.collection.JavaConverters._
  private val environment = System.getenv().asScala
  val MAGICK_KEY = "MAGICK_KEY"
  val MAGICK_PATH = environment.getOrElse(MAGICK_KEY, "/usr/local/bin/convert")
}

object MagickResize
  extends MagickRequest("resize", "-resize 50%")

object MagickBlackAndWhite
  extends MagickRequest("blackwhite", "-black -white")

/**
 * Unused
 */
class ArgStack {
  val stringBuffer = new StringBuffer

  /** appends a -XXX type option
   */
  def option(a: String, b:Any) {
    stringBuffer.append("-").append(a).append(" ").append(b)
    newArg
  }

  /** adds a bit of space between arguments
   */
  def newArg {
    stringBuffer.append(" ")
  }

  def arg(a: String) {
    stringBuffer.append(a)
    newArg
  }

  def toCommandLine =
    stringBuffer.toString
}

class MagickRequest (
  val name: String,
  val args: String
) extends Converter {

  val BUFFER_SIZE = 1024 * 1024

  protected def getConvertPath =
    MagickRequest.MAGICK_PATH

  private def getTemporaryFile =
    File.createTempFile("bitshow", ".png")

  private def writeBytesToTemporaryFile(input: InputStream) = {
    val bufferedIn = new BufferedInputStream(input)
    val outFile = getTemporaryFile
    val bufferedOut = new BufferedOutputStream(new FileOutputStream(outFile))

    @annotation.tailrec()
    def readByte() {
      bufferedOut.write(bufferedIn.read)
      
      if(bufferedIn.available() > 0)
        readByte()
    }

    readByte()
    
    bufferedOut.close()
    bufferedIn.close()

    outFile
  }

  private def readBytesFromFile(file: File) =
    Source.fromFile(file)(scala.io.Codec.ISO8859).map(_.toByte).toArray

  def buildCommandLine(inputFile: File, outputFile: File) =
    getConvertPath+" "+inputFile.getAbsolutePath+" "+args+" "+outputFile.getAbsolutePath

  def execute(inputFile: File, outputFile: File): Int = {
    import scala.sys.process._
    (buildCommandLine(inputFile, outputFile)) !
  }

  def execute(inputFile: String, outputFile: String): Int =
    execute(new File(inputFile), new File(outputFile))

  def execute(input: InputStream): Array[Byte] = {
    val inputFile = writeBytesToTemporaryFile(input)
    val outputFile = getTemporaryFile

    val exitCode = execute(inputFile, outputFile)
    readBytesFromFile(outputFile)
  }

  def apply(item: Item) =
    item
}
Back to Top