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_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 #3
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 #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 del_files_lib(self, uuid):
    """001删除到回收站"""
    files = Files.by_uuid(uuid)
    files.files_users.remove(self.current_user)
    files.files_users_del.append(self.current_user)
    self.db.add(files)
    self.db.commit()
Exemple #7
0
def files_recovery_lib(self, uuid):
    """文件恢复"""
    files = Files.by_uuid(uuid)
    files.files_users.append(self.current_user)
    files.files_users_del.remove(self.current_user)
    self.db.add(files)
    self.db.commit()
Exemple #8
0
def files_delete_final_lib(self, uuid):
    """彻底删除文件"""
    files = Files.by_uuid(uuid)
    files.files_users_del.remove(self.current_user)
    # self.current_user.users_files_del.remove(files)  # 等效于上面的删除句子
    self.db.add(files)
    self.db.commit()
Exemple #9
0
def save_sharing_files_lib(self, uu):
    """004保存分享的文件"""
    if uu == '':
        return {'status': False, 'msg': 'uuid不存在', 'data': ''}
    # 获取当前用户的提取密码
    password = self.session.get('sharing_links_password')
    if not password:
        # 如果没有密码重新获取链接
        return {'status': False, 'msg': '没有权限', 'data': ''}
    # 查询redis
    redis_json = self.conn.get('sharing_links:%s' % uu)
    # 如果没有返回已经过期,并删除session
    if redis_json is None:
        del self.session['sharing_links_password']
        return {'status': False, 'msg': '分享已经过期', 'data': ''}
    # 如果有反序列化
    redis_dict = json.loads(redis_json)
    # 对比用户session中保存的密码和redis保存的密码
    if password != redis_dict['password']:
        # 如果不相等提示错误
        return {'status': False, 'msg': '您没有获得这个文件的链接', 'data': ''}
    # 把文件保存到当前用户

    files = Files.by_uuid(redis_dict['file_uuid'])
    files.files_users.append(self.current_user)
    self.db.add(files)
    self.db.commit()
    return {'status': True, 'msg': '保存成功', 'data': ''}
Exemple #10
0
def del_file(self, uuid):
    files = Files.by_uuid(uuid)
    files.files_users.remove(self.current_user)
    files.files_users_del.append(self.current_user)
    self.db.add(files)
    self.db.commit()
    return {'status': True, 'msg': '成功删除'}
Exemple #11
0
def files_delete_lib(self, uuid):
    """删除文件"""
    files = Files.by_uuid(uuid)
    # 删除文件关联表关系
    files.files_users.remove(self.current_user)
    # 将当前文件放入删除文件表中
    files.files_users_del.append(self.current_user)
    self.db.add(files)
    self.db.commit()
Exemple #12
0
def del_final_files_lib(self, uuid):
    """彻底删除文件"""
    try:
        files = Files.by_uuid(uuid)
        files.files_users_del.remove(self.current_user)
        self.db.add(files)
        self.db.commit()
    except Exception as e:
        print e
Exemple #13
0
def files_download_qiniu_lib(self, uuid):
    if uuid == '':
        return {'status': False, 'msg': '没有文件ID'}
    old_file = Files.by_uuid(uuid)
    if old_file is None:
        return {'status': False, 'msg': '文件不存在'}
    qiniu_url = 'http://192.168.169.138:8000/images?uuid=%s' % uuid  # 这里要改成七牛云的链接
    url = down_qiniu_file(qiniu_url)
    print url
    return {'status': True, 'data': url}
Exemple #14
0
def recovery_files_lib(self, uuid):
    """恢复文件"""
    try:
        files = Files.by_uuid(uuid)
        files.files_users_del.remove(self.current_user)
        files.files_users.append(self.current_user)
        self.db.add(files)
        self.db.commit()
    except Exception as e:
        print e
Exemple #15
0
def files_download_qiniu_lib(self, uuid):
    if uuid == '':
        return {'status': False, 'msg': '没有文件ID'}
    old_file = Files.by_uuid(uuid)
    if old_file is None:
        return {'status': False, 'msg': '文件不存在'}
    qiniu_url = 'http://oq54v29ct.bkt.clouddn.com/%s' % uuid
    url = down_qiniu_file(qiniu_url)
    print url
    return {'status': True, 'data': url}
Exemple #16
0
def del_files_lib(self, uuid):
    """删除文章到回收站"""
    try:
        files = Files.by_uuid(uuid)
        files.files_users.remove(self.current_user)
        files.files_users_del.append(self.current_user)
        self.db.add(files)
        self.db.commit()
    except Exception as e:
        print e
Exemple #17
0
def files_download_lib(self, uuid):
    filepath = 'files/%s' % uuid
    filename = Files.by_uuid(uuid).filename
    self.set_header('Content-Type', 'application/octet-stream')
    self.set_header('Content-Disposition',
                    'attachment; filename=%s' % filename)
    with open(filepath, 'rb') as f:
        while 1:
            data = f.read(1024 * 5)
            if not data:
                break
            self.write(data)
            self.flush()
            time.sleep(1)
    self.finish()
Exemple #18
0
def files_sharing_list_lib(self, uu):
    """003查看分享的文件"""
    if uu == '':
        return {'status': False, 'msg': 'uuid不存在', 'data': ''}
    # 获取当前用户的提取密码
    password = self.session.get('sharing_links_password')
    if not password:
        # 如果没有密码重新获取链接
        return {'status': False, 'msg': '请重新获取链接', 'data': ''}
    # 查询redis
    redis_json = self.conn.get('sharing_links:%s' % uu)
    # 如果没有返回已经过期,并删除session
    if redis_json is None:
        del self.session['sharing_links_password']
        return {'status': False, 'msg': '分享已经过期', 'data': ''}
    # 如果有反序列化
    redis_dict = json.loads(redis_json)
    # 对比用户session中保存的密码和redis保存的密码
    if password != redis_dict['password']:
        # 如果不相等提示错误
        return {'status': False, 'msg': '您没有获得这个文件的链接', 'data': ''}
    # 返回文件
    files = Files.by_uuid(redis_dict['file_uuid'])
    return {'status': True, 'msg': '分享成功', 'data': [files], 'uuid': uu}
Exemple #19
0
def files_message_lib(self, file_uuid):
    ''' 显示文件返回文件 '''
    files = Files.by_uuid(file_uuid)
    return files
Exemple #20
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}
Exemple #21
0
def get_files_lib(self, page):
    ''' 获取所有文件 '''
    files = Files.all()
    return files
Exemple #22
0
def files_message_lib(self, uuid):
    """查询文件uuid"""
    return Files.by_uuid(uuid)
Exemple #23
0
def files_delete_lib(self, uuid):
    files = Files.by_uuid(uuid)
    files.files_users.remove(self.current_user)
    files.files_users_del.append(self.current_user)
    self.db.add(files)
    self.db.commit()
Exemple #24
0
def files_delete_final_lib(self, uuid):
    files = Files.by_uuid(uuid)
    files.files_users_del.remove(self.current_user)
    #self.current_user.users_files_del.remove(files)
    self.db.add(files)
    self.db.commit()
Exemple #25
0
def files_list_lib(self):
    return Files.all()
Exemple #26
0
def files_list_lib(self):
    """文件列表"""
    files = Files.all()
    return files
Exemple #27
0
def files_message_lib(self, uuid):
    return Files.by_uuid(uuid)
Exemple #28
0
def files_message_lib(self, uuid):
    """获取文件详情"""
    files = Files.by_uuid(uuid)
    return files
Exemple #29
0
def files_message_lib(self, uuid):
    """03文件详情页"""
    return Files.by_uuid(uuid)
Exemple #30
0
def del_final_files_lib(self, uuid):
    """002彻底删除"""
    files = Files.by_uuid(uuid)
    files.files_users_del.remove(self.current_user)
    self.db.add(files)
    self.db.commit()