src/main/scala/s3/website/S3.scala in s3_website-2.0.1 vs src/main/scala/s3/website/S3.scala in s3_website-2.1.0

- old
+ new

@@ -16,37 +16,37 @@ object S3 { def uploadRedirect(redirect: Redirect, a: Attempt = 1) (implicit config: Config, s3Settings: S3Setting, pushMode: PushMode, executor: ExecutionContextExecutor, logger: Logger) = - upload(Right(redirect)) + uploadToS3(Right(redirect)) - def uploadFile(localFile: LocalFile, a: Attempt = 1) + def uploadFile(up: Upload, a: Attempt = 1) (implicit config: Config, s3Settings: S3Setting, pushMode: PushMode, executor: ExecutionContextExecutor, logger: Logger) = - upload(Left(localFile)) + uploadToS3(Left(up)) - def upload(source: Either[LocalFile, Redirect], a: Attempt = 1) + def uploadToS3(source: Either[Upload, Redirect], a: Attempt = 1) (implicit config: Config, s3Settings: S3Setting, pushMode: PushMode, executor: ExecutionContextExecutor, logger: Logger): Future[Either[FailedUpload, SuccessfulUpload]] = Future { val putObjectRequest = toPutObjectRequest(source).get val uploadDuration = if (pushMode.dryRun) None else Some(recordUploadDuration(putObjectRequest, s3Settings.s3Client(config) putObject putObjectRequest)) val report = SuccessfulUpload( source.fold(_.s3Key, _.s3Key), source.fold( - localFile => Left(SuccessfulNewOrCreatedDetails(localFile.uploadType, localFile.uploadFile.get.length(), uploadDuration)), + upload => Left(SuccessfulNewOrCreatedDetails(upload.uploadType, upload.uploadFile.get.length(), uploadDuration, upload.reasonForUpload)), redirect => Right(SuccessfulRedirectDetails(redirect.uploadType, redirect.redirectTarget)) ), putObjectRequest ) logger.info(report) Right(report) } recoverWith retry(a)( createFailureReport = error => FailedUpload(source.fold(_.s3Key, _.s3Key), error), - retryAction = newAttempt => this.upload(source, newAttempt) + retryAction = newAttempt => this.uploadToS3(source, newAttempt) ) def delete(s3Key: S3Key, a: Attempt = 1) (implicit config: Config, s3Settings: S3Setting, pushMode: PushMode, executor: ExecutionContextExecutor, logger: Logger): Future[Either[FailedDelete, SuccessfulDelete]] = @@ -58,30 +58,30 @@ } recoverWith retry(a)( createFailureReport = error => FailedDelete(s3Key, error), retryAction = newAttempt => this.delete(s3Key, newAttempt) ) - def toPutObjectRequest(source: Either[LocalFile, Redirect])(implicit config: Config): Try[PutObjectRequest] = + def toPutObjectRequest(source: Either[Upload, Redirect])(implicit config: Config): Try[PutObjectRequest] = source.fold( - localFile => + upload => for { - uploadFile <- localFile.uploadFile - contentType <- localFile.contentType + uploadFile <- upload.uploadFile + contentType <- upload.contentType } yield { val md = new ObjectMetadata() md setContentLength uploadFile.length md setContentType contentType - localFile.encodingOnS3.map(_ => "gzip") foreach md.setContentEncoding - localFile.maxAge foreach { seconds => + upload.encodingOnS3.map(_ => "gzip") foreach md.setContentEncoding + upload.maxAge foreach { seconds => md.setCacheControl( if (seconds == 0) s"no-cache; max-age=$seconds" else s"max-age=$seconds" ) } - val req = new PutObjectRequest(config.s3_bucket, localFile.s3Key, new FileInputStream(uploadFile), md) + val req = new PutObjectRequest(config.s3_bucket, upload.s3Key, new FileInputStream(uploadFile), md) config.s3_reduced_redundancy.filter(_ == true) foreach (_ => req setStorageClass ReducedRedundancy) req } , redirect => { @@ -144,11 +144,11 @@ sealed trait PushSuccessReport extends SuccessReport { def s3Key: String } case class SuccessfulRedirectDetails(uploadType: UploadType, redirectTarget: String) - case class SuccessfulNewOrCreatedDetails(uploadType: UploadType, uploadSize: Long, uploadDuration: Option[Long]) + case class SuccessfulNewOrCreatedDetails(uploadType: UploadType, uploadSize: Long, uploadDuration: Option[Long], reasonForUpload: String) case class SuccessfulUpload(s3Key: S3Key, details: Either[SuccessfulNewOrCreatedDetails, SuccessfulRedirectDetails], putObjectRequest: PutObjectRequest) (implicit pushMode: PushMode, logger: Logger) extends PushSuccessReport { @@ -165,14 +165,22 @@ md.getCacheControl :: md.getContentType :: md.getContentEncoding :: putObjectRequest.getStorageClass :: Nil map (Option(_)) // AWS SDK may return nulls - ) :+ uploadSizeForHumans :+ uploadSpeedForHumans + ) :+ uploadSizeForHumans :+ uploadSpeedForHumans :+ uploadReason detailFragments.collect { case Some(detailFragment) => detailFragment }.mkString(" | ") } + + lazy val uploadReason = + details + .fold(uploadDetails => Some(uploadDetails.reasonForUpload), _ => None) + .collect { + case reasonForUpload if logger.verboseOutput => + s"upload reason: $reasonForUpload" + } lazy val uploadSize = details.fold( newOrCreatedDetails => Some(newOrCreatedDetails.uploadSize), redirectDetails => None )