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

s3.lisp 1.9KB

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