Skip to content

Instantly share code, notes, and snippets.

@jcranky
Last active November 4, 2023 00:19
Show Gist options
  • Save jcranky/5656899 to your computer and use it in GitHub Desktop.
Save jcranky/5656899 to your computer and use it in GitHub Desktop.
Script scala para baixar imagens do Amazon S3, criar um thumb e subí-lo de volta para lá. Video explicando o Script: https://www.youtube.com/watch?v=BR4OUTAF8T8. | ----- | Scala script that downloads images from Amazon S3, creates a thumb and sends it back. The video mentioned above explains the code, but it is only in portuguese. I might add ca…
import com.amazonaws.auth.PropertiesCredentials
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.services.s3.model._
import java.io.File
import scala.collection.JavaConverters._
object migrator extends App {
val imagens = new File("/tmp/imagens")
if (!imagens.exists) imagens.mkdir
val s3 = new AmazonS3Client(new PropertiesCredentials(new File("aws.credentials")))
val bucketName = "jcranky-dev"
val sufixos = List("-small.png", "-medium.png", "-large.png", "-verylarge.png")
var objListing = s3.listObjects(bucketName)
processListing(objListing)
while (objListing.isTruncated) {
objListing = s3.listNextBatchOfObjects(objListing)
processListing(objListing)
}
def processListing(listing: ObjectListing) = {
val sumarios = listing.getObjectSummaries.asScala.toList
val filtrada = sumarios.filterNot(s => sufixos.exists(s.getKey.endsWith(_)))
filtrada.foreach(s => process(s))
def process(s: S3ObjectSummary) = {
val imgFile = new File(imagens, s.getKey)
s3.getObject(new GetObjectRequest(bucketName, s.getKey), imgFile)
val noExtKey = s.getKey.take(s.getKey.length-4)
new ImageThumber(imgFile, noExtKey).generateThumb(VeryLargeThumb)
val thumbFile = new File(imagens, s"${noExtKey}-verylarge.png")
val putRequest = new PutObjectRequest(bucketName, thumbFile.getName, thumbFile)
putRequest.setCannedAcl(CannedAccessControlList.PublicRead)
println("sending to s3: " + thumbFile.getName)
s3.putObject(putRequest)
}
}
}
// ctrl + c lojinha
import java.awt.AlphaComposite
import java.awt.image.BufferedImage
import java.io.File
import javax.imageio.ImageIO
class ImageThumber(image: File, imageKey: String) {
def generateThumbs(): List[(File, String)] = ImageThumber.sizes map generateThumb
def imageNameGen(imageKey: String, thumbSize: ThumbSize): String = imageKey + thumbSize.suffix + ".png"
def generateThumb(thumbSize: ThumbSize): (File, String) = {
val imageBuf = ImageIO.read(image)
val height = imageBuf.getHeight
val width = imageBuf.getWidth
val imageName = imageNameGen(imageKey, thumbSize)
if (width <= thumbSize.width && height <= thumbSize.height)
(image, imageName)
else {
val (newWidth, newHeight) = ImageThumber.newSizesFor(thumbSize, width, height)
(writeImage(newWidth, newHeight, imageBuf, thumbSize, imageName), imageName)
}
}
private def writeImage(width: Int, height: Int, imageBuf: BufferedImage, thumbSize: ThumbSize, imageName: String) = {
val scaledImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
val g = scaledImage.createGraphics
g.setComposite(AlphaComposite.Src)
g.drawImage(imageBuf, 0, 0, width, height, null);
g.dispose
val destFile = new File(image.getParentFile, imageName)
ImageIO.write(scaledImage, "png", destFile)
destFile
}
}
object ImageThumber {
val sizes = List(SmallThumb, MediumThumb, LargeThumb, VeryLargeThumb)
def newSizesFor(thumbSize: ThumbSize, originalWidth: Int, originalHeight: Int): (Int, Int) = {
var newWidth: Double = originalWidth
var newHeight: Double = originalHeight
if (newWidth > thumbSize.width){
newWidth = thumbSize.width
newHeight = originalHeight * newWidth / originalWidth
}
if (newHeight > thumbSize.height) {
val oldHeight = newHeight
newHeight = thumbSize.height
newWidth = newHeight * newWidth / oldHeight
}
(newWidth.toInt, newHeight.toInt)
}
}
sealed case class ThumbSize(width: Int, height: Int, suffix: String)
object SmallThumb extends ThumbSize(100, 100, "-small")
object MediumThumb extends ThumbSize(200, 200, "-medium")
object LargeThumb extends ThumbSize(300, 300, "-large")
object VeryLargeThumb extends ThumbSize(600, 600, "-verylarge")
object OriginalSize extends ThumbSize(0, 0, "")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment