コード例 #1
0
def get_double_fold():
    req_json = request.json
    if not req_json:
        return jsonReturn.falseReturn(request.path, '请传必要参数')
    origin_img_id = req_json.get('originImgId')
    open_id = req_json.get('openId')
    if not all([origin_img_id is not None, open_id is not None]):
        return jsonReturn.falseReturn(request.path, '请传必要参数')
    pic = Picture.objects(openid=open_id,
                          origin_image_id=origin_img_id).first()
    if pic:
        return jsonReturn.trueReturn({'imgId': str(pic.id)}, 'ok')
    else:
        return jsonReturn.falseReturn('', '还没搞好')
コード例 #2
0
def img_upload():
    req_args = request.form
    print(req_args)
    if not req_args:
        return jsonReturn.falseReturn(request.path, '请传必要参数')
    open_id = req_args.get('openId')
    if open_id is None:
        return jsonReturn.falseReturn(request.path, '请传必要参数')
    if 'img' not in request.files:
        return jsonReturn.falseReturn(request.path, '请上传file文件')
    img_obj = request.files.get("img")
    filename = img_obj.filename
    if not filename:
        return jsonReturn.falseReturn(request.path, '请上传file文件')
    img_type = filename[filename.rfind(".") + 1:].lower()
    # print(img_type)
    # pic = Picture(image=img_obj)
    if img_type in Config.ALLOWED_IMAGE:
        try:
            file_MD5 = hashlib.md5(img_obj.read()).hexdigest()
            img_url = Config.SITE + file_MD5
            pic = Picture.objects(img_md5=file_MD5).first()
            if pic:
                return jsonReturn.trueReturn({'imgId': str(pic.id)},
                                             '该文件已经存在了')
            img_obj.seek(0)  # 前面计算 md5 的时候已经 read() 一次了,这里要再读取一次,需要把游标归 0
            # 保存图片到 mongodb 的 picture 中
            pic = Picture(img_md5=file_MD5, url=img_url, openid=open_id)
            pic.image.put(img_obj)
            pic.save()
            return jsonReturn.trueReturn({'imgId': str(pic.id)}, '上传成功')
        except CosServiceError as e:
            return jsonReturn.falseReturn(e.get_error_msg(), '上传失败')
        except Exception as e:
            return jsonReturn.falseReturn('', '上传失败 ' + str(e))
    else:
        return jsonReturn.falseReturn(request.path,
                                      '格式不对,仅支持: ' + str(Config.ALLOWED_IMAGE))