Last active
August 29, 2015 14:22
-
-
Save dbousamra/7d9e7fc4fbe26b5e4852 to your computer and use it in GitHub Desktop.
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
| case class ImageParsingException(reason: String) extends Exception | |
| sealed trait CameraImage { | |
| def image: Image | |
| } | |
| case class FullRes(image: Image) extends CameraImage | |
| case class StdRes(image: Image) extends CameraImage | |
| case class Thumbnail(image: Image) extends CameraImage | |
| // TODO - Fix | |
| object CameraImage { | |
| def fromFile(file: File): ImageParsingException \/ FullRes = { | |
| try { | |
| \/-(FullRes(Image(file))) | |
| } catch { | |
| case e: Exception => -\/ (ImageParsingException(e.getMessage)) | |
| } | |
| } | |
| } |
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 com.cammy.firehose.domain | |
| import java.io.{IOException, File} | |
| import java.nio.file.attribute.BasicFileAttributeView | |
| import java.nio.file.{Files, Paths} | |
| import com.cammy.firehose.uploader.imaging.Resizer | |
| import scalaz._ | |
| sealed trait InvalidSnapshotException extends Exception | |
| case class CameraIdParsingException(detail: String) extends InvalidSnapshotException | |
| case class CTimeOfImageException(detail: String) extends InvalidSnapshotException | |
| case class TimestampParseException(detail: String) extends InvalidSnapshotException | |
| case class ImageReadingException(detail: String) extends InvalidSnapshotException | |
| case class UnuploadedCameraSnapshot(cameraId: CameraId, timestamp: CameraImageTimestamp, uploadedTimestamp: CameraImageTimestamp, fullRes: FullRes) | |
| case class ResizedCameraSnapshot(UnuploadedSnapshot: UnuploadedCameraSnapshot, stdRes: StdRes, thumbnail: Thumbnail) | |
| object ResizedCameraSnapshot { | |
| def resize(resizer: Resizer, snapshot: UnuploadedCameraSnapshot): Exception \/ ResizedCameraSnapshot = for { | |
| thumbnail <- resizer.createThumbnail(snapshot.fullRes, ImageDimensions(Width(360), Height(240))) | |
| stdRes <- resizer.createStdRes(snapshot.fullRes, ImageDimensions(Width(1280), Height(720))) | |
| } yield ResizedCameraSnapshot(snapshot, stdRes, thumbnail) | |
| } | |
| case class UploadedCameraSnapshot(resizedCameraSnapshot: ResizedCameraSnapshot, stdResURL: RemoteStorageUrl, thumbnailURL: RemoteStorageUrl) | |
| object UnuploadedCameraSnapshot { | |
| val remoteFilename: String = ??? | |
| // TODO - Test the fall through | |
| // TODO - A mess | |
| def fromFileAndUsername(file: File, username: String): InvalidSnapshotException \/ UnuploadedCameraSnapshot = { | |
| val uploadedTimestamp = getUploadedTimestampFromFile(file) | |
| val canonicalTimestamp = getCanonicalTimestampFromFile(file) | |
| val timestamp = canonicalTimestamp.orElse(uploadedTimestamp) | |
| val cameraId = CameraId.fromString(username).disjunction.leftMap(e => CameraIdParsingException(e.toString)) | |
| val bytes = CameraImage.fromFile(file).leftMap(e => ImageReadingException(e.toString)) | |
| for { | |
| cameraId <- cameraId | |
| uploadedTimestamp <- uploadedTimestamp | |
| timestamp <- timestamp | |
| bytes <- bytes | |
| } yield UnuploadedCameraSnapshot(cameraId, timestamp, uploadedTimestamp, bytes) | |
| } | |
| private def getUploadedTimestampFromFile(file: File): CTimeOfImageException \/ CameraImageTimestamp = { | |
| val view = Files.getFileAttributeView(Paths.get(file.getAbsolutePath), classOf[BasicFileAttributeView]) | |
| \/.fromTryCatchNonFatal { | |
| val view = Files.getFileAttributeView(Paths.get(file.getAbsolutePath), classOf[BasicFileAttributeView]).readAttributes(); | |
| CameraImageTimestamp(view.creationTime().toMillis) | |
| }.leftMap(x => CTimeOfImageException("Tried to read the CTime from the file and failed.")) | |
| } | |
| private def getCanonicalTimestampFromFile(file: File): TimestampParseException \/ CameraImageTimestamp = { | |
| \/-(CameraImageTimestamp(1L)) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment