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 file_upload(return_url_key="urls", return_state_key="state", return_success="success", return_error="error", save_temporary_url=False, file_type="image", return_key=False, prefix=""): try: return_success = int(return_success) except: pass try: return_error = int(return_error) except: pass if request.files: files = request.files.values() if files: r = file_up(files, prefix=prefix, file_name=None) if r: urls = [] if save_temporary_url: for key in r: if key: r1 = mdb_web.db.tempfile.update_one({"type":file_type, "user_id":current_user.str_id}, {"$addToSet":{"paths":key}}, upsert=True) if not r1.modified_count: mdb_web.db.tempfile.insert_one({"type":file_type, "user_id":current_user.str_id, "paths":[key]}) urls.append(get_file_url(key)) else: for key in r: if key: urls.append(get_file_url(key)) if urls: data = {return_url_key: urls, return_state_key: return_success, "msg": gettext("Uploaded successfully"), "msg_type": "s", "http_status": 201} if return_key: data["keys"] = r else: data = { return_state_key: return_error, "msg": gettext("Upload failed"), "msg_type": "e", "http_status": 400 } else: data = { return_state_key:return_error, "msg":gettext("Get file error"), "msg_type":"e", "http_status":400 } else: data = {return_state_key:return_error,"msg":gettext("Get file error"), "msg_type":"e", "http_status":400} else: data = {return_state_key:return_error,"msg":gettext("No file submitted"), "msg_type":"e", "http_status":400} return data
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