Created
October 4, 2020 17:59
-
-
Save djspiewak/b1bfa4f7fe30bdd0ead89f5286f9ad06 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
* 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 flatMap { name => | |
val jfile = new File(name) | |
val parent = jfile.getParent() | |
val decoded = file.readAll[IO](jfile.toPath, blocker, 1024 * 1024) | |
.through(text.utf8Decode) | |
AllCharsets map { | |
case (name, charset) => | |
decoded.through(text.encode(charset)) | |
.through(file.writeAll(new File(parent, jfile.getName() + s".$name").toPath, blocker)) | |
.handleErrorWith(_ => Stream.empty) | |
} | |
} | |
_ <- 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