Getting started on attachment retrieval

remotes/upstream/api-rewrite
Mike Koch 7 years ago
parent 999cb74865
commit 3cec244e15

@ -0,0 +1,24 @@
<?php
namespace BusinessLogic\Attachments;
use DataAccess\Attachments\AttachmentGateway;
use DataAccess\Files\FileReader;
class AttachmentRetriever {
/* @var $attachmentGateway AttachmentGateway */
private $attachmentGateway;
/* @var $fileReader FileReader */
private $fileReader;
function __construct($attachmentGateway, $fileReader) {
$this->attachmentGateway = $attachmentGateway;
$this->fileReader = $fileReader;
}
function getAttachmentContentsForTicket($id, $heskSettings) {
}
}

@ -10,12 +10,22 @@ use BusinessLogic\Helpers;
use Controllers\JsonRetriever;
class StaffTicketAttachmentsController {
function post($ticketId) {
function get($attachmentId) {
global $hesk_settings, $applicationContext;
if (!$hesk_settings['attachments']['use']) {
$this->verifyAttachmentsAreEnabled($hesk_settings);
}
private function verifyAttachmentsAreEnabled($heskSettings) {
if (!$heskSettings['attachments']['use']) {
throw new ApiFriendlyException('Attachments are disabled on this server', 'Attachments Disabled', 404);
}
}
function post($ticketId) {
global $hesk_settings, $applicationContext;
$this->verifyAttachmentsAreEnabled($hesk_settings);
/* @var $attachmentHandler AttachmentHandler */
$attachmentHandler = $applicationContext->get[AttachmentHandler::class];

@ -3,6 +3,7 @@
namespace DataAccess\Attachments;
use BusinessLogic\Attachments\Attachment;
use BusinessLogic\Attachments\TicketAttachment;
use DataAccess\CommonDao;
@ -27,4 +28,29 @@ class AttachmentGateway extends CommonDao {
return $attachmentId;
}
function getAttachmentById($id, $heskSettings) {
$this->init();
$rs = hesk_dbQuery("SELECT *
FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "attachments`
WHERE `att_id` = " . intval($id));
if (hesk_dbNumRows($rs) === 0) {
return null;
}
$row = hesk_dbFetchAssoc($rs);
$attachment = new Attachment();
$attachment->id = $row['att_id'];
$attachment->savedName = $row['saved_name'];
$attachment->displayName = $row['real_name'];
$attachment->downloadCount = $row['download_count'];
$attachment->fileSize = $row['size'];
$this->close();
return $attachment;
}
}

@ -0,0 +1,25 @@
<?php
namespace DataAccess\Files;
class FileReader {
/**
* @param $name string - The file name (including extension)
* @param $folder - The folder name (relative to the ROOT of the helpdesk)
* @param $contents string - The contents of the file to write
* @return int The file size, in bytes
* @throws \Exception When the file fails to save
*/
function readFromFile($name, $folder, $contents) {
// __DIR__ === '/{ROOT}/api/DataAccess/Files
$location = __DIR__ . "/../../../{$folder}/{$name}";
$fileContents = file_get_contents($location);
if ($fileContents === false) {
throw new \Exception("Failed to read the file!");
}
return $fileContents;
}
}

@ -0,0 +1,31 @@
<?php
namespace BusinessLogic\Attachments;
use DataAccess\Attachments\AttachmentGateway;
use DataAccess\Files\FileReader;
use PHPUnit\Framework\TestCase;
class AttachmentRetrieverTest extends TestCase {
/* @var $attachmentGateway \PHPUnit_Framework_MockObject_MockObject */
private $attachmentGateway;
/* @var $fileReader \PHPUnit_Framework_MockObject_MockObject */
private $fileReader;
/* @var $attachmentRetriever AttachmentRetriever */
private $attachmentRetriever;
protected function setUp() {
$this->attachmentGateway = $this->createMock(AttachmentGateway::class);
$this->fileReader = $this->createMock(FileReader::class);
$this->attachmentRetriever = new AttachmentRetriever($this->attachmentGateway, $this->fileReader);
}
function testItGetsTheAttachmentFromTheFilesystem() {
}
}

@ -155,6 +155,7 @@ Link::all(array(
// Attachments
'/v1/staff/tickets/{i}/attachments' => \Controllers\Attachments\StaffTicketAttachmentsController::class,
'/v1/staff/attachments/{i}' => \Controllers\Attachments\StaffTicketAttachmentsController::class,
// Any URL that doesn't match goes to the 404 handler
'404' => 'handle404'

Loading…
Cancel
Save