diff --git a/README.md b/README.md index 81b1a84..5e212d5 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,10 @@ org-roam-server :config (setq org-roam-server-host "127.0.0.1" org-roam-server-port 8080 - org-roam-server-export-inline-images t org-roam-server-authenticate nil + org-roam-server-export-inline-images t + org-roam-server-serve-files t + org-roam-server-served-file-extensions '("pdf" "mp4" "ogv") org-roam-server-network-poll t org-roam-server-network-arrows nil org-roam-server-network-label-truncate t @@ -45,34 +47,6 @@ feature to work, org-roam protocol should be configured in the system. Also make sure the emacs server is started; `M-x server-start RET` - -## Expose Local files - -![localfiles](https://raw.githubusercontent.com/goktug97/org-roam-server/master/local-files.gif) - -If you want to expose your local files to be accessible through the Preview -modal page you can check perform the following actions: - -```elisp -(setq org-roam-server-enable-access-to-local-files t - org-roam-server-webserver-prefix "/home" - org-roam-server-webserver-address "127.0.0.1:8887/" - org-roam-server-webserver-supported-extensions '("pdf" "mp4" "ogv")) -``` - -There are security reasons your browse disable this feature to be used, -however if you are aware of them and want to expose only in your local -network your files, you can use a Web Server like `python3 -m http.server -8887` and expose a specific *folder*. - -This *folder* needs to be setup in the `org-roam-server-webserver-prefix` -and the address of the webserver configured at -`org-roam-server-webserver-address` variable. - -Remember to keep your Web Server active in the background while navigating in your Roam Graph. - -Be safe, but also be happy. - ## License org-roam-server is licensed under the MIT License. diff --git a/local-files.gif b/local-files.gif deleted file mode 100644 index 5451cdb..0000000 Binary files a/local-files.gif and /dev/null differ diff --git a/org-roam-server.el b/org-roam-server.el index b0a00b7..ec57954 100644 --- a/org-roam-server.el +++ b/org-roam-server.el @@ -124,23 +124,13 @@ or { \"physics\": { \"enabled\": false } }" :group 'org-roam-server :type 'boolean) -(defcustom org-roam-server-enable-access-to-local-files nil - "Enable access to your local files through a Web Server." +(defcustom org-roam-server-serve-files t + "Serve local files when `t`." :group 'org-roam-server :type 'boolean) -(defcustom org-roam-server-webserver-address "127.0.0.1:8887/" - "Address to access your local web server." - :group 'org-roam-server - :type 'string) - -(defcustom org-roam-server-webserver-prefix nil - "Prefix of the folder being hosted by your webserver e.g. /home/." - :group 'org-roam-server - :type 'string) - -(defcustom org-roam-server-webserver-supported-extensions '("pdf" "mp4" "ogv") - "Supported file extensions to be opened by your browser." +(defcustom org-roam-server-served-file-extensions '("pdf" "mp4" "ogv") + "Supported file extensions to be served." :group 'org-roam-server :type 'list) @@ -166,23 +156,23 @@ the reload button." (defcustom org-roam-server-extra-node-options nil "Additional options for nodes. -A list suitable for `json-encode', e.g. (list (cons 'shape \"box\")), +A list suitable for `json-encode', e.g. (list (cons 'shape \"box\")), or a custom function with one argument NODE producing such a list. In the first case options are applied to all nodes." :group 'org-roam-server :type '(choice (list :tag "Argument to json-encode") - (function :tag "Custom function"))) + (function :tag "Custom function"))) (defcustom org-roam-server-extra-edge-options nil "Additional options for edges. -A list suitable for `json-encode', e.g. (list (cons 'width 3)), +A list suitable for `json-encode', e.g. (list (cons 'width 3)), or a custom function with one argument EDGE producing such a list. In the first case options are applied to all edges." :group 'org-roam-server :type '(choice (list :tag "Argument to json-encode") - (function :tag "Custom function"))) + (function :tag "Custom function"))) (define-obsolete-variable-alias 'org-roam-server-label-wrap-length 'org-roam-server-network-label-wrap-length "org-roam-server 1.0.3") @@ -191,6 +181,21 @@ In the first case options are applied to all edges." (define-obsolete-variable-alias 'org-roam-server-label-truncate-length 'org-roam-server-network-label-truncate-length "org-roam-server 1.0.3") +(define-obsolete-variable-alias 'org-roam-server-webserver-supported-extensions + 'org-roam-server-served-file-extension "org-roam-server 1.0.5") + +(make-obsolete 'org-roam-server-enable-access-to-local-files + "the files are served with the org-roam-server instead of an external server." + "org-roam-server 1.0.5") + +(make-obsolete 'org-roam-server-webserver-address + "the files are served with the org-roam-server instead of an external server." + "org-roam-server 1.0.5") + +(make-obsolete 'org-roam-server-webserver-prefix + "the files are served with the org-roam-server instead of an external server." + "org-roam-server 1.0.5") + (defun org-roam-server-random-token (length) "Create a random token with length of `LENGTH`." (with-temp-buffer @@ -218,25 +223,23 @@ In the first case options are applied to all edges." (setq-local org-export-with-sub-superscripts nil) (setq-local org-html-style-default org-roam-server-export-style) (insert-file-contents ,file) - - ;; Handle opening media in your computer - (if org-roam-server-enable-access-to-local-files + + ;; Handle served files + (if org-roam-server-serve-files (let* ((file-string (buffer-string)) (-regexp (format "\\[\\[\\(file:\\)\\(.*\\.\\(%s\\)\\)\\]\\(\\[.*\\]\\)?\\]" - (org-roam-server-concat-or-regexp-tokens - org-roam-server-webserver-supported-extensions))) + (org-roam-server-concat-or-regexp-tokens + org-roam-server-served-file-extensions))) (positions (s-matched-positions-all -regexp file-string)) (matches (s-match-strings-all -regexp file-string))) (dolist (match matches) + (eval (org-roam-server-export-file-servlet match)) (let ((path (elt match 2)) (link (elt match 0))) - (unless (file-name-absolute-p path) - (setq path (concat (file-name-directory ,file) path))) - (setq path (file-truename path)) (if (file-exists-p path) (setq file-string - (s-replace link (format "[[http://%s%s]%s]" org-roam-server-webserver-address - (string-remove-prefix org-roam-server-webserver-prefix path) + (s-replace link (format "[[file:%s]%s]" + (secure-hash 'sha256 path) (elt match 4)) file-string))))) (erase-buffer) @@ -371,7 +374,18 @@ DESCRIPTION is the shown attribute to the user." (if org-roam-server-authenticate (if (not (string= org-roam-server-token token)) (httpd-error httpd-current-proc 403))) - (insert-file-contents ,link)))) + (set-buffer-multibyte nil) + (insert-file-contents-literally ,link)))) + +(defun org-roam-server-export-file-servlet (file) + "Create servlet in the server for the given FILE LINK." + `(defservlet* ,(intern (secure-hash 'sha256 (elt file 2))) + ,(httpd-get-mime (elt file 3)) (token) + (if org-roam-server-authenticate + (if (not (string= org-roam-server-token token)) + (httpd-error httpd-current-proc 403))) + (set-buffer-multibyte nil) + (insert-file-contents-literally ,(elt file 2)))) (defun org-roam-server-export-file-id (link description format) "Append token to the file links.