Allow to undo file deletions

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl
2018-06-12 15:33:06 +02:00
parent 0c711b2b0b
commit ee5a54a575
12 changed files with 152 additions and 20 deletions

View File

@@ -68,4 +68,8 @@ class AttachmentController extends Controller {
public function delete($cardId, $attachmentId) {
return $this->attachmentService->delete($cardId, $attachmentId);
}
public function restore($cardId, $attachmentId) {
return $this->attachmentService->restore($cardId, $attachmentId);
}
}

View File

@@ -21,25 +21,28 @@
*
*/
/**
* Created by PhpStorm.
* User: jus
* Date: 16.05.17
* Time: 12:34
*/
namespace OCA\Deck\Cron;
use OC\BackgroundJob\Job;
use OCA\Deck\Db\AttachmentMapper;
use OCA\Deck\Db\BoardMapper;
use OCA\Deck\InvalidAttachmentType;
use OCA\Deck\Service\AttachmentService;
class DeleteCron extends Job {
/** @var BoardMapper */
private $boardMapper;
/** @var AttachmentService */
private $attachmentService;
/** @var AttachmentMapper */
private $attachmentMapper;
public function __construct(BoardMapper $boardMapper) {
public function __construct(BoardMapper $boardMapper, AttachmentService $attachmentService, AttachmentMapper $attachmentMapper) {
$this->boardMapper = $boardMapper;
$this->attachmentService = $attachmentService;
$this->attachmentMapper = $attachmentMapper;
}
/**
@@ -51,6 +54,18 @@ class DeleteCron extends Job {
foreach ($boards as $board) {
$this->boardMapper->delete($board);
}
$attachments = $this->attachmentMapper->findToDelete();
foreach ($attachments as $attachment) {
try {
$service = $this->attachmentService->getService($attachment->getType());
$service->delete($attachment);
} catch (InvalidAttachmentType $e) {
// Just delete the attachment if no service is available
}
$this->attachmentMapper->delete($attachment);
}
}
}

View File

@@ -28,10 +28,10 @@ class Attachment extends RelationalEntity {
protected $cardId;
protected $type;
protected $data;
protected $lastModified;
protected $createdAt;
protected $lastModified = 0;
protected $createdAt = 0;
protected $createdBy;
protected $deletedAt;
protected $deletedAt = 0;
protected $extendedData = [];
public function __construct() {

View File

@@ -82,6 +82,24 @@ class AttachmentMapper extends DeckMapper implements IPermissionMapper {
return $entities;
}
public function findToDelete() {
// add buffer of 5 min
$timeLimit = time() - (60 * 5);
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from('deck_attachment')
->where($qb->expr()->gt('deleted_at', '0', IQueryBuilder::PARAM_INT))
->andWhere($qb->expr()->lt('deleted_at', (string)$timeLimit, IQueryBuilder::PARAM_INT));
$entities = [];
$cursor = $qb->execute();
while($row = $cursor->fetch()){
$entities[] = $this->mapRowToEntity($row);
}
$cursor->closeCursor();
return $entities;
}
/**
* @param Attachment $attachment
* @throws \Exception

View File

@@ -30,6 +30,7 @@ use OCA\Deck\Db\Attachment;
use OCA\Deck\Db\AttachmentMapper;
use OCA\Deck\Db\CardMapper;
use OCA\Deck\InvalidAttachmentType;
use OCA\Deck\NoPermissionException;
use OCA\Deck\NotFoundException;
use OCP\AppFramework\Http\Response;
@@ -181,17 +182,46 @@ class AttachmentService {
return $attachment;
}
/**
* Either mark an attachment as deleted for later removal or just remove it depending
* on the IAttachmentService implementation
*
* @param $cardId
* @param $attachmentId
* @return \OCP\AppFramework\Db\Entity
* @throws \OCA\Deck\NoPermissionException
* @throws \OCP\AppFramework\Db\DoesNotExistException
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
*/
public function delete($cardId, $attachmentId) {
$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_EDIT);
$attachment = $this->attachmentMapper->find($attachmentId);
try {
$service = $this->getService($attachment->getType());
if ($service->allowUndo()) {
$service->markAsDeleted($attachment);
return $this->attachmentMapper->update($attachment);
}
$service->delete($attachment);
} catch (InvalidAttachmentType $e) {
// just delete without further action
}
$this->attachmentMapper->delete($attachment);
return $attachment;
return $this->attachmentMapper->delete($attachment);
}
public function restore($cardId, $attachmentId) {
$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_EDIT);
$attachment = $this->attachmentMapper->find($attachmentId);
try {
$service = $this->getService($attachment->getType());
if ($service->allowUndo()) {
$attachment->setDeletedAt(0);
return $this->attachmentMapper->update($attachment);
}
} catch (InvalidAttachmentType $e) {
}
throw new NoPermissionException();
}
}

View File

@@ -149,4 +149,22 @@ class FileService implements IAttachmentService {
$response->addHeader('Content-Type', $file->getMimeType());
return $response;
}
/**
* Should undo be allowed and the delete action be done by a background job
*
* @return bool
*/
public function allowUndo() {
return true;
}
/**
* Mark an attachment as deleted
*
* @param Attachment $attachment
*/
public function markAsDeleted(Attachment $attachment) {
$attachment->setDeletedAt(time());
}
}

View File

@@ -81,4 +81,19 @@ interface IAttachmentService {
* @param Attachment $attachment
*/
public function delete(Attachment $attachment);
/**
* Should undo be allowed and the delete action be done by a background job
*
* @return bool
*/
public function allowUndo();
/**
* Mark an attachment as deleted
*
* @param Attachment $attachment
*/
public function markAsDeleted(Attachment $attachment);
}