Skip to content

Commit

Permalink
chore: Refactor to be less static
Browse files Browse the repository at this point in the history
  • Loading branch information
mpalourdio committed May 10, 2024
1 parent d0275fb commit 2035ab1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ class FlhackerApplication {
@Bean
fun run(): CommandLineRunner {
return CommandLineRunner { args ->
AudiofileHandler.setUp()
val cmd: CommandLine = CliHandler.run(args)
val audiofileHandler = AudiofileHandler(cmd.getOptionValue(CliHandler.FILE_CMD_LONG_OPTION))
try {
AudiofileHandler.extractResizeSaveArtwork(cmd.getOptionValue(CliHandler.FILE_CMD_LONG_OPTION))
AudiofileHandler.generateAsciiArt()
audiofileHandler.setUp()
audiofileHandler.extractResizeSaveArtwork()
audiofileHandler.generateAsciiArt()
} finally {
AudiofileHandler.tearDown()
audiofileHandler.tearDown()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,49 @@ import java.io.File
import java.io.IOException
import javax.imageio.ImageIO

object AudiofileHandler {
private val TMP_DIR: String = System.getProperty("java.io.tmpdir")
private val TMP_RESIZED_ARTWORK = "$TMP_DIR/resized.jpg"
private const val TARGET_SIZE = 80
private val TMP_RESIZED_FILE = File(TMP_RESIZED_ARTWORK)
class AudiofileHandler(private val audioFilePath: String) {

companion object {
private const val TARGET_SIZE = 80
private val TMP_DIR: String = System.getProperty("java.io.tmpdir")
private val TMP_RESIZED_ARTWORK = "$TMP_DIR/resized.jpg"
private val TMP_RESIZED_FILE = File(TMP_RESIZED_ARTWORK)
}

@JvmStatic
fun setUp() {
FileUtils.deleteQuietly(TMP_RESIZED_FILE)
}

@JvmStatic
fun tearDown() {
setUp()
}

@JvmStatic
@Throws(
CannotReadException::class,
TagException::class,
InvalidAudioFrameException::class,
ReadOnlyFileException::class,
IOException::class
IOException::class,
)
fun extractResizeSaveArtwork(audioFilePath: String) {
fun extractResizeSaveArtwork() {
val audioFile = File(audioFilePath)
val extractedArtwork = AudioFileIO.read(audioFile)
.tag
.firstArtwork
.image

val scaledArtwork =
(extractedArtwork as BufferedImage).getScaledInstance(TARGET_SIZE, TARGET_SIZE, Image.SCALE_DEFAULT)
(extractedArtwork as BufferedImage).getScaledInstance(
TARGET_SIZE,
TARGET_SIZE,
Image.SCALE_DEFAULT
)
val resized = BufferedImage(TARGET_SIZE, TARGET_SIZE, BufferedImage.TYPE_INT_RGB)
resized.graphics.drawImage(scaledArtwork, 0, 0, null)

ImageIO.write(resized, "png", TMP_RESIZED_FILE)
}

@JvmStatic
fun generateAsciiArt() {
val symbols = arrayOf(" ", ".", "-", "I", "W", "@")
val parserConfig = ParserBuilder.startBuild()
Expand Down

0 comments on commit 2035ab1

Please sign in to comment.