Example #1
0
 public static function UploadFile($hash, &$file, &$package, &$upload, &$error)
 {
     $post = \Bitrix\Main\Context::getCurrent()->getRequest()->getPostList()->toArray();
     $post['PARAMS'] = CUtil::JsObjectToPhp($post['REG_PARAMS']);
     $chatId = intval($post['REG_CHAT_ID']);
     if (intval($chatId) <= 0) {
         $error = GetMessage('IM_DISK_ERR_UPLOAD');
         return false;
     }
     $chatRelation = CIMChat::GetRelationById($chatId);
     if (!$chatRelation[self::GetUserId()]) {
         $error = GetMessage('IM_DISK_ERR_UPLOAD');
         return false;
     }
     $folderModel = self::GetFolderModel($chatId);
     if (!$folderModel) {
         $error = GetMessage('IM_DISK_ERR_UPLOAD');
         return false;
     }
     $fileId = $post['PARAMS'][$file["id"]];
     if (!$fileId) {
         $error = GetMessage('IM_DISK_ERR_UPLOAD');
         return false;
     }
     if (!$file["files"]["default"]) {
         $error = GetMessage('IM_DISK_ERR_UPLOAD');
         return false;
     }
     /** @var $fileModel \Bitrix\Disk\File */
     $fileModel = \Bitrix\Disk\File::getById($fileId);
     if (!$fileModel || $fileModel->getParentId() != $folderModel->getId()) {
         $error = GetMessage('IM_DISK_ERR_UPLOAD');
         return false;
     }
     $resultUpdate = $fileModel->uploadVersion($file["files"]["default"], self::GetUserId());
     if (!$resultUpdate) {
         $errors = $fileModel->getErrors();
         $message = '';
         foreach ($errors as $errorCode) {
             $message = $message . ' ' . $errorCode->getMessage();
         }
         $message = trim($message);
         if (strlen($message) > 0) {
             $error = $message;
         }
         return false;
     }
     $messageId = intval($post['REG_MESSAGE_ID']);
     $file['fileId'] = $fileId;
     $file['fileTmpId'] = $file["id"];
     $file['fileMessageId'] = $messageId;
     $file['fileChatId'] = $chatId;
     $file['fileParams'] = self::GetFileParams($chatId, $fileModel);
     foreach ($chatRelation as $relation) {
         if ($relation['MESSAGE_TYPE'] != IM_MESSAGE_PRIVATE) {
             break;
         }
         if (self::GetUserId() == $relation['USER_ID']) {
             continue;
         }
         \Bitrix\Disk\Driver::getInstance()->getRecentlyUsedManager()->push($relation['USER_ID'], $fileId);
     }
     if (CModule::IncludeModule('pull')) {
         $pullMessage = array('module_id' => 'im', 'command' => 'fileUpload', 'params' => array('fileChatId' => $file['fileChatId'], 'fileId' => $file['fileId'], 'fileTmpId' => $file["id"], 'fileMessageId' => $file["fileMessageId"], 'fileParams' => $file['fileParams']));
         CPullStack::AddByUsers(array_keys($chatRelation), $pullMessage);
         $orm = \Bitrix\Im\ChatTable::getById($chatId);
         $chat = $orm->fetch();
         if ($chat['TYPE'] == IM_MESSAGE_OPEN) {
             CPullWatch::AddToStack('IM_PUBLIC_' . $chat['ID'], $pullMessage);
         }
     }
     $arFiles[$fileId] = $file['fileParams'];
     $file['fileMessageOut'] = CIMMessenger::GetFormatFilesMessageOut($arFiles);
     CIMMessage::UpdateMessageOut($messageId, $file['fileMessageOut']);
     foreach (GetModuleEvents("im", "OnAfterFileUpload", true) as $arEvent) {
         ExecuteModuleEventEx($arEvent, array(array('CHAT_ID' => $file['fileChatId'], 'FILE_ID' => $file['fileId'], 'MESSAGE_ID' => $file['fileMessageId'], 'MESSAGE_OUT' => $file['fileMessageOut'], 'FILE' => $file['fileParams'])));
     }
     return true;
 }
Example #2
0
 public static function UploadFile($hash, &$file, &$package, &$upload, &$error)
 {
     $post = \Bitrix\Main\Context::getCurrent()->getRequest()->getPostList()->toArray();
     $post['PARAMS'] = CUtil::JsObjectToPhp($post['REG_PARAMS']);
     $chatId = intval($post['REG_CHAT_ID']);
     if (intval($chatId) <= 0) {
         return false;
     }
     $chatRelation = CIMChat::GetRelationById($chatId);
     if (!$chatRelation[self::GetUserId()]) {
         $error = GetMessage('IM_DISK_ERR_UPLOAD');
         return false;
     }
     $folderModel = self::GetFolderModel($chatId);
     if (!$folderModel) {
         $error = GetMessage('IM_DISK_ERR_UPLOAD');
         return false;
     }
     $fileId = $post['PARAMS'][$file["id"]];
     if (!$fileId) {
         return false;
     }
     $fileModel = \Bitrix\Disk\File::getById($fileId);
     if (!$fileModel || $fileModel->getParentId() != $folderModel->getId()) {
         $error = GetMessage('IM_DISK_ERR_UPLOAD');
         return false;
     }
     $resultUpdate = $fileModel->uploadVersion($file["files"]["default"], self::GetUserId());
     if (!$resultUpdate) {
         return false;
     }
     $file['fileId'] = $fileId;
     $file['fileTmpId'] = $file["id"];
     $file['fileMessageId'] = $messageId;
     $file['fileChatId'] = $chatId;
     $file['fileParams'] = self::GetFileParams($chatId, $fileModel);
     $messageId = intval($post['REG_MESSAGE_ID']);
     if (CModule::IncludeModule('pull')) {
         foreach ($chatRelation as $relation) {
             CPullStack::AddByUser($relation['USER_ID'], array('module_id' => 'im', 'command' => 'fileUpload', 'params' => array('fileChatId' => $file['fileChatId'], 'fileId' => $file['fileId'], 'fileTmpId' => $file["id"], 'fileMessageId' => $file["fileMessageId"], 'fileParams' => $file['fileParams'])));
         }
     }
     $arFiles[$fileId] = $file['fileParams'];
     $file['fileMessageOut'] = CIMMessenger::GetFormatFilesMessageOut($arFiles);
     CIMMessage::UpdateMessageOut($messageId, $file['fileMessageOut']);
     foreach (GetModuleEvents("im", "OnAfterFileUpload", true) as $arEvent) {
         ExecuteModuleEventEx($arEvent, array(array('CHAT_ID' => $file['fileChatId'], 'FILE_ID' => $file['fileId'], 'MESSAGE_ID' => $file['fileMessageId'], 'MESSAGE_OUT' => $file['fileMessageOut'], 'FILE' => $file['fileParams'])));
     }
     return true;
 }