Esempio n. 1
0
def up_token():
    """
    生成 token 给前端,前端直接上传
    """
    access_key = lin_config.get_config("qiniu.access_key")
    secret_key = lin_config.get_config("qiniu.secret_key")
    # 构建鉴权对象
    q = Auth(access_key, secret_key)
    # 要上传的空间
    bucket_name = lin_config.get_config("qiniu.bucket_name")
    # 允许的文件类型
    allowed_extensions = lin_config.get_config("qiniu.allowed_extensions")
    # 上传后保存的文件名
    filename = request.args.get("filename", str())
    if filename.split(".")[-1] in allowed_extensions:
        # 生成上传 Token,可以指定过期时间等
        # 上传策略示例
        # https://developer.qiniu.com/kodo/manual/1206/put-policy
        policy = {
            # 'callbackUrl':'https://requestb.in/1c7q2d31',
            # 'callbackBody':'filename=$(fname)&filesize=$(fsize)'
            # 'persistentOps':'imageView2/1/w/200/h/200'
        }
        token = q.upload_token(
            bucket_name,
            filename,
            lin_config.get_config("qiniu.token_expire_time"),
            policy,
        )
        return {"token": token}
    else:
        raise FileExtensionError
Esempio n. 2
0
def upload_image_bytes(name: str, data: bytes):
    access_key_id = lin_config.get_config("oss.access_key_id")
    access_key_secret = lin_config.get_config("oss.access_key_secret")
    auth = oss2.Auth(access_key_id, access_key_secret)
    bucket = oss2.Bucket(
        auth,
        lin_config.get_config("oss.endpoint"),
        lin_config.get_config("oss.bucket_name"),
    )
    suffix = name.split(".")[-1]
    rand_name = get_random_str(15) + "." + suffix
    res = bucket.put_object(rand_name, data)
    if res.resp.status == 200:
        return res.resp.response.url
    return None
Esempio n. 3
0
def upload():
    image = request.files.get("image", None)
    if not image:
        raise ParameterError("没有找到图片")
    if image and allowed_file(image.filename):
        path = os.path.join(lin_config.get_config("oss.upload_folder"),
                            image.filename)
        image.save(path)
    else:
        raise ParameterError("图片类型不允许或图片key不合法")
    return Success()
Esempio n. 4
0
    def get_all(self, form):
        query = self.query.filter_by(delete_time=None)

        if form.author.data:
            query = query.filter_by(author=form.author.data)

        limit = (
            form.count.data if form.count.data else lin_config.get_config("poem.limit")
        )

        poems = query.limit(limit).all()

        if not poems:
            raise NotFound("没有找到相关诗词")
        return poems
Esempio n. 5
0
def allowed_file(filename):
    return "." in filename and filename.rsplit(
        ".", 1)[1] in lin_config.get_config("oss.allowed_extensions", [])