/** * Remove irrelevant tmp files. * @return string */ public static function removeIrrelevant() { $query = TmpFile::getList(array('filter' => array('IRRELEVANT' => true), 'order' => array('ID' => 'ASC'), 'limit' => 20)); /** @noinspection PhpAssignmentInConditionInspection */ while ($row = $query->fetch()) { $tmpFile = TmpFile::buildFromArray($row); if (!$tmpFile) { continue; } $tmpFile->delete(); } return get_called_class() . '::removeIrrelevant();'; }
/** * @param $name * @param $targetElementId * @param TmpFile $tmpFile * @param array $data * @return array|bool * @throws AccessDeniedException */ public function updateFile($name, $targetElementId, TmpFile $tmpFile, array $data = array()) { /** @var File $file */ $file = File::loadById($targetElementId); if (!$file) { $this->errorCollection->add(array(new Error("Could not " . __METHOD__ . " by id {$targetElementId}", 11154))); $tmpFile->delete(); return false; } if (!$file->canUpdate($this->storage->getCurrentUserSecurityContext())) { $tmpFile->delete(); throw new AccessDeniedException(); } /** @var array $fileArray */ if ($tmpFile->isCloud() && $tmpFile->getContentType()) { /** @noinspection PhpDynamicAsStaticMethodCallInspection */ $fileId = \CFile::saveFile(array('name' => $tmpFile->getFilename(), 'tmp_name' => $tmpFile->getAbsolutePath(), 'type' => $tmpFile->getContentType()), Driver::INTERNAL_MODULE_ID, true, true); /** @noinspection PhpDynamicAsStaticMethodCallInspection */ $fileArray = \CFile::getFileArray($fileId); if (!$fileArray) { $this->errorCollection->add(array(new Error("Could not " . __METHOD__ . " getFileArray", 1115541))); $tmpFile->delete(); return false; } if (!empty($data['originalTimestamp'])) { $fileArray['UPDATE_TIME'] = DateTime::createFromTimestamp($this->convertFromExternalVersion($data['originalTimestamp'])); } if ($file->addVersion($fileArray, $this->getUser()->getId())) { $tmpFile->delete(); $this->loadFormattedFolderTreeAndBreadcrumbs(); return $this->formatFileToResponse($file); } else { \CFile::delete($fileId); } } else { $fileArray = \CFile::makeFileArray($tmpFile->getAbsolutePath()); if (!$fileArray) { $this->errorCollection->add(array(new Error("Could not " . __METHOD__ . " MakeFileArray", 11155))); $tmpFile->delete(); return false; } if (!empty($data['originalTimestamp'])) { $fileArray['UPDATE_TIME'] = DateTime::createFromTimestamp($this->convertFromExternalVersion($data['originalTimestamp'])); } if ($file->uploadVersion($fileArray, $this->getUser()->getId())) { $tmpFile->delete(); $this->loadFormattedFolderTreeAndBreadcrumbs(); return $this->formatFileToResponse($file); } } $this->errorCollection->add(array(new Error("Could not " . __METHOD__ . ", uploadVersion", 11156))); $this->errorCollection->add($file->getErrors()); $tmpFile->delete(); return false; }
/** * @param $name * @param $targetElementId * @param TmpFile $tmpFile * @return array|boolean */ public function updateFile($name, $targetElementId, TmpFile $tmpFile) { /** @var File $file */ $file = File::loadById($targetElementId); if (!$file) { $this->errorCollection->add(array(new Error("Could not " . __METHOD__ . " by id {$targetElementId}", 11154))); $tmpFile->delete(); return false; } if (!$file->canUpdate($this->storage->getCurrentUserSecurityContext())) { $tmpFile->delete(); throw new AccessDeniedException(); } $fileArray = \CFile::makeFileArray($tmpFile->getAbsolutePath()); if (!$fileArray) { $this->errorCollection->add(array(new Error("Could not " . __METHOD__ . " MakeFileArray", 11155))); $tmpFile->delete(); return false; } if ($file->uploadVersion($fileArray, $this->getUser()->getId())) { $tmpFile->delete(); $this->loadFormattedFolderTreeAndBreadcrumbs(); return $this->formatFileToResponse($file); } $this->errorCollection->add(array(new Error("Could not " . __METHOD__ . ", uploadVersion", 11156))); $this->errorCollection->add($file->getErrors()); $tmpFile->delete(); return false; }