diff --git a/src/mirror-img.lisp b/src/mirror-img.lisp index 525d1bf..850c486 100644 --- a/src/mirror-img.lisp +++ b/src/mirror-img.lisp @@ -89,13 +89,12 @@ among the given tags." "Given a URL, return the corresponding path we would download it to, were we to mirror it." (let* ((url (quri:uri url)) - (path (quri:uri-path url))) + (path (uri-path+query url))) (pathname - (format nil "~@[~A/~]~A/~A~@[.~A~]" + (format nil "~@[~A/~]~A/~A" base-dir (quri:uri-host url) - (pathname-name path) - (pathname-type path))))) + (pathname-leaf path))))) @@ -183,3 +182,9 @@ That is, 443 is implied by HTTPS, so nil is returned; but 998 wouldn’t be impl (defun uri-path+query (uri) "Return everything in a URI after the TLD — that is, both the path _and_ the query." (format nil "~A~@[?~A~]" (quri:uri-path uri) (quri:uri-query uri))) + +;; pathname → string +(defun pathname-leaf (pathname) + "Given a pathname, return the entirety of the file leaf. +That is, everything following the last directory name." + (car (last (cl-strings:split (namestring pathname) #\/)))) diff --git a/t/mirror-img.lisp b/t/mirror-img.lisp index 0fa5e41..227232f 100644 --- a/t/mirror-img.lisp +++ b/t/mirror-img.lisp @@ -70,6 +70,12 @@ (mirror-img::mirrored-pathname "https://invalid.tld/dir/bird apple.txt" :base-dir #p"base/"))) +(define-test mirrored-pathname.esperanto-question (:tags '(base)) + (assert-equal + #p"base/invalid.tld/?!?! ĉu ne?!?!.png" + (mirror-img::mirrored-pathname "https://invalid.tld/dir/s/d/ askldjas/ asldkja/?!?! ĉu ne?!?!.png" + :base-dir #p"base/"))) + (define-test linked-urls (:tags '(dom)) (assert-equal '("http://localhost:4242/res/img/b/fireplace.jpg" @@ -119,3 +125,12 @@ nil (mirror-img::uri-explicit-port (quri:uri "https://xwx.moe:443/bird.png")))) +(define-test pathname-leaf.simple (:tags '(util)) + (assert-equal + "apple_berries.png" + (mirror-img::pathname-leaf #p"/mom/dad/apple/laksdjal/apple_berries.png"))) + +(define-test pathname-leaf.very-weird (:tags '(util)) + (assert-equal + " lak -!?ĉ_berries.png" + (mirror-img::pathname-leaf #p"/mom/dad?!?Ljla asdasd'' aslkdjĉ/apple/laksdjal/ lak -!?ĉ_berries.png")))