Created
May 15, 2014 14:48
-
-
Save asouza/4063adea8464eb5f97ef to your computer and use it in GitHub Desktop.
adapter class to be used in your java side of play project
This file contains hidden or 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
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