/** * 创建文件 * @param array $fileDetail * @throws * @return array */ private function createFile($fileDetail) { // // 如果创建文件标志为false,则不执行创建 // if ($this->create_file == false) { return; } // // 是否有标记为删除的对象,可能存在多个 // $conflictFile = MFiles::queryFilesByPath($this->file_path, TRUE); if ($conflictFile != false && empty($conflictFile) == false) { foreach ($conflictFile as $file) { // // 如果非文件类型,删除 // if ($file["file_type"] == MConst::OBJECT_TYPE_FILE) { $fileDetail = MFiles::exchange2Object($file, TRUE); $this->creatFileDeleted($fileDetail); return; } else { // 彻底删除之后再进行创建 $trash = new Trash(); $trash->_userId = $this->user_id; $trash->fromIds = $file['id']; try { $trash->invoke(Trash::DELETE); } catch (Exception $e) { throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500); } } } } $this->spaceFilter($this->size); // 过滤器,空间大小计算 $fileDetail->file_create_time = $this->file_create_time; $fileDetail->file_update_time = $this->file_update_time; $fileDetail->file_size = $this->size; $fileDetail->file_type = MConst::OBJECT_TYPE_FILE; $fileDetail->parent_file_id = $this->parent_file_id; $fileDetail->version_id = $this->version_id; $fileDetail->file_path = $this->path; $fileDetail->file_name = $this->file_name; $fileDetail->event_uuid = MiniUtil::getEventRandomString(MConst::LEN_EVENT_UUID); $fileDetail->mime_type = $this->type; // // 创建文件时,如果存在老的版本 需要兼容 不能覆盖 // $metaValue = null; $fileMeta = MFileMetas::queryFileMeta($fileDetail->file_path, MConst::VERSION); if ($fileMeta) { $metaValue = $fileMeta[0]['meta_value']; } // // 文件meta属性,版本信息 // $version = MUtils::getFileVersions($this->user_device_name, $fileDetail->file_size, $this->version_id, $this->action, $this->user_id, $this->user_nick, $metaValue); // // 保存文件元数据 // $retVal = MFiles::CreateFileDetail($fileDetail, $this->user_id, $this->user_nick); if ($retVal === false) { throw new MFilesException(Yii::t('api', MConst::INTERNAL_SERVER_ERROR), MConst::HTTP_CODE_500); } // // 保存事件 // $this->context = array("hash" => $this->file_hash, "rev" => (int) $this->version_id, "bytes" => (int) $this->size, "update_time" => (int) $this->file_update_time, "create_time" => (int) $this->file_create_time); $retVal = MiniEvent::getInstance()->createEvent($this->currentUserId, $this->user_device_id, $this->action, $this->file_path, serialize($this->context), $fileDetail->event_uuid, $this->share_filter->type); // 为每个共享用户创建事件 $this->share_filter->handlerAction($this->action, $this->user_device_id, $this->file_path, $this->context); if (isset($fileDetail->event_uuid)) { $this->event_uuid = $fileDetail->event_uuid; } if ($retVal === false) { throw new MFilesException(Yii::t('api', MConst::INTERNAL_SERVER_ERROR), MConst::HTTP_CODE_500); } // 文件版本引用次数更新 if (MiniVersion::getInstance()->updateRefCount($this->version_id) == false) { throw new MFilesException(Yii::t('api', MConst::INTERNAL_SERVER_ERROR), MConst::HTTP_CODE_500); } if ($fileMeta) { $retVal = MFileMetas::updateFileMeta($this->file_path, MConst::VERSION, $version); } else { $retVal = MFileMetas::createFileMeta($this->file_path, MConst::VERSION, $version); $pathArr = explode('/', $this->file_path); $user = Yii::app()->session["user"]; if ((int) $pathArr[1] !== (int) $user['user_id']) { //只有当被共享者在共享目录下创建文件时,才会记录create_id MFileMetas::createFileMeta($this->file_path, 'create_id', $user['user_id']); } } if ($retVal === false) { throw new MFilesException(Yii::t('api', MConst::INTERNAL_SERVER_ERROR), MConst::HTTP_CODE_500); } }
/** * 处理添加当前文件记录的版本 * @param string $file_path * @param int $version_id * @param string $user_nick */ private function handleFileMeta($file_path, $version_id, $user_id, $user_nick, $deviceName, $fileSize) { // // 查询之前的版本 // $file_meta = MFileMetas::queryFileMeta($file_path, MConst::VERSION); if ($file_meta) { $meta_value = MUtils::getFileVersions($deviceName, $fileSize, $version_id, MConst::DELETE, $user_id, $user_nick, $file_meta[0]["meta_value"]); $ret = MFileMetas::updateFileMeta($file_meta[0]["file_path"], MConst::VERSION, $meta_value); } else { $meta_value = MUtils::getFileVersions($deviceName, $fileSize, $version_id, MConst::DELETE, $user_id, $user_nick); $ret = MFileMetas::createFileMeta($file_path, MConst::VERSION, $meta_value); } return $ret; }
/** * 处理创建文件信息及事件 */ private function createFile($path, $parentFileId, $hadFileDelete) { $fileName = MUtils::get_basename($path); // 组装对象信息 $fileDetail = array(); $fileDetail["file_create_time"] = time(); $fileDetail["file_update_time"] = time(); $fileDetail["file_name"] = $fileName; $fileDetail["file_path"] = $path; $fileDetail["file_size"] = 0; $fileDetail["file_type"] = MConst::OBJECT_TYPE_DIRECTORY; $fileDetail["parent_file_id"] = $parentFileId; $fileDetail["event_uuid"] = MiniUtil::getEventRandomString(MConst::LEN_EVENT_UUID); $fileDetail["mime_type"] = NULL; // 保存文件元数据 if ($hadFileDelete) { $updates = array(); $updates["file_update_time"] = time(); $updates["is_deleted"] = intval(false); $updates["file_type"] = MConst::OBJECT_TYPE_DIRECTORY; $updates["event_uuid"] = $fileDetail["event_uuid"]; // 存在已被删除的数据,只需更新 $retValue = MiniFile::getInstance()->updateByPath($path, $updates); } else { $pathArr = explode("/", $path); // if($this->_user_id!=$pathArr[1]){ // $this->_user_id = $pathArr[1]; // } // 不存在数据,添加 $retValue = MiniFile::getInstance()->create($fileDetail, $pathArr[1]); $user = Yii::app()->session["user"]; if ((int) $pathArr[1] !== (int) $user['user_id']) { //只有当被共享者在共享目录下创建目录时,才会记录create_id MFileMetas::createFileMeta($path, 'create_id', $user['user_id']); } } if ($retValue === false) { throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500); } // 保存事件 $event_action = MConst::CREATE_DIRECTORY; $retValue = MiniEvent::getInstance()->createEvent($this->_user_id, $this->_user_device_id, $event_action, $fileDetail["file_path"], $fileDetail["file_path"], $fileDetail["event_uuid"], $this->share_filter->type); if ($retValue === false) { throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500); } // 为每个共享用户创建事件 if (isset($this->share_filter)) { $this->share_filter->handlerAction($event_action, $this->_user_device_id, $fileDetail["file_path"], $fileDetail["file_path"]); } return $fileDetail; }