Ejemplo n.º 1
0
def avatar_modify():
    if request.method == 'POST':
        userid = session['userid']
        
        try:
            file = request.files['file']
        except Exception, e:
            message = {'message': u'图片上传域file错误', 'code': 133}
            return json.dumps(message, sort_keys=True)

        if file and utils.allowed_file(file.filename, app.config['ALLOWED_EXTENSIONS']):
            filename = secure_filename(file.filename)
            directoryname = os.path.join(app.config['UPLOAD_AVATAR'], utils.getdirectoryname())

            if not os.path.exists(directoryname):
                os.makedirs(directoryname)
                os.chmod(directoryname, stat.S_IRWXU|stat.S_IRWXG|stat.S_IRWXO) # mode:777

            newfilename = getfilename(filename)

            try:
                file.save(os.path.join(directoryname, newfilename))
            except Exception, e:
                message = {'message': u'图片上传异常', 'code': 133}
                return json.dumps(message, sort_keys=True)

            avatar = '%s/%s' % (os.path.join(app.config['UPLOAD_AVATAR_SQL'], utils.getdirectoryname()), newfilename)

            queryparams = {'userid': userid, 'avatar': avatar}
            result = handle_user_avatar_modify(queryparams)
            if result:
                message = {'message': u'修改头像成功', 'code': 131}
            else:
                message = {'message': u'修改头像失败', 'code': 132}
Ejemplo n.º 2
0
def picture_post():
    if request.method == 'POST':
        # 获取上传的图集id 和图片说明
        galleryid = int(request.form['galleryid'])
        content = request.form['content']
        # 获取图片域
        try:
            file = request.files['file']
        except Exception, e:
            message = {'message': u'图片上传域file错误', 'code': 133}
            return json.dumps(message, sort_keys=True)
        # 验证图片扩展名
        if file and utils.allowed_file(file.filename, app.config['ALLOWED_EXTENSIONS']):
            # 中文名字会报错
            filename = secure_filename(file.filename)
            # 获取以上传日期为命名的目录名
            directoryname = os.path.join(app.config['UPLOAD_GALLERY'], utils.getdirectoryname())
            # 如果日期目录不存在则新建,并赋予权限
            if not os.path.exists(directoryname):
                os.makedirs(directoryname)
                os.chmod(directoryname, stat.S_IRWXU|stat.S_IRWXG|stat.S_IRWXO) # mode:777
            # 获取新的文件名
            newfilename = getfilename(filename)
            # 获取文件数存储名
            orgin_pic = os.path.join(directoryname, newfilename)
            # 保存图片到硬盘上
            try:
                file.save(orgin_pic)
            except Exception, e:
                message = {'message': u'图片上传异常', 'code': 133}
                return json.dumps(message, sort_keys=True)
            # 获取图片在硬盘上的目录名
            dirname = os.path.join(app.config['UPLOAD_GALLERY_SQL'], utils.getdirectoryname())
            # 获取文件数据库存储名
            orgin_url = os.path.join(dirname, newfilename)

            # 裁剪图片
            thumb_reust = utils.thumb_pic(orgin_pic, 300)
            if thumb_reust:
                # 组合数据库存储名
                thumb_url = os.path.join(dirname, thumb_reust)
            else:
                message = {'message': u'图片上传异常', 'code': 133}
                return json.dumps(message, sort_keys=True)

            queryparams = {'galleryid': galleryid, 'orgin_url': orgin_url, 'thumb_url': thumb_url, 'content': content}
            # 将上传的图片地址信息写入数据库
            message = handle_gallery_picture_post(queryparams)