def UploadClipperAvatar(): if request.form.get("action") == "add": data = request.form.get("picStr") imgdata = base64.b64decode(data) filename = gen_rnd_filename() + ".png" #随机命名 if PLUGINS['UpYunStorage']['enable'] in ('true', 'True', True): imgUrl = "/EauDouce/avatar/" + filename upres = UploadImage2Upyun(imgUrl, base64str(data)) imgUrl = PLUGINS['UpYunStorage']['dn'].strip("/") + imgUrl logger.sys.info( "Avatar to Upyun file saved, its url is %s, result is %s" % (imgUrl, upres)) else: if not os.path.exists(UPLOAD_FOLDER): os.makedirs(UPLOAD_FOLDER) file = open(os.path.join(UPLOAD_FOLDER, filename), 'wb') file.write(imgdata) file.close() imgUrl = "/" + IMAGE_FOLDER + filename logger.sys.info("Avatar to local file saved in %s, its url is %s" % (UPLOAD_FOLDER, imgUrl)) # return user home and write avatar url into mysql db. res = g.api.user_update_avatar(g.username, imgUrl) else: res = {"success": False, "msg": u"不支持的action"} logger.sys.info(res) return jsonify(res)
def UploadAvatarImage(): logger.sys.debug(request.files) f = request.files.get('file') # Check if the file is one of the allowed types/extensions if f and allowed_file(f.filename): filename = secure_filename(gen_rnd_filename() + "." + f.filename.split('.')[-1]) #随机命名 if PLUGINS['UpYunStorage']['enable'] in ('true', 'True', True): imgUrl = "/EauDouce/avatar/" + filename upres = UploadImage2Upyun(imgUrl, f.stream.read()) imgUrl = PLUGINS['UpYunStorage']['dn'].strip("/") + imgUrl logger.sys.info( "Avatar to Upyun file saved, its url is %s, result is %s" % (imgUrl, upres)) else: if not os.path.exists(UPLOAD_FOLDER): os.makedirs(UPLOAD_FOLDER) f.save(os.path.join(UPLOAD_FOLDER, filename)) imgUrl = "/" + IMAGE_FOLDER + filename logger.sys.info("Avatar to local file saved in %s, its url is %s" % (UPLOAD_FOLDER, imgUrl)) # return user home and write avatar url into mysql db. res = g.api.user_update_avatar(g.username, imgUrl) else: res = {"success": False, "msg": u"上传失败: 未成功获取文件或格式不允许"} logger.sys.info(res) return jsonify(res)
def test(): logger.sys.debug(request.files) f = request.files.get('file') # Check if the file is one of the allowed types/extensions if f and allowed_file(f.filename): filename = secure_filename(gen_rnd_filename() + "." + f.filename.split('.')[-1]) #随机命名 if PLUGINS['UpYunStorage']['enable'] in ('true', 'True', True): imgUrl = "/EauDouce/test/" + filename upres = UploadImage2Upyun(imgUrl, f.stream.read()) imgUrl = PLUGINS['UpYunStorage']['dn'].strip("/") + imgUrl logger.sys.info( "TEST to Upyun file saved, its url is %s, result is %s" % (imgUrl, upres)) else: if not os.path.exists(UPLOAD_FOLDER): os.makedirs(UPLOAD_FOLDER) f.save(os.path.join(UPLOAD_FOLDER, filename)) imgUrl = "/" + IMAGE_FOLDER + filename logger.sys.info("TEST to local file saved in %s, its url is %s" % (UPLOAD_FOLDER, imgUrl)) res = dict(code=0, imgUrl=imgUrl) else: res = {"code": -1, "msg": u"上传失败: 未成功获取文件或格式不允许"} logger.sys.info(res) return jsonify(res)
def useruploadpub(): # 通过表单形式上传图片 res = dict(code=1, msg=None) logger.debug(request.files) f = request.files.get('file') callableAction = request.args.get("callableAction") if f and allowed_file(f.filename): filename = gen_rnd_filename() + "." + secure_filename( f.filename).split('.')[-1] # 随机命名 # 判断是否上传到又拍云还是保存到本地 if Upyun['enable'] in ('true', 'True', True): basedir = Upyun['basedir'] if Upyun['basedir'].startswith( '/') else "/" + Upyun['basedir'] imgUrl = os.path.join(basedir, filename) try: # 又拍云存储封装接口 UploadImage2Upyun(imgUrl, f.stream.read()) except Exception, e: logger.error(e, exc_info=True) res.update(code=2, msg="System is abnormal") else: imgUrl = Upyun['dn'].strip("/") + imgUrl res.update(data=dict(src=imgUrl), code=0) else: if not os.path.exists(UPLOAD_FOLDER): os.makedirs(UPLOAD_FOLDER) f.save(os.path.join(UPLOAD_FOLDER, filename)) imgUrl = request.url_root + IMAGE_FOLDER + filename res.update(data=dict(src=imgUrl), code=0)
def userupload(): # 通过base64形式上传图片 res = dict(code=1, msg=None) picStr = request.form.get('picStr') if picStr: # 判断是否上传到又拍云还是保存到本地 if Upyun['enable'] in ('true', 'True', True): basedir = Upyun['basedir'] if Upyun['basedir'].startswith('/') else "/" + Upyun['basedir'] imgUrl = os.path.join(basedir, gen_rnd_filename() + ".png") try: # 又拍云存储封装接口 UploadImage2Upyun(imgUrl, base64.b64decode(picStr)) except Exception, e: logger.error(e, exc_info=True) res.update(code=2, msg="System is abnormal") else: imgUrl = Upyun['dn'].strip("/") + imgUrl res.update(imgUrl=imgUrl, code=0) else: filename = gen_rnd_filename() + ".png" if not os.path.exists(UPLOAD_FOLDER): os.makedirs(UPLOAD_FOLDER) with open(os.path.join(UPLOAD_FOLDER, filename), "wb") as f: f.write(base64.b64decode(picStr)) imgUrl = request.url_root + IMAGE_FOLDER + filename res.update(imgUrl=imgUrl, code=0) # "回调"动作 if res.get("imgUrl"): imgUrl = res.get("imgUrl") callableAction = request.args.get("callableAction") if callableAction == "UpdateAvatar": resp = g.api.userprofile.updateUserAvatar(uid=g.uid, avatarUrl=imgUrl) res.update(resp) if resp["code"] == 0: # 同步头像 g.api.usersso.clientsConSync(g.api.userapp.getUserApp, g.uid, dict(CallbackType="user_avatar", CallbackData=imgUrl))
def userupload(): # 通过base64形式上传图片 res = dict(code=1, msg=None) picStr = request.form.get('picStr') if picStr: # 判断是否上传到picbed图床、又拍云还是保存到本地 if PICBED['enable'] in ('true', 'True', True): try: resp = requests.post( PICBED["api"], data=dict(picbed=picStr, album="passport"), headers=dict(Authorization="LinkToken %s" % PICBED["LinkToken"]), timeout=5).json() if not isinstance(resp, dict): raise except Exception as e: res.update(code=4, msg=e) else: if resp.get('code') == 0: res.update(imgUrl=resp['src'], code=0) else: res.update(resp) elif Upyun['enable'] in ('true', 'True', True): basedir = Upyun['basedir'] if Upyun['basedir'].startswith( '/') else "/" + Upyun['basedir'] imgUrl = os.path.join(basedir, gen_rnd_filename() + ".png") try: # 又拍云存储封装接口 UploadImage2Upyun(imgUrl, base64.b64decode(picStr)) except Exception, e: logger.error(e, exc_info=True) res.update(code=2, msg="System is abnormal") else: imgUrl = Upyun['dn'].strip("/") + imgUrl res.update(imgUrl=imgUrl, code=0) else: filename = gen_rnd_filename() + ".png" if not os.path.exists(UPLOAD_FOLDER): os.makedirs(UPLOAD_FOLDER) with open(os.path.join(UPLOAD_FOLDER, filename), "wb") as f: f.write(base64.b64decode(picStr)) imgUrl = request.url_root + IMAGE_FOLDER + filename res.update(imgUrl=imgUrl, code=0) # "回调"动作 if res.get("imgUrl"): imgUrl = res.get("imgUrl") callableAction = request.args.get("callableAction") if callableAction == "UpdateAvatar": resp = g.api.userprofile.updateUserAvatar(uid=g.uid, avatarUrl=imgUrl) res.update(resp) if resp["code"] == 0: # 同步头像 g.api.usersso.clientsConSync( g.api.userapp.getUserApp, g.uid, dict(CallbackType="user_avatar", CallbackData=imgUrl))
def useruploadpub(): # 通过表单形式上传图片 res = dict(code=1, msg=None) logger.debug(request.files) f = request.files.get('file') callableAction = request.args.get("callableAction") if f and allowed_file(f.filename): filename = gen_rnd_filename() + "." + secure_filename( f.filename).split('.')[-1] # 随机命名 # 判断是否上传到又拍云还是保存到本地 if PICBED['enable'] in ('true', 'True', True): try: files = { 'picbed': (filename, f.stream.read(), 'image/%s' % filename.split(".")[-1]) } resp = requests.post( PICBED["api"], files=files, data=dict(album="passport"), headers=dict(Authorization="LinkToken %s" % PICBED["LinkToken"]), timeout=5).json() if not isinstance(resp, dict): raise except Exception as e: res.update(code=4, msg=e) else: if resp.get('code') == 0: res.update(data=dict(src=resp['src']), code=0) else: res.update(resp) elif Upyun['enable'] in ('true', 'True', True): basedir = Upyun['basedir'] if Upyun['basedir'].startswith( '/') else "/" + Upyun['basedir'] imgUrl = os.path.join(basedir, filename) try: # 又拍云存储封装接口 UploadImage2Upyun(imgUrl, f.stream.read()) except Exception, e: logger.error(e, exc_info=True) res.update(code=2, msg="System is abnormal") else: imgUrl = Upyun['dn'].strip("/") + imgUrl res.update(data=dict(src=imgUrl), code=0) else: if not os.path.exists(UPLOAD_FOLDER): os.makedirs(UPLOAD_FOLDER) f.save(os.path.join(UPLOAD_FOLDER, filename)) imgUrl = request.url_root + IMAGE_FOLDER + filename res.update(data=dict(src=imgUrl), code=0)
def UploadBlogImage(): editorType = request.args.get("editorType", "wangEditor") logger.sys.debug(request.files) f = request.files.get("WriteBlogImage") or request.files.get( "editormd-image-file") if f and allowed_file(f.filename): filename = secure_filename(gen_rnd_filename() + "." + f.filename.split('.')[-1]) #随机命名 if PLUGINS['UpYunStorage']['enable'] in ('true', 'True', True): imgUrl = "/EauDouce/blog/" + filename upres = UploadImage2Upyun(imgUrl, f.stream.read()) imgUrl = PLUGINS['UpYunStorage']['dn'].strip("/") + imgUrl logger.sys.info( "Blog to Upyun file saved, its url is %s, result is %s" % (imgUrl, upres)) else: if not os.path.exists(UPLOAD_FOLDER): os.makedirs(UPLOAD_FOLDER) f.save(os.path.join(UPLOAD_FOLDER, filename)) imgUrl = request.url_root + IMAGE_FOLDER + filename logger.sys.info("Blog to local file saved in %s, its url is %s" % (UPLOAD_FOLDER, imgUrl)) #返回数据加上自定义头 if editorType == "wangEditor": res = Response(imgUrl) res.headers["ContentType"] = "text/html" else: res = jsonify(url=imgUrl, message=None, success=1) res.headers["ContentType"] = "application/json" else: result = r"error|未成功获取文件或格式不允许,上传失败" if editorType == "wangEditor": res = Response(result) res.headers["ContentType"] = "text/html" else: res = jsonify(message=result, success=0) res.headers["ContentType"] = "application/json" res.headers["Charset"] = "utf-8" return res
def UploadBlogImage(): editorType = request.args.get("editorType", "wangEditor") logger.sys.debug(request.files) f = request.files.get("WriteBlogImage") or request.files.get( "editormd-image-file") if f and allowed_file(f.filename): filename = secure_filename(gen_rnd_filename() + "." + f.filename.split('.')[-1]) #随机命名 # 判断是否上传到又拍云还是保存到本地 if PICBED['enable'] in ('true', 'True', True): success = True try: files = { 'picbed': (filename, f.stream.read(), 'image/%s' % filename.split(".")[-1]) } resp = requests.post( PICBED["api"], files=files, data=dict(album="blog"), headers=dict(Authorization="LinkToken %s" % PICBED["LinkToken"]), timeout=5).json() if not isinstance(resp, dict): raise except Exception as e: success = False result = e.message else: if resp.get('code') == 0: imgUrl = resp["src"] else: success = False result = resp["msg"] if success is False: if editorType == "wangEditor": res = Response(result) res.headers["ContentType"] = "text/html" else: res = jsonify(message=result, success=0) res.headers["ContentType"] = "application/json" res.headers["Charset"] = "utf-8" return res elif PLUGINS['UpYunStorage']['enable'] in ('true', 'True', True): imgUrl = "/EauDouce/blog/" + filename upres = UploadImage2Upyun(imgUrl, f.stream.read()) imgUrl = PLUGINS['UpYunStorage']['dn'].strip("/") + imgUrl logger.sys.info( "Blog to Upyun file saved, its url is %s, result is %s" % (imgUrl, upres)) else: if not os.path.exists(UPLOAD_FOLDER): os.makedirs(UPLOAD_FOLDER) f.save(os.path.join(UPLOAD_FOLDER, filename)) imgUrl = request.url_root + IMAGE_FOLDER + filename logger.sys.info("Blog to local file saved in %s, its url is %s" % (UPLOAD_FOLDER, imgUrl)) #返回数据加上自定义头 if editorType == "wangEditor": res = Response(imgUrl) res.headers["ContentType"] = "text/html" else: res = jsonify(url=imgUrl, message=None, success=1) res.headers["ContentType"] = "application/json" else: result = r"error|未成功获取文件或格式不允许,上传失败" if editorType == "wangEditor": res = Response(result) res.headers["ContentType"] = "text/html" else: res = jsonify(message=result, success=0) res.headers["ContentType"] = "application/json" res.headers["Charset"] = "utf-8" return res