Flexible Lisp Blogware. Fork for personal use. Mirrored from https://github.com/kingcons/coleslaw originally.

s3.lisp 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. (eval-when (:compile-toplevel)
  2. (ql:quickload 'zs3))
  3. (defpackage :coleslaw-s3
  4. (:use :cl :zs3)
  5. (:import-from :coleslaw #:deploy
  6. #:*config*)
  7. (:import-from :zs3 #:all-keys
  8. #:etag
  9. #:file-etag
  10. #:put-file)
  11. (:export #:enable))
  12. (in-package :coleslaw-s3)
  13. (defparameter *credentials* nil
  14. "The credentials to authenticate with Amazon Web Services.
  15. Stored in a file with the access key on the first line
  16. and the secret key on the second.")
  17. (defparameter *content-type-map* '(("html" . "text/html")
  18. ("css" . "text/css")
  19. ("png" . "image/png")
  20. ("jpg" . "image/jpg"))
  21. "A mapping from file extensions to content types.")
  22. (defparameter *cache* (make-hash-table :test #'equal)
  23. "A cache of keys in a given bucket hashed by etag.")
  24. (defparameter *bucket* nil
  25. "A string designating the bucket to upload to.")
  26. (defun content-type (extension)
  27. (cdr (assoc extension *content-type-map* :test #'equal)))
  28. (defun stale-keys ()
  29. (loop for key being the hash-values in *cache* collecting key))
  30. (defun s3-sync (filepath dir)
  31. (let ((etag (file-etag filepath))
  32. (key (enough-namestring filepath dir)))
  33. (if (gethash etag *cache*)
  34. (remhash etag *cache*)
  35. (put-file filepath *bucket* key :public t
  36. :content-type (content-type (pathname-type filepath))))))
  37. (defun dir->s3 (dir)
  38. (flet ((upload (file) (s3-sync file dir)))
  39. (cl-fad:walk-directory dir #'upload)))
  40. (defmethod deploy :after (staging)
  41. (let ((blog (deploy *config*)))
  42. (loop for key across (all-keys *bucket*)
  43. do (setf (gethash (etag key) *cache*) key))
  44. (dir->s3 blog)
  45. (delete-objects (stale-keys) *bucket*)))
  46. (defun enable (&key auth-file bucket)
  47. (setf *credentials* (file-credentials auth-file)
  48. *bucket* bucket))