Example #1
0
 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'));
 }