validity of file

3 views
Skip to first unread message

Thadeus Burgess

unread,
Nov 22, 2009, 10:41:41 PM11/22/09
to web...@googlegroups.com
Is there a way, through SQLFORM to open a file, and make sure it is the kind of file you are expecting.

I would like to check to see if the files users are uploading, are indeed w2p tarred files with only .py files with a naming scheme in them.

I know I could use SQLFORM.factory and then open the file after it is saved to the server, then delete it if it was no good, I was just wondering if there was a way to "hook" into the validation.

-Thadeus


mdipierro

unread,
Nov 22, 2009, 10:46:50 PM11/22/09
to web2py-users
There a IS_IMAGE that can check for BMP, GIF, JPEG, and PNG.
You can copy and modify it to check for other formats.
What formats are you thinking about?

Thadeus Burgess

unread,
Nov 22, 2009, 10:50:59 PM11/22/09
to web...@googlegroups.com
This is for uploaded plug-ins (for plugincentral).

I want to make sure they are actually tarred archives with python code in them. I need to check the actual file contents instead of just the filename (which gets checked first, to make sure it is "web2py.plugin.*.w2p"

-Thadeus

Richard

unread,
Nov 23, 2009, 8:49:31 PM11/23/09
to web2py-users
could a new validator be added to check the mime type from the POSTed
content-type?
IS_MIME_TYPE('png')

Django has something like this:
http://docs.djangoproject.com/en/dev/topics/http/file-uploads/?from=olddocs#uploadedfile-objects

Richard


On Nov 23, 2:50 pm, Thadeus Burgess <thade...@thadeusb.com> wrote:
> This is for uploaded plug-ins (for plugincentral).
>
> I want to make sure they are actually tarred archives with python code in
> them. I need to check the actual file contents instead of just the filename
> (which gets checked first, to make sure it is "web2py.plugin.*.w2p"
>
> -Thadeus
>

Thadeus Burgess

unread,
Nov 23, 2009, 9:37:02 PM11/23/09
to web...@googlegroups.com
>>> import tarfile
>>> tarfile.is_tarfile("web2py.plugin.tagging.w2p")
True
>>> tarfile.is_tarfile("cookbook2.pdf")
False
>>> :)
  File "<stdin>", line 1
    :)
    ^
SyntaxError: invalid syntax
>>>

Now this needs to integrate with SQLFORM on_validation.

Should I also open the tar archive, and verify its contents are actually, python code, html, images (such as verifying each of them individually or just their filenames?)

-Thadeus

mdipierro

unread,
Nov 24, 2009, 12:10:12 AM11/24/09
to web2py-users
Unfortunately checking the mime type is no guarantee that the file
format is conform to specs.

On Nov 23, 7:49 pm, Richard <richar...@gmail.com> wrote:
> could a new validator be added to check the mime type from the POSTed
> content-type?
> IS_MIME_TYPE('png')
>
> Django has something like this:http://docs.djangoproject.com/en/dev/topics/http/file-uploads/?from=o...

Richard

unread,
Nov 24, 2009, 6:53:57 PM11/24/09
to web2py-users
no not a guarantee, but would it be worth adding anyway?

Thadeus Burgess

unread,
Nov 24, 2009, 7:37:16 PM11/24/09
to web...@googlegroups.com
I wish there was a python equivalent to the linux "file" command. That thing rocks.

Since my web2py runs on a linux server, I just fork the process over to the command line to verify the files, and then parse the output, and life is good.

-Thadeus
Reply all
Reply to author
Forward
0 new messages