pythondockersynologyhome-assistant

shutil.copy2() file size doesn't match


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


Solution

  • 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.