emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Juan Manuel Macías" <maciaschain@posteo.net>
To: orgmode <emacs-orgmode@gnu.org>
Subject: [tip] Create and Insert a public Nextcloud/Owncloud link
Date: Sat, 08 Oct 2022 14:29:35 +0000	[thread overview]
Message-ID: <87bkqme6cw.fsf@posteo.net> (raw)

Hi all,

One of the few things that I miss from a desktop environment like Gnome
Shell or KDE Plasma (currently I use EXWM), is the good integration
these environments had with Nextcloud, as I am a heavy user of
Nextcloud. Many times I need to create and share a public link to a file
in my local folder. In the Nextcloud forum I learned how it can be done
from the command line using curl, so starting from that, it wasn’t very
difficult to write this function in Elisp to insert a public link at
point. It can be inserted in two ways: literally or as an Org bracket
link, with description.

For the function to work we must have: a) a folder within our local
Nextcloud directory dedicated exclusively to public files; b) a line
with our personal data in the ~/.authinfo file. Assuming, for example,
that we use a third-party Nextcloud service like Disroot, the necessary
line in authinfo would be:

machine cloud.disroot.org login <my-user-name> password <my-password>

And let’s go with the function. Before I have defined these three
variables:

┌────
│ (defvar nextcloud-url "https://cloud.disroot.org")
│ (defvar nextcloud-public-folder "~/disroot/Public/")
│ (defvar nextcloud-public-folder-name "Public")
└────

And the function itself (caveat: the function is not asynchronous):

┌────
│ (defun insert-nextcloud-link ()
│   (interactive)
│   (let* ((file-name (read-file-name "File: "
│ 				    nextcloud-public-folder))
│ 	 (file (if (directory-name-p
│ 		    file-name)
│ 		   (replace-regexp-in-string
│ 		    (expand-file-name
│ 		     nextcloud-public-folder)
│ 		    ""
│ 		    file-name)
│ 		 (file-name-nondirectory file-name)))
│ 	 (desc
│ 	  (read-from-minibuffer
│ 	   "Link description (enter = no description): "))
│ 	 (auth
│ 	  (nth 0
│ 	       (auth-source-search
│ 		:host "cloud.disroot.org"
│ 		:requires '(user secret))))
│ 	 (my-username (plist-get auth :user))
│ 	 (my-passwd (funcall (plist-get auth :secret)))
│ 	 (result-raw (shell-command-to-string
│ 		      (concat "curl -u "
│ 			      "\""
│ 			      my-username
│ 			      ":"
│ 			      my-passwd
│ 			      "\""
│ 			      " -H \"OCS-APIRequest: true\" -X POST "
│ 			      nextcloud-url
│ 			      "/ocs/v1.php/apps/files_sharing/api/v1/shares -d path="
│ 			      "\""
│ 			      nextcloud-public-folder-name
│ 			      "/"
│ 			      file
│ 			      "\""
│ 			      " -d shareType=3"
│ 			      " -d permissions=1")))
│ 	 (result (with-temp-buffer
│ 		   (insert result-raw)
│ 		   (goto-char (point-min))
│ 		   (re-search-forward "<url>" nil t)
│ 		   (let ((desde (point)))
│ 		     (re-search-forward "<" nil t)
│ 		     (narrow-to-region desde (- (point) 1))
│ 		     (buffer-string)))))
│     (if (string= desc "")
│ 	(insert result)
│       (insert (format "[[%s][%s]]" result desc)))))
└────

Best regards,

Juan Manuel 


             reply	other threads:[~2022-10-08 14:31 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-08 14:29 Juan Manuel Macías [this message]
2022-10-09  3:32 ` [tip] Create and Insert a public Nextcloud/Owncloud link Max Nikulin
2022-10-09 12:21   ` Juan Manuel Macías

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87bkqme6cw.fsf@posteo.net \
    --to=maciaschain@posteo.net \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).