Skip to content

Commit

Permalink
Merge pull request 'feat: generate url to file stream on check conver…
Browse files Browse the repository at this point in the history
…t' from feature/check-emptyfile-without-creation-of-tempfile into develop

Reviewed-on: https://git.onlyoffice.com/ONLYOFFICE/onlyoffice-chamilo/pulls/9
  • Loading branch information
LinneyS committed Oct 17, 2024
2 parents 0e7f30d + 144bd5c commit f41525c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 38 deletions.
51 changes: 51 additions & 0 deletions callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
case 'download':
$callbackResponseArray = download();
exit(json_encode($callbackResponseArray));
case 'empty':
$callbackResponseArray = emptyFile();
exit(json_encode($callbackResponseArray));
default:
$callbackResponseArray['status'] = 'error';
$callbackResponseArray['error'] = '404 Method not found';
Expand Down Expand Up @@ -210,3 +213,51 @@ function download()
readfile($filePath);
exit;
}

/**
* Downloading empty file by the document service.
*/
function emptyFile()
{
global $plugin;
global $type;
global $courseCode;
global $userId;
global $docId;
global $groupId;
global $sessionId;
global $courseInfo;
global $appSettings;
global $jwtManager;


if ($type !== 'empty') {
$result['status'] = 'error';
$result['error'] = 'Download empty with other action';
return $result;
}

if ($jwtManager->isJwtEnabled()) {
$token = substr(getallheaders()[$appSettings->getJwtHeader()], strlen('Bearer '));
try {
$payload = $jwtManager->decode($token, $appSettings->getJwtKey());
} catch (UnexpectedValueException $e) {
$result['status'] = 'error';
$result['error'] = '403 Access denied';
return $result;
}
}

$template = TemplateManager::getEmptyTemplate('docx');

if (!$template) {
$result['status'] = 'error';
$result['error'] = 'File not found';
return $result;
}

@header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
@header('Content-Disposition: attachment; filename='.'docx.docx');
readfile($template);
exit;
}
1 change: 0 additions & 1 deletion create.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
$values = $form->exportValues();

$fileType = $values['fileFormat'];
var_dump($fileType);
$fileExt = $documentManager->getDocExtByType($fileType);

$result = OnlyofficeDocumentManager::createFile(
Expand Down
45 changes: 8 additions & 37 deletions lib/onlyofficeAppRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,47 +29,18 @@ class OnlyofficeAppRequests extends RequestService
public function __construct($settingsManager, $httpClient, $jwtManager)
{
parent::__construct($settingsManager, $httpClient, $jwtManager);
$tempFile = self::createTempFile();
$this->convertFileUrl = $tempFile['fileUrl'];
$this->convertFilePath = $tempFile['filePath'];
}

public function __destruct()
{
unlink($this->convertFilePath);
}

public function getFileUrlForConvert()
{
return $this->convertFileUrl;
}

/**
* Create temporary file for convert service testing.
*
* @return array
*/
private function createTempFile()
{
$fileUrl = null;
$fileName = 'convert.docx';
$fileExt = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
$baseName = strtolower(pathinfo($fileName, PATHINFO_FILENAME));
$templatePath = TemplateManager::getEmptyTemplate($fileExt);
$folderPath = api_get_path(SYS_PLUGIN_PATH).$this->settingsManager->plugin->getPluginName();
$filePath = $folderPath.'/'.$fileName;

if ($fp = @fopen($filePath, 'w')) {
$content = file_get_contents($templatePath);
fputs($fp, $content);
fclose($fp);
chmod($filePath, api_get_permissions_for_new_files());
$fileUrl = api_get_path(WEB_PLUGIN_PATH).$this->settingsManager->plugin->getPluginName().'/'.$fileName;
}

return [
'fileUrl' => $fileUrl,
'filePath' => $filePath,
$data = [
'type' => 'empty',
'courseId' => api_get_course_int_id(),
'userId' => api_get_user_id(),
'sessionId' => api_get_session_id(),
];
$hashUrl = $this->jwtManager->getHash($data);
return api_get_path(WEB_PLUGIN_PATH).'onlyoffice/callback.php?hash='.$hashUrl;
}

}

0 comments on commit f41525c

Please sign in to comment.