def copy_file(): '''复制文件''' validator = MoveOrCopyFileValidator().nt_data FileDao.copy_file( dest_parent_id=validator.parent_id, src_file_id=validator.file_id ) return Success(error_code=1)
def move_files(): '''批量移动文件''' validator = MoveOrCopyFileValidator().nt_data FileDao.move_files( dest_parent_id=validator.parent_id, file_ids=validator.ids ) return Success(error_code=1)
def upload(self): ret = [] self.mkdir_if_not_exists() for single in self._file_storage: file_md5 = self._generate_md5(single.read()) single.seek(0) file = File.query.filter_by(md5=file_md5).first() # 如果,文件已存在 # 如果,已存在的文件的父文件夹和目标文件夹不一致,则复制 # 否者,沿用file文件(不用else) if file and file.parent_id != self.parent_id: if not File.query.filter_by(parent_id=self.parent_id, md5=file_md5).first(): file = FileDao.copy_file(dest_parent_id=self.parent_id, src_file_id=file.id) # 如果,文件不存在 if not file: absolute_path, relative_path, uuid_filename = self._get_store_path( single.filename) secure_filename(single.filename) single.save(absolute_path) File.create(parent_id=self.parent_id, name=single.filename, uuid_name=uuid_filename, path=relative_path, extension=self._get_ext(single.filename), size=self._get_size(single), md5=file_md5) file = File.get(parent_id=self.parent_id, md5=file_md5) ret.append(file) return ret
def upload(self): ret = [] for single in self._file_storage: file_md5 = self._generate_md5(single.read()) single.seek(0) file = File.query.filter_by(md5=file_md5).first() parent_folder = File.get_or_404(id=self.parent_id) if file and self.parent_id != file.parent_id: if not File.query.filter_by(parent_id=self.parent_id, md5=file_md5).first(): file = FileDao.copy_file(dest_parent_id=self.parent_id, src_file_id=file.id, user_id=parent_folder.user_id) file = File.get(md5=file_md5) if not file: absolute_path, relative_path, uuid_filename = self._get_store_path( single.filename) # 上传到七牛云,获取存储地址 path = self.save(single.filename, single.read()) if path: secure_filename(single.filename) File.create(parent_id=self.parent_id, name=single.filename, uuid_name=uuid_filename, path=path, extension=self._get_ext(single.filename), size=self._get_size(single), md5=file_md5, _from=UrlFromEnum.NETWORK.value) file = File.get(parent_id=self.parent_id, md5=file_md5) ret.append(file) return ret
def create_folder(): '''新建文件夹''' validator = CreateFileValidator().dt_data FileDao.create_folder(**validator) return Success()
def get_folder_tree(): '''获取目录树''' return Success(FileDao.get_folder_tree())
def delete_files(): '''批量删除文件''' ids = IDCollectionValidator().nt_data.ids FileDao.delete_files(ids) return Success(error_code=2)
def rename_file(): '''重命名文件''' validator = UpdateFileValidator().nt_data FileDao.rename_file(file_id=validator.file_id, new_filename=validator.filename) return Success(error_code=1)