Skip to content

Instantly share code, notes, and snippets.

@seanparsons
Created July 31, 2013 22:38
Show Gist options
  • Save seanparsons/6126830 to your computer and use it in GitHub Desktop.
Save seanparsons/6126830 to your computer and use it in GitHub Desktop.
import org.apache.commons.codec.binary.Base64
import java.nio.file._
import java.nio.file.attribute._
import scala.collection.mutable.ListBuffer
val htmlTemplate = """
<html>
<head>
<title>Encoded Images!</title>
</head>
<body>
%s
</body>
</html>"""
val filesystem = FileSystems.getDefault
val path = filesystem.getPath(".")
val matcher = filesystem.getPathMatcher("glob:{**.png,**.jpg,**.gif}")
val files = ListBuffer.empty[Path]
Files.walkFileTree(path, new SimpleFileVisitor[Path]() {
override def visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult = {
if (matcher.matches(file)) files += file
FileVisitResult.CONTINUE
}
})
val embeddedImages = files.toList.map(file => """<img src="data:image/png;base64,%s" />""".format(Base64.encodeBase64String(Files.readAllBytes(file))))
Files.write(filesystem.getPath("result.html"), htmlTemplate.format(embeddedImages.mkString).getBytes("UTF-8"))
@paulecoyote
Copy link

seems legit ;) #KnowsNothingAboutScala

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment