instead of using is_tarfile() you could use the TarFile class (which
can take a file object) to check whether the data can be interpreted
as a tar file:


from tarfile import TarFile, TarError
from StringIO import StringIO

try:
    TarFile(fileobj=StringIO(data))
except TarError:
    print "Not a tar file"



On Nov 24, 3:47 pm, Thadeus Burgess <[email protected]> wrote:
> 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
>
> On Mon, Nov 23, 2009 at 10:31 PM, Richard <[email protected]> wrote:
>
> > IIRC, you can use db.table.field.store(request.vars.field) to get the
> > web2py generated filename.
>
> > On Nov 24, 2:53 pm, Thadeus Burgess <[email protected]> wrote:
> > > The problem is I am trying to get the file from the function in
> > > SQLFORM.onvalidation... and this happens before the file gets renamed or
> > > saved to the disk. The only way i see of accomplishing this is
>
> > > if form.accepts(request.vars, session, dbio=False):
>
> > >                 #def is_plugin_archive(form)
> > >                 #import tarfile, os
> > >                 #path = os.path.join(db._folder, '..', 'uploads',
> > > form.vars.file_newfilename)
>
> > >                 #if not tarfile.is_tarfile(path):
> > >                     #form.errors.file = "Not a valid tar archive"
>
> > >                 is_plugin_archive(form)
>
> > >                 if not form.errors:
> > >                     session.flash = "Version uploaded."
> > >                     redirect(URL(r=request, f='plug', args=plugin.id))
> > >                 else:
> > >                     response.flash = "There were errors with the file"
>
> > >             elif form.errors:
> > >                 response.flash = "There were errors"
>
> > > -Thadeus
>
> > > On Mon, Nov 23, 2009 at 9:46 PM, mr.freeze <[email protected]> wrote:
> > > > ieve it is form.vars.<upload_field>_newfilename. You should be
> > > > able to get the path
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to