Beispiel #1
0
    def upload_file(file):
        config_upload = app.config['UPLOAD']
        res = {'code': 200, 'msg': '操作成功', 'data': {}}
        # 获取安全的文件名
        filename = secure_filename(file.filename)
        ext = filename.rsplit('.', 1)[1]
        if ext not in config_upload['ext']:
            res['code'] = -1
            res['msg'] = '不被允许的扩展类型'
            return res

        # 拼接文件目录
        root_path = app.root_path + config_upload['prefix_path']
        # 以当前时间创建目录,避免不兼容get_current_time
        file_dir = datetime.datetime.now().strftime('%Y%m%d')
        save_dir = root_path + file_dir
        if not os.path.exists(save_dir):
            os.mkdir(save_dir)
            # 赋予目录以及其他用户文件读写权限
            os.chmod(save_dir, stat.S_IRWXU | stat.S_IRGRP | stat.S_IRWXO)
        # 构造文件名及其扩展
        filename = str(uuid.uuid4()).replace('-', '') + '.' + ext
        # 直接保存文件
        file.save(f'{save_dir}/{filename}')

        # 将完整的文件路径加文件名存入数据库
        model_image = Image()
        model_image.file_key = file_dir + '/' + filename
        model_image.created_time = get_current_time()
        db.session.add(model_image)
        db.session.commit()

        res['data'] = {'file_key': model_image.file_key}
        return res
Beispiel #2
0
    def uploadByFile(file):
        config_upload = app.config['UPLOAD']
        resp = {'code': 200, 'msg': "操作成功", 'data': {}}
        filename = secure_filename(file.filename)
        ext = filename.rsplit(".", 1)[1]  # 取切割之后的第一个,[0]是文件名
        if ext not in config_upload['ext']:
            resp['code'] = -1
            resp['msg'] = '不受支持的格式'
            return resp
        #上传
        root_path = os.getcwd() + config_upload['prefix_path']
        file_dir = datetime.datetime.now().strftime("%Y%m%d")
        save_dir = root_path + file_dir
        if not os.path.exists(save_dir):
            os.mkdir(save_dir)
            os.chmod(save_dir, stat.S_IRWXU | stat.S_IRGRP | stat.S_IRWXO)
            #https://www.runoob.com/python/os-chmod.html 权限分配
        file_name = str(uuid.uuid4()).replace("-", "") + "." + ext
        file.save("{0}/{1}".format(save_dir, file_name))

        model_image = Image()
        model_image.file_key = file_dir + "/" + file_name
        model_image.member_id = g.member_info.id
        model_image.random_code = ''.join(
            random.sample(string.ascii_letters + string.digits, 8))
        model_image.created_time = getCurrentDate()
        db.session.add(model_image)
        db.session.commit()

        resp['data'] = {
            'file_key': model_image.file_key,
            'random_code': model_image.random_code
        }
        return resp
Beispiel #3
0
    def uploadByFile(file):
        config_upload = app.config['UPLOAD']
        resp = {'code': 200, 'msg': '操作成功', 'data': {}}
        filename = secure_filename(file.filename)
        app.logger.error(filename + "aaaaaaaaaaaaaaaaaaaaa")
        ext = filename.rsplit(".", 1)[1]
        if ext not in config_upload['ext']:
            resp['code'] = -1
            resp['msg'] = "不允许的扩展类型文件"
            return resp

        root_path = app.root_path + config_upload['prefix_path']
        #不使用getCurrentDate创建目录,为了保证其他写的可以用,这里改掉,服务器上好像对时间不兼容
        file_dir = datetime.datetime.now().strftime("%Y%m%d")
        save_dir = root_path + file_dir
        if not os.path.exists(save_dir):
            os.mkdir(save_dir)
            os.chmod(save_dir, stat.S_IRWXU | stat.S_IRGRP | stat.S_IRWXO)

        file_name = str(uuid.uuid4()).replace("-", "") + "." + ext
        file.save("{0}/{1}".format(save_dir, file_name))

        model_image = Image()
        model_image.file_key = file_dir + "/" + file_name
        model_image.created_time = getCurrentDate()
        db.session.add(model_image)
        db.session.commit()

        resp['data'] = {'file_key': model_image.file_key}
        return resp
    def uploadByFile(file):
        config_upload = app.config['UPLOAD']
        resp = {'code': 200, 'msg': '操作成功~~', 'data': {}}
        filename = file.filename
        ext = filename.rsplit(".", 1)[1]
        if ext not in config_upload['ext']:
            resp['code'] = -1
            resp['msg'] = "不允许的扩展类型文件"
            return resp

        root_path = app.root_path + config_upload['prefix_path']
        # 不使用getCurrentDate创建目录,为了保证其他写的可以用,这里改掉,服务器上好像对时间不兼容
        file_dir = "http://oy98650kl.bkt.clouddn.com"
        # save_dir = root_path + file_dir
        # if not os.path.exists(save_dir):
        #     os.mkdir(save_dir)
        #     os.chmod(save_dir, stat.S_IRWXU | stat.S_IRGRP | stat.S_IRWXO)

        # print(file.read())
        # file_name = str(uuid.uuid4()).replace("-", "") + "." + ext

        result = qiniu_store.save(file.read(), filename)
        # print(result)
        # print(info)

        # file.save("{0}/{1}".format(save_dir, file_name))

        model_image = Image()
        model_image.file_key = file_dir + "/" + filename
        model_image.created_time = getCurrentDate()
        db.session.add(model_image)
        db.session.commit()

        resp['data'] = {'file_key': model_image.file_key}
        return resp
Beispiel #5
0
    def uploadFile(file):
        config_upload = app.config['UPLOAD']
        resp = {'code': 200, 'msg': '操作成功', 'data': {}}
        filename =  file.filename
        ext = filename.rsplit('.', 1)[1]
        if ext not in config_upload['ext']:
            resp['code'] = -1
            resp['msg'] = "不允许的扩展类型文件"
            return resp

        root_path = app.root_path + config_upload['prefix_path']
        file_dir = getCurrentDate("%Y%m%d")
        save_dir = root_path + file_dir
        if not os.path.exists(save_dir):
            os.mkdir(save_dir)
            os.chmod(save_dir, stat.S_IRWXU | stat.S_IRGRP | stat.S_IRWXO)   # 赋予权限
        # uuid 根据硬件和程序生成已个唯一不重复的字符串
        file_name = str(uuid.uuid4()).replace("-", "") + "." + ext
        file.save("{0}/{1}".format(save_dir, file_name))

        model_image = Image()
        model_image.file_key = file_dir + "/" + file_name
        model_image.created_time = getCurrentDate()
        db.session.add(model_image)
        db.session.commit()

        resp['data'] = {
            'file_key': model_image.file_key
        }
        return resp
Beispiel #6
0
    def uploadByfile(file):
        config_upload = app.config['UPLOAD']
        resp = {'code': 200, 'msg': '操作成功', 'data': {}}
        filename = secure_filename(file.filename)
        ext = filename.rsplit(".", 1)[1]
        #判断后缀名是否在配置允许当中
        if ext not in config_upload['ext']:
            resp['code'] = -1
            resp['msg'] = '不允许的扩展类型文件!!'
            return resp
        #当前项目的目录+配置中的'prefix_path':'/web/static/upload/'
        root_path = app.root_path + config_upload['prefix_path']
        #生成时间格式的文件夹名
        file_dir = getCurrentDate("%Y%m%d")
        #图片的位置
        save_dir = root_path + file_dir
        #如果每天该地址则创建
        if not os.path.exists(save_dir):
            os.mkdir(save_dir)
            #给图片权限
            os.chmod(save_dir, stat.S_IRWXU | stat.S_IRGRP | stat.S_IRWXO)
        #拼接文件名
        file_name = str(uuid.uuid4()).replace("-", "") + "." + ext
        file.save("{0}/{1}".format(save_dir, file_name))
        #存入数据库用于在线管理
        model_image = Image()
        model_image.file_key = file_dir + "/" + file_name
        model_image.created_time = getCurrentDate()
        db.session.add(model_image)
        db.session.commit()

        resp['data'] = {'file_key': model_image.file_key}
        return resp
Beispiel #7
0
    def upload_by_file(file):
        config_upload = app.config['UPLOAD']
        resp = {'code': 200, 'msg': '操作成功', 'data': {}}
        filename = secure_filename(file.filename)
        ext = filename.rsplit('.', 1)[1]
        if ext not in config_upload['ext']:
            resp['code'] = -1
            resp['msg'] = '不允许的扩展类型文件!'

        root_path = app.root_path + config_upload['prefix_path']
        file_dir = getCurrentDate('%Y%m%d')
        save_dir = root_path + file_dir
        if not os.path.exists(save_dir):
            os.mkdir(save_dir)
            os.chmod(save_dir, stat.S_IRWXU | stat.S_IRGRP | stat.S_IRWXO)

        file_name = str(uuid.uuid4()).replace('-', '') + '.' + ext
        file.save('{0}/{1}'.format(save_dir, file_name))

        model_iamge = Image()
        model_iamge.file_key = file_dir + '/' + file_name
        model_iamge.created_time = getCurrentDate()
        db.session.add(model_iamge)
        db.session.commit()

        resp['data'] = {'file_key': file_dir + '/' + file_name}

        return resp
Beispiel #8
0
    def uploadByFile(file):
        config_ext = app.config['UPLOAD']
        resp = {'code':200, 'msg':'操作成功', 'data':{}}
        # 获取安全的文件名字
        filename = secure_filename(file.filename)
        ext = filename.rsplit(".", 1)[1]    # 扩展
        if ext not in config_ext['ext']:
            resp['code'] = -1
            resp['msg'] = '不允许扩展类型文件'
            return resp
        root_path = app.root_path + config_ext['prefix_path']
        file_dir = getCurrentDate("%Y%m%d")
        save_dir = root_path+file_dir
        if not os.path.exists(save_dir):
            os.mkdir(save_dir)
            os.chmod(save_dir, stat.S_IRWXG|stat.S_IRGRP| stat.S_IRWXO)
        file_name = str(uuid.uuid4()).replace("-", "")+ '.' + ext
        file.save("{0}/{1}".format(save_dir, file_name))

        # 存储上传的文件  保存到数据库中 为了实现ueditor listimage在线管理

        model_image = Image()
        model_image.file_key = file_dir + "/" + file_name
        model_image.created_time = getCurrentDate()
        db.session.add(model_image)
        db.session.commit()

        resp['data'] = {
            'file_key': model_image.file_key
        }
        return resp
    def uploadByFile(file):
        config_upload = app.config["UPLOAD"]
        resp = {"code": 200, "msg": "操作成功~", "data": {}}
        filename = secure_filename(file.filename)
        ext = filename.rsplit(".", 1)[1]
        if ext not in config_upload["ext"]:
            resp["code"] = -1
            resp["msg"] = "不允许的扩展类型文件~~"
            return resp

        root_path = app.root_path + config_upload["prefix_path"]
        file_dir = getCurrentDate("%Y%m%d")
        save_dir = root_path + file_dir
        if not os.path.exists(save_dir):
            os.mkdir(save_dir)
            os.chmod(save_dir, stat.S_IRWXU | stat.S_IRGRP | stat.S_IRWXO)

        file_name = str(uuid.uuid4()).replace("-", "") + "." + ext
        file.save("{0}/{1}".format(save_dir, file_name))

        model_image = Image()
        model_image.file_key = file_dir + "/" + file_name
        model_image.created_time = getCurrentDate()
        db.session.add(model_image)
        db.session.commit()

        resp["data"] = {"file_key": model_image.file_key}
        return resp
Beispiel #10
0
    def uploadByFile(file): # 定义上传文件函数。参数是文件类
        config_upload = app.config['UPLOAD']
        resp = {'code': 200, 'msg': '操作成功', 'data': {}}
        filename = secure_filename(file.filename) # 获得上传的文件名
        ext = filename.rsplit(".", 1)[1]  # 获得类型(扩展名)。即文件名以.切割,拿到后面部分。
        if ext not in config_upload['ext']:
            resp['code'] = -1
            resp['msg'] = "不允许的扩展类型文件"
            return resp

        root_path = app.root_path + config_upload['prefix_path']  # 图片存放路径。app.root_path获取全局路径
        file_dir = getCurrentData("%Y%m%d")  # 按照日期生成文件夹
        save_dir = root_path + file_dir  # 最终的保存地址
        if not os.path.exists( save_dir ):
            os.mkdir(save_dir)
            os.chmod(save_dir, stat.S_IRWXU | stat.S_IRGRP | stat.S_IRWXO )  # 给这个文件赋予权限。拥有者最高权限|。权限参考:http://www.runoob.com/python/os-chmod.html

        file_name = str(uuid.uuid4()).replace("-", "") + "." + ext  # 重命名文件名
        file.save("{0}/{1}".format(save_dir, file_name))  # 保存文件。在save_dir路径下

        # 存储图片路径到数据库
        model_image = Image()
        model_image.file_key = file_dir + "/" + file_name
        model_image.created_time = getCurrentData()
        db.session.add(model_image)
        db.session.commit()

        resp['data'] = {
            'file_key': file_dir + "/" + file_name
        }
        return resp
Beispiel #11
0
    def uploadByFile(file):
        # 也需要定义resp用于返回信息
        config_upload = app.config['UPLOAD']
        resp = {'code':200,'msg':'操作成功',"data":{}}

        # 通过安全的方式获取文件名,但这里这个方法行不通
        # filename = secure_filename(file.filename)
        filename = file.filename
        app.logger.info(filename)

        # 以第一个点进行切割,取后面的值
        ext = filename.rsplit('.',1)[1]
        if ext not in config_upload['ext']:
            resp['code']= -1
            resp['msg'] = '不允许的扩展类型文件'
            return resp

        # 获取保存图片的路径
        root_path = app.root_path + config_upload['prefix_path']

        # 文件夹的名称
        file_dir = getCurrentDate('%Y%m%d')

        # 最终的保存文件夹的路径
        save_dir = root_path + file_dir

        if not os.path.exists(save_dir):
            # 创建文件夹
            os.mkdir(save_dir)

            # 让拥有者有777的权限和组用户有读权限,其他用户全部权限
            os.chmod(save_dir,stat.S_IRWXU | stat.S_IRGRP | stat.S_IRWXO)

        # 利用uuid这个模块生成不重复的字符串当成保存的图片名称
        filename = str(uuid.uuid4()).replace('-','') + '.' + ext

        # 保存
        file.save('{0}/{1}'.format(save_dir,filename))

        # 保存图片的file_key到数据库
        model_image = Image()
        model_image.file_key = file_dir + '/' + filename
        model_image.created_time = getCurrentDate()
        db.session.add(model_image)
        db.session.commit()

        resp['data'] = {
            'file_key':file_dir + '/' + filename
        }
        return resp
    def uploadByFile(file):
        config_upload = app.config['UPLOAD']
        resp = {'code': 200, 'msg': '操作成功~~', 'data': {}}
        # 获取文件名 【secure_filename() 获取一个安全的文件名】
        filename = secure_filename(file.filename)
        # 获取文件的后缀
        ext = filename.rsplit(".", 1)[1]
        # 判断 这个 后缀 是否在 规定的后缀里面
        if ext not in config_upload['ext']:
            resp['code'] = -1
            resp['msg'] = "不允许的扩展类型文件"
            return resp

        # 存放图片文件夹的 全局路径
        root_path = app.root_path + config_upload['prefix_path']
        # 'prefix_path': '/web/static/upload/',  # 上传目录

        # 不使用getCurrentDate创建目录,为了保证其他写的可以用,这里改掉,服务器上好像对时间不兼容
        file_dir = datetime.datetime.now().strftime("%Y%m%d")
        # 最终存放上传 图片的路径
        save_dir = root_path + file_dir
        # 判断文件路径是否存在,不存在就创建
        if not os.path.exists(save_dir):
            os.mkdir(save_dir)
            # S_IRWXU 700权限, S_IRGRP 040权限, S_IRWXO 007权限
            os.chmod(save_dir, stat.S_IRWXU | stat.S_IRGRP | stat.S_IRWXO)
        # 生成文件名 uuid 来做,再加上后缀
        file_uuid_name = str(uuid.uuid4()).replace("-", "")
        file_name = file_uuid_name + "." + ext
        # 存储文件 [路径+文件名]就可以了
        file.save("{0}/{1}".format(save_dir, file_name))

        model_image = Image()
        model_image.file_key = file_dir + "/" + file_name
        model_image.created_time = getCurrentDate()
        db.session.add(model_image)
        db.session.commit()

        resp['data'] = {
            'file_key': model_image.file_key,
            'file_uuid_name': file_uuid_name
        }
        return resp
Beispiel #13
0
    def uploadByFile( file ):
        config_upload = app.config['UPLOAD']
        resp = { 'code': 200, 'msg': '操作成功~', 'data': {} }
        # 获取文件相关属性  一致?
        filename = secure_filename( file.filename )         # 获取安全的文件名称
        app.logger.info( "filename is :%s" % filename)
        ext = filename
        # ext = filename.split(".", 1)[1]
        
        # 扩展名是否 一致
        if ext not in config_upload['ext']:
            # 报错
            resp['code'] = -1
            resp['msg'] = "不允许的扩展类型文件~"
            return resp

        # 上传过程
        # 保证文件目录存在
        root_path = app.root_path + config_upload['prefix_path']
        # 文件夹名称
        file_dir = getCurrentDate( "%Y%m%d" )
        save_dir = root_path + file_dir
        # 判断路径是否存在
        if not os.path.exists( save_dir ):
            os.mkdir( save_dir )
            os.chmod( save_dir, stat.S_IRWXU | stat.S_IRGRP | stat.S_IRWXO)         # 文件夹有 777权限
        
        # 文件名称—— uuid生成全局不重复的唯一的 文件名称
        file_name = str( uuid.uuid4() ).replace( "-", "" ) + "." + ext
        file.save( "{0}/{1}".format( save_dir, file_name ) )                        # 存储文件—— 路径+文件名称
        
        # 存储image
        model_Image = Image()
        model_Image.file_key = file_dir + "/" + file_name
        model_Image.created_time = getCurrentDate()
        db.session.add( model_Image )
        db.session.commit()

        # resp['data'] = {
        #     'file_key': file_dir + "/" + file_name
        # }
        resp['data']['file_key'] = model_Image.file_key
        return resp
    def uploadByFile(file):
        app_config = current_app.config
        config_upload = app_config["UPLOAD"]
        resp = {"code": 200, "msg": "操作成功", "data": ""}
        filename = secure_filename(file.filename)
        ext = os.path.splitext(filename)[1].lstrip(".")
        if ext not in config_upload['ext']:
            resp['code'] = -1
            resp['msg'] = "不允许的扩展类型文件"
        else:
            if win:
                root_path = current_app.root_path + config_upload[
                    'win_prefix_path']
            else:
                root_path = current_app.root_path + config_upload['prefix_path']
            file_dir = datetime.datetime.now().strftime('%Y%m%d')
            save_dir = os.path.join(root_path, file_dir)
            if not os.path.exists(save_dir):
                os.mkdir(save_dir)
                if not win:
                    # linux 下修改权限: RWX 读写执行权限,  USR GRP OTH
                    os.chmod(save_dir,
                             stat.S_IRWXU | stat.S_IRGRP | stat.S_IRWXO)
            #  图片重命名
            filename = str(uuid.uuid4()).replace("-", "") + "." + ext
            final_path = os.path.join(save_dir, filename)
            file.save(final_path)

            # 信息出入数据库
            model_image = Image()
            # 图片名 + 文件夹的名称
            dir_image = file_dir + '/' + filename  # todo 这里是拼URL,而不是文件路径
            model_image.file_key = dir_image
            model_image.created_time = getCurrentDate()  # 该时间应该设置为数据库自动添加
            db.session.add(model_image)
            db.session.commit()
            resp['data'] = {'file_key': model_image.file_key}
        return resp
Beispiel #15
0
    def uploadByFile(file):
        config_upload = app.config['UPLOAD']
        resp = {'code': 200, 'msg': '操作成功', 'data': {}}
        # 进行扩展名的判断
        filename = secure_filename(file.filename)

        ext = filename.rsplit(".", 1)[1]
        print(ext)
        if ext not in config_upload['ext']:
            resp['code'] = -1
            resp['msg'] = '不允许的扩展类型文件'
            return resp
        # 获取保存文件的全局路径
        root_path = app.root_path + config_upload['prefix_path']
        # 生成文件夹名称,以年月日来命名
        file_dir = getCurrentDate(("%Y%m%d"))
        # 获取保存图片的路径
        save_dir = root_path + file_dir
        if not os.path.exists(save_dir):
            os.mkdir(save_dir)
            # 拥有者有全部权限,组用户有读权限,其他用户有全部权限
            os.chmod(save_dir, stat.S_IRWXU | stat.S_IRGRP | stat.S_IRWXO)

        file_name = str(uuid.uuid4()).replace("-", "") + "." + ext
        print("#################################")
        print(dir(file))
        print(type(file))
        file.save("{0}/{1}".format(save_dir, file_name))

        model_image = Image()
        model_image.file_key = file_dir + "/" + file_name
        model_image.created_time = getCurrentDate()
        db.session.add(model_image)
        db.session.commit()
        # 'file_key':file_dir + "/" + file_name
        resp['data'] = {'file_key': model_image.file_key}
        return resp
Beispiel #16
0
    def uploadByFile(file):
        config_upload = app.config['UPLOAD']
        resp = {'code': 200, 'msg': '上传成功', 'data': {}}
        filename = secure_filename(file.filename)
        ext = filename.rsplit('.', 1)[1]  # 切割获取扩展名

        if ext not in config_upload['ext']:  # 判断上传的扩展名是否在配置文件内
            resp['code'] = -1
            resp['msg'] = '不允许的扩展类型文件'
            return resp

        root_path = app.root_path + config_upload['prefix_path']
        # 不使用getCurrentDate创建目录,为了保证其他写的可以用,这里改掉,服务器上好像对时间不兼容
        # file_dir = datetime.datetime.now().strftime("%Y%m%d")
        file_dir = getCurrentDate('%Y%m%d')
        save_dir = root_path + file_dir

        if not os.path.exists(save_dir):  # 判断上传日期文件是否存在,不存在创建
            os.mkdir(save_dir)
            os.chmod(save_dir, stat.S_IRWXU | stat.S_IRGRP | stat.S_IRWXO)  # 文件需要777权限

        file_name = str(uuid.uuid4()).replace('-', '') + '.' + ext  # 拼接文件名
        file.save(f'{save_dir}/{file_name}')

        model_image = Image()
        model_image.file_key = file_dir + '/' + file_name
        model_image.created_time = getCurrentDate()
        db.session.add(model_image)
        db.session.commit()

        # 返回文件名
        resp['data'] = {
            'file_key': model_image.file_key
        }

        return resp
Beispiel #17
0
    def uploadByFile(file):

        resp = {'code': 200, 'msg': '上传成功', 'data': {}}

        filename = secure_filename(file.filename)  #获得安全的文件名
        ext = filename.rsplit('.', 1)[1]  #截取文件名的拓展名,格式
        config_upload = app.config['UPLOAD']  #获取app 中格式要求
        if ext not in config_upload['ext']:
            resp['code'] = -1
            resp['msg'] = '不允许扩展类型文件'
            return resp

        root_path = app.root_path + config_upload[
            'prefix_path']  #文件储存的路径存在了static中的upload文件夹里
        file_dir = datetime.datetime.now().strftime("%Y%m%d")  #设置存储文件名称,用时间命名
        save_dir = root_path + file_dir  #完整的文件路径
        if not os.path.exists(save_dir):  #判断是否有该路径
            os.mkdir(save_dir)  #如果没有就生成一个
            os.chmod(save_dir,
                     stat.S_IRWXU | stat.S_IRGRP | stat.S_IRWXO)  #赋予权限

        file_name = str(uuid.uuid4()).replace(
            "-", "") + "." + ext  #file名,uuid用来生成文件名,用时间和硬件名
        file.save("{0}/{1}".format(save_dir, file_name))  #存储文件

        # ======= 上传的图片存入数据库 =========

        model_image = Image()
        model_image.file_key = file_dir + '/' + file_name
        model_image.created_time = getCurrentDate()
        db.session.add(model_image)
        db.session.commit()

        resp['data'] = {'file_key': file_dir + '/' + file_name}

        return resp