}
                if ($ob->CheckWebRights('', array('action' => 'read', 'arElement' => $wdElement), false)) {
                    $storage = new CWebDavStorageCore();
                    $storage->setStorageId(array('IBLOCK_ID' => $data['IBLOCK_ID'], 'IBLOCK_SECTION_ID' => $data['SECTION_ID']));
                    $filename = $storage->regenerateNameIfNonUnique($wdElement['element_name'], $savedFolderId);
                    $tmpFile = new CWebDavStubTmpFile();
                    if ($_REQUEST['v']) {
                        list($arFileH, $optionsH, $fullpath, $filenameH) = $ob->getHistoryFileData($wdElement['element_array']['ID'], (int) $_REQUEST['v'], $p);
                        $tmpFile->path = $fullpath;
                    } else {
                        $copyTmpFile = CFile::MakeFileArray($wdElement['element_array']['PROPERTY_FILE_VALUE']);
                        $tmpFile->path = $copyTmpFile['tmp_name'];
                    }
                    $response = array();
                    try {
                        $fileData = $storage->addFile($filename, $savedFolderId, $tmpFile);
                        $savedName = CWebDavIblock::getSavedMetaData();
                        $pathToUserLib = str_replace(array('#USER_ID#', '#user_id#'), array($USER->GetID(), $USER->GetID()), CWebDavIblock::LibOptions('lib_paths', true, $data['IBLOCK_ID']));
                        $pathToUserLib = strstr($pathToUserLib, 'files/element', true) . 'files/lib';
                        $pathToUserLib = $pathToUserLib . '/' . $savedName['alias'] . '?result=doc' . $fileData['extra']['id'];
                        $response = array('status' => 'success', 'newId' => $fileData['extra']['id'], 'viewUrl' => $pathToUserLib);
                    } catch (Exception $e) {
                        $response['status'] = 'error';
                        $fileData = array();
                    }
                    CWebDavTools::sendJsonResponse($response);
                }
            }
        }
    }
} elseif (!empty($_REQUEST['downloadHistory']) && !empty($_REQUEST['id'])) {
 protected function saveNewFile(array $fileData)
 {
     $filename = CTempFile::GetFileName(uniqid('_wd'));
     CheckDirPath($filename);
     $doc = $this->getDocHandler()->downloadFile(array('id' => $this->getFileId(), 'mimeType' => $this->getWebdav()->get_mime_type('1.' . $fileData['createType'])));
     file_put_contents($filename, $doc['content']);
     global $USER;
     $dataUserSection = CWebDavIblock::getRootSectionDataForUser($USER->GetID());
     if (!$dataUserSection) {
         return array('status' => 'error');
     }
     $createdDocFolderId = CIBlockWebdavSocnet::createCreatedDocFolder($dataUserSection['IBLOCK_ID'], $dataUserSection['SECTION_ID'], $USER->GetID());
     if (!$createdDocFolderId) {
         return array('status' => 'error');
     }
     $storage = new CWebDavStorageCore();
     $storage->setStorageId(array('IBLOCK_ID' => $dataUserSection['IBLOCK_ID'], 'IBLOCK_SECTION_ID' => $dataUserSection['SECTION_ID']));
     $nameToSavedFile = $storage->regenerateNameIfNonUnique($doc['name'], $createdDocFolderId);
     $tmpFile = new CWebDavStubTmpFile();
     $tmpFile->path = $filename;
     try {
         $fileData = $storage->addFile($nameToSavedFile, $createdDocFolderId, $tmpFile);
         $response = array('status' => 'success', 'elementId' => $fileData['extra']['id'], 'sectionId' => $fileData['extra']['sectionId'], 'name' => $nameToSavedFile, 'sizeInt' => $fileData['size'], 'type' => CWebDavBase::get_mime_type($nameToSavedFile), 'link' => str_replace('#element_id#', $fileData['extra']['id'], CWebDavSocNetEvent::getRuntime()->arPath['ELEMENT_EDIT_INLINE_URL']), 'nameWithoutExtension' => GetFileNameWithoutExtension($nameToSavedFile));
     } catch (Exception $e) {
         $response = array('status' => 'error');
     }
     return $response;
 }