def avatar_upload(): ''' 头像上传 :return: ''' result = None imgfile_base = request.argget.all("imgfile_base") if imgfile_base: result = fileup_base_64(uploaded_files = [imgfile_base], prefix="user_avatar/") else: file = request.files['imgfile'] if file: tailoring = request.argget.all('tailoring') if tailoring: if not isinstance(tailoring, dict): tailoring = json.loads(tailoring) for k in ["width", "height", "x", "y", "rotate"]: tailoring.setdefault(k, 0) result = file_up(uploaded_files=[file], prefix="user_avatar/", tailoring=tailoring) data = {} if result: result = result[0] user = mdb_user.db.user.find_one({"_id":current_user.id}) if user: if result["key"] != user['avatar_url']["key"]: # 当使用了不同的名字删除老的头像 file_del(user['avatar_url']) update_data = { "avatar_url" : result } r = mdb_user.db.user.update_one({"_id":current_user.id}, {"$set":update_data}) if not r.matched_count: data = {'msg':gettext("Save failed"), 'msg_type':"w", "http_status":400} else: if result["type"] == "local": # 如果保存再本地的话, 保存为一定尺寸大小 path = "{}{}".format(APPS_PATH, get_file_url(result)) imgcp = ImageCompression(path, path) ava_size = get_config("account", "USER_AVATAR_SIZE") imgcp.custom_pixels(ava_size[0], ava_size[1]) data = {'msg':gettext("Save successfully"), 'msg_type':"s", "http_status":201} if not data: data = {'msg':gettext("Upload failed"), 'msg_type':"w", "http_status":400} return data
def theme_static_file(path): """ 获取主题下静态文件 注意: theme主题图片获取路由 (目录themes/<theme name>/static下图片) 1.对于图片,本路由只能获取目录themes/<theme name>/static下定义尺寸图片 参数w,h可指定图片大小 :param path:原图片路径 :param w:获取的宽 :param h:获取的高 :return:w和h都大于0则返回相应尺寸图片; w和h都等于0则返回原图; 其中一个值大于0则返回以这个值为基础等比缩放图片 """ w = str_to_num(request.args.get("w", 0)) h = str_to_num(request.args.get("h", 0)) if w or h: path_list = os.path.splitext(path.rstrip().rstrip("/")) absolute_path = os.path.abspath( "{}/{}/static/{}_w_{}_h_{}{}".format( theme_view.template_folder, get_config( "theme", "CURRENT_THEME_NAME"), path_list[0], w, h, path_list[1])) if not os.path.isfile(absolute_path): img_path = os.path.abspath( "{}/{}/static/{}".format( theme_view.template_folder, get_config( "theme", "CURRENT_THEME_NAME"), path)) try: imgcs = ImageCompression(img_path, absolute_path) except BaseException: abort(404) if w and h: # 自定义长宽 imgcs.custom_pixels(w, h) else: # 等比缩放 imgcs.isometric(w, h) else: absolute_path = os.path.abspath( "{}/{}/static/{}".format( theme_view.template_folder, get_config( "theme", "CURRENT_THEME_NAME"), path)) if not os.path.isfile(absolute_path): abort(404) return send_file(filename_or_fp=absolute_path, conditional=True, last_modified=True)
def static_file(path): """ apps/static下静态文件获取(本视图函数只针对apps/static下的图片),apps/static下其他可以直接哟你flask默认的 注意:图片获取路由(apps/static下) 参数w,h可指定图片大小 :param path:原图片路径 :param w:获取的宽 :param h:获取的高 :return:w和h都大于0则返回相应尺寸图片; w和h都等于0则返回原图; 其中一个值大于0则返回以这个值为基础等比缩放图片 """ w = str_to_num(request.args.get("w", 0)) h = str_to_num(request.args.get("h", 0)) if w or h: path_list = os.path.splitext(path.rstrip().rstrip("/")) absolute_path = os.path.abspath("{}/{}_w_{}_h_{}{}".format( static.template_folder, path_list[0], w, h, path_list[1])) if not os.path.isfile(absolute_path): img_path = os.path.abspath("{}/{}".format(static.template_folder, path)) try: imgcs = ImageCompression(img_path, absolute_path) except BaseException: abort(404) if w and h: # 自定义长宽 imgcs.custom_pixels(w, h) else: # 等比缩放 imgcs.isometric(w, h) else: absolute_path = os.path.abspath("{}/{}".format(static.template_folder, path)) if not os.path.isfile(absolute_path): abort(404) absolute_path = banel_translate_js_files(prefix="static", route_relative_path=path, absolute_path=absolute_path) return send_file(filename_or_fp=absolute_path, conditional=True, last_modified=True)
def avatar_upload(): """ 头像上传 :return: """ result = None imgfile_base = request.argget.all("imgfile_base") max_size_mb = get_config("account", "USER_AVATAR_MAX_SIZE") max_size_b = max_size_mb * 1024 * 1024 if imgfile_base: if len(imgfile_base) > max_size_b: data = { "msg": gettext("Upload avatar image can not exceed {}M".format( max_size_mb)), "msg_type": "w", "http_status": 413 } return data else: result = fileup_base_64(uploaded_files=[imgfile_base], prefix="user_avatar/") else: file = request.files['upfile'] if len(file.read()) > max_size_b: data = { "msg": gettext("Upload avatar image can not exceed {}M".format( max_size_mb)), "msg_type": "w", "http_status": 413 } return data if file: tailoring = request.argget.all('tailoring') if tailoring: if not isinstance(tailoring, dict): tailoring = json.loads(tailoring) for k in ["width", "height", "x", "y", "rotate"]: tailoring.setdefault(k, 0) result = file_up(uploaded_files=[file], prefix="user_avatar/", tailoring=tailoring) data = {} if result: result = result[0] user = get_one_user(user_id=current_user.str_id) if user: if user['avatar_url'] and "key" in user['avatar_url'] \ and result["key"] != user['avatar_url']["key"]: # 当使用了不同的名字删除老的头像 file_del(user['avatar_url']) update_data = {"avatar_url": result} r = update_one_user(user_id=current_user.str_id, updata={"$set": update_data}) if not r.matched_count: data = { 'msg': gettext("Save failed"), 'msg_type': "w", "http_status": 400 } else: if result["type"] == "local": # 如果保存再本地的话, 保存为一定尺寸大小 path = "{}{}".format(APPS_PATH, get_file_url(result)) imgcp = ImageCompression(path, path) ava_size = get_config("account", "USER_AVATAR_SIZE") imgcp.custom_pixels(ava_size[0], ava_size[1]) data = { 'msg': gettext("Save successfully"), 'msg_type': "s", "http_status": 201 } if not data: data = { 'msg': gettext("Upload failed"), 'msg_type': "w", "http_status": 400 } # 清理user信息数据缓存 delete_user_info_cache(user_id=current_user.str_id) return data