The app which enables the users to edit office documents from Nextcloud using ONLYOFFICE Document Server, allows multiple users to collaborate in real time and to save back those changes to Nextcloud
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
onlyoffice-nextcloud/lib/Controller/TemplateController.php

204 lines
6.8 KiB

<?php
4 weeks ago
/*
* Copyright (C) Ascensio System SIA, 2009-2026
*
4 weeks ago
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation, together with the
* additional terms provided in the LICENSE file.
*
4 weeks ago
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: https://www.gnu.org/licenses/agpl-3.0.html
*
4 weeks ago
* You can contact Ascensio System SIA by email at info@onlyoffice.com
* or by postal mail at 20A-6 Ernesta Birznieka-Upisha Street, Riga,
* LV-1050, Latvia, European Union.
*
4 weeks ago
* The interactive user interfaces in modified versions of the Program
* are required to display Appropriate Legal Notices in accordance with
* Section 5 of the GNU AGPL version 3.
*
4 weeks ago
* No trademark rights are granted under this License.
*
4 weeks ago
* All non-code elements of the Product, including illustrations,
* icon sets, and technical writing content, are licensed under the
* Creative Commons Attribution-ShareAlike 4.0 International License:
* https://creativecommons.org/licenses/by-sa/4.0/legalcode
*
4 weeks ago
* This license applies only to such non-code elements and does not
* modify or replace the licensing terms applicable to the Program's
* source code, which remains licensed under the GNU Affero General
* Public License v3.
*
4 weeks ago
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\Onlyoffice\Controller;
3 years ago
use OCA\Onlyoffice\TemplateManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\FileDisplayResponse;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\Files\NotFoundException;
use OCP\IL10N;
use OCP\IPreview;
3 years ago
use OCP\IRequest;
use OCP\Preview\IMimeIconProvider;
use Psr\Log\LoggerInterface;
/**
* OCS handler
*/
class TemplateController extends Controller {
3 years ago
public function __construct(
string $appName,
3 years ago
IRequest $request,
private readonly IL10N $trans,
private readonly LoggerInterface $logger,
private readonly IPreview $preview,
private readonly IMimeIconProvider $mimeIconProvider,
3 years ago
) {
parent::__construct($appName, $request);
}
/**
* Get templates
*/
#[NoAdminRequired]
public function getTemplates(): DataResponse {
$templatesList = TemplateManager::getGlobalTemplates();
$templates = [];
foreach ($templatesList as $templatesItem) {
$template = [
"id" => $templatesItem->getId(),
"name" => $templatesItem->getName(),
"type" => TemplateManager::getTypeTemplate($templatesItem->getMimeType()),
"icon" => $this->mimeIconProvider->getMimeIconUrl($templatesItem->getMimeType())
];
$templates[] = $template;
}
return new DataResponse($templates);
}
/**
* Add global template
*
* @return DataResponse
*/
public function addTemplate(): DataResponse {
$file = $this->request->getUploadedFile("file");
if (!empty($file) && is_uploaded_file($file["tmp_name"]) && $file["error"] === 0) {
if (!TemplateManager::isTemplateType($file["name"])) {
return new DataResponse([
"error" => $this->trans->t("Template must be in OOXML format")
]);
}
$templateDir = TemplateManager::getGlobalTemplateDir();
if ($templateDir->nodeExists($file["name"])) {
return new DataResponse([
"error" => $this->trans->t("Template already exists")
]);
}
$templateContent = file_get_contents($file["tmp_name"]);
$template = $templateDir->newFile($file["name"]);
$template->putContent($templateContent);
return new DataResponse([
"id" => $template->getId(),
"name" => $template->getName(),
"type" => TemplateManager::getTypeTemplate($template->getMimeType()),
"icon" => $this->mimeIconProvider->getMimeIconUrl($template->getMimeType())
]);
}
return new DataResponse(["error" => $this->trans->t("Invalid file provided")]);
}
5 years ago
/**
* Delete template
*
* @param int $templateId - file identifier
5 years ago
*
* @return DataResponse
5 years ago
*/
public function deleteTemplate(int $templateId): DataResponse {
$templateDir = TemplateManager::getGlobalTemplateDir();
5 years ago
try {
$templates = $templateDir->getById($templateId);
} catch (\Exception $e) {
$this->logger->error("deleteTemplate: $templateId", ["exception" => $e]);
return new DataResponse([
5 years ago
"error" => $this->trans->t("Failed to delete template")
]);
5 years ago
}
if (empty($templates)) {
$this->logger->info("Template not found: $templateId");
return new DataResponse([
5 years ago
"error" => $this->trans->t("Failed to delete template")
]);
5 years ago
}
$templates[0]->delete();
$this->logger->debug("Template: deleted " . $templates[0]->getName());
return new DataResponse();
5 years ago
}
/**
* Returns the origin document key for editor
*
* @param int $fileId - file identifier
* @param int $x - x
* @param int $y - y
* @param bool $crop - crop
* @param string $mode - mode
*
* @return DataResponse|FileDisplayResponse
*/
#[NoAdminRequired]
#[NoCSRFRequired]
public function preview(
int $fileId,
int $x = 256,
int $y = 256,
bool $crop = false,
string $mode = IPreview::MODE_FILL
): DataResponse|FileDisplayResponse {
if (empty($fileId) || $x === 0 || $y === 0) {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
$template = TemplateManager::getTemplate($fileId);
if (empty($template)) {
$this->logger->error("Template not found: $fileId");
return new DataResponse([], Http::STATUS_NOT_FOUND);
}
try {
$f = $this->preview->getPreview($template, $x, $y, $crop, $mode);
$response = new FileDisplayResponse($f, Http::STATUS_OK, ["Content-Type" => $f->getMimeType()]);
$response->cacheFor(3600 * 24);
return $response;
} catch (NotFoundException) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
} catch (\InvalidArgumentException) {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
}
3 years ago
}