This repository was archived by the owner on Aug 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Added File Connector #9
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fb2a8b7
First Version of File Connectors
derNiklaas 90a0a40
Removed console log
derNiklaas 1dacc40
Fixed crash if file does not exists
derNiklaas 3fb608a
Changed return values to optionals
derNiklaas 98feebd
Added createDirectory and changed Image methods
derNiklaas 0f193a1
Merge branch 'master' into master
derNiklaas ca02c89
Improved performance when loading files
derNiklaas ba3cbc7
Merge branch 'master' of https://github.com/derNiklaas/chatoverflow
derNiklaas b485e15
Merge pull request #1 from codeoverflow-org/master
derNiklaas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/scala/org/codeoverflow/chatoverflow/requirement/service/file/FileConnector.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
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] = { | ||
if (fileActor.??[Some[String]](5) {LoadFile(pathInResources)}.isDefined) { | ||
fileActor.??[Some[String]](5) {LoadFile(pathInResources)}.get | ||
}else{ | ||
None | ||
} | ||
} | ||
|
||
def getBinaryFile(pathInResources: String): Option[Array[Byte]] = { | ||
if(fileActor.??[Some[Array[Byte]]](5){LoadBinaryFile(pathInResources)}.isDefined){ | ||
derNiklaas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fileActor.??[Some[Array[Byte]]](5){LoadBinaryFile(pathInResources)}.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 | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
...ain/scala/org/codeoverflow/chatoverflow/requirement/service/file/impl/FileInputImpl.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
40 changes: 40 additions & 0 deletions
40
...in/scala/org/codeoverflow/chatoverflow/requirement/service/file/impl/FileOutputImpl.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.