Skip to content

Commit

Permalink
90+ percent of code tested
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre LEJEUNE committed Dec 26, 2019
1 parent 790c699 commit 238ddb6
Show file tree
Hide file tree
Showing 9 changed files with 497 additions and 138 deletions.
2 changes: 2 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
<whitelist>
<directory>./src</directory>
<exclude>
<file>./src/Darkanakin41MediaBundle.php</file>
<directory>./src/Resources</directory>
<directory>./src/DependencyInjection</directory>
</exclude>
</whitelist>
</filter>
Expand Down
16 changes: 14 additions & 2 deletions src/Field/MediaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

namespace Darkanakin41\MediaBundle\Field;

use Darkanakin41\MediaBundle\Model\File;
use Darkanakin41\MediaBundle\DependencyInjection\Darkanakin41MediaExtension;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
Expand All @@ -17,11 +18,22 @@

class MediaType extends AbstractType
{

/**
* @var array
*/
private $bundleConfiguration;

public function __construct(ParameterBagInterface $parameterBag)
{
$this->bundleConfiguration = $parameterBag->get(Darkanakin41MediaExtension::CONFIG_KEY);
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'category' => 'all',
'class' => File::class,
'class' => $this->bundleConfiguration['file_class'],
'choice_label' => 'filename',
));
}
Expand Down
133 changes: 76 additions & 57 deletions src/Service/FileUploadService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ class FileUploadService

/**
* FileUpload constructor.
*
* @param string $rootFolder
*/
public function __construct(ParameterBagInterface $parameterBag, ResizeImageService $resizeImage)
{
Expand All @@ -56,26 +54,57 @@ public function upload(UploadedFile $file, $name = 'image', $category = '')

$folder = sprintf('%s%s/%s/%s/', $this->getTargetFolder(), $category, $now->format('Y'), $now->format('m'));

$mimeType = $file->getMimeType();
$file->move($folder, $fileName);

if ($this->isResizeEnabled()) {
$this->resize($file->getMimeType(), $name, $category, $fileName, $extension);
$this->resize($mimeType, $category, $folder, $fileName);
}

return $this->calculatePath($folder.$fileName, self::PATH_RELATIVE);
}

/**
* Get the base folder for web part.
*
* @return string
*/
public function getBaseFolder()
{
return $this->config['base_folder'];
}

/**
* Get the folder in which the upload must happen.
*
* @return string
*/
public function getTargetFolder()
{
return $this->config['base_folder'].$this->config['storage_folder'];
return $this->getBaseFolder().$this->config['storage_folder'];
}

/**
* Check if the resize function is enabled.
*
* @return bool
*/
public function isResizeEnabled()
{
return $this->config['resize'];
}

public function resize(string $mimeType, $name, $category, $folder, $filename, $extension)
/**
* Resize the given file.
*
* @param string $mimeType the mimeType of the file
* @param string $category
* @param string $folder
* @param string $filename
*
* @throws \Exception
*/
public function resize(string $mimeType, $category, $folder, $filename)
{
if (!in_array($mimeType, array('image/jpg', 'image/jpeg', 'image/gif', 'image/png'))) {
return;
Expand All @@ -84,90 +113,80 @@ public function resize(string $mimeType, $name, $category, $folder, $filename, $
return;
}

try {
$this->resizeImage->process($folder.$filename, $extension, $category);
foreach ($this->config['image_formats'][$category] as $key => $values) {
$quality = 90;
if (isset($values['quality'])) {
$quality = $values['quality'];
}
$resizer->resizeTo($values['width'], $values['height'], $values['resize']);
$resizeFileName = sprintf('%s-%s-%s.%s', Slugify::process($name), time(), $key, $extension);

$resizer->saveImage($folder.DIRECTORY_SEPARATOR.$resizeFileName, $quality);
}
} catch (\Exception $e) {
}
$this->resizeImage->process($folder.$filename, $category);
}

/**
* Calculate the path based on the selected type.
*
* @param string $path the path to process
* @param string $pathType the type of path (PATH_RELATIVE or PATH_ABSOLUTE)
*
* @return string
*/
public function calculatePath($path, $pathType)
{
if (self::PATH_RELATIVE === $pathType && 0 === stripos($path, $this->getRootFolder())) {
return str_ireplace($this->getRootFolder().'/public/', '', $path);
} elseif (self::PATH_ABSOLUTE === $pathType && false === stripos($path, $this->getRootFolder())) {
return $this->getRootFolder().'/public/'.$path;
if (self::PATH_RELATIVE === $pathType && 0 === stripos($path, $this->getBaseFolder())) {
return str_ireplace($this->getBaseFolder(), '', $path);
} elseif (self::PATH_ABSOLUTE === $pathType && false === stripos($path, $this->getBaseFolder())) {
return $this->getBaseFolder().$path;
}

return $path;
}

public function delete(File $file)
/**
* Delete the given File.
*
* @param string $filepath the path to the file to remove
* @param string $category the category of the file
*/
public function delete($filepath, $category)
{
$path = $file->getFilepath();
$path = $this->calculatePath($filepath, self::PATH_ABSOLUTE);
$otherFiles = $this->resizeImage->getResizedFiles($path, $category);

$otherFiles = $this->getOtherFiles($file);
foreach ($otherFiles as $tmp) {
@unlink($this->calculatePath($tmp['path'], self::PATH_ABSOLUTE));
}
@unlink($this->calculatePath($path, self::PATH_ABSOLUTE));
@unlink($path);
}

/**
* Get other versions of the file if available
* Retrieve the version of the file if exist, otherwise, return the default one.
*
* @param File $file
* @param string $version
*
* @return array
* @return string
*/
public function getOtherFiles(File $file)
public function getVersion($filepath, $category, $version)
{
$mimeType = $file->getFiletype();
$category = $file->getCategory();
$path = $file->getFilepath();

$retour = array();
if (in_array($mimeType, array('image/jpg', 'image/jpeg', 'image/gif', 'image/png')) && in_array($category, array_keys($this->config['image_formats']))) {
foreach ($this->config['image_formats'][$category] as $key => $values) {
$infoParts = pathinfo($path);
$resizedPath = str_ireplace('.'.$infoParts['extension'], sprintf('-%s.%s', $key, $infoParts['extension']), $path);
if (file_exists($this->calculatePath($resizedPath, self::PATH_ABSOLUTE))) {
$data = array('path' => $resizedPath);
if (isset($values['min_width'])) {
$data += array('minWidth' => $values['min_width']);
}
$retour[$key] = $data;
}
}
$versions = $this->resizeImage->getResizedFiles($filepath, $category);

if (isset($versions[$version])) {
return $versions[$version]['path'];
}

return $retour;
return $filepath;
}


/**
* Retrieve the version of the file if exist, otherwise, return the default one.
*
* @param string $version
* @param string $filepath the path of the original file
* @param string $category the category of the file
* @param string $pathFormat the path format to output (default : PATH_RELATIVE)
*
* @return string
* @return array
*/
public function getVersion(File $file, $version)
public function getOtherVersions($filepath, $category, $pathFormat = self::PATH_RELATIVE)
{
$versions = $this->getOtherFiles($file);
if (in_array($version, array_keys($versions))) {
return $versions[$version]['path'];
$versions = $this->resizeImage->getResizedFiles($this->calculatePath($filepath, self::PATH_ABSOLUTE), $category);

foreach (array_keys($versions) as $format) {
$versions[$format]['path'] = $this->calculatePath($versions[$format]['path'], $pathFormat);
}

return $file->getFilepath();
return $versions;
}
}
Loading

0 comments on commit 238ddb6

Please sign in to comment.