Skip to content

Commit

Permalink
2017-02-22 1827
Browse files Browse the repository at this point in the history
  • Loading branch information
allanmcarvalho committed Feb 22, 2017
1 parent 44cd860 commit cec9586
Show file tree
Hide file tree
Showing 6 changed files with 583 additions and 2 deletions.
56 changes: 56 additions & 0 deletions src/Database/Type/FileType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

namespace Upload\Database\Type;

/**
* Description of FileType
*
* @author allan
*/
use Cake\Database\Type;
use Cake\Database\Exception;

class FileType extends Type
{

/**
* Marshalls flat data into PHP objects.
*
* Most useful for converting request data into PHP objects
* that make sense for the rest of the ORM/Database layers.
*
* @param mixed $value The value to convert.
* @return mixed Converted value.
*/
public function marshal($value)
{
if (!is_array($value))
{
throw new Exception(__d('upload', "Misconfigured form"));
} else
{
$mustHave = [
'tmp_name',
'error',
'name',
'type',
'size'
];
foreach ($value as $key => $content)
{
if (!in_array($key, $mustHave))
{
throw new Exception(__d('upload', "Misconfigured form"));
}
}
}
return $value;
}

}
126 changes: 124 additions & 2 deletions src/File/Writer/DefaultWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,135 @@
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

namespace Upload\File\Writer;

use Cake\ORM\Table;
use Cake\ORM\Entity;
use Cake\Filesystem\File;
use Cake\Filesystem\Folder;
use Cake\Utility\Hash;
use Intervention\Image\ImageManager;

/**
* Description of DefaultWriter
*
* @author allancarvalho
*/
class DefaultWriter
class DefaultWriter implements WriterInterface
{
//put your code here

/**
* Table Object
* @var Table
*/
protected $table;

/**
* Entity Object
* @var Entity
*/
protected $entity;

/**
* Name of field of file
* @var string
*/
protected $field;

/**
* Array of settings
* @var array
*/
protected $settings;

/**
* Info from file
* @var array
*/
protected $fileInfo;

/**
* Default destination file path
* @var string
*/
protected $defaultPath = '';

/**
* Final file name
* @var string
*/
protected $fileName = null;

/**
* Construct Method
* @param Table $table
* @param Entity $entity
* @param type $field
* @param type $settings
*/
public function __construct(Table $table, Entity $entity, $field, $settings)
{
$this->table = $table;
$this->entity = $entity;
$this->field = $field;
$this->settings = $settings;
$this->fileInfo = $this->entity->get($this->field);
$this->defaultPath = WWW_ROOT . 'files' . DS . $this->table->getAlias() . DS;
}

public function write()
{

}

public function delete()
{

}

/**
* Get a path to save file
* @return string
*/
protected function getPath()
{
$path = Hash::get($this->settings, 'path', $this->defaultPath);

return empty($path) ? $this->defaultPath : (substr($path, -1) === DS ? $path : $path . DS);
}

/**
* Check if path exist
* @param bool $create Create a path if not exist
*/
protected function checkPath($create = true)
{
if (!new Folder($this->getPath(), $create))
{
\Cake\Log\Log::error(__d('upload', 'Unable to create directory: {0}', $this->getPath()));
}
}

/**
* Return a file name
* @return string
*/
protected function getFileName()
{
if (debug_backtrace()[1]['function'] == 'write')
{
if ($this->fileName === null)
{
$filePrefix = Hash::get($this->settings, 'prefix', '');
$fileUniqidMoreEntropy = Hash::get($this->settings, 'more_entropy', true);
$this->fileName = Hash::get($this->settings, 'filename', uniqid($filePrefix, $fileUniqidMoreEntropy));
}
}elseif(debug_backtrace()[1]['function'] == 'delete')
{
$this->fileName = $this->entity->get($this->field);
}
return $this->fileName;
}

}
60 changes: 60 additions & 0 deletions src/File/Writer/FileWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

namespace Upload\File\Writer;

use Cake\Filesystem\File;
use Cake\Utility\Hash;
use Cake\Log\Log;

/**
* Description of DefaultWriter
*
* @author allancarvalho
*/
class FileWriter extends DefaultWriter
{

public function write()
{
$this->checkPath();
$file = new File($this->fileInfo['tmp_name']);

if($file->copy("{$this->getPath()}{$this->getFileName()}{$this->getFileFormat()}"))
{
$this->entity->set($this->field, "{$this->getFileName()}{$this->getFileFormat()}");
return true;
}else
{
Log::error(__d('upload', 'Unable to save file "{0}" in path "{1}"', $this->getFileName(), $this->getPath()));
return false;
}

}

public function delete()
{
$file = new File("{$this->getPath()}{$this->getFileName()}");
if ($file->exists())
{
if (!$file->delete())
{
Log::error(__d('upload', 'Unable to delete file "{0}" in path "{1}"', $this->getFileName(), $this->getPath()));
}
} else
{
Log::error(__d('upload', 'Unable to delete file "{0}" in path "{1}" because it does not exist', $this->getFileName(), $this->getPath()));
}
}

private function getFileFormat()
{
return '.' . pathinfo($this->fileInfo['name'], PATHINFO_EXTENSION);
}

}
Loading

0 comments on commit cec9586

Please sign in to comment.