Skip to content

Mito mixin class for file management outside of RDBMS

Notifications You must be signed in to change notification settings

fukamachi/mito-attachment

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mito-attachment

The place to store files would be a problem when you intend to write a web application which allows file-uploading. These days, AWS S3 is a common place to store/serve files, however, it's not easy to manage like RDBMS.

Mito-attachment provides a Mito mixin class for managing files outside of RDBMS. It stores files before mito:save-dao and deletes them before mito:delete-dao.

Besides, the backend storage can be replaced easily. This makes it easy that using cloud storage services for production environment and using local filesystem for development environment.

Usage

Setting up the storage

(defvar *appenv* (uiop:getenv "APP_ENV"))

;; Setup storage class
(setf *storage*
      (if (string= *appenv* "production")
          ;; Store files in AWS S3 for production environment
          (make-instance 's3-storage
                         :bucket "mito-attachment-example"
                         :endpoint "s3-ap-northeast-1.amazonaws.com"
                         :access-key (uiop:getenv "AWS_ACCESS_KEY")
                         :secret-key (uiop:getenv "AWS_SECRET_KEY"))
          ;; Store files in local filesystem for development environment
          (make-instance 'disk-storage
                         :bucket "mito-attachment-example"
                         :directory #P"/tmp/attachment/")))

Defining an attachment Mito class

;; Attachment class for saving metadata into RDBMS
(defclass image (attachment) ()
  (:metaclass mito:dao-table-class))

Saving

;; :content can be specified as a pathname or a stream.
(mito:create-dao 'image :content #P"uploaded-file.png")

;; Override the file content-type
(mito:create-dao 'image :content #P"uploaded-file.png" :content-type "image/png")

;; Use an original file-key
(mito:create-dao 'image :content #P"uploaded-file.png" :file-key "image.png")

Getting the URL

(let ((file (mito:find-dao 'image :id 1)))
  (file-url file))
;-> ;; SELECT * FROM "image" WHERE ("id" = ?) LIMIT 1 (1) [1 row] | MITO.DB:RETRIEVE-BY-SQL
;=> "/mito-attachment-example/3616D80112884799B272DC962F4BBF97.jpg"

Additionally, file-signed-url can also be used for getting a signed URL (ex. Amazon S3 Presigned URL).

See example.lisp for getting the full example. It's a Lack web application which allows users to upload image files.

Installation

(ql:quickload :mito-attachment)

See Also

Author

Copyright

Copyright (c) 2016 Eitaro Fukamachi ([email protected])

License

Licensed under the LLGPL License.

About

Mito mixin class for file management outside of RDBMS

Resources

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published