Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace sidebar to unify components #1950

Merged
merged 15 commits into from
Nov 23, 2023
Merged
1 change: 1 addition & 0 deletions appinfo/routes/routesFileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
'ocs' => [
['name' => 'File#save', 'url' => '/api/{apiVersion}/file', 'verb' => 'POST', 'requirements' => $requirements],
['name' => 'File#list', 'url' => '/api/{apiVersion}/file/list', 'verb' => 'GET', 'requirements' => $requirements],
['name' => 'File#validate', 'url' => '/api/{apiVersion}/file/validate/', 'verb' => 'GET', 'requirements' => $requirements],
['name' => 'File#validateUuid', 'url' => '/api/{apiVersion}/file/validate/uuid/{uuid}', 'verb' => 'GET', 'requirements' => $requirements],
['name' => 'File#validateFileId', 'url' => '/api/{apiVersion}/file/validate/file_id/{fileId}', 'verb' => 'GET', 'requirements' => $requirements],
['name' => 'File#getPage', 'url' => '/api/{apiVersion}/file/page/{uuid}/{page}.png', 'verb' => 'GET', 'requirements' => $requirements],
Expand Down
36 changes: 32 additions & 4 deletions lib/Controller/FileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,29 @@ public function validateFileId($fileId): JSONResponse {
return $this->validate('FileId', $fileId);
}

private function validate(string $type, $identifier): JSONResponse {
#[NoAdminRequired]
#[NoCSRFRequired]
#[PublicPage]
public function validate(?string $type = null, $identifier = null): JSONResponse {
try {
$this->fileService->setFileByType($type, $identifier);
if (!empty($type) && !empty($identifier)) {
$this->fileService
->setFileByType($type, $identifier);
} elseif ($this->request->getParam('path')) {
$this->fileService
->setMe($this->userSession->getUser())
->setFileByPath($this->request->getParam('path'));
} elseif ($this->request->getParam('fileId')) {
$this->fileService->setFileByType(
'FileId',
$this->request->getParam('fileId')
);
} elseif ($this->request->getParam('uuid')) {
$this->fileService->setFileByType(
'Uuid',
$this->request->getParam('uuid')
);
}
$return = [];
$statusCode = Http::STATUS_OK;
} catch (LibresignException $e) {
Expand Down Expand Up @@ -137,13 +157,21 @@ public function getPage(string $uuid, int $page) {
#[NoAdminRequired]
#[NoCSRFRequired]
#[RequireManager]
public function save(string $name, array $file, array $settings = []): JSONResponse {
public function save(array $file, string $name = '', array $settings = []): JSONResponse {
try {
if (empty($name)) {
if (!empty($file['url'])) {
$name = rawurldecode(pathinfo($file['url'], PATHINFO_FILENAME));
}
}
if (empty($name)) {
// The name of file to sign is mandatory. This phrase is used when we do a request to API sending a file to sign.
throw new \Exception($this->l10n->t('Name is mandatory'));
}
$this->validateHelper->validateNewFile(['file' => $file]);
$this->validateHelper->validateNewFile([
'file' => $file,
'userManager' => $this->userSession->getUser(),
]);
$this->validateHelper->canRequestSign($this->userSession->getUser());

$node = $this->fileService->getNodeFromData([
Expand Down
33 changes: 27 additions & 6 deletions lib/Helper/ValidateHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
use OCP\Files\Config\IUserMountCache;
use OCP\Files\IMimeTypeDetector;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IL10N;
Expand Down Expand Up @@ -88,6 +89,14 @@ public function validateNewFile(array $data): void {
$this->validateFile($data, self::TYPE_TO_SIGN);
if (!empty($data['file']['fileId'])) {
$this->validateNotRequestedSign((int)$data['file']['fileId']);
} elseif (!empty($data['file']['path'])) {
$userFolder = $this->root->getUserFolder($data['userManager']->getUID());
try {
$node = $userFolder->get($data['file']['path']);
} catch (NotFoundException $e) {
throw new LibresignException($this->l10n->t('Invalid data to validate file'), 404);
}
$this->validateNotRequestedSign($node->getId());
}
}

Expand All @@ -107,18 +116,30 @@ public function validateFile(array $data, int $type = self::TYPE_TO_SIGN): void
}
return;
}
if (empty($data['file']['url']) && empty($data['file']['base64']) && empty($data['file']['fileId'])) {
throw new LibresignException($this->l10n->t('File type: %s. Specify a URL, a base64 string or a fileID.', [$this->getTypeOfFile($type)]));
}
if (!empty($data['file']['fileId'])) {
if (!empty($data['file']['url'])) {
if (!filter_var($data['file']['url'], FILTER_VALIDATE_URL)) {
throw new LibresignException($this->l10n->t('File type: %s. Specify a URL, a base64 string or a fileID.', [$this->getTypeOfFile($type)]));
}
} elseif (!empty($data['file']['fileId'])) {
if (!is_numeric($data['file']['fileId'])) {
throw new LibresignException($this->l10n->t('File type: %s. Invalid fileID.', [$this->getTypeOfFile($type)]));
}
$this->validateIfNodeIdExists((int)$data['file']['fileId'], $type);
$this->validateMimeTypeAcceptedByNodeId((int)$data['file']['fileId'], $type);
}
if (!empty($data['file']['base64'])) {
} elseif (!empty($data['file']['base64'])) {
$this->validateBase64($data['file']['base64'], $type);
} elseif (!empty($data['file']['path'])) {
if (!is_a($data['userManager'], IUser::class)) {
throw new LibresignException($this->l10n->t('User not found.'));
}
$userFolder = $this->root->getUserFolder($data['userManager']->getUID());
try {
$userFolder->get($data['file']['path']);
} catch (NotFoundException $e) {
throw new LibresignException($this->l10n->t('Invalid data to validate file'), 404);
}
} else {
throw new LibresignException($this->l10n->t('File type: %s. Specify a URL, base64 string, path or a fileID.', [$this->getTypeOfFile($type)]));
}
}

Expand Down
6 changes: 6 additions & 0 deletions lib/Service/FileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,12 @@ public function getPage(string $uuid, int $page, string $uid): string {
return $imagick->getImageBlob();
}

public function setFileByPath(string $path): self {
$node = $this->folderService->getFileByPath($path);
$this->setFileByType('FileId', $node->getId());
return $this;
}

/**
* @return array[]
*
Expand Down
11 changes: 11 additions & 0 deletions lib/Service/FolderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
namespace OCA\Libresign\Service;

use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Exception\LibresignException;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IConfig;
Expand Down Expand Up @@ -149,4 +151,13 @@ public function getFolderName(array $data, IUser $owner): string {
}
return implode($data['settings']['separator'], $folderName);
}

public function getFileByPath(string $path): Node {
$userFolder = $this->root->getUserFolder($this->getUserId());
try {
return $userFolder->get($path);
} catch (NotFoundException $e) {
throw new LibresignException($this->l10n->t('Invalid data to validate file'), 404);
}
}
}
7 changes: 4 additions & 3 deletions lib/Service/TFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public function getNodeFromData(array $data): Node {
$userFolder = $this->folderService->getFolder($data['file']['fileId']);
return $userFolder->getById($data['file']['fileId'])[0];
}
if (isset($data['file']['path'])) {
return $this->folderService->getFileByPath($data['file']['path']);
}

$content = $this->getFileRaw($data);

Expand Down Expand Up @@ -99,16 +102,14 @@ private function getFileRaw(array $data) {
}
$response = $this->client->newClient()->get($data['file']['url']);
$mimetypeFromHeader = $response->getHeader('Content-Type');
$content = $response->getBody();
$content = (string) $response->getBody();
if (!$content) {
throw new \Exception($this->l10n->t('Empty file'));
}
$this->validateHelper->validateBase64($content);
$mimeTypeFromContent = $this->getMimeType($content);
if ($mimetypeFromHeader !== $mimeTypeFromContent) {
throw new \Exception($this->l10n->t('Invalid URL file'));
}
$this->setMimeType($mimeTypeFromContent);
} else {
$content = $this->getFileFromBase64($data['file']['base64']);
}
Expand Down
20 changes: 15 additions & 5 deletions src/Components/File/LibresignTab.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<template>
<NcAppSidebar :title="propName"
:subtitle="subTitle">
:subtitle="subTitle"
:active="propName"
@close="closeSidebar">
<RequestSignature :file="propFile"
:signers="propSigners"
:name="propName" />
Expand Down Expand Up @@ -47,10 +49,18 @@ export default {
},
computed: {
subTitle() {
return t('libresign', 'Requested by {name}, at {date}', {
name: this.propRequestedBy.uid,
date: Moment(Date.parse(this.propRequestDate)).format('LL LTS'),
})
if (this.propRequestedBy?.uid) {
return t('libresign', 'Requested by {name}, at {date}', {
name: this.propRequestedBy.uid,
date: Moment(Date.parse(this.propRequestDate)).format('LL LTS'),
})
}
return t('libresign', 'Enter who will receive the request')
},
},
methods: {
closeSidebar() {
this.$emit('close')
},
},
}
Expand Down
2 changes: 0 additions & 2 deletions src/domains/files/service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable valid-jsdoc */
import { confirmPassword } from '@nextcloud/password-confirmation'
import '@nextcloud/password-confirmation/dist/style.css' // Required for dialog styles
import axios from '@nextcloud/axios'
import { deburr } from 'lodash-es'
Expand All @@ -20,7 +19,6 @@ const slugfy = val =>
*/
const buildService = (http) => ({
async uploadFile({ file, name }) {
await confirmPassword()
const url = generateOcsUrl('/apps/libresign/api/v1/file')

const settings = {
Expand Down
1 change: 0 additions & 1 deletion src/views/Account/partials/Documents.vue
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ export default {
const fileFullName = await getFilePickerBuilder(t('libresign', 'Select a file'))
.setMultiSelect(false)
.allowDirectories(false)
.setModal(true)
.setType(1) // FilePickerType.Choose
.setMimeTypeFilter([PDF_MIME_TYPE])
.build()
Expand Down
Loading