예제 #1
0
def import_food_img():
    food_id = request.args.get('id')
    file_type = request.args.get('file_type', 'food')

    upload_dir = app.config['MY_UPLOAD_FOOD_DIR']

    if not food_id:
        return jsonify(error_message('菜品iD不存在')), 421

    file = request.files['file']
    if not file or not allowed_file(file.filename):
        return jsonify(error_message('文件不存在,或者文件格式不对')), 421
    filename = secure_filename(file.filename) if not check_contain_chinese(file.filename) else file.filename

    path = os.path.join(upload_dir, filename)
    url = '{}/{}/{}'.format(app.config['PRE_UPLOAD_PATH'], file_type, filename)

    old_food = Food.query.filter_by(id=food_id).first()
    if not old_food:
        return jsonify(error_message('菜品iD不存在')), 421

    del_os_filename(upload_dir, old_food.img_name)

    old_food.img_name = filename
    old_food.img_url = url
    old_food.img_path = path

    file.save(path)
    return jsonify(ok_message())
예제 #2
0
def import_img():
    # file id 存在 上传,否则编辑
    file_id = request.args.get('id')
    file_type = request.args.get('file_type', 'dash')
    language = request.args.get('language', 'zh')

    upload_dir = app.config['MY_UPLOAD_DASH_DIR']

    file = request.files['file']
    if not file or not allowed_file(file.filename):
        return jsonify(error_message('文件不存在,或者文件格式不对')), 421
    filename = secure_filename(file.filename) if not check_contain_chinese(file.filename) else file.filename

    path = os.path.join(upload_dir, filename)
    url = '{}/{}/{}'.format(app.config['PRE_UPLOAD_PATH'], file_type, filename)

    if file_id:
        old_img = Picture.query.get_or_404(file_id)
        del_os_filename(upload_dir, old_img.img_name)

        old_img.img_name = filename
        old_img.img_url = url
        old_img.img_path = path
    else:
        img = Picture(img_name=filename,
                      language=language,
                      img_url=url,
                      img_path=path)
        db.session.add(img)

    file.save(path)
    return jsonify(ok_message())