Skip to content
Permalink
Browse files

Merge pull request #9 from derNiklaas/master

Added File Connector
  • Loading branch information...
sebinside committed Jun 11, 2019
2 parents 45dacb8 + b485e15 commit ac09a8ce62468ef84ae93370dcf58c0eaf0be9b4
@@ -40,7 +40,7 @@ class FileSystemActor extends Actor {
try {
sender ! Some(Files.readAllBytes(fixPath(pathInResources).toPath))
} catch {
case e: Exception => None
case _: Exception => None
}
case SaveFile(pathInResources, content) =>
try {
@@ -0,0 +1,47 @@
package org.codeoverflow.chatoverflow.requirement.service.file

import org.codeoverflow.chatoverflow.WithLogger
import org.codeoverflow.chatoverflow.connector.Connector
import org.codeoverflow.chatoverflow.connector.actor.FileSystemActor
import org.codeoverflow.chatoverflow.connector.actor.FileSystemActor._

class FileConnector(override val sourceIdentifier: String) extends Connector(sourceIdentifier) with WithLogger {
override protected var requiredCredentialKeys: List[String] = List()
override protected var optionalCredentialKeys: List[String] = List()
private val fileActor = createActor[FileSystemActor]()

def getFile(pathInResources: String): Option[String] = {
val file: Option[Option[String]] = fileActor.??[Some[String]](5) {LoadFile(pathInResources)}
if (file.isDefined) {
file.get
}else{
None
}
}

def getBinaryFile(pathInResources: String): Option[Array[Byte]] = {
val binaryFile: Option[Option[Array[Byte]]] = fileActor.??[Some[Array[Byte]]](5){LoadBinaryFile(pathInResources)}
if(binaryFile.isDefined){
binaryFile.get
}else{
None
}
}

def saveFile(pathInResources: String, content: String): Boolean = fileActor.??[Boolean](5){SaveFile(pathInResources, content)}.get

def saveBinaryFile(pathInResources: String, content: Array[Byte]): Boolean = fileActor.??[Boolean](5){SaveBinaryFile(pathInResources, content)}.get

def createDirectory(folderName: String): Boolean = fileActor.??[Boolean](5){CreateDirectory(folderName)}.get

override def start(): Boolean = {
logger info s"Started file connector! Source identifier is: '$sourceIdentifier'."
true
}

override def stop(): Boolean = {
logger info "Stopped file connector!"
true
}

}
@@ -0,0 +1,35 @@
package org.codeoverflow.chatoverflow.requirement.service.file.impl

import java.awt.image.BufferedImage
import java.io.ByteArrayInputStream
import java.util.Optional

import javax.imageio.ImageIO
import org.codeoverflow.chatoverflow.WithLogger
import org.codeoverflow.chatoverflow.api.io.input.FileInput
import org.codeoverflow.chatoverflow.registry.Impl
import org.codeoverflow.chatoverflow.requirement.InputImpl
import org.codeoverflow.chatoverflow.requirement.service.file.FileConnector

@Impl(impl = classOf[FileInput], connector = classOf[FileConnector])
class FileInputImpl extends InputImpl[FileConnector] with FileInput with WithLogger {

override def init(): Boolean = {
sourceConnector.get.init()
}

override def getFile(pathInResources: String): Optional[String] = Optional.ofNullable(sourceConnector.get.getFile(pathInResources).orNull)

override def getBinaryFile(pathInResources: String): Optional[Array[Byte]] = Optional.ofNullable(sourceConnector.get.getBinaryFile(pathInResources).orNull)

override def getImage(pathInResources: String): Optional[BufferedImage] = {
val data = sourceConnector.get.getBinaryFile(pathInResources)
if (!data.isDefined) {
None
}
val bis = new ByteArrayInputStream(data.get)
Optional.of(ImageIO.read(bis))
}

override def start(): Boolean = true
}
@@ -0,0 +1,40 @@
package org.codeoverflow.chatoverflow.requirement.service.file.impl

import java.awt.image.BufferedImage
import java.io.ByteArrayOutputStream

import javax.imageio.ImageIO
import org.codeoverflow.chatoverflow.WithLogger
import org.codeoverflow.chatoverflow.api.io.output.FileOutput
import org.codeoverflow.chatoverflow.registry.Impl
import org.codeoverflow.chatoverflow.requirement.OutputImpl
import org.codeoverflow.chatoverflow.requirement.service.file.FileConnector

@Impl(impl = classOf[FileOutput], connector = classOf[FileConnector])
class FileOutputImpl extends OutputImpl[FileConnector] with FileOutput with WithLogger {

override def init() = {
sourceConnector.get.init()
}

override def saveFile(content: String, pathInResources: String): Boolean = {
sourceConnector.get.saveFile(pathInResources, content)
}

override def saveBinaryFile(bytes: Array[Byte], pathInResources: String): Boolean = {
sourceConnector.get.saveBinaryFile(pathInResources, bytes)
}

override def saveImage(image: BufferedImage, format: String, pathInResources: String): Boolean = {
val bao = new ByteArrayOutputStream()
ImageIO.write(image, format.toLowerCase, bao)
sourceConnector.get.saveBinaryFile(s"$pathInResources.${format.toLowerCase}", bao.toByteArray)
}

override def createDirectory(folderName: String): Boolean = {
sourceConnector.get.createDirectory(folderName)
}

override def start() = true

}

0 comments on commit ac09a8c

Please sign in to comment.
You can’t perform that action at this time.