def upload_head(): # 上传文件的头像字段 img # 表单参数token file = request.files.get('img',None) token = request.form.get('token',None) if all((bool(file),bool(token))): # 验证图片类型 print(type(file)) if file.content_type in ('image/png','image/jpeg'): filename = uuid.uuid4().hex+os.path.splitext(file.filename)[-1] file.save(filename) # 上传到云服务器 file_key = oss.upload_file(filename) os.remove(filename) user_id = get_token_user_id(token) UserDao().set_userinfo(key='u_headpic',value=file_key,where='id',args=user_id) img_url = oss.get_url(file_key) return jsonify({ 'code':200, 'msg':'头像上传成功', 'img':img_url }) else: return jsonify({ 'code': 207, 'msg': '图片只支持png和jpeg' }) else: return jsonify({ 'code':207, 'msg':'POST参数必须有img和token' })
def upload_avator(): # 上传的头像字段为 img # 表单参数: token file: FileStorage = request.files.get('img', None) token = request.form.get('token', None) if all((bool(file), bool(token))): # 验证文件的类型, png/jpeg/jpg, 单张不能超过2M # content-type: image/png, image/jpeg user_id = get_token_user_id(token) print(file.content_length, 'bytes') if file.content_type in ( 'image/png', 'image/jpeg', ): filename = uuid.uuid4().hex \ + os.path.splitext(file.filename)[-1] file.save(filename) # 上传到oss云服务器上 key = oss.upload_file(filename) os.remove(filename) # 删除临时文件 # 将key写入到DB中 # dao.update_pwd(key,user_id) return jsonify({'code': 200, 'msg': '上传文件成功', 'file_key': key}) else: return jsonify({'code': 201, 'msg': '图片格式只支持png或jpeg'}) return jsonify({'code': 100, 'msg': 'POST请求参数必须有img和token'})
def changeinfo(): resp = eval(request.get_data()) if resp: token = resp.pop('token') id = get_token_user_id(token) file: FileStorage = request.files.get('img', None) if all((bool(token),bool(id))): # 验证文件的类型, png/jpeg/jpg, 单张不能超过2M # content-type: image/png, image/jpeg if bool(file): if file.content_type in ('image/png','image/jpeg'): filename = uuid.uuid4().hex + os.path.splitext(file.filename)[-1] file.save(filename) key = oss.upload_file(filename) # 上传到oss云服务器上 os.remove(filename) # 删除临时文件 PersonDao().perupdate('u_pic', key, id) # 将key写入到DB中 for k,v in resp.items(): PersonDao().perupdate(k,v,id) return ({'code':200,'msg':'ok!','file_key': key}) return jsonify({'code': 201,'msg': '图片格式只支持png或jpeg'}) for k, v in resp.items(): PersonDao().perupdate(k, v, id) return ({'code': 200, 'msg': 'ok!'}) else: return jsonify({'code': 204, 'msg': 'POST请求参数必须有token!'}) return jsonify({'code': 304, 'msg': '传入数据为空'})
def upload_avator(): # 上传的头像字段为 img # 表单参数: token file: FileStorage = request.files.get('img', None) token = request.form.get('token', None) if all((bool(file), bool(token))): # 验证文件的类型, png/jpeg/jpg, 单张不能超过2M # content-type: image/png, image/jpeg print(file.content_length, 'bytes') if file.content_type in ('image/png', 'image/jpeg'): filename = uuid.uuid4().hex \ + os.path.splitext(file.filename)[-1] file.save(filename) # 上传到oss云服务器上 key = oss.upload_file(filename) os.remove(filename) # 删除临时文件 # 将key写入到DB中 resp = request.get_json() token = resp.get('token', None) user_id = get_token_user_id(token) u_dao = UserDao() user_id = u_dao.get_profile(user_id) if user_id: pass # if udao.check_login_phone(u_phone): # login_user = udao.login_msg(u_phone, msg_code) # 检查验证码 # if login_user.get('id'): # token = cache.new_token() # cache.save_token(token, id) # udao.user_update('u_auth_string', u_auth_string, 'u_phone', u_phone) # 更新密码 # udao.user_update('is_active', 1, 'u_phone', u_phone) # 更新状态 return jsonify({ 'code': 200, 'msg': '上传文件成功', 'file_key': key }) else: return jsonify({ 'code': 201, 'msg': '图片格式只支持png或jpeg' }) return jsonify({ 'code': 100, 'msg': 'POST请求参数必须有img和token' })
def upload_avator(): # 上传的头像字段为 img # 表单参数: token try: file: FileStorage = request.files.get('img', None) token = request.args.get('token', None) if all((bool(file), bool(token))): # 验证文件的类型, png/jpeg/jpg, 单张不能超过2M # content-type: image/png, image/jpeg if file.content_type in ('image/png', 'image/jpeg'): filename = uuid.uuid4().hex \ + os.path.splitext(file.filename)[-1] file.save(filename) # 上传到oss云服务器上 key = oss.upload_file(filename) os.remove(filename) # 删除临时文件 # token = request.args.get('token') # 将key写入到DB中 id = get_token_user_id(token) sql = "update yl_user set photo=(%s) where id=%s" db = BaseDao() db.query(sql, key, id) return jsonify({ 'code': 200, 'msg': '上传文件成功', 'file_key': key }) else: return jsonify({ 'code': 201, 'msg': '图片格式只支持png或jpeg' }) return jsonify({ 'code': 100, 'msg': 'POST请求参数必须有img和token' }) except: return jsonify({ "code":400, "msg":"更换头像失败" })
def update_avatar(request): if request.method == 'GET': filename = request.GET.get('filename') key = oss.upload_file(settings.BASE_DIR + filename) login_user = request.session['login_user'] login_user['avatar'] = oss.get_small_url(key) user = User.objects.get(pk=login_user.get('uid')) user.avatar = key user.save() # 更新头像的oss的key # 删除临时文件 os.remove(settings.BASE_DIR + filename) return render_json(200, {'url': login_user['avatar'], 'msg': '更新头像成功'}) return render_json(100, '只允许GET请求')
def test_upload(self): filename = 'mm6.jpg' key = oss.upload_file(filename) print(key) self.assertIsNotNone(key, '上传失败')