Skip to content

Instantly share code, notes, and snippets.

@mpilquist
Forked from djspiewak/Converter.scala
Created October 5, 2020 13:24
Show Gist options
  • Save mpilquist/59eadae7939423690d0898befe66b30c to your computer and use it in GitHub Desktop.
Save mpilquist/59eadae7939423690d0898befe66b30c to your computer and use it in GitHub Desktop.
/*
* Copyright 2020 Daniel Spiewak
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package transcoder
import cats.effect.{Blocker, ExitCode, IO, IOApp}
import fs2.{io, text, Stream}, io.file
import scala.jdk.CollectionConverters._
import java.io.File
import java.nio.charset.Charset
object Converter extends IOApp {
private val AllCharsets = Charset.availableCharsets().asScala.toList
def run(args: List[String]): IO[ExitCode] = {
val main = for {
blocker <- Stream.resource(Blocker[IO])
workers = args map { name =>
val jfile = new File(name)
val parent = jfile.getParent()
val decoded = file.readAll[IO](jfile.toPath, blocker, 1024 * 1024)
.through(text.utf8Decode)
decoded.broadcastTo {
AllCharsets map { case (name, charset) =>
text.encode(charset)
.andThen(file.writeAll[IO](new File(parent, jfile.getName() + s".$name").toPath, blocker))
.andThen(_.handleErrorWith(_ => Stream.empty))
.andThen(_.drain)
}: _*
}
}
_ <- Stream.emits(workers).parJoinUnbounded
} yield ()
main.compile.drain.as(ExitCode.Success)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment