emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] make org-attach-url download function as an option
@ 2020-04-29 13:49 stardiviner
  2020-05-24 14:01 ` Bastien
  0 siblings, 1 reply; 7+ messages in thread
From: stardiviner @ 2020-04-29 13:49 UTC (permalink / raw)
  To: Org Mode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256


I found when network is bad and slow, or the download file is big, the
org-attach-url will suspend Emacs for a long time. User might have to cancel
downloading, and start again later.

I hope to make "org-attach-url" download file asynchronously. But function
org-attach-attach hardcoded this function for 'url method. Here is a patch to
make it into an option. So user can specify a custom function to download file.

# ==============================================================================

BTW, I'm wandering why function "url-retrieve" download file is not complete and
failed. Bellowing code snippets are my try:

Use function ~url-copy-file~ works fine:

#+begin_src emacs-lisp :eval no
(url-copy-file
 "https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2592521688.webp"
 (expand-file-name "~/kk.webp"))
#+end_src

I check out function ~url-copy-file~ implement, try to implement an async version like this:

#+begin_src emacs-lisp
(defun url-copy-file-asynchronous (url newname &optional ok-if-already-exists &rest _ignored)
  "Copy URL to NEWNAME.  Both arguments must be strings.
Signal a `file-already-exists' error if file NEWNAME already
exists, unless a third argument OK-IF-ALREADY-EXISTS is supplied
and non-nil.  An integer as third argument means request
confirmation if NEWNAME already exists."
  (and (file-exists-p newname)
       (or (not ok-if-already-exists)
           (and (integerp ok-if-already-exists)
                (not (yes-or-no-p
                      (format "File %s already exists; copy to it anyway? "
                              newname)))))
       (signal 'file-already-exists (list "File already exists" newname)))
  (let* ((buffer (or (url-retrieve url (lambda (&rest ignored)))
                     (signal 'file-missing
                             (list "Opening URL"
                                   "No such file or directory" url))))
         (handle (with-current-buffer buffer
                   (mm-dissect-buffer t))))
    (let ((mm-attachment-file-modes (default-file-modes)))
      (mm-save-part-to-file handle newname))
    (kill-buffer buffer)
    (mm-destroy-parts handle)))

(put 'copy-file 'url-file-handlers #'url-copy-file-asynchronous)
#+end_src

#+begin_src emacs-lisp
(url-copy-file-asynchronous
 "https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2592521688.webp"
 (expand-file-name "~/kk.webp"))
#+end_src

But it download seems incomplete and failed. Don't know why.

Also there are extra async libraries support download file asynchronously. Like
"deferred", "async" etc. Those libraries can be used for user define their own
functions.

Is you have better idea, please tell me, thanks in advance.

- -- 
[ stardiviner ]
       I try to make every word tell the meaning that I want to express.

       Blog: https://stardiviner.github.io/
       IRC(freenode): stardiviner, Matrix: stardiviner
       GPG: F09F650D7D674819892591401B5DF1C95AE89AC3
      
-----BEGIN PGP SIGNATURE-----

iQFIBAEBCAAyFiEE8J9lDX1nSBmJJZFAG13xyVromsMFAl6phdUUHG51bWJjaGls
ZEBnbWFpbC5jb20ACgkQG13xyVromsNswQgAjuFtM/+WU0EXdpQOx3C4kymgUTex
jEZgw96JlfHSP3JcoOuA5Ll2F+nXShWwIeGJAe1aRgjLTy9O5XSZO6BdlxoN2QRB
RfrZvl5X5cJu9NIyiwhSYbIMM8+DJKHSp/nBGIimXwDoNU/ye43E4OmApVKfWjf0
TmMXUzLnXWSxdSubFe/M7SEGUaarFI5y2bNwytBMOQr7q1yY6UgynlB6Vda06mqb
BO/AnJgYtP69XvghK14NJ6/X8nol5oFijLtt67QUNKBCdUlp3tx/G+6lFebGruHS
kZ6PjTzoAwZOk5l7lTx9mG+dle1g/7rNce1TRVFP+cT1+qS5+7UHhwfnwg==
=pYDj
-----END PGP SIGNATURE-----


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-07-16 23:21 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-04-29 13:49 [PATCH] make org-attach-url download function as an option stardiviner
2020-05-24 14:01 ` Bastien
2020-05-25  0:12   ` stardiviner
2020-07-15  9:05   ` [QUESTION] " stardiviner
2020-07-16  0:52     ` Ihor Radchenko
2020-07-16 22:54       ` stardiviner
2020-07-16 23:20         ` Ihor Radchenko

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).