Skip to content

Commit

Permalink
Merge pull request #2903 from nextcloud/backport/2902/stable24
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusknorr authored Apr 11, 2023
2 parents 17d7438 + d3cfe0b commit 94c586c
Show file tree
Hide file tree
Showing 14 changed files with 109 additions and 78 deletions.
13 changes: 7 additions & 6 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@

return [
'routes' => [
//documents
// Page rendering of documents
['name' => 'document#index', 'url' => 'index', 'verb' => 'GET'],
['name' => 'document#remote', 'url' => 'remote', 'verb' => 'GET'],

['name' => 'document#createFromTemplate', 'url' => 'indexTemplate', 'verb' => 'GET'],
['name' => 'document#publicPage', 'url' => '/public', 'verb' => 'GET'],

Expand All @@ -44,18 +43,18 @@
['name' => 'wopi#postFile', 'url' => 'wopi/files/{fileId}', 'verb' => 'POST'],
['name' => 'wopi#getTemplate', 'url' => 'wopi/template/{fileId}', 'verb' => 'GET'],

//settings
// Settings
['name' => 'settings#setPersonalSettings', 'url' => 'ajax/personal.php', 'verb' => 'POST'],
['name' => 'settings#setSettings', 'url' => 'ajax/admin.php', 'verb' => 'POST'],
['name' => 'settings#getSettings', 'url' => 'ajax/settings.php', 'verb' => 'GET'],
['name' => 'settings#updateWatermarkSettings', 'url' => 'settings/watermark', 'verb' => 'POST'],
['name' => 'settings#checkSettings', 'url' => 'settings/check', 'verb' => 'GET'],
['name' => 'settings#demoServers', 'url' => 'settings/demo', 'verb' => 'GET'],

//Mobile access
// Direct Editing: Webview
['name' => 'directView#show', 'url' => '/direct/{token}', 'verb' => 'GET'],

//assets
// Direct Editing: Assets
['name' => 'assets#create', 'url' => 'assets', 'verb' => 'POST'],
['name' => 'assets#get', 'url' => 'assets/{token}', 'verb' => 'GET'],

Expand All @@ -65,13 +64,15 @@
['name' => 'templates#delete', 'url' => '/template/{fileId}', 'verb' => 'DELETE'],
],
'ocs' => [
// Public pages: new file creation
['name' => 'documentAPI#create', 'url' => '/api/v1/file', 'verb' => 'POST'],

// Client API endpoints
['name' => 'OCS#createDirect', 'url' => '/api/v1/document', 'verb' => 'POST'],
['name' => 'OCS#createPublic', 'url' => '/api/v1/share', 'verb' => 'POST'],
['name' => 'OCS#createPublicFromInitiator', 'url' => '/api/v1/direct/share/initiator', 'verb' => 'POST'],
['name' => 'OCS#getTemplates', 'url' => '/api/v1/templates/{type}', 'verb' => 'GET'],
['name' => 'OCS#createFromTemplate', 'url' => '/api/v1/templates/new', 'verb' => 'POST'],
['name' => 'OCS#getTemplates', 'url' => '/api/v1/templates/{type}', 'verb' => 'GET'],

['name' => 'OCS#updateGuestName', 'url' => '/api/v1/wopi/guestname', 'verb' => 'POST'],

Expand Down
8 changes: 7 additions & 1 deletion lib/Controller/DocumentAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function __construct(IRequest $request, IRootFolder $rootFolder, IManager
* @NoAdminRequired
* @PublicPage
*/
public function create(string $mimeType, string $fileName, string $directoryPath = '/', string $shareToken = null): JSONResponse {
public function create(string $mimeType, string $fileName, string $directoryPath = '/', string $shareToken = null, ?int $templateId = null): JSONResponse {
try {
if ($shareToken !== null) {
$share = $this->shareManager->getShareByToken($shareToken);
Expand Down Expand Up @@ -123,8 +123,14 @@ public function create(string $mimeType, string $fileName, string $directoryPath
try {
$file = $folder->newFile($fileName);
$templateType = $this->templateManager->getTemplateTypeForExtension(pathinfo($fileName, PATHINFO_EXTENSION));

$empty = $this->templateManager->getEmpty($templateType);
$templateFile = array_shift($empty);

if ($templateId) {
$templateFile = $this->templateManager->get($templateId);
}

$file->putContent($this->templateManager->getEmptyFileContent($file->getExtension()));
if ($this->templateManager->isSupportedTemplateSource($templateFile->getExtension())) {
// Only use TemplateSource if supported filetype
Expand Down
9 changes: 7 additions & 2 deletions lib/Controller/DocumentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,13 @@ public function publicPage($shareToken, $fileName, $fileId) {
'userId' => $this->uid,
];

list($urlSrc, $token, $wopi) = $this->tokenManager->getToken($item->getId(), $shareToken, $this->uid);
$params['token'] = $token;
$templateFile = $this->templateManager->getTemplateSource($item->getId());
if ($templateFile) {
list($urlSrc, $wopi) = $this->tokenManager->getTokenForTemplate($templateFile, $share->getShareOwner(), $item->getId());
} else {
list($urlSrc, $token, $wopi) = $this->tokenManager->getToken($item->getId(), $shareToken, $this->uid);
}
$params['token'] = $wopi->getToken();
$params['urlsrc'] = $urlSrc;
$params['hideCloseButton'] = $node instanceof File && $wopi->getHideDownload();

Expand Down
1 change: 1 addition & 0 deletions lib/Controller/OCSController.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ public function updateGuestName(string $access_token, string $guestName): DataRe

/**
* @NoAdminRequired
* @PublicPage
*
* @param string $type The template type
* @return DataResponse
Expand Down
1 change: 1 addition & 0 deletions lib/Controller/TemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public function __construct($appName,
/**
* @NoAdminRequired
* @NoCSRFRequired
* @PublicPage
*
* Get preview for a specific template
*
Expand Down
37 changes: 26 additions & 11 deletions lib/TemplateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function setUserId(?string $userId): void {
* @param int $fileId
* @return File
*/
public function get($fileId) {
public function get(int $fileId) {
// is this a global template ?
$files = $this->getEmptyTemplateDir()->getDirectoryListing();

Expand All @@ -164,6 +164,10 @@ public function get($fileId) {
}
}

if ($this->userId === null && $this->config->getAppValue(Application::APPNAME, 'template_public', 'yes') !== 'yes') {
throw new NotFoundException();
}

// is this a global template ?
$files = $this->getSystemTemplateDir()->getDirectoryListing();

Expand Down Expand Up @@ -259,6 +263,9 @@ public function updateEmptyTemplates() {
* @return File[]
*/
public function getSystem($type = null) {
if ($this->userId === null && $this->config->getAppValue(Application::APPNAME, 'template_public', 'yes') !== 'yes') {
return [];
}
$folder = $this->getSystemTemplateDir();

$templateFiles = $folder->getDirectoryListing();
Expand Down Expand Up @@ -289,6 +296,10 @@ public function getSystemFormatted($type = null) {
* @return File[]
*/
public function getUser($type = null) {
if ($this->userId === null) {
return [];
}

try {
$templateDir = $this->getUserTemplateDir();
$templateFiles = $templateDir->getDirectoryListing();
Expand All @@ -303,6 +314,10 @@ public function getUser($type = null) {
* @return array
*/
public function getUserFormatted($type) {
if ($this->userId === null) {
return [];
}

$templates = $this->getUser($type);

return array_map(function(File $file) {
Expand All @@ -316,13 +331,13 @@ public function getUserFormatted($type) {
* @return File[]
*/
public function getAll($type = 'document') {
$system = $this->getSystem();
$user = $this->getUser();

if (!array_key_exists($type, self::$tplTypes)) {
return [];
}

$system = $this->getSystem();
$user = $this->getUser();

return array_values(array_filter(array_merge($user, $system), function (File $template) use ($type) {
foreach (self::$tplTypes[$type] as $mime) {
if ($template->getMimeType() === $mime) {
Expand Down Expand Up @@ -458,12 +473,12 @@ public function formatNodeReturn(File $template) {
$ooxml = $this->config->getAppValue(Application::APPNAME, 'doc_format', '') === 'ooxml';
$documentType = $this->flipTypes()[$template->getMimeType()];
return [
'id' => $template->getId(),
'name' => $template->getName(),
'preview' => $this->urlGenerator->linkToRouteAbsolute('richdocuments.templates.getPreview', ['fileId' => $template->getId()]),
'type' => $this->flipTypes()[$template->getMimeType()],
'delete' => $this->urlGenerator->linkToRouteAbsolute('richdocuments.templates.delete', ['fileId' => $template->getId()]),
'extension' => ($ooxml && isset(self::TYPE_EXTENSION_OOXML[$documentType])) ? self::TYPE_EXTENSION_OOXML[$documentType] : self::TYPE_EXTENTION[$documentType],
'id' => $template->getId(),
'name' => $template->getName(),
'preview' => $this->urlGenerator->linkToRouteAbsolute('richdocuments.templates.getPreview', ['fileId' => $template->getId()]),
'type' => $this->flipTypes()[$template->getMimeType()],
'delete' => $this->urlGenerator->linkToRouteAbsolute('richdocuments.templates.delete', ['fileId' => $template->getId()]),
'extension' => $template->getExtension(),
];
}

Expand Down Expand Up @@ -555,7 +570,7 @@ public function getTemplateSource(int $fileId): ?File {
$query = $this->db->getQueryBuilder();
$query->select('templateid')
->from('richdocuments_template')
->where($query->expr()->eq('userid', $query->createNamedParameter($this->userId)))
->where($this->userId !== null ? $query->expr()->eq('userid', $query->createNamedParameter($this->userId, IQueryBuilder::PARAM_STR)) : $query->expr()->isNull('userid'))
->andWhere($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
$result = $query->executeQuery();
$templateId = (int)$result->fetchOne();
Expand Down
4 changes: 3 additions & 1 deletion src/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ new Vue({
/**
* Append a new template to the dom
*
* @param {Object} data the template data from the template controller response
* @param {object} data the template data from the template controller response
*/
function appendTemplateFromData(data) {
const template = document.querySelector('.template-model').cloneNode(true)
template.className = ''
template.dataset.filename = data.name
template.querySelector('img').src = data.preview
template.querySelector('figcaption').textContent = data.name
template.querySelector('.delete-template').href = data.delete
Expand Down Expand Up @@ -117,6 +118,7 @@ function initTemplateManager() {
},

success(e) {
document.querySelector(`[data-filename="${e.data.name}"]`)?.remove()
inputElmt.disabled = false
buttonElmt.className = 'icon-add'
// add template to dom
Expand Down
4 changes: 4 additions & 0 deletions src/components/SettingsSelectTag.vue
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ const xmlToTagList = (xml) => {
for (const index in list) {
const tag = list[index]['d:propstat']
if (!tag) {
continue
}
if (tag['d:status']['#text'] !== 'HTTP/1.1 200 OK') {
continue
}
Expand Down
10 changes: 5 additions & 5 deletions src/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ $.widget('oc.guestNamePicker', {
/**
* Type definitions for WOPI Post message objects
*
* @typedef {Object} View
* @property {Number} ViewId
* @typedef {object} View
* @property {number} ViewId
* @property {string} UserName
* @property {string} UserId
* @property {Number} Color
* @property {Boolean} ReadOnly
* @property {Boolean} IsCurrentView
* @property {number} Color
* @property {boolean} ReadOnly
* @property {boolean} IsCurrentView
*/

const documentsMain = {
Expand Down
8 changes: 8 additions & 0 deletions src/helpers/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ const getFileTypes = () => {
extension: 'pptx',
mime: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
},
drawing: {
extension: 'odg',
mime: 'application/vnd.oasis.opendocument.graphics',
},
}
}
return {
Expand All @@ -53,6 +57,10 @@ const getFileTypes = () => {
extension: 'odp',
mime: 'application/vnd.oasis.opendocument.presentation',
},
drawing: {
extension: 'odg',
mime: 'application/vnd.oasis.opendocument.graphics',
},
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/services/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import axios from '@nextcloud/axios'
import { generateOcsUrl } from '@nextcloud/router'

export const createEmptyFile = async(mimeType, fileName) => {
export const createEmptyFile = async(mimeType, fileName, templateId = null) => {
const shareToken = document.getElementById('sharingToken')?.value
const directoryPath = document.getElementById('dir')?.value

Expand All @@ -32,6 +32,7 @@ export const createEmptyFile = async(mimeType, fileName) => {
fileName,
directoryPath,
shareToken,
templateId,
})

return response.data
Expand Down
Loading

0 comments on commit 94c586c

Please sign in to comment.