2013-04-21 12:13:03 -04:00
|
|
|
(eval-when (:compile-toplevel :load-toplevel)
|
2013-01-30 23:18:10 -05:00
|
|
|
(ql:quickload 'zs3))
|
2011-04-20 13:45:59 -04:00
|
|
|
|
|
|
|
(defpackage :coleslaw-s3
|
2013-03-06 13:05:57 -05:00
|
|
|
(:use :cl)
|
2013-02-01 11:25:59 -05:00
|
|
|
(:import-from :coleslaw #:deploy
|
2013-04-22 09:27:43 -04:00
|
|
|
#:deploy-dir
|
2013-02-01 11:25:59 -05:00
|
|
|
#:*config*)
|
2013-01-30 23:18:10 -05:00
|
|
|
(:export #:enable))
|
2011-04-20 13:45:59 -04:00
|
|
|
|
|
|
|
(in-package :coleslaw-s3)
|
|
|
|
|
2011-04-23 15:42:09 -04:00
|
|
|
(defparameter *content-type-map* '(("html" . "text/html")
|
|
|
|
("css" . "text/css")
|
|
|
|
("png" . "image/png")
|
|
|
|
("jpg" . "image/jpg"))
|
|
|
|
"A mapping from file extensions to content types.")
|
|
|
|
|
|
|
|
(defparameter *cache* (make-hash-table :test #'equal)
|
|
|
|
"A cache of keys in a given bucket hashed by etag.")
|
|
|
|
|
|
|
|
(defparameter *bucket* nil
|
|
|
|
"A string designating the bucket to upload to.")
|
|
|
|
|
|
|
|
(defun content-type (extension)
|
|
|
|
(cdr (assoc extension *content-type-map* :test #'equal)))
|
|
|
|
|
2013-01-30 23:18:10 -05:00
|
|
|
(defun stale-keys ()
|
|
|
|
(loop for key being the hash-values in *cache* collecting key))
|
|
|
|
|
|
|
|
(defun s3-sync (filepath dir)
|
2013-03-06 13:05:57 -05:00
|
|
|
(let ((etag (zs3:file-etag filepath))
|
2013-01-30 23:18:10 -05:00
|
|
|
(key (enough-namestring filepath dir)))
|
|
|
|
(if (gethash etag *cache*)
|
|
|
|
(remhash etag *cache*)
|
2013-03-06 13:05:57 -05:00
|
|
|
(zs3:put-file filepath *bucket* key :public t
|
|
|
|
:content-type (content-type (pathname-type filepath))))))
|
2013-01-30 23:18:10 -05:00
|
|
|
|
|
|
|
(defun dir->s3 (dir)
|
2013-01-30 23:24:46 -05:00
|
|
|
(flet ((upload (file) (s3-sync file dir)))
|
2013-01-30 23:18:10 -05:00
|
|
|
(cl-fad:walk-directory dir #'upload)))
|
|
|
|
|
|
|
|
(defmethod deploy :after (staging)
|
2013-04-22 09:27:43 -04:00
|
|
|
(let ((blog (deploy-dir *config*)))
|
2013-03-06 13:05:57 -05:00
|
|
|
(loop for key across (zs3:all-keys *bucket*)
|
|
|
|
do (setf (gethash (zs3:etag key) *cache*) key))
|
2013-01-30 23:18:10 -05:00
|
|
|
(dir->s3 blog)
|
2013-03-06 13:05:57 -05:00
|
|
|
(zs3:delete-objects (stale-keys) *bucket*)))
|
2013-01-30 23:18:10 -05:00
|
|
|
|
|
|
|
(defun enable (&key auth-file bucket)
|
2015-06-07 18:12:04 -04:00
|
|
|
"AUTH-FILE: Path to file with the access key on the first line and the secret
|
|
|
|
key on the second."
|
|
|
|
(setf zs3:*credentials* (zs3:file-credentials auth-file)
|
2013-01-30 23:18:10 -05:00
|
|
|
*bucket* bucket))
|