Ah yes would love to, however it ends up copy the file twice, since it will do it in my function, and then again later on in SQLFORM.accepts.
This is the modified version, it works, I just need to make sure the file is not too large for the server to handle.
The limitation really comes from tarfile.is_tarfile, it expects and requires a filename, because it wants to open the file itself. Therefore I need a copy of the file somewhere besides in memory.
Any ideas on improvements or a better way?
def is_valid_tar(form):
import tarfile, os, random, shutil
tmppath = os.path.join(request.folder, 'uploads', 'tmpupf%f.temp.w2p' % random.random())
f = form.vars.file.file
try:
dest_file = open(tmppath, 'wb')
shutil.copyfileobj(f, dest_file)
except:
pass
finally:
dest_file.close()
if not tarfile.is_tarfile(tmppath):
form.errors.file = "Invalid file format"
try:
os.remove(tmppath)
except:
pass
#...
form.accepts(...onvalidation=is_valid_tar)
-Thadeus