Include images and other files from your Nextcloud in Writefreely

Tags: #nextcloud #writefreely #linux

Writefreely is a superb, minimalistic blogging platform, but it lacks an easy means of integrating images (and other files). Imagine you could use simple Markdown to include images from your Nextcloud instance, like so:

![Frog](/files/USERNAME/Frog.jpg)

Well, I show you how to!

The idea is to mount a special folder from your Nextcloud “Files” section—let’s call it Blog—into your Writefreely instance, under /var/www/writefreely/static/files/USERNAME, using the WebDAV protocol.

Nextcloud → Writefreely

For simplicity’s sake, let’s assume that…

Adapt the following to your needs! Things you need to adapt I’ve noted in UPPERCASE.

Note: Should you run your Writefreely instance in a Proxmox container, you must check FUSE, under Options → Features!

  1. Log in to your Writefreely server using SSH. It’s good practice not to log in as root, so we assume a normal user (with sudo rights) here. Just leave out the sudo in the commands listed if you are logged in as root.

  2. We most probably need to install FUSE and the DAV file system first:

    sudo apt install fuse davfs2
    
  3. Assuming you already have a working Writefreely installed at /var/www/writefreely, and are running it under the www-data user, we now create a mount point for our Nextcloud files:

    cd /var/www/writefreely/static/
    sudo -u www-data mkdir -p files/USERNAME
    sudo chmod -R 755 files/
    

    Note we create the new folders as user www-data (the user Writefreely runs as). Without need, never run anything as the root user! It adds an extra layer of protection.

  4. For accessing our Nextcloud data via WebDAV, we need to give the system our Nextcloud credentials (Nextcloud username and password), hidden away in the file /etc/davfs2/secrets, which must have access rights of 600 (-rw-------) and thus be only available to the root user, for security.
    We use

    sudo nano /etc/davfs/secrets
    

    to edit this file, and under the section Credential Line add our mount point and our Nextcloud credentials:

    /var/www/writefreely/static/files/USERNAME USERNAME "MYSECRETPASSWORD"
    

    Using Ctrl+O and Enter we save the changes, then exit using Ctrl+X.

  5. Time for testing:

    sudo mount -t davfs https://CLOUD.EXAMPLE.COM/remote.php/dav/files/USERNAME/Blog /var/www/writefreely/static/files/USERNAME -o ro,uid=www-data,gid=www-data
    ls -la /var/www/writefreely/static/files/USERNAME/
    

    All files from your Nextcloud “Blog” folder should appear with uid=www-data and gid=www-data, like so:

    -rw-r--r-- 1 www-data www-data 457744 Mar 27 13:07 Frog.jpg
    -rw-r--r-- 1 www-data www-data 474653 Mar 27 13:52 Gorilla.jpg
    

    (In Nextcloud, I simply copied two of the demo files from “Photos” into my “Blog” folder.)
    Unmount again, using:

    sudo umount /var/www/writefreely/static/files/USERNAME/
    
  6. If step 5 went okay, it’s now time to set up your /etc/fstab, so your Nextcloud “Blog” folder will be automatically mounted into your Writefreely instance on every boot:

    sudo nano /etc/fstab
    

    Add the following and be sure not to make any typing mistakes—your Writefreely instance might not boot up anymore if you do:

    https://CLOUD.EXAMPLE.COM/remote.php/dav/files/USERNAME/Blog /var/www/writefreely/static/files/USERNAME davfs ro,uid=www-data,gid=www-data 0 0
    

    Before rebooting, try it out using:

    sudo mount -a
    ls -la /var/www/writefreely/static/files/USERNAME/
    

    The result should be as above in step 5.

  7. Reboot and enjoy!
    Your files and images should now be available in Writefreely using simple Markdown commands. Let’s include the Frog.jpg here:

    ![Frog](/files/USERNAME/Frog.jpg)
    

    Result: Frog

Note: Should you run both Nextcloud and Writefreely as separate VMs or containers on a Proxmox host, ýou must assure that Nextcloud is started before Writefreely! Just use Options → Start/Shutdown order and set to appropriate values. I have Nextcloud at 30 and Writefreely at 40, giving Nextcloud 30s to start up.

Note: Since Writefreely can be used as a multi-user blog, I showed you the (marginally) more complicated way using a USERNAME. This makes it easier adding more users and their blogs later on.

Note: The method described here keeps a WebDAV connection open to your Nextcloud instance. It is most appropriate in stable network conditions, but it will break if your Nextcloud sits behind a home router without a fixed IP address (i.e. one that periodically changes its outside IP address). Coping with such situations is a subject for another post, though. (Hint: Use autofs/automount.)