And again, I solve a problem exactly 14 seconds after asking it...

DOH!

I had a hander method that was reading the file object prior to the
tests. So, the "empty" was actually correct.

I changed the order of the methods and it alls works as expected now.

Oops.

W

On 29 October 2010 18:15, wawa wawawa <wazawaz...@gmail.com> wrote:
> Hi All,
> I'm trying to allow some flexibility for my end users. I want to
> support the uploading of multiple file archive types (tar.gz,
> tar.bzip2, zip, rar etc).
> I want to use python-magic to guess the filetype from the files magic
> number and then use the appropriate decompression modules to get at
> the data in the files.
> Basically, I'm not sure how to make the type of the
> request.FILES['datafile'] be compatible with the requirements of
> python-magic (which needs a file-location or a string)
>
> I have a view:
>
> def upload_file(request):
>      c ={}
>      c.update(csrf(request))
>      if request.method == 'POST':
>         frag = request.FILES['datafile'].read(65536)
>         ft = magic.from_buffer(frag)
>         res = {}
>         res.update({ "result" : ft })
>         res.update({ "datafile" : type(request.FILES['datafile']) })
>         res.update({ "frag"  : type(frag) })
>         return render_to_response("simple.html", {'content': res })
>      else:
>         form = UploadFileForm()
>         return render_to_response('upload.html')
> "simple.html" just outputs the data structure "res", as below.
>
> {'content':
>     {'datafile': <class
> 'django.core.files.uploadedfile.TemporaryUploadedFile'>,
>      'frag': <type 'str'>,
>      'result': 'empty'}}
>
>
> I've tried different combinations of magic.from_file and
> magic.from_buffer and both read() and chunk() from the
> TemporaryUploadedFile object but I either get a type error or an
> "empty" result. I think the above should work fine!
>
> Using python-magic on the temp object that's been uploaded (and saved
> to disk 'cos is > 2.5MB) works with both
> magic.from_buffer(open("f").read(65536)) and magic.from_file("f").
>
> Any ideas?
>
> (also, anyone have any quick drop in helper methods for debugging I
> can use instead of the horrible "res" dict I'm using?)
>
> Thanks in advance
>
> Wa
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to