Skip to content

Instantly share code, notes, and snippets.

@dbousamra
Created June 4, 2015 07:59
Show Gist options
  • Select an option

  • Save dbousamra/0d1fd0d62ab7fc2f2c13 to your computer and use it in GitHub Desktop.

Select an option

Save dbousamra/0d1fd0d62ab7fc2f2c13 to your computer and use it in GitHub Desktop.
package com.cammy.firehose.domain
import java.io.File
import java.nio.file.attribute.BasicFileAttributeView
import java.nio.file.{Paths, Files}
import org.joda.time.Instant
import scalaz._
import Scalaz._
sealed trait CameraImageTimestamp {
def value: Instant
}
case class UploadedTimestamp(value: Instant) extends CameraImageTimestamp
case class CanonicalTimestamp(value: Instant) extends CameraImageTimestamp
case class CameraImageTimestamps(canonical: Option[CanonicalTimestamp], uploaded: UploadedTimestamp) {
def timestamp: CameraImageTimestamp = canonical.getOrElse(uploaded)
}
object CameraImageTimestamps {
def fromFile(file: File): TimestampParseException \/ CameraImageTimestamps = {
for {
uploaded <- getUploadedTimestampFromFile(file)
canonical <- getCanonicalTimestampFromFile(file)
} yield CameraImageTimestamps(canonical, uploaded)
}
private def getUploadedTimestampFromFile(file: File): TimestampParseException \/ UploadedTimestamp = {
try {
val view = Files.getFileAttributeView(Paths.get(file.getAbsolutePath), classOf[BasicFileAttributeView]).readAttributes();
\/-(UploadedTimestamp(instantFromLong(view.creationTime().toMillis)))
} catch {
case e: Exception => -\/(TimestampParseException("Tried to read the CTime from the file and failed."))
}
}
private def getCanonicalTimestampFromFile(file: File): TimestampParseException \/ Option[CanonicalTimestamp] = {
// Would be parsing based on some filename stuff
-\/(TimestampParseException("Cannot parse timestamp"))
}
private def instantFromLong(v: Long) = new Instant(v)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment