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()
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()
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': '成功删除'}
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': ''}
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()
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}
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()
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
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}
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
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
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}
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}
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()
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}
def files_message_lib(self, file_uuid): ''' 显示文件返回文件 ''' files = Files.by_uuid(file_uuid) return files
def files_message_lib(self, uuid): """查询文件uuid""" return Files.by_uuid(uuid)
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()
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()
def files_message_lib(self, uuid): """获取文件详情""" files = Files.by_uuid(uuid) return files
def files_message_lib(self, uuid): """03文件详情页""" return Files.by_uuid(uuid)
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()
def files_message_lib(self, uuid): return Files.by_uuid(uuid)