Exemple #1
0
def save_file(self, upload_file):
    """02保存单个文件"""
    files_ext = upload_file['filename'].split('.')[-1]
    files_type = ['jpg', 'bmp', 'png', 'mp4', 'ogg', 'mp3', 'txt']
    if files_ext not in files_type:
        return {'status': False, 'msg': '文件格式不正确', 'data': ''}
    uuidname = str(uuid4()) + '.%s' % files_ext
    #'sefaegaer.txt'   'sejfiajelgaeirf.jpg'
    file_content = upload_file['body']
    old_file = Files.file_is_existed(file_content)
    if old_file is not None:
        file_path = 'http://106.14.163.146:8000/images/' + old_file.uuid
        return {'status': True, 'msg': '文件保存成功(其实文件在硬盘上)', 'data': file_path}

    url = 'files/' + uuidname
    with open(url, 'wb') as f:
        f.write(file_content)

    file_name = upload_file['filename']
    files = Files()
    files.filename = file_name
    files.uuid = uuidname
    files.content_length = len(file_content)
    files.content_type = upload_file['content_type']
    files.update_time = datetime.now()
    files.file_hash = upload_file['body']
    files.files_users.append(self.current_user)
    self.db.add(files)
    self.db.commit()
    file_path = 'http://106.14.163.146:8000/images/' + files.uuid
    return {'status': True, 'msg': '文件保存成功', 'data': file_path}
Exemple #2
0
def save_file(self, upload_file):
    ''' 文件的存储 '''
    files_ext = upload_file['filename'].split('.')[-1]
    if files_ext not in ['jpg', 'bmp', 'png', 'ogg', 'mp3', 'mp4']:
        return {'status': False, 'msg': ' 不支持该格式'}

    uuidname = str(uuid4()) + '.{}'.format(files_ext)
    file_content = upload_file['body']

    old_file = Files.file_is_existed(file_content)
    if old_file is not None:
        file_path = 'http://127.0.0.1:8000/images/' + old_file.uuid
        return {'status': True, 'msg': '文件保存成功(其实以前有人上传过了)', 'data': file_path}

    path = 'files/' + uuidname
    with open(path, 'wb') as f:
        f.write(file_content)

    file = Files()
    file.filename = upload_file['filename']
    file.content_length = len(file_content)
    file.uuid = uuidname
    file.content_type = upload_file['content_type']
    file.update_time = datetime.now()
    file.file_hash = file_content

    file.files_users.append(self.current_user)

    self.db.add(file)
    self.db.commit()
    file_path = 'http://127.0.0.1:8000/images/' + file.uuid
    return {'status': True, 'msg': '文件保存成功', 'data': file_path}
Exemple #3
0
def save_qiniu_file(self, upload_file):
    """02保存单个文件到七牛"""
    files_ext = upload_file['filename'].split('.')[-1]
    files_type = ['jpg', 'bmp', 'png', 'mp4', 'ogg', 'mp3', 'txt']
    if files_ext not in files_type:
        return {'status': False, 'msg': '文件格式不正确', 'data': ''}

    file_content = upload_file['body']
    old_file = Files.file_is_existed(file_content)
    if old_file is not None:
        file_path = 'http://oq54v29ct.bkt.clouddn.com/' + old_file.uuid
        return {'status': True, 'msg': '文件保存成功(其实文件在硬盘上)', 'data': file_path}

    #上传到七牛
    ret, info = upload_qiniu_file_content(file_content)
    print ret
    print info
    if info.status_code != 200:
        return {'status': False, 'msg': '文件上传到七牛失败', 'data': ''}

    file_name = upload_file['filename']
    files = Files()
    files.filename = file_name
    files.uuid = ret  #保存的七牛返回的文件名
    files.content_length = len(file_content)
    files.content_type = upload_file['content_type']
    files.update_time = datetime.now()
    files.file_hash = upload_file['body']
    files.files_users.append(self.current_user)
    self.db.add(files)
    self.db.commit()
    file_path = 'http://oq54v29ct.bkt.clouddn.com/' + files.uuid
    return {'status': True, 'msg': '文件上传到七牛成功', 'data': file_path}
Exemple #4
0
def save_file(self, upload_file):
    # [{'body': 'abcdefg', 'content_type': u'text/plain', 'filename': u'111.txt'}]
    files_ext = upload_file['filename'].split('.')[-1]
    if files_ext not in ['jpg', 'bmp', 'png', 'ogg', 'mp3', 'mp4']:
        return {'status': False, 'msg': '文件格式不正确', 'data': ''}

    uuidname = str(
        uuid4()) + '.%s' % files_ext  #siejflajgierjeioagi3fj83erj3.jpg

    file_content = upload_file['body']

    old_file = Files.file_is_existed(file_content)
    if old_file is not None:
        file_path = 'http://192.168.10.128:9000/images/' + old_file.uuid  #url
        return {'status': True, 'msg': '文件保存成功(其实以前有人上传过了)', 'data': file_path}

    path = 'files/' + uuidname  #硬盘路径
    with open(path, 'wb') as f:
        f.write(file_content)

    files = Files()
    files.filename = upload_file['filename']
    files.uuid = uuidname
    files.content_length = len(file_content)
    files.content_type = upload_file['content_type']
    files.update_time = datetime.now()
    files.file_hash = file_content

    files.files_users.append(self.current_user)
    self.db.add(files)
    self.db.commit()
    file_path = 'http://192.168.10.128:9000/images/' + uuidname
    return {'status': True, 'msg': '文件保存成功', 'data': file_path}
Exemple #5
0
def save_file(self, upload_file):
    """保存文件"""
    # [{'body': '123abc', 'content_type': u'text/plain', 'filename': u'tornado\u4e0a\u4f20\u6587\u4ef6\u6d4b\u8bd5.txt'}]
    # 获取文件扩展名(.jpg)
    # 使用split方法以'.'为切割对象,得到一个列表,然后取列表的最后一个元素既是文件扩展名
    files_ext = upload_file['filename'].split('.')[-1]
    # 判断上传文件的格式,不是的返回格式不正确,是,执行上传数据库
    if files_ext not in ['jpg', 'JPG', 'bmp', 'png', 'ogg', 'mp3', 'mp4']:
        return {'status': False, 'msg': '文件格式不正确', 'data': ''}
    # 定义一个文件名字
    # uuidname = cf43df90-0602-4416-a2b0-9fd814c0e2f2.jpg
    # uuid4() 随机生成16个一组的随机数,每个随机数使用2位十六进制表示,
    """
	        if version is not None:
            if not 1 <= version <= 5:
                raise ValueError('illegal version number')
            # Set the variant to RFC 4122.
            # int = int & ~(0xc000 << 48L)
            int &= ~(0xc000 << 48L)
            int |= 0x8000 << 48L
            # Set the version number.
            int &= ~(0xf000 << 64L)
            int |= version << 76L
        self.__dict__['int'] = int
	"""
    uuidname = str(uuid4()) + '.%s' % files_ext

    file_content = upload_file['body']
    # 判断文件是否存在
    old_file = Files.file_is_existed(file_content)
    if old_file is not None:
        file_path = 'http://192.168.10.128:9000/images/' + old_file.uuid  # url
        return {'status': True, 'msg': '文件保存成功(以前有人上传过啦)', 'data': file_path}

    # 如果文件不存在:保存文件(路径)
    # 保存文件
    path = 'files/' + uuidname  # 文件保存的硬盘路径
    # 打开文件,wb是二进制保存文件
    with open(path, 'wb') as f:
        f.write(file_content)

    files = Files()  # 实例化一个对象
    files.filename = upload_file['filename']  # 取出文件名
    files.uuid = uuidname
    files.content_length = len(file_content)  # 计算文件长度
    files.content_type = upload_file['content_type']  # 文件类型
    files.update_time = datetime.now()  # 上传时间
    files.file_hash = file_content  # 上传的文件hash值,用于文件是否存在的判断

    files.files_users.append(self.current_user)  # 上传文件用户
    self.db.add(files)  # 添加到数据库
    self.db.commit()
    # http://127.0.0.1:8000/images/cf43df90-0602-4416-a2b0-9fd814c0e2f2.jpg
    # http://127.0.0.1:8000/images/83a3b62b-384a-4fce-8c7e-47ac1910b331.jpg
    file_path = 'http://127.0.0.1:8000/images/' + uuidname
    return {'status': True, 'msg': '文件保存成功', 'data': file_path}
Exemple #6
0
def save_qiniu_file(self, upload_file):
    """保存单个文件"""
    files_ext = upload_file['filename'].split('.')[-1]
    files_type = ['jpg', 'bmp', 'png', 'mp4', 'ogg', 'mp3', 'txt']
    if files_ext not in files_type:
        return {'status': False, 'msg': '文件格式不正确', 'data': ''}

    files_content = upload_file['body']
    old_file = Files.file_is_existed(files_content)
    if old_file is not None:
        file_path = 'http://192.168.169.138:8000/images?uuid=' + old_file.uuid  # 这里要改成七牛云的链接
        file_id = Files.by_uuid(old_file.uuid).id  # 老文件的uuid
        # 连表查询  查询删除的表中是否有老文件的id
        isExist = self.db.query(
            Files.id, DelFilesToUser.files_id).join(DelFilesToUser).filter_by(
                files_id=file_id).first()
        # 如果存在则从回收站中恢复
        if isExist != None:
            files = Files.by_uuid(old_file.uuid)
            files.files_users_del.remove(self.current_user)
            files.files_users.append(self.current_user)
            self.db.add(files)
            self.db.commit()
        return {'status': True, 'msg': '文件上传成功(硬盘中存在文件)', 'data': file_path}

    ret, info = upload_qiniu_file_content(files_content)
    print ret
    print info
    if info.status_code != 200:
        return {'status': False, 'msg': '文件上传到七牛失败', 'data': ''}

    file_name = upload_file['filename']
    files = Files()
    files.uuid = ret  # 保存的七牛返回的文件名
    files.filename = file_name
    files.content_length = len(files_content)
    files.content_type = upload_file['content_type']
    files.updata_time = datetime.now()
    files.file_hash = upload_file['body']
    files.files_users.append(self.current_user)
    files.user_id = self.current_user.id
    self.db.add(files)
    self.db.commit()
    file_path = 'http://192.168.169.138:8000/images/' + files.uuid  # 这里要改成七牛云的链接
    return {'status': True, 'msg': '文件上传到七牛成功', 'data': file_path}
Exemple #7
0
def save_file(self, upload_file):
    """保存单个文件"""
    files_ext = upload_file['filename'].split('.')[-1]
    files_type = ['jpg', 'bmp', 'png', 'mp4', 'ogg', 'mp3', 'txt']
    if files_ext not in files_type:
        return {'status': False, 'msg': '文件格式不正确', 'data': ''}
    uuidname = str(uuid4()) + '.%s' % files_ext
    file_content = upload_file['body']
    old_file = Files.file_is_existed(file_content)
    if old_file is not None:
        file_path = 'http://192.168.169.138:8000/images?uuid=' + old_file.uuid
        file_id = Files.by_uuid(old_file.uuid).id  # 老文件的uuid
        # 连表查询  查询删除的表中是否有老文件的id
        isExist = self.db.query(
            Files.id, DelFilesToUser.files_id).join(DelFilesToUser).filter_by(
                files_id=file_id).first()
        # 如果存在则从回收站中恢复
        if isExist != None:
            files = Files.by_uuid(old_file.uuid)
            files.files_users_del.remove(self.current_user)
            files.files_users.append(self.current_user)
            self.db.add(files)
            self.db.commit()
        return {'status': True, 'msg': '文件上传成功(硬盘中存在文件)', 'data': file_path}

    url = 'files/' + uuidname
    with open(url, 'wb') as f:
        f.write(file_content)
    file_name = upload_file['filename']
    files = Files()
    files.uuid = uuidname
    files.filename = file_name
    files.content_length = len(file_content)
    files.content_type = upload_file['content_type']
    files.updata_time = datetime.now()
    files.file_hash = upload_file['body']
    files.files_users.append(self.current_user)
    files.user_id = self.current_user.id
    self.db.add(files)
    self.db.commit()
    file_path = 'http://192.168.169.138:8000/images/' + files.uuid
    return {'status': True, 'msg': '文件上传成功', 'data': file_path}