Skip to content

Instantly share code, notes, and snippets.

@alaa-alawi
Created May 21, 2012 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alaa-alawi/2763794 to your computer and use it in GitHub Desktop.
Save alaa-alawi/2763794 to your computer and use it in GitHub Desktop.
(defun make-upload-filename ()
(etypecase *upload-filename-generator*
;; the old behaviour.
(null t)
;; the new behaviour.
((or symbol function)
(lambda (&rest args)
(let ((filename (apply *upload-filename-generator* args)))
(when *file-upload-hook*
(funcall *file-upload-hook* filename))
filename)))
(directory-pathname
(lambda (&rest args &key file-name)
(let ((filename (make-pathname :name file-name
:directory *upload-filename-generator*)))
(when *file-upload-hook*
(funcall *file-upload-hook* filename))
filename)))))
(deftype directory-pathname ()
'(and pathname (satisfies cl-fad:directory-pathname-p)))
@alaa-alawi
Copy link
Author

(defun make-upload-filename-generator ()
  (etypecase *upload-filename-generator* 
    ;; the old behaviour.
    (null (lambda (&rest args)
            (declare (ignore args))
            (funcall #'make-tmp-file-name)))
    ;; the new behaviour.
    ((or symbol function) 
     (lambda (&rest args)
       (let ((filename (apply *upload-filename-generator* args :allow-other-keys t)))
         (when *file-upload-hook*
           (funcall *file-upload-hook* filename))
         filename)))))

@hanshuebner
Copy link

This looks good - One more thing - The file-upload-hook invocation should be moved out of make-tmp-file-name and called from the function that is returned from make-upload-filename-generator, something like;

(let ((make-filename (ecase ...)))
(lambda (&rest args)
(let ((filename (funcall make-filename args)))
(when file-upload-hook
(funcall ..))
filename)))

i.e. don't spread the hacky file-upload-hook throughout the code if it can be localized to one place.

Thanks,
Hans

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