Synology offers great tools: Cloud Station automatically synchronizes your files between your PC and the Synology NAS, and Photo Station has a nice web interface to let your family and friends access your pictures.

Unfortunately, these tools do not play together. I want my pictures to be automatically synchronized with my Synology NAS and be available from Photo Station. The Photo Station includes a transfer tool but that's not an automatic solution. I'll use Cloud Station since it can also be used to synchronize other files. The solution has three parts:

  • First, install Cloud station and create a folder to put the pictures in.
  • Then, mount the folder into a subfolder of Photos to make it available from Photo Station.
  • Finally, monitor any change to the folder to force generation of a thumbnail for each new picture.

Install Cloud station

I won't go into details for this package provided by Synology: just follow their tutorial: https://help.synology.com/dsm/?section=DSM&version=5.2&link=Tutorial%2Fcloud_sync_files.html Once done, create a folder to store the pictures.

Mount folder

Assuming the picture folder is named "Photos", the absolute path should look like /volume1/homes/<username>/CloudStation/Photos. To make the folder content available from Photo Station, use mount with --bind option:

mount --bind /volume1/homes/<username>/CloudStation/Photos /volume1/photo/<username>

Monitor changes

To monitor every change in the folder, we use a Python script. First install the Python package in Synology to be able to run Python scripts. Then, install pyinotifiy

~$ wget http://peak.telecommunity.com/dist/ez_setup.py
~$ python ez_setup.py pyinotify
Searching for pyinotify
Reading http://pypi.python.org/simple/pyinotify/
Best match: pyinotify 0.9.4
Downloading https://pypi.python.org/packages/source/p/pyinotify/pyinotify-0.9.4.tar.gz#md5=701c91854d241514ede7ffe72086566d
Processing pyinotify-0.9.4.tar.gz
Running pyinotify-0.9.4/setup.py -q bdist_egg --dist-dir /tmp/easy_install-wjy0eW/pyinotify-0.9.4/egg-dist-tmp-eOvHXs
zip_safe flag not set; analyzing archive contents...
Adding pyinotify 0.9.4 to easy-install.pth file

Installed /usr/local/packages/@appstore/Python/usr/local/lib/python2.7/site-packages/pyinotify-0.9.4-py2.7.egg
Processing dependencies for pyinotify
Finished processing dependencies for pyinotify

Now the Python script: it can be created in any location (I created a /volume1/Extensions/scripts folder):

cat > /volume1/Extensions/scripts/monitor.py << EOF
import pyinotify
import sys
import os.path
from subprocess import call
import signal

log_file = open("/var/log/monitor.log", "a")

def log(text):
log_file.write(text + "\n")
log_file.flush()

def signal_handler(signal, frame):
log("Exiting")
sys.exit(0)


log("Starting")

signal.signal(signal.SIGTERM, signal_handler)

allowed_exts = ["jpg", "jpeg", "png", "tga", "gif", "bmp", "mp3", "flac", "aac", "wma", "ogg", "ogv", "mp4", "avi"]

wm = pyinotify.WatchManager()  # Watch Manager
mask = pyinotify.IN_CLOSE_WRITE | pyinotify.IN_DELETE | pyinotify.IN_CREATE | pyinotify.IN_MOVED_TO | pyinotify.IN_MOVED_FROM  # watched events


class EventHandler(pyinotify.ProcessEvent):
def __init__(self):
self.modified_files = set()

def process_IN_CREATE(self, event):
self.process_create(event)

def process_IN_MOVED_TO(self, event):
self.process_create(event)

def process_IN_MOVED_FROM(self, event):
self.process_delete(event)

def process_IN_DELETE(self, event):
self.process_delete(event)

def process_create(self, event):
arg = ''
if event.dir:
arg = "-A"
else:
arg = "-a"
self.do_index_command(event, arg)

def process_delete(self, event):
arg = ''
if event.dir:
arg = "-D"
else:
arg = "-d"
self.do_index_command(event, arg)

def process_IN_MODIFY(self, event):
if self.is_allowed_path(event.pathname, event.dir):
self.modified_files.add(event.pathname)

def process_IN_CLOSE_WRITE(self, event):
# ignore close_write unlesss the file has previously been modified.
if (event.pathname in self.modified_files):
self.do_index_command(event, "-a")

def do_index_command(self, event, index_argument):
if self.is_allowed_path(event.pathname, event.dir):
log("synoindex %s %s" % (index_argument, event.pathname))
call(["synoindex", index_argument, event.pathname])

# Remove from list of modified files.
try:
self.modified_files.remove(event.pathname)
except KeyError, e:
# Don't care.
pass
else:
log("%s is not an allowed path" % event.pathname)


def is_allowed_path(self, filename, is_dir):
# Don't check the extension for directories
if not is_dir:
ext = os.path.splitext(filename)[1][1:].lower()
if ext not in allowed_exts:
return False
if filename.find("@eaDir") > 0:
return False
return True

handler = EventHandler()
notifier = pyinotify.Notifier(wm, handler)
wdd = wm.add_watch(["/volume1/music", "/volume1/photo", "/volume1/video"], mask, rec=True, auto_add=True)

try:
notifier.loop(daemonize=True, pid_file='/var/run/monitor.pid')
except pyinotify.NotifierError, err:
print >> sys.stderr, err
EOF

To run the script, just type:

python /volume1/Extensions/scripts/monitor.py

Automate Loading

This final part is about making sure the new system works after a NAS upgrade or reboot. Create a startup script that will handle the mount and startup of the monitoring script.

~$ cat > /usr/local/etc/rc.d/S99customMountPhotoStation.sh << EOF
~$ mount --bind /volume1/homes/<username>/CloudStation/Photos /volume1/photo/<username>
~$ # Install pyinotify package (in case Python package had an update on Synology).
~$ cd /tmp
~$ wget http://peak.telecommunity.com/dist/ez_setup.py
~$ python ez_setup.py pyinotify
~$ rm ez_setup.py
~$ # Start monitoring script
~$ python /volume1/Extensions/scripts/monitor.py
~$ EOF
~$ chmod u+x /usr/local/etc/rc.d/S99customMountPhotoStation.sh

To check the setup is working, just restart the NAS and add some pictures using Cloud Station.

Source for Python notify script: http://codesourcery.wordpress.com/2012/11/29/more-on-the-synology-nas-automatically-indexing-new-files/

Comments   

# python beginnerJosef 2015-09-20 16:04
hello neo73,

this should be that i need, but iám a absolute beginner in python and i didn´t found a beginner tutorial about python in conjunction with a synology nas. Do you have a link or a little tutorial how to use this script above to use Cloud and Photostation together :dry:

thanks
Reply
# greatiostrym 2016-07-11 15:28
thanks a lot, it is exactly what I was looking for.
But did you try symbolic link instead of the "mount" procedure ?
It seems that there is no more the indexing problem that need the python script and symbolic link don't need to be created at each startup :

https://forum.synology.com/enu/viewtopic.php?f=17&t=68474&p=442785#p442785
Reply