Skip to content

Commit

Permalink
Task techjoomla#205 feat : File download functionality from backend
Browse files Browse the repository at this point in the history
  • Loading branch information
“Rajnish committed Nov 7, 2019
1 parent 0854c9d commit ce924fc
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 2 deletions.
89 changes: 88 additions & 1 deletion administrator/controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

// No direct access
defined('_JEXEC') or die;

use Joomla\CMS\Table\Table;
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\CMS\Language\Text;
/**
* TJ Fields Controller
*
Expand Down Expand Up @@ -38,4 +41,88 @@ public function display($cachable = false, $urlparams = false)

return $this;
}

/**
* Fuction to get download media file
*
* @return object
*/
public function getMediaFile()
{
JLoader::import("/techjoomla/media/storage/local", JPATH_LIBRARIES);
$app = Factory::getApplication();
$jinput = $app->input;
$mediaLocal = TJMediaStorageLocal::getInstance();

// Here, fpht means file encoded name
$encodedFileName = $jinput->get('fpht', '', 'STRING');
$decodedFileName = base64_decode($encodedFileName);

// Subform File field Id for checking autherization for specific field under subform
$subformFileFieldId = $jinput->get('subFormFileFieldId', '', 'INT');

// Get media storage path
JLoader::import('components.com_tjfields.models.fields', JPATH_SITE);
$fieldsModel = BaseDatabaseModel::getInstance('Fields', 'TjfieldsModel', array('ignore_request' => true));
$data = $fieldsModel->getMediaStoragePath($jinput->get('id', '', 'INT'), $subformFileFieldId);

if ($data->tjFieldFieldTable->type == "file")
{
$extraFieldParams = json_decode($data->tjFieldFieldTable->params);
$storagePath = $extraFieldParams->uploadpath;
$decodedPath = $storagePath . '/' . $decodedFileName;
}
else
{
$fieldType = $data->tjFieldFieldTable->type;
$decodedPath = JPATH_SITE . '/' . $fieldType . 's/tjmedia/' . str_replace(".", "/", $data->tjFieldFieldTable->client) . '/' . $decodedFileName;
}

if ($data->tjFieldFieldTable->fieldValueId)
{
$user = Factory::getUser();

if ($subformFileFieldId)
{
$canView = $user->authorise('core.field.viewfieldvalue', 'com_tjfields.field.' . $subformFileFieldId);
}
else
{
$canView = $user->authorise('core.field.viewfieldvalue', 'com_tjfields.field.' . $data->tjFieldFieldTable->field_id);
}

$canDownload = 0;

// Allow to view own data
if ($data->tjFieldFieldTable->user_id != null && ($user->id == $data->tjFieldFieldTable->user_id))
{
$canDownload = true;
}

if ($canView || $canDownload)
{
$down_status = $mediaLocal->downloadMedia($decodedPath, '', '', 0);

if ($down_status === 2)
{
$app->enqueueMessage(Text::_('COM_TJFIELDS_FILE_NOT_FOUND'), 'error');
$app->redirect($this->returnURL);
}

return;
}
else
{
$app->enqueueMessage(Text::_('JERROR_ALERTNOAUTHOR'), 'error');
$app->redirect($this->returnURL);
}
}
else
{
$app->enqueueMessage(Text::_('JERROR_ALERTNOAUTHOR'), 'error');
$app->redirect($this->returnURL);
}

jexit();
}
}
2 changes: 1 addition & 1 deletion site/helpers/tjfields.php
Original file line number Diff line number Diff line change
Expand Up @@ -1919,7 +1919,7 @@ public function getMediaUrl($fileName, $extraUrlParamsArray = '')
// Here, fpht means file encoded path
$encodedFileName = base64_encode($fileName);
$basePathLink = 'index.php?option=com_tjfields&task=getMediaFile&fpht=';
$mediaURLlink = JUri::root() . substr(JRoute::_($basePathLink . $encodedFileName . $extraUrlParams), strlen(JUri::base(true)) + 1);
$mediaURLlink = JUri::base() . substr(JRoute::_($basePathLink . $encodedFileName . $extraUrlParams), strlen(JUri::base(true)) + 1);

return $mediaURLlink;
}
Expand Down

0 comments on commit ce924fc

Please sign in to comment.