/** * @param $name * @param $targetElementId * @param CWebDavTmpFile $tmpFile * @return array|boolean */ public function updateFile($name, $targetElementId, CWebDavTmpFile $tmpFile) { /** @var File $file */ $file = File::loadById($targetElementId); if (!$file) { $this->errorCollection->add(array(new Error("Could not " . __METHOD__ . " by id {$targetElementId}", 11154))); return false; } if (!$file->canUpdate($this->storage->getCurrentUserSecurityContext())) { throw new CWebDavAccessDeniedException(); } $fileArray = CFile::MakeFileArray($tmpFile->getAbsolutePath()); if (!$fileArray) { $this->errorCollection->add(array(new Error("Could not " . __METHOD__ . " MakeFileArray", 11155))); return false; } if ($file->uploadVersion($fileArray, $this->getUser()->getId())) { $this->loadFormattedFolderTreeAndBreadcrumbs(); return $this->formatFileToResponse($file); } $this->errorCollection->add(array(new Error("Could not " . __METHOD__ . ", uploadVersion", 11156))); $this->errorCollection->add($file->getErrors()); return false; }
/** * @param CWebDavTmpFile $file * @param $startRange * @param $endRange * @param $fileSize * @return bool * @throws WebDavTmpFileErrorException */ private function internalAppendNonCloud(CWebDavTmpFile $file, $startRange, $endRange, $fileSize) { if (!$this->existsFile()) { throw new WebDavTmpFileErrorException('Append content. Not exists file (target)'); } if (!$file->existsFile()) { throw new WebDavTmpFileErrorException('Append content. Not exists file'); } $fp = fopen($this->getAbsolutePath(), 'ab'); if (!$fp) { throw new WebDavTmpFileErrorException('Could not open file'); } $appendContent = $file->getContent(); if ($appendContent === false) { throw new WebDavTmpFileErrorException('Could not get contents file (target)'); } if (fwrite($fp, $appendContent) === false) { throw new WebDavTmpFileErrorException('Error in fwrite (append)'); } fclose($fp); return true; }
/** * @param $name * @param $targetElementId * @param CWebDavTmpFile $tmpFile * @throws CWebDavAccessDeniedException * @throws WebDavStorageBreakDownException * @return bool|array */ public function updateFile($name, $targetElementId, CWebDavTmpFile $tmpFile) { $this->init(); $name = $this->getWebDav()->correctName($name); if (!$this->checkRights('update', array('name' => $name, 'targetElementId' => $targetElementId))) { throw new CWebDavAccessDeniedException(); } $options = array('new' => false, 'FILE_NAME' => $name, 'ELEMENT_ID' => $targetElementId, 'arUserGroups' => $this->getWebDav()->USER['GROUPS'], 'TMP_FILE' => $tmpFile->getAbsolutePath(), 'WIDTH' => $tmpFile->width, 'HEIGHT' => $tmpFile->height); $this->getDb()->startTransaction(); if (!$this->getWebDav()->put_commit($options)) { $this->getDb()->rollback(); $tmpFile->delete(); return false; } $this->getDb()->commit(); $tmpFile->delete(); if (!empty($options['ELEMENT_ID'])) { $file = $this->getFile(null, array('id' => $options['ELEMENT_ID']), true); if ($file) { return $file; } } throw new WebDavStorageBreakDownException('bd updateFile'); }
public function processActionUpdate(array $params) { $this->checkRequiredParams($params, array('storageExtra', 'storageId', 'name')); $tmpFile = $parentFolderId = $targetSectionId = $elementId = null; $storage = $this->getStorageObject($params['storageExtra'], $params['storageId']); $filename = $params['name']; $token = empty($params['token']) ? null : $params['token']; $inRoot = (bool) $params['inRoot']; $isUpdate = (bool) $params['update']; if ($token && !($tmpFile = CWebDavTmpFile::buildByName($token))) { throw new Exception('Not found file by token'); } if (!$storage->isCorrectName($filename, $msg)) { $tmpFile && $tmpFile->delete(); return $this->sendResponse(array('status' => static::STATUS_DENIED, 'message' => $msg)); } if ($inRoot) { $storageExtra = $storage->getStorageExtra(); $targetSectionId = $storageExtra['sectionId']; $parentFolderId = $storageExtra['sectionId']; } else { $this->checkRequiredParams($params, array('parentExtra')); $parentFolderExtra = $storage->parseElementExtra($params['parentExtra']); $targetSectionId = $parentFolderExtra['id']; $parentFolderId = $parentFolderExtra['id']; } if ($isUpdate) { $this->checkRequiredParams($params, array('id', 'version')); $version = $params['version']; $fileExtra = $storage->parseElementExtra($params['extra']); $elementId = $fileExtra['id']; $file = $storage->getFile($params['id'], $fileExtra); if (empty($file)) { return $this->sendResponse(array('status' => static::STATUS_NOT_FOUND)); } if ($storage::compareVersion($file['version'], $version) > 0) { $file['status'] = static::STATUS_OLD_VERSION; return $this->sendResponse($file); } //todo simple check for move/rename if ($filename != $file['extra']['name'] || $parentFolderId != $file['extra']['sectionId']) { if (!$storage->isUnique($filename, $parentFolderId)) { $file['status'] = static::STATUS_OLD_VERSION; return $this->sendResponse($file); } if ($filename != $file['extra']['name']) { $file = $storage->renameFile($filename, $elementId, $parentFolderId); if (!$file) { return $this->sendError('Error in rename (update) file'); } } if ($parentFolderId != $file['extra']['sectionId']) { $file = $storage->moveFile($filename, $elementId, $parentFolderId); } if (!$file) { return $this->sendError('Error in move/rename (update) file'); } if (!$tmpFile) { return $this->sendSuccess($file); } } unset($file); if ($tmpFile) { $file = $storage->updateFile($filename, $elementId, $tmpFile); if ($file) { CWebDavTools::runEvent(static::ON_AFTER_DISK_FILE_UPDATE, array($file['extra']['id'], $file)); return $this->sendSuccess($file); } return $this->sendResponse(array('status' => static::STATUS_DENIED, 'message' => 'Error in updateFile')); } } else { if (!$storage->isUnique($filename, $targetSectionId, $opponentId)) { $opponentFile = array(); if ($opponentId) { $opponentFile = $storage->getFile(null, array('id' => $opponentId), true); } $opponentFile['status'] = static::STATUS_OLD_VERSION; return $this->sendResponse($opponentFile); } $newFile = $storage->addFile($filename, $targetSectionId, $tmpFile); if ($newFile) { CWebDavTools::runEvent(static::ON_AFTER_DISK_FILE_ADD, array($newFile['extra']['id'], $newFile)); return $this->sendSuccess($newFile); } //else denied } return $this->sendResponse(array('status' => static::STATUS_DENIED, 'message' => 'Error in add/update file')); }