场景描述
我们服务器的存储空间是很宝贵的,我们不想存储那些一样的文件
这时候我们可以用MD5 签名,确保文件的唯一性
解决方案
flask代码
PICTURE_PATH = "E:/pictures/"
ALLOWED_EXTENSIONS = {'png', "PNG", 'jpg', "JPG", 'jpeg', "JPEG", 'gif', "GIF", }
CHANGE_EXTENSIONS = {
'png': 'png',
'PNG': 'png',
'jpg': 'jpg',
'JPG': 'jpg',
'jpeg': 'jpg',
'JPEG': 'jpg',
'gif': 'gif',
'GIF': 'gif',
}
@picture_space_blue_print.route("/picture", methods=["POST"])
def add_picture():
img = request.files["picture"]
if img and '.' in img.filename:
ext = img.filename.rsplit('.', 1)[1]
if ext in ALLOWED_EXTENSIONS:
ext = CHANGE_EXTENSIONS