Beispiel #1
0
def config():
    try:
        set_site_config(request.form.to_dict())
    except Exception as e:
        logger.error(e, exc_info=True)
        return dict(code=1, msg="An unknown error occurred in the program")
    else:
        return dict(code=0)
Beispiel #2
0
def upimg_save(**kwargs):
    res = dict(code=1)
    try:
        filename = kwargs["filename"]
        stream = kwargs["stream"]
        upload_path = kwargs.get("upload_path") or ""
        if not filename or not stream:
            return ValueError
    except (KeyError, ValueError):
        res.update(msg="Parameter error")
    else:
        dn = g.cfg.tencent_dn
        bucket = g.cfg.tencent_bucket
        sid = g.cfg.tencent_sid
        skey = g.cfg.tencent_skey
        tencent_basedir = g.cfg.tencent_basedir or ''
        if not dn or not bucket or not sid or not skey:
            res.update(msg="The tencent parameter error")
            return res
        if isinstance(upload_path, string_types):
            if upload_path.startswith("/"):
                upload_path = upload_path.lstrip('/')
            filepath = join(tencent_basedir, upload_path, filename)
            #: 使用腾讯云云COS官方SDK上传
            config = get_config()
            region = g.cfg.tencent_region
            if not region:
                info = get_bucket_info(bucket, config)
                if info and isinstance(info, dict) and "Location" in info:
                    region = info['Location']
                    config = get_config(region)
                    set_site_config(dict(tencent_region=region))
            client = CosS3Client(config)
            result = client.put_object(
                Bucket=bucket,
                Key=filepath,
                Body=stream,
                EnableMD5=False
            )
            ETag = result['ETag'].replace('"', '')
            if ETag:
                res.update(
                    code=0,
                    etag=ETag,
                    src=slash_join(dn, filepath),
                    basedir=tencent_basedir,
                )
        else:
            res.update(msg="The upload_path type error")
    return res
Beispiel #3
0
def upimg_save(**kwargs):
    res = dict(code=1)
    try:
        filename = kwargs["filename"]
        stream = kwargs["stream"]
        upload_path = kwargs.get("upload_path") or ""
        if not filename or not stream:
            return ValueError
    except (KeyError, ValueError):
        res.update(msg="Parameter error")
    else:
        dn = g.cfg.aliyun_dn
        bucket = g.cfg.aliyun_bucket
        ak = g.cfg.aliyun_ak
        sk = g.cfg.aliyun_sk
        aliyun_basedir = g.cfg.aliyun_basedir or ''
        if not dn or not bucket or not ak or not sk:
            res.update(msg="The aliyun parameter error")
            return res
        if isinstance(upload_path, string_types):
            if upload_path.startswith("/"):
                upload_path = upload_path.lstrip('/')
            if aliyun_basedir.startswith("/"):
                aliyun_basedir = aliyun_basedir.lstrip('/')
            filepath = join(aliyun_basedir, upload_path, filename)
            #: 使用阿里云云OSS官方SDK上传
            auth = get_auth()
            endpoint = g.cfg.aliyun_endpoint
            if not endpoint:
                info = get_bucket_info(bucket, auth)
                if info and isinstance(info, dict) and "endpoint" in info:
                    endpoint = info['endpoint']
                    set_site_config(dict(aliyun_endpoint=endpoint))
            obj = Bucket(auth, endpoint, bucket)
            result = obj.put_object(filepath, stream)
            if result.status == 200:
                res.update(
                    code=0,
                    etag=result.etag,
                    src=slash_join(dn, filepath),
                    basedir=aliyun_basedir,
                )
        else:
            res.update(msg="The upload_path type error")
    return res