diff --git a/uploadr.py b/uploadr.py index 263bc5ff..db84d97f 100755 --- a/uploadr.py +++ b/uploadr.py @@ -65,13 +65,26 @@ import json from xml.dom.minidom import parse import hashlib -import fcntl import errno import subprocess import re import ConfigParser from multiprocessing.pool import ThreadPool +try: + # fcntl module only available in linux/unix platforms + import fcntl +except ImportError: + sys.stderr.write('No fcntl module available. Skipping multiple instances check.') + +try: + # Try to import a module used to check if the files are valid image files + from PIL import Image + chkImages = True +except ImportError: + sys.stderr.write('Image module not available. Not checking is image files are valid.\n') + chkImages = False + if sys.version_info < (2, 7): sys.stderr.write("This script requires Python 2.7 or newer.\n") sys.stderr.write("Current version: " + sys.version + "\n") @@ -455,10 +468,29 @@ def grabNewFiles(self): if ext in ALLOWED_EXT: fileSize = os.path.getsize(dirpath + "/" + f) if (fileSize < FILE_MAX_SIZE): - files.append(os.path.normpath(dirpath + "/" + f).replace("'", "\'")) + # add check if file has valid image + if isValidImageFile(dirpath + "/" + f): + files.append(os.path.normpath(dirpath + "/" + f).replace("'", "\'")) + else: + print ' ! Skipping invalid file %s' % (dirpath + "/" + f) files.sort() return files + def isValidImageFile(self, ffname): + """check if image file is a valid image file + """ + if not chkImages: + return True + try: + fd = open(ffname, 'rb') + im = Image.open(fd) + im.verify() + fd.close() + return True + except IOError: + fd.close() + return False + def uploadFile(self, file): """ uploadFile """ @@ -1125,6 +1157,8 @@ def print_stat(self): f = open(LOCK_PATH, 'w') try: fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB) + except NameError: + pass except IOError, e: if e.errno == errno.EAGAIN: sys.stderr.write('[%s] Script already running.\n' % time.strftime('%c'))