Skip to content

Instantly share code, notes, and snippets.

@dfsilva
Created August 26, 2020 10:50
Show Gist options
  • Save dfsilva/3a22d6c1340355f3c0723bd23c21fb59 to your computer and use it in GitHub Desktop.
Save dfsilva/3a22d6c1340355f3c0723bd23c21fb59 to your computer and use it in GitHub Desktop.
path("picture") {
post {
parameterMap { params =>
fileUpload("file") {
case (fileInfo, fileStream) =>
val fuid = params("fuid")
val uid = params("uid")
val mine = params("mine")
val tempDir = System.getProperty("java.io.tmpdir") + File.separator
val imagePath = tempDir + fileInfo.fileName
val compressedImagePath = tempDir + "comp_" + fileInfo.fileName
val thumbPath = tempDir + "thumb_" + fileInfo.fileName
val sink = FileIO.toPath(Paths.get(tempDir) resolve fileInfo.fileName)
val writeResult = fileStream.runWith(sink)
onSuccess(writeResult) { result =>
result.status match {
case scala.util.Success(_) => {
//TODO:
// - colocar para excluir o arquivo antigo do firebase
implicit val writer = new JpegWriter().withCompression(50)
ImmutableImage.loader().fromStream(new FileInputStream(new File(imagePath)))
.output(writer, compressedImagePath)
ImmutableImage.loader().fromStream(new FileInputStream(new File(imagePath)))
.fit(150, 150)
.output(writer, thumbPath)
val bucketPath = s"pictures/${uid}/${fileInfo.fileName}"
val bucketThumbPath = s"pictures/${uid}/thumb_${fileInfo.fileName}"
val bucket = StorageClient.getInstance.bucket
val blobInfo = BlobInfo.newBuilder(BlobId.of(bucket.getName, bucketPath))
.setContentType(mine)
.setMetadata(Map("owner" -> s"usuarios/$uid", "fuid" -> fuid).asJava)
.build
val blobThumbInfo = BlobInfo.newBuilder(BlobId.of(bucket.getName, bucketThumbPath))
.setContentType(mine)
.setMetadata(Map("owner" -> s"usuarios/$uid", "fuid" -> fuid).asJava)
.build
val blob = bucket.getStorage.create(blobInfo, Files.readAllBytes(Paths.get(compressedImagePath)))
val blobThumb = bucket.getStorage.create(blobThumbInfo, Files.readAllBytes(Paths.get(thumbPath)))
val pictureUrl = blob.signUrl(10000, java.util.concurrent.TimeUnit.DAYS)
val thumbUrl = blobThumb.signUrl(10000, java.util.concurrent.TimeUnit.DAYS)
val localFile = new File(imagePath)
if (localFile.exists()) {
localFile.delete()
}
val localThumb = new File(thumbPath)
if (localThumb.exists()) {
localThumb.delete()
}
val locaoCompressed = new File(compressedImagePath)
if (locaoCompressed.exists()) {
locaoCompressed.delete()
}
val action = (for {
pictureFile <- SmFileRepo.add(SmFile(uid = fuid,
name = fileInfo.fileName,
address = pictureUrl.toString,
bucketPath = bucketPath,
mine = mine,
updated = new Timestamp(new java.util.Date().getTime)))
thumbFile <- SmFileRepo.add(SmFile(uid = s"${fuid}_t",
name = s"thumb_${fileInfo.fileName}",
address = thumbUrl.toString,
bucketPath = bucketThumbPath,
mine = mine,
updated = new Timestamp(new java.util.Date().getTime)))
user <- UserRepo.load(uid) map {
case Some(value) => value
case None => throw new RuntimeException("Nenhum usuario encontrado")
}
updatedUser <- UserRepo.updatePic(user, pictureFile, thumbFile)
// _ = UserFirestoreRepo.set(UserFirestore.fromUser(user, picture = Some(SMFileFirestore.fromSmFile(pictureFile)))
// .copy(picture = SMFileFirestore.fromSmFile(pictureFile), thumb = SMFileFirestore.fromSmFile(thumbFile)))
} yield updatedUser)
complete(db.run {
action
})
}
case Failure(e) => throw e
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment