Skip to content

Instantly share code, notes, and snippets.

@alaa-alawi
Created May 21, 2012 16:07
Show Gist options
  • Save alaa-alawi/2763054 to your computer and use it in GitHub Desktop.
Save alaa-alawi/2763054 to your computer and use it in GitHub Desktop.
implementation comparision
;; original implementation
;; hunchentoot;utils.lisp
;;
(let ((counter 0))
(declare (ignorable counter))
(defun make-tmp-file-name (&optional (prefix "hunchentoot"))
"Generates a unique name for a temporary file. This function is
called from the RFC2388 library when a file is uploaded."
(let ((tmp-file-name
#+:allegro
(pathname (system:make-temp-file-name prefix *tmp-directory*))
#-:allegro
(loop for pathname = (make-pathname :name (format nil "~A-~A"
prefix (incf counter))
:type nil
:defaults *tmp-directory*)
unless (probe-file pathname)
return pathname)))
(push tmp-file-name *tmp-files*)
;; maybe call hook for file uploads
(when *file-upload-hook*
(funcall *file-upload-hook* tmp-file-name))
tmp-file-name)))
;; users are responsible for making sure *file-upload-hook* is called in their supplied implementation in *upload-filename-generator*
;;
;; make-tmp-file-name implementation is the same as the original one
(defun make-upload-filename ()
(let ((designator (cond
;; the old behaviour.
((null *upload-filename-generator*)
t)
;; the new behaviour.
((or (symbolp *upload-filename-generator*)
(functionp *upload-filename-generator*))
*upload-filename-generator*))))
designator))
;; we wrap the supplied one with our own lambda to ensure that *file-upload-hook* is called properly.
;;; make-tmp-file-name implementation is the same as the original one
(defun make-upload-filename ()
(let ((designator (cond
;; the old behaviour.
((null *upload-filename-generator*)
t)
;; the new behaviour.
((or (symbolp *upload-filename-generator*)
(functionp *upload-filename-generator*))
(lambda (&key field-name file-name content-type :allow-other-keys t)
(let ((filename (funcall *upload-filename-generator*
:field-name field-name
:file-name file-name
:content-type content-type
:allow-other-keys t)))
(when *file-upload-hook*
(funcall *file-upload-hook* filename))
filename))))))
designator))
@alaa-alawi
Copy link
Author

alaa-alawi commented May 21, 2012 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment