Exemplo n.º 1
0
 /**
  * 创建文件
  * @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);
     }
 }