Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
adapter class to be used in your java side of play project
package helpers
import java.io.File
import java.io.FileInputStream
import play.api.libs.concurrent.Execution.Implicits._
import org.apache.commons.io.FilenameUtils
import org.apache.commons.io.IOUtils
import fly.play.s3.BucketFile
import fly.play.s3.S3
import fly.play.s3.S3Exception
import fly.play.s3.acl.CanonicalUser
import fly.play.s3.acl.Grant
import fly.play.s3.acl.Permission
import play.Logger
import fly.play.s3.PUBLIC_READ
import scala.concurrent.Future
object AmazonS3 {
private val bucket = S3("youpet")
def upload(folder: String, file: File,fileName:String): WaitingForS3 = {
val timestampedName = FilenameUtils.getBaseName(fileName)+"_"+System.currentTimeMillis()+"."+FilenameUtils.getExtension(fileName)
val s3Url = bucket.url(s"${folder}/${timestampedName}")
val result:Future[Unit] = bucket + BucketFile(s"${folder}/${timestampedName}",
extractMimeType(fileName),
IOUtils.toByteArray(new FileInputStream(file)),Some(PUBLIC_READ))
result.map { unit =>
Logger.debug(s"Enviou o arquivo $s3Url")
}.recover {
case S3Exception(status, code, message, originalXml) => Logger.info("Error: " + message)
}
new WaitingForS3(s3Url,result)
}
private def extractMimeType(fileName:String) = {
FilenameUtils.getExtension(fileName).toLowerCase() match {
case "pdf" => "application/pdf"
case "png" => "image/png"
case "jpg" => "image/jpeg"
case "jpeg" => "image/jpeg"
case "png" => "image/png"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment