Attachments... .

This commit is contained in:
Matias De lellis
2020-06-14 13:55:25 -03:00
parent 16d82bcbae
commit 20e150341c
16 changed files with 532 additions and 73 deletions

View File

@@ -22,12 +22,15 @@
namespace OCA\QuickNotes\Controller;
use OCP\IRequest;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Controller;
use OCP\IRequest;
use OCA\QuickNotes\Db\Attach;
use OCA\QuickNotes\Db\AttachMapper;
use OCA\QuickNotes\Db\Color;
use OCA\QuickNotes\Db\ColorMapper;
use OCA\QuickNotes\Db\Note;
@@ -39,13 +42,18 @@ use OCA\QuickNotes\Db\NoteShareMapper;
use OCA\QuickNotes\Db\Tag;
use OCA\QuickNotes\Db\TagMapper;
use OCA\QuickNotes\Service\FileService;
class NoteController extends Controller {
private $notemapper;
private $notetagmapper;
private $colormapper;
private $notesharemapper;
private $attachMapper;
private $tagmapper;
private $fileService;
private $userId;
public function __construct($AppName,
@@ -54,16 +62,21 @@ class NoteController extends Controller {
NoteTagMapper $notetagmapper,
NoteShareMapper $notesharemapper,
ColorMapper $colormapper,
AttachMapper $attachMapper,
TagMapper $tagmapper,
FileService $fileService,
$UserId)
{
parent::__construct($AppName, $request);
$this->notemapper = $notemapper;
$this->notetagmapper = $notetagmapper;
$this->colormapper = $colormapper;
$this->notemapper = $notemapper;
$this->notetagmapper = $notetagmapper;
$this->colormapper = $colormapper;
$this->notesharemapper = $notesharemapper;
$this->tagmapper = $tagmapper;
$this->userId = $UserId;
$this->attachMapper = $attachMapper;
$this->tagmapper = $tagmapper;
$this->fileService = $fileService;
$this->userId = $UserId;
}
/**
@@ -110,6 +123,15 @@ class NoteController extends Controller {
$note->setIsPinned($note->getPinned() ? true : false);
}
// Insert true attachts to response
foreach ($notes as $note) {
$attachts = $this->attachMapper->findFromNote($this->userId, $note->getId());
foreach ($attachts as $attach) {
$attach->setPreviewUrl($this->fileService->getPreviewUrl($attach->getFileId(), 512));
}
$note->setAttachts($attachts);
}
return new DataResponse($notes);
}
@@ -158,6 +180,7 @@ class NoteController extends Controller {
$newNote->setColor($hcolor->getColor());
$newNote->setIsPinned(false);
$newNote->setTags([]);
$newNote->setAttachts([]);
return new DataResponse($newNote);
}
@@ -168,11 +191,12 @@ class NoteController extends Controller {
* @param int $id
* @param string $title
* @param string $content
* @param array $attachts
* @param boolean $pinned
* @param array $tags
* @param string $color
*/
public function update($id, $title, $content, $pinned, $tags, $color = "#F7EB96") {
public function update($id, $title, $content, $attachts, $pinned, $tags, $color = "#F7EB96") {
// Get current Note and Color.
try {
$note = $this->notemapper->find($id, $this->userId);
@@ -190,6 +214,33 @@ class NoteController extends Controller {
$hcolor = $this->colormapper->insert($hcolor);
}
// Delete old attachts
$dbAttachts = $this->attachMapper->findFromNote($this->userId, $id);
foreach ($dbAttachts as $dbAttach) {
$delete = true;
foreach ($attachts as $attach) {
if ($dbAttach->getFileId() === $attach['file_id']) {
$delete = false;
break;
}
}
if ($delete) {
$this->attachMapper->delete($dbAttach);
}
}
// Add new attachts
foreach ($attachts as $attach) {
if (!$this->attachMapper->fileAttachExists($this->userId, $id, $attach['file_id'])) {
$hAttach = new Attach();
$hAttach->setUserId($this->userId);
$hAttach->setNoteId($id);
$hAttach->setFileId($attach['file_id']);
$hAttach->setCreatedAt(time());
$this->attachMapper->insert($hAttach);
}
}
// Delete old tag relations
$dbTags = $this->tagmapper->getTagsForNote($this->userId, $id);
foreach ($dbTags as $dbTag) {
@@ -244,6 +295,13 @@ class NoteController extends Controller {
// Fill new tags
$newnote->setTags($this->tagmapper->getTagsForNote($this->userId, $newnote->getId()));
// Fill attachts to response
$attachts = $this->attachMapper->findFromNote($this->userId, $newnote->getId());
foreach ($attachts as $attach) {
$attach->setPreviewUrl($this->fileService->getPreviewUrl($attach->getFileId(), 512));
}
$newnote->setAttachts($attachts);
// Remove old color if necessary
if (($oldcolorid !== $hcolor->getId()) &&
(!$this->notemapper->colorIdCount($oldcolorid))) {
@@ -282,6 +340,11 @@ class NoteController extends Controller {
$this->colormapper->delete($oldcolor);
}
$attachts = $this->attachMapper->findFromNote($this->userId, $id);
foreach ($attachts as $attach) {
$this->attachMapper->delete($attach);
}
return new DataResponse($note);
}

28
lib/Db/Attach.php Normal file
View File

@@ -0,0 +1,28 @@
<?php
namespace OCA\QuickNotes\Db;
use JsonSerializable;
use OCP\AppFramework\Db\Entity;
class Attach extends Entity implements JsonSerializable {
protected $userId;
protected $noteId;
protected $fileId;
protected $createdAt;
protected $previewUrl;
public function setPreviewUrl($previewUrl) {
$this->previewUrl = $previewUrl;
}
public function jsonSerialize() {
return [
'id' => $this->id,
'note_id' => $this->noteId,
'file_id' => $this->fileId,
'created_at' => $this->createdAt,
'preview_url' => $this->previewUrl,
];
}
}

89
lib/Db/AttachMapper.php Normal file
View File

@@ -0,0 +1,89 @@
<?php declare(strict_types=1);
namespace OCA\QuickNotes\Db;
use OCP\IDBConnection;
use OCP\AppFramework\Db\QBMapper;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\DB\QueryBuilder\IQueryBuilder;
class AttachMapper extends QBMapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'quicknotes_attach');
}
/**
* @param int $id
* @param string $userId
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
* @return Attach
*/
public function find($id, $userId) {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->tableName)
->where(
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)),
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
);
return $this->findEntity($qb);
}
public function findAll($userId) {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->tableName)
->where(
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
);
return $this->findEntities($qb);
}
/**
* @param string $userId
* @param int $noteId
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
* @return Attach
*/
public function findFileAttachFromNote($userId, $noteId, $fileId) {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->tableName)
->where(
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)),
$qb->expr()->eq('note_id', $qb->createNamedParameter($noteId, IQueryBuilder::PARAM_INT)),
$qb->expr()->eq('file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))
);
return $this->findEntity($qb);
}
public function fileAttachExists($userId, $noteId, $fileId) {
try {
return $this->findFileAttachFromNote($userId, $noteId, $fileId);
} catch (DoesNotExistException $e) {
return false;
}
return false;
}
/**
* @param string $userId
* @param int $noteId
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
* @return Note[]
*/
public function findFromNote($userId, $noteId) {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->tableName)
->where(
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)),
$qb->expr()->eq('note_id', $qb->createNamedParameter($noteId, IQueryBuilder::PARAM_INT))
);
return $this->findEntities($qb);
}
}

View File

@@ -16,6 +16,7 @@ class Note extends Entity implements JsonSerializable {
protected $sharedWith;
protected $isShared;
protected $tags;
protected $attachts;
protected $color;
protected $isPinned;
@@ -41,7 +42,8 @@ class Note extends Entity implements JsonSerializable {
'userid' => $this->userId,
'sharedwith' => $this->sharedWith,
'isshared' => $this->isShared,
'tags' => $this->tags
'tags' => $this->tags,
'attachts' => $this->attachts
];
}
}

View File

@@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
namespace OCA\QuickNotes\Migration;
use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
/**
* Auto-generated migration step: Please modify to your needs!
*/
class Version000301Date20200613151711 extends SimpleMigrationStep {
/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
if (!$schema->hasTable('quicknotes_attach')) {
$table = $schema->createTable('quicknotes_attach');
$table->addColumn('id', 'bigint', [
'autoincrement' => true,
'notnull' => true,
'length' => 8,
'unsigned' => true,
]);
$table->addColumn('user_id', 'string', [
'notnull' => true,
'length' => 200,
'default' => '',
]);
$table->addColumn('note_id', 'bigint', [
'notnull' => true,
'length' => 8,
'unsigned' => true,
]);
$table->addColumn('file_id', 'bigint', [
'notnull' => true,
'length' => 10,
]);
$table->addColumn('created_at', 'bigint', [
'notnull' => false,
'length' => 8,
'default' => 0,
'unsigned' => true,
]);
$table->setPrimaryKey(['id']);
$table->addIndex(['user_id'], 'attach_user_id_index');
$table->addIndex(['note_id'], 'attach_note_id_index');
$table->addIndex(['file_id'], 'attach_file_id_index');
}
return $schema;
}
}

View File

@@ -0,0 +1,67 @@
<?php declare(strict_types=1);
/**
* @copyright Copyright (c) 2020 Matias De lellis <mati86dl@gmail.com>
*
* @author Matias De lellis <mati86dl@gmail.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\QuickNotes\Service;
use OCP\IURLGenerator;
use OCP\Files\IRootFolder;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
class FileService {
/** @var string|null */
private $userId;
/** @var IRootFolder */
private $rootFolder;
/** @var IURLGenerator */
protected $urlGenerator;
public function __construct($userId,
IRootFolder $rootFolder,
IURLGenerator $urlGenerator)
{
$this->userId = $userId;
$this->rootFolder = $rootFolder;
$this->urlGenerator = $urlGenerator;
}
public function getPreviewUrl($fileId, $sideSize): string {
$userFolder = $this->rootFolder->getUserFolder($this->userId);
$node = current($userFolder->getById($fileId));
$path = $userFolder->getRelativePath($node->getPath());
return $this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreview', [
'file' => $path,
'x' => $sideSize,
'y' => $sideSize
]);
}
}