Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions uploadr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
"""
Expand Down Expand Up @@ -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'))
Expand Down