Skip to content

Commit

Permalink
Code compliance; refactoring of list model with new proper models
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Dec 9, 2015
1 parent dc0969e commit 2b413e9
Show file tree
Hide file tree
Showing 13 changed files with 551 additions and 231 deletions.
231 changes: 176 additions & 55 deletions administrator/components/com_media/models/file.php

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* @package Joomla.Administrator
* @subpackage com_media
*
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('_JEXEC') or die;

jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.file');

/**
* Media Manager model to abstract file actions
*/
abstract class MediaModelFileAdapterAbstract implements MediaModelInterfaceFileAdapter
{
}
20 changes: 20 additions & 0 deletions administrator/components/com_media/models/file/adapter/local.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* @package Joomla.Administrator
* @subpackage com_media
*
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('_JEXEC') or die;

jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.file');

/**
* Media Manager model to abstract the usage of local file actions
*/
class MediaModelFileAdapterLocal implements MediaModelInterfaceFileAdapter
{
}
23 changes: 4 additions & 19 deletions administrator/components/com_media/models/file/type/abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,15 @@
*/
abstract class MediaModelFileTypeAbstract implements MediaModelInterfaceFileType
{
/**
* File group
*/
protected $_group = 'docs';

/**
* File extensions supported by this file type
*/
protected $_extensions = array();
protected $extensions = array();

/**
* MIME types supported by this file type
*/
protected $_mimeTypes = array();
protected $mimeTypes = array();

/**
* Return the list of supported exensions
Expand All @@ -39,7 +34,7 @@ abstract class MediaModelFileTypeAbstract implements MediaModelInterfaceFileType
*/
public function getExtensions()
{
return $this->_extensions;
return $this->extensions;
}

/**
Expand All @@ -49,7 +44,7 @@ public function getExtensions()
*/
public function getMimeTypes()
{
return $this->_mimeTypes;
return $this->mimeTypes;
}

/**
Expand All @@ -63,14 +58,4 @@ public function getProperties($filePath)
{
return array();
}

/**
* Return the group name
*
* @return string
*/
public function getGroup()
{
return $this->_group;
}
}
15 changes: 8 additions & 7 deletions administrator/components/com_media/models/file/type/image.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@
*/
class MediaModelFileTypeImage extends MediaModelFileTypeAbstract implements MediaModelInterfaceFileType
{
/**
* File group
*/
protected $_group = 'images';

/**
* File extensions supported by this file type
*/
protected $_extensions = array(
protected $extensions = array(
'jpg',
'png',
'gif',
Expand All @@ -39,9 +34,15 @@ class MediaModelFileTypeImage extends MediaModelFileTypeAbstract implements Medi
/**
* MIME types supported by this file type
*/
protected $_mimeTypes = array(
protected $mimeTypes = array(
'image/png',
'image/gif'.
'image/x-icon',
'image/jpeg',
'image/bmp',
'image/xcf',
'image/odg',
'image/x-windows-bmp',
);

/**
Expand Down
4 changes: 2 additions & 2 deletions administrator/components/com_media/models/file/type/pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ class MediaModelFileTypePdf extends MediaModelFileTypeAbstract implements MediaM
/**
* File extensions supported by this file type
*/
protected $_extensions = array(
protected $extensions = array(
'pdf',
);

/**
* MIME types supported by this file type
*/
protected $_mimeTypes = array(
protected $mimeTypes = array(
'application/pdf',
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@
*/
class MediaModelFileTypeVideo extends MediaModelFileTypeAbstract implements MediaModelInterfaceFileType
{
/**
* File group
*/
protected $_group = 'videos';

/**
* File extensions supported by this file type
*/
protected $_extensions = array(
protected $extensions = array(
'mp4',
);

Expand Down
160 changes: 159 additions & 1 deletion administrator/components/com_media/models/files.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,126 @@
*/
class MediaModelFiles extends JModelLegacy
{
public function getFiles($folder)
/**
* Lists the files in a folder
*
* @var array
*/
protected $files = array();

/**
* Folder to search for files
*
* @var string
*/
protected $currentFolder = '';

/**
* Get the current folder
*
* @return string
*/
public function getCurrentFolder()
{
return $this->currentFolder;
}

/**
* Set the current folder
*
* @param $folder
*/
public function setCurrentFolder($currentFolder)
{
$this->currentFolder = $currentFolder;

return $this;
}

/**
* Build browsable list of files
*
* @return array
*/
public function getFiles()
{
if (!empty($this->files))
{
return $this->files;
}

$currentFolder = $this->getCurrentFolder();

if (!file_exists($currentFolder))
{
return $this->files;
}

if (!file_exists($currentFolder))
{
return $this->files;
}

$fileList = JFolder::files($currentFolder);
$fileHashes = array();
$storedFiles = $this->getStoredFiles($currentFolder);

// Iterate over the files if they exist
if ($fileList !== false)
{
// Add all files that are physically detected in this folder
foreach ($fileList as $file)
{
if (!$this->isFileBrowsable($currentFolder . '/' . $file))
{
continue;
}

$fileModel = $this->getFileModel();
$fileModel->loadByPath($currentFolder . '/' . $file);

// Construct the file object for use in the Media Manager
$tmp = new JObject;
$tmp->setProperties($fileModel->getFileProperties());
$tmpHash = md5($currentFolder . '/' . $file);
$fileHashes[] = $tmpHash;

$this->files[] = $tmp;
}

// Add all files that are in the database and are not detected in this folder
foreach ($storedFiles as $storedFile)
{
$fileModel = $this->getFileModel();
$fileModel->loadByPath($currentFolder . '/' . $storedFile->filename);

// Skip files already detected
$tmpHash = md5($currentFolder . '/' . $storedFile->filename);

if (in_array($tmpHash, $fileHashes))
{
continue;
}

// Construct the file object for use in the Media Manager
$tmp = new JObject;
$tmp->setProperties($fileModel->getFileProperties());

$this->files[] = $tmp;
}
}

return $this->files;
}

/**
* Return a list of the files stored in the database for a specific folder
*
* @param $folder
*
* @return mixed
*/
public function getStoredFiles($folder)
{
$db = JFactory::getDbo();

Expand All @@ -31,4 +150,43 @@ public function getFiles($folder)

return $results;
}

/**
* Check whether this file is browsable in the Media Manager
*
* @param $file
*
* @return bool
*/
protected function isFileBrowsable($file)
{
$relativeFile = basename($file);

if (!is_file($file))
{
return false;
}

if (substr($relativeFile, 0, 1) == '.')
{
return false;
}

if (strtolower($relativeFile) == 'index.html')
{
return false;
}

return true;
}

/**
* Return the file model
*
* @return MediaModelFile
*/
protected function getFileModel()
{
return new MediaModelFile;
}
}
2 changes: 1 addition & 1 deletion administrator/components/com_media/models/folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ class MediaModelFolder extends JModelLegacy
/**
* Identifier
*/
protected $_id = null;
protected $id = null;
}

0 comments on commit 2b413e9

Please sign in to comment.