s3_website_monadic 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 39f140d0639eb128178c9ec33176ea0f4a842904
4
- data.tar.gz: 95e6aae697bf30b9134180521540948f49051b15
3
+ metadata.gz: 92e9b4b0029ead9d9f441f2a5fdb2687f535a729
4
+ data.tar.gz: ea74c668aa1f6accc39a1541e56af5b16781f507
5
5
  SHA512:
6
- metadata.gz: 9779e122e2d5cef1d51e8e085e2ec41882d98f277b9bccabb9afeab065d8ecdc61380a0ac39fb7eb968d03a7d8d1631f45fbbb60c0e79a1264f9c1a65d1b9a60
7
- data.tar.gz: 5f501c7f29b0b92d7949f465d965693e3a1ec7dd863dec2135eb7d667c67cbf9a3ed3cd11543e17d61c557af1a4f968fa49f08165fe2631707b0c0142b4eabbf
6
+ metadata.gz: 866b36966c21be8e2dd88748e9ca3955807668b3bd4efe9a33c70d1458c3d45ed4dc81d8a2b26b2399dfaf7c2a874ebd72006bc9324d7326a43f4457a2629415
7
+ data.tar.gz: 25613874bf0d7bc119f0307aabf6eecb70a958d010ab486a9ab931b2ae3b6183ebda195c888040f6a0c660b8c50be0cbf9aea5d22e90f3aa11dec2c7cf2a250a
data/s3_website.gemspec CHANGED
@@ -3,11 +3,11 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "s3_website_monadic"
6
- s.version = "0.0.8"
6
+ s.version = "0.0.9"
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ["Lauri Lehmijoki"]
9
9
  s.email = ["lauri.lehmijoki@iki.fi"]
10
- s.homepage = "https://github.com/laurilehmijoki/s3_website"
10
+ s.homepage = "https://github.com/laurilehmijoki/s3_website/tree/s3_website_monadic"
11
11
  s.summary = %q{Manage your S3 website}
12
12
  s.description = %q{
13
13
  Sync website files, set redirects, use HTTP performance optimisations, deliver via
@@ -65,7 +65,14 @@ class S3(implicit s3Settings: S3Settings, executor: ExecutionContextExecutor) {
65
65
  md setContentLength uploadBody.contentLength
66
66
  md setContentType uploadBody.contentType
67
67
  uploadBody.contentEncoding foreach md.setContentEncoding
68
- uploadBody.maxAge foreach (seconds => md.setCacheControl(s"max-age=$seconds"))
68
+ uploadBody.maxAge foreach { seconds =>
69
+ md.setCacheControl(
70
+ if (seconds == 0)
71
+ s"no-cache; max-age=$seconds"
72
+ else
73
+ s"max-age=$seconds"
74
+ )
75
+ }
69
76
  val req = new PutObjectRequest(config.s3_bucket, upload.s3Key, uploadBody.openInputStream(), md)
70
77
  config.s3_reduced_redundancy.filter(_ == true) foreach (_ => req setStorageClass ReducedRedundancy)
71
78
  req
@@ -76,17 +76,6 @@ object LocalFile {
76
76
  val md5 = using(fis(sourceFile)) { inputStream =>
77
77
  DigestUtils.md5Hex(inputStream)
78
78
  }
79
- val maxAge = config.max_age.flatMap { maxAgeIntOrGlob =>
80
- maxAgeIntOrGlob.fold(
81
- (seconds: Int) => Some(seconds),
82
- (globs2Ints: Map[String, Int]) =>
83
- globs2Ints.find { globAndInt =>
84
- (rubyRuntime evalScriptlet s"File.fnmatch('${globAndInt._1}', '${localFile.s3Key}')")
85
- .toJava(classOf[Boolean])
86
- .asInstanceOf[Boolean]
87
- } map (_._2)
88
- )
89
- }
90
79
 
91
80
  Upload(
92
81
  s3Key = localFile.s3Key,
@@ -95,8 +84,8 @@ object LocalFile {
95
84
  md5 = md5,
96
85
  contentEncoding = localFile.encodingOnS3.map(_ => "gzip"),
97
86
  contentLength = sourceFile.length(),
98
- maxAge = maxAge,
99
- contentType = tika.detect(localFile.sourceFile),
87
+ maxAge = resolveMaxAge(localFile),
88
+ contentType = resolveContentType(localFile.sourceFile),
100
89
  openInputStream = () => new FileInputStream(sourceFile)
101
90
  )
102
91
  )
@@ -108,6 +97,33 @@ object LocalFile {
108
97
 
109
98
  lazy val tika = new Tika()
110
99
 
100
+ def resolveContentType(file: File) = {
101
+ val mimeType = tika.detect(file)
102
+ if (mimeType.startsWith("text/") || mimeType == "application/json")
103
+ mimeType + "; charset=utf-8"
104
+ else
105
+ mimeType
106
+ }
107
+
108
+ def resolveMaxAge(localFile: LocalFile)(implicit config: Config): Option[Int] = {
109
+ type GlobsMap = Map[String, Int]
110
+ config.max_age.flatMap { (intOrGlobs: Either[Int, GlobsMap]) =>
111
+ type GlobsSeq = Seq[(String, Int)]
112
+ def respectMostSpecific(globs: GlobsMap): GlobsSeq = globs.toSeq.sortBy(_._1).reverse
113
+ intOrGlobs
114
+ .right.map(respectMostSpecific)
115
+ .fold(
116
+ (seconds: Int) => Some(seconds),
117
+ (globs: GlobsSeq) =>
118
+ globs.find { globAndInt =>
119
+ (rubyRuntime evalScriptlet s"File.fnmatch('${globAndInt._1}', '${localFile.s3Key}')")
120
+ .toJava(classOf[Boolean])
121
+ .asInstanceOf[Boolean]
122
+ } map (_._2)
123
+ )
124
+ }
125
+ }
126
+
111
127
  def resolveLocalFiles(implicit site: Site): Either[ErrorReport, Seq[LocalFile]] = Try {
112
128
  val files = recursiveListFiles(new File(site.rootDirectory)).filterNot(_.isDirectory)
113
129
  files map { file =>
@@ -328,6 +328,27 @@ class S3WebsiteSpec extends Specification {
328
328
  Push.pushSite
329
329
  sentPutObjectRequest.getMetadata.getCacheControl must beNull
330
330
  }
331
+
332
+ "be used to disable caching" in new SiteDirectory with MockAWS {
333
+ implicit val site = siteWithFiles(defaultConfig.copy(max_age = Some(Left(0))), localFiles = "index.html" :: Nil)
334
+ Push.pushSite
335
+ sentPutObjectRequest.getMetadata.getCacheControl must equalTo("no-cache; max-age=0")
336
+ }
337
+ }
338
+
339
+ "max-age in config" should {
340
+ "respect the more specific glob" in new SiteDirectory with MockAWS {
341
+ implicit val site = siteWithFiles(
342
+ defaultConfig.copy(max_age = Some(Right(Map(
343
+ "**" -> 150,
344
+ "assets/**" -> 86400
345
+ )))),
346
+ localFiles = "index.html" :: "assets/picture.gif" :: Nil
347
+ )
348
+ Push.pushSite
349
+ sentPutObjectRequests.find(_.getKey == "index.html").get.getMetadata.getCacheControl must equalTo("max-age=150")
350
+ sentPutObjectRequests.find(_.getKey == "assets/picture.gif").get.getMetadata.getCacheControl must equalTo("max-age=86400")
351
+ }
331
352
  }
332
353
 
333
354
  "s3_reduced_redundancy: true in config" should {
@@ -379,6 +400,32 @@ class S3WebsiteSpec extends Specification {
379
400
  sentPutObjectRequest.getKey must equalTo(".vimrc")
380
401
  }
381
402
  }
403
+
404
+ "content type inference" should {
405
+ "add charset=utf-8 to all html documents" in new SiteDirectory with MockAWS {
406
+ implicit val site = siteWithFiles(localFiles = "file.html" :: Nil)
407
+ Push.pushSite
408
+ sentPutObjectRequest.getMetadata.getContentType must equalTo("text/html; charset=utf-8")
409
+ }
410
+
411
+ "add charset=utf-8 to all text documents" in new SiteDirectory with MockAWS {
412
+ implicit val site = siteWithFiles(localFiles = "file.txt" :: Nil)
413
+ Push.pushSite
414
+ sentPutObjectRequest.getMetadata.getContentType must equalTo("text/plain; charset=utf-8")
415
+ }
416
+
417
+ "add charset=utf-8 to all json documents" in new SiteDirectory with MockAWS {
418
+ implicit val site = siteWithFiles(localFiles = "file.json" :: Nil)
419
+ Push.pushSite
420
+ sentPutObjectRequest.getMetadata.getContentType must equalTo("application/json; charset=utf-8")
421
+ }
422
+
423
+ "resolve the content type from file contents" in new SiteDirectory with MockAWS {
424
+ implicit val site = siteWithFilesAndContent(localFilesWithContent = ("index", "<html><body><h1>hi</h1></body></html>") :: Nil)
425
+ Push.pushSite
426
+ sentPutObjectRequest.getMetadata.getContentType must equalTo("text/html; charset=utf-8")
427
+ }
428
+ }
382
429
 
383
430
  trait MockAWS extends MockS3 with MockCloudFront with Scope
384
431
 
@@ -479,7 +526,7 @@ class S3WebsiteSpec extends Specification {
479
526
 
480
527
  def sentPutObjectRequests: Seq[PutObjectRequest] = {
481
528
  val req = ArgumentCaptor.forClass(classOf[PutObjectRequest])
482
- verify(amazonS3Client).putObject(req.capture())
529
+ verify(amazonS3Client, Mockito.atLeast(1)).putObject(req.capture())
483
530
  req.getAllValues
484
531
  }
485
532
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: s3_website_monadic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lauri Lehmijoki
@@ -380,7 +380,7 @@ files:
380
380
  - src/main/scala/s3/website/model/push.scala
381
381
  - src/main/scala/s3/website/package.scala
382
382
  - src/test/scala/s3/website/S3WebsiteSpec.scala
383
- homepage: https://github.com/laurilehmijoki/s3_website
383
+ homepage: https://github.com/laurilehmijoki/s3_website/tree/s3_website_monadic
384
384
  licenses:
385
385
  - MIT
386
386
  metadata: {}