2

Setup: Synology with Docker running Home-Assistant with HACS integration and pyscript.

I have made the following two functions:

@service

def getListOfFiles(dirName):
    import os
    # create a list of file and sub directories 
    # names in the given directory
    listOfFile = os.listdir(dirName)
    allFiles = list()
    # Iterate over all the entries
    for entry in listOfFile:
        # Create full path
        fullPath = os.path.join(dirName, entry)
        # If entry is a directory then get the list of files in this directory 
        if os.path.isdir(fullPath):
            allFiles = allFiles + getListOfFiles(fullPath)
        else:
            if fullPath.endswith('jpg'):
                allFiles.append(fullPath)
            elif fullPath.endswith('jpeg'):
                allFiles.append(fullPath)
            elif fullPath.endswith('png'):
                allFiles.append(fullPath)
    return allFiles

@service
def slideshow():
    import random
    import os
    import shutil
    path = '/Slideshow'
    listOfFiles = getListOfFiles(path)
    random_image = random.choice([x for x in listOfFiles])
    image_path = '{}'.format(random_image)
    shutil.copy2(image_path, '/config/www/slide.jpg')

Now everything works, BUT the destination file (slide.jpg) is never the correct size. It varies between 10kB - 1000kB, while the original image is often between 7-10 MB.

Any suggestions?

Running the same code (with different source and destination, of course) on Mac works perfectly.

Same results using .copyfile and .copy

GKH
  • 179
  • 1
  • 13
  • Could there be a race condition going on somewhere, with the output file not fully written to disk by the time you inspect it? I'm grasping at straws, I know, but this is odd behaviour that smells like a synching delay issue. Extra details that could help troubleshoot: how/when/where are you checking the size of the results? Does a (stupid) sleep inserted between copy and size check make a difference? – joanis Oct 31 '21 at 18:32
  • I'm checking in Synology File Station, and the copied image has a reduced resolution. If there's something I should write into the code to help troubleshoot, let me know. This is the first python script I've written for Home Assistant – GKH Oct 31 '21 at 19:00
  • Interesting, if the image is valid with reduced resolution, it's not a case of corruption. It's mysterious to me what could reduce the resolution in the code you show, however! – joanis Oct 31 '21 at 23:17
  • So digging around a lot I accidentally found out, that if the web interface for Synology (DSM) is open in the background, this situation happens. If I close the web interface, the image is copied over, in correct size. Any ideas on why, but more importantly, how to fix that? – GKH Oct 31 '21 at 23:27
  • Frankly, I have no idea. I'm not actually familiar with Synology, I am analyzing your question based on the code you posted. If I have to hazard a guess, maybe the interactive web interface is detecting that a new file was added and has some rules to process it? What's clear is that the code you show, by itself, cannot be the cause. – joanis Oct 31 '21 at 23:32
  • Looks like the web interface application is doing something with your images – Gonzalo Odiard Nov 01 '21 at 00:31
  • Yeah, I was thinking if there would be any benefit of many trying the shutil.copyfileobj() but I cannot get the arguments to work with the goal forcing a bigger buffer. – GKH Nov 01 '21 at 00:42
  • Update: now it doesn't matter if the web interface is open or not – GKH Nov 01 '21 at 00:52
  • I've now tried to also run the script through the Synology Task Scheduler, setting it to go off every minute, with the exact same results. – GKH Nov 01 '21 at 12:02

1 Answers1

0

So after a lot of digging around, the issue was found. Synology creates a directory: /@eaDir/ for every file with a thumbnail in S, M, L which turned out to be the root cause. This (sometimes) was the file being transferred over and not the assumed image, hence the smaller size.

GKH
  • 179
  • 1
  • 13