Advertisement
Guest User

Untitled

a guest
Jun 26th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.06 KB | None | 0 0
  1. post {
  2.       formFields('albumId, 'fileName, 'file.as[Array[Byte]]) { (albumId, fileName, bytes) =>
  3.        
  4.        val cdnBaseUrl = config.getString("niniprint.cdn.url")
  5.        
  6.        def uploadPhoto(ext: String) = {
  7.          val photoId = BSONObjectID.generate
  8.          val photoPath = s"$albumId/${photoId.stringify}.$ext"
  9.          val photo = Photo(fileName, photoPath)
  10.          s3.upload(s"$photoPath", bytes)
  11.          query(albums.addPhotoToAlbum(BSONObjectID(albumId), photo)) { lastError =>
  12.              complete(200, s"$cdnBaseUrl/$photoPath")
  13.          } { error =>
  14.            reject(DatabaseRejection("Photo upload error", error.lastError))
  15.          }
  16.        }
  17.        fileName.split('.').drop(1).map(_.toLowerCase).lastOption match {
  18.          case Some("jpg") | Some("jpeg") => uploadPhoto("jpg")
  19.          case Some("png") => uploadPhoto("png")
  20.          case Some("gif") => uploadPhoto("gif")
  21.          case ext => complete(400, "Image file not valid, only JPEG, PNG and GIF image formats are supported")
  22.        }
  23.  
  24.      }
  25.    }
  26.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement