Skip to content

Commit

Permalink
remove external server dependency for local files and fix gif playback
Browse files Browse the repository at this point in the history
  • Loading branch information
goktug97 committed Aug 15, 2020
1 parent bc880c1 commit b8e3f4c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 58 deletions.
32 changes: 3 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.

Expand Down
Binary file removed local-files.gif
Binary file not shown.
72 changes: 43 additions & 29 deletions org-roam-server.el
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit b8e3f4c

Please sign in to comment.