private function initializeFileEngine() { $this->filePath = COption::getOptionString("security", "security_event_file_path"); if (checkDirPath($this->filePath)) { $this->isFileEngineActive = true; } }
protected function processActionCommit() { $this->checkRequiredPostParams(array('editSessionId')); if ($this->errorCollection->hasErrors()) { $this->sendJsonErrorResponse(); } $this->checkUpdatePermissions(); $currentSession = $this->getEditSessionByCurrentUser((int) $this->request->getPost('editSessionId')); if (!$currentSession) { $this->errorCollection->add(array(new Error(Loc::getMessage('DISK_DOC_CONTROLLER_ERROR_COULD_NOT_FIND_EDIT_SESSION'), self::ERROR_COULD_NOT_FIND_EDIT_SESSION))); $this->sendJsonErrorResponse(); } $tmpFile = \CTempFile::getFileName(uniqid('_wd')); checkDirPath($tmpFile); $fileData = new FileData(); $fileData->setId($currentSession->getServiceFileId()); $fileData->setSrc($tmpFile); $newNameFileAfterConvert = null; if ($this->documentHandler->isNeedConvertExtension($this->file->getExtension())) { $newNameFileAfterConvert = getFileNameWithoutExtension($this->file->getName()) . '.' . $this->documentHandler->getConvertExtension($this->file->getExtension()); $fileData->setMimeType(TypeFile::getMimeTypeByFilename($newNameFileAfterConvert)); } else { $fileData->setMimeType(TypeFile::getMimeTypeByFilename($this->file->getName())); } $fileData = $this->documentHandler->downloadFile($fileData); if (!$fileData) { if ($this->documentHandler->isRequiredAuthorization()) { $this->sendNeedAuth(); } $this->errorCollection->add($this->documentHandler->getErrors()); $this->sendJsonErrorResponse(); } $this->deleteEditSession($currentSession); $oldName = $this->file->getName(); //rename in cloud service $renameInCloud = $fileData->getName() && $fileData->getName() != $this->file->getName(); if ($newNameFileAfterConvert || $renameInCloud) { if ($newNameFileAfterConvert && $renameInCloud) { $newNameFileAfterConvert = getFileNameWithoutExtension($fileData->getName()) . '.' . getFileExtension($newNameFileAfterConvert); } $this->file->rename($newNameFileAfterConvert); } $fileArray = \CFile::makeFileArray($tmpFile); $fileArray['name'] = $this->file->getName(); $fileArray['type'] = $fileData->getMimeType(); $fileArray['MODULE_ID'] = Driver::INTERNAL_MODULE_ID; /** @noinspection PhpDynamicAsStaticMethodCallInspection */ $fileId = \CFile::saveFile($fileArray, Driver::INTERNAL_MODULE_ID); if (!$fileId) { \CFile::delete($fileId); $this->errorCollection->add(array(new Error(Loc::getMessage('DISK_DOC_CONTROLLER_ERROR_COULD_NOT_SAVE_FILE'), self::ERROR_COULD_NOT_SAVE_FILE))); $this->sendJsonErrorResponse(); } $versionModel = $this->file->addVersion(array('ID' => $fileId, 'FILE_SIZE' => $fileArray['size']), $this->getUser()->getId(), true); if (!$versionModel) { \CFile::delete($fileId); $this->errorCollection->add(array(new Error(Loc::getMessage('DISK_DOC_CONTROLLER_ERROR_COULD_NOT_ADD_VERSION'), self::ERROR_COULD_NOT_ADD_VERSION))); $this->errorCollection->add($this->file->getErrors()); $this->sendJsonErrorResponse(); } if ($this->isLastEditSessionForFile()) { $this->deleteFile($currentSession, $fileData); } $this->sendJsonSuccessResponse(array('objectId' => $this->file->getId(), 'newName' => $this->file->getName(), 'oldName' => $oldName)); }
protected static function generatePath() { $tmpName = md5(mt_rand() . mt_rand()); $dir = rtrim(CTempFile::getDirectoryName(2, Driver::INTERNAL_MODULE_ID), '/') . '/'; checkDirPath($dir); //make folder recursive $pathItems = explode(CTempFile::getAbsoluteRoot(), $dir . $tmpName); return array(array_pop($pathItems), $dir . $tmpName); }
/** * Repacks exported document from Google.Drive, which has wrong order files in archive to show preview * in Google.Viewer. In Google.Viewer document should have file '[Content_Types].xml' on first position in archive. * @param FileData $fileData * @return FileData * @throws IO\FileNotFoundException * @throws IO\InvalidPathException * @internal */ public function repackDocument(FileData $fileData) { if (!extension_loaded('zip')) { return null; } if (!TypeFile::isDocument($fileData->getName())) { return null; } $file = new IO\File($fileData->getSrc()); if (!$file->isExists() || $file->getSize() > 15 * 1024 * 1024) { return null; } unset($file); $ds = DIRECTORY_SEPARATOR; $targetDir = \CTempFile::getDirectoryName(2, 'disk_repack' . $ds . md5(uniqid('di', true))); checkDirPath($targetDir); $targetDir = IO\Path::normalize($targetDir) . $ds; $zipOrigin = new \ZipArchive(); if (!$zipOrigin->open($fileData->getSrc())) { return null; } if ($zipOrigin->getNameIndex(0) === '[Content_Types].xml') { $zipOrigin->close(); return null; } if (!$zipOrigin->extractTo($targetDir)) { $zipOrigin->close(); return null; } $zipOrigin->close(); unset($zipOrigin); if (is_dir($targetDir) !== true) { return null; } $newName = md5(uniqid('di', true)); $newFilepath = $targetDir . '..' . $ds . $newName; $repackedZip = new \ZipArchive(); if (!$repackedZip->open($newFilepath, \ZipArchive::CREATE)) { return null; } $source = realpath($targetDir); $repackedZip->addFile($source . $ds . '[Content_Types].xml', '[Content_Types].xml'); $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST); foreach ($files as $file) { if ($file->getBasename() === '[Content_Types].xml') { continue; } $file = str_replace('\\', '/', $file); $file = realpath($file); if (is_dir($file) === true) { $repackedZip->addEmptyDir(str_replace('\\', '/', str_replace($source . $ds, '', $file . $ds))); } elseif (is_file($file) === true) { $repackedZip->addFile($file, str_replace('\\', '/', str_replace($source . $ds, '', $file))); } } $repackedZip->close(); $newFileData = new FileData(); $newFileData->setSrc($newFilepath); return $newFileData; }
/** * Uploads new chunk from cloud drive if necessary. * @param Entry $entry Cloud import entry. * @return bool */ public function uploadChunk(Entry $entry) { $tmpFile = \CTempFile::getFileName(uniqid('_wd', true)); checkDirPath($tmpFile); $fileData = new Document\FileData(); $fileData->setId($entry->getServiceObjectId()); $fileData->setMimeType($entry->getMimeType()); $fileData->setSrc($tmpFile); $chunkSize = self::CHUNK_SIZE; $downloadedContentSize = $entry->getDownloadedContentSize(); $contentSize = $entry->getContentSize(); if ($contentSize == 0 && $this->documentHandler instanceof Document\GoogleHandler) { return $this->uploadEmptyFileFromGoogle($entry, $fileData); } if ($contentSize - $downloadedContentSize < $chunkSize) { $chunkSize = $contentSize - $downloadedContentSize; } $startRange = $downloadedContentSize; if (!$this->documentHandler->downloadPartFile($fileData, $startRange, $chunkSize)) { $this->errorCollection->add($this->documentHandler->getErrors()); return false; } $token = null; if ($entry->getTmpFile() && $entry->getTmpFile()->getToken()) { //todo it's strange, fix it $token = $entry->getTmpFile()->getToken(); } $uploadFileManager = new Bitrix24Disk\UploadFileManager(); $uploadFileManager->setTmpFileClass(TmpFile::className())->setUser($this->documentHandler->getUserId())->setFileSize($contentSize)->setToken($token)->setContentRange(array($startRange, $startRange + $chunkSize - 1)); $tmpFileArray = \CFile::makeFileArray($tmpFile); if (!$uploadFileManager->upload($fileData->getId(), $tmpFileArray)) { $this->errorCollection->add($uploadFileManager->getErrors()); return false; } if ($token === null) { $entry->linkTmpFile(TmpFile::load(array('=TOKEN' => $uploadFileManager->getToken()))); } return $entry->increaseDownloadedContentSize($chunkSize); }