如何在 Django 表单清理方法中使用魔术来验证文件类型? [英] How does one use magic to verify file type in a Django form clean method?

查看:25
本文介绍了如何在 Django 表单清理方法中使用魔术来验证文件类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Django 中编写了一个带有 FileField 的电子邮件表单类.我想通过检查其 mimetype 来检查上传文件的类型.随后,我想将文件类型限制为 pdf、word 和开放式办公文档.

I have written an email form class in Django with a FileField. I want to check the uploaded file for its type via checking its mimetype. Subsequently, I want to limit file types to pdfs, word, and open office documents.

为此,我已经安装了 python-magic 并希望按照 python-magic 的规范检查文件类型如下:

To this end, I have installed python-magic and would like to check file types as follows per the specs for python-magic:

mime = magic.Magic(mime=True)
file_mime_type = mime.from_file('address/of/file.txt')

但是,最近上传的文件在我的服务器上缺少地址.我也不知道类似于from_file_content"的 mime 对象的任何方法,它检查给定文件内容的 mime 类型.

However, recently uploaded files lack addresses on my server. I also do not know of any method of the mime object akin to "from_file_content" that checks for the mime type given the content of the file.

使用魔法验证Django表单中上传文件的文件类型的有效方法是什么?

What is an effective way to use magic to verify file types of uploaded files in Django forms?

推荐答案

Stan 描述了带有缓冲区的良好变体.不幸的是,这种方法的弱点是将文件读入内存.另一种选择是使用临时存储的文件:

Stan described good variant with buffer. Unfortunately the weakness of this method is reading file to the memory. Another option is using temporary stored file:

import tempfile
import magic
with tempfile.NamedTemporaryFile() as tmp:
    for chunk in form.cleaned_data['file'].chunks():
        tmp.write(chunk)
    print(magic.from_file(tmp.name, mime=True))

此外,您可能需要检查文件大小:

Also, you might want to check the file size:

if form.cleaned_data['file'].size < ...:
    print(magic.from_buffer(form.cleaned_data['file'].read()))
else:
    # store to disk (the code above)

另外:

是否可以使用名称再次打开文件,而命名的临时文件仍然打开,因平台而异(在 Unix 上可以这样使用;在 Windows NT 或更高版本上不能).

Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later).

所以你可能想要像所以那样处理它:

So you might want to handle it like so:

import os
tmp = tempfile.NamedTemporaryFile(delete=False)
try:
    for chunk in form.cleaned_data['file'].chunks():
        tmp.write(chunk)
    print(magic.from_file(tmp.name, mime=True))
finally:
    os.unlink(tmp.name)
    tmp.close()

此外,您可能想要<read()之后的code>seek(0):

Also, you might want to seek(0) after read():

if hasattr(f, 'seek') and callable(f.seek):
    f.seek(0)

上传到哪里数据存储

这篇关于如何在 Django 表单清理方法中使用魔术来验证文件类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆