Skip to content

Commit

Permalink
added fileSystem property to fileProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelAlphonso committed Feb 7, 2017
1 parent 16dd28e commit ffefedb
Showing 1 changed file with 62 additions and 26 deletions.
88 changes: 62 additions & 26 deletions src/Charcoal/Property/FileProperty.php
Expand Up @@ -83,6 +83,13 @@ class FileProperty extends AbstractProperty
*/
private $filesize;

/**
* The filesystem to use while uploading a file.
*
* @var string
*/
private $filesystem = 'public';

/**
* @return string
*/
Expand All @@ -101,7 +108,7 @@ public function setDependencies(Container $container)
{
parent::setDependencies($container);

$this->basePath = $container['config']['base_path'];
$this->basePath = $container['config']['base_path'];
$this->publicPath = $container['config']['public_path'];
}

Expand Down Expand Up @@ -205,6 +212,7 @@ public function overwrite()
public function setAcceptedMimetypes(array $mimetypes)
{
$this->acceptedMimetypes = $mimetypes;

return $this;
}

Expand All @@ -227,6 +235,7 @@ public function setMimetype($type)
{
if ($type === null || $type === false) {
$this->mimetype = null;

return $this;
}

Expand Down Expand Up @@ -271,6 +280,7 @@ public function mimetype()
public function mimetypeFor($file)
{
$info = new finfo(FILEINFO_MIME_TYPE);

return $info->file($file);
}

Expand All @@ -294,8 +304,8 @@ protected function parseIniSize($size)
}

$quant = 'bkmgtpezy';
$unit = preg_replace('/[^'.$quant.']/i', '', $size);
$size = preg_replace('/[^0-9\.]/', '', $size);
$unit = preg_replace('/[^'.$quant.']/i', '', $size);
$size = preg_replace('/[^0-9\.]/', '', $size);

if ($unit) {
$size = ($size * pow(1024, stripos($quant, $unit[0])));
Expand Down Expand Up @@ -339,12 +349,12 @@ public function maxFilesize()
* as configured in {@link http://php.net/manual/en/ini.php `php.ini`}.
*
* @param string|null $iniDirective If $iniDirective is provided, then it is filled with
* the name of the PHP INI directive corresponding to the maximum size allowed.
* the name of the PHP INI directive corresponding to the maximum size allowed.
* @return integer
*/
public function maxFilesizeAllowedByPhp(&$iniDirective = null)
{
$postMaxSize = $this->parseIniSize(ini_get('post_max_size'));
$postMaxSize = $this->parseIniSize(ini_get('post_max_size'));
$uploadMaxFilesize = $this->parseIniSize(ini_get('upload_max_filesize'));

if ($postMaxSize < $uploadMaxFilesize) {
Expand All @@ -371,6 +381,7 @@ public function setFilesize($size)
);
}
$this->filesize = $size;

return $this;
}

Expand All @@ -387,6 +398,7 @@ public function filesize()
$this->filesize = filesize($val);
}
}

return $this->filesize;
}

Expand All @@ -396,6 +408,7 @@ public function filesize()
public function validationMethods()
{
$parentMethods = parent::validationMethods();

return array_merge($parentMethods, [ 'accepted_mimetypes', 'max_filesize' ]);
}

Expand Down Expand Up @@ -498,7 +511,8 @@ public function save($val)
// Upload file, if in request.
if (isset($_FILES[$i])
&& (isset($_FILES[$i]['name']) && $_FILES[$i]['name'])
&& (isset($_FILES[$i]['tmp_name']) && $_FILES[$i]['tmp_name'])) {
&& (isset($_FILES[$i]['tmp_name']) && $_FILES[$i]['tmp_name'])
) {
$file = $_FILES[$i];

if (is_array($file['name']) && $this->multiple() && $this->l10n()) {
Expand All @@ -513,11 +527,11 @@ public function save($val)
$total = count($file['name'][$lang]);
for (; $k < $total; $k++) {
$data = [];
$data['name'] = $file['name'][$lang][$k];
$data['tmp_name'] = $file['tmp_name'][$lang][$k];
$data['error'] = $file['error'][$lang][$k];
$data['type'] = $file['type'][$lang][$k];
$data['size'] = $file['size'][$lang][$k];
$data['name'] = $file['name'][$lang][$k];
$data['tmp_name'] = $file['tmp_name'][$lang][$k];
$data['error'] = $file['error'][$lang][$k];
$data['type'] = $file['type'][$lang][$k];
$data['size'] = $file['size'][$lang][$k];

$f[$lang][] = $this->fileUpload($data);
}
Expand All @@ -526,13 +540,13 @@ public function save($val)
$f = [];
$k = 0;
$total = count($file['name']);
for (; $k< $total; $k++) {
for (; $k < $total; $k++) {
$data = [];
$data['name'] = $file['name'][$k];
$data['tmp_name'] = $file['tmp_name'][$k];
$data['error'] = $file['error'][$k];
$data['type'] = $file['type'][$k];
$data['size'] = $file['size'][$k];
$data['name'] = $file['name'][$k];
$data['tmp_name'] = $file['tmp_name'][$k];
$data['error'] = $file['error'][$k];
$data['type'] = $file['type'][$k];
$data['size'] = $file['size'][$k];

$f[] = $this->fileUpload($data);
}
Expand All @@ -549,11 +563,11 @@ public function save($val)
$f[$lang] = isset($val[$lang]) ? $val[$lang] : '';
continue;
}
$data['name'] = $file['name'][$lang];
$data['tmp_name'] = $file['tmp_name'][$lang];
$data['error'] = $file['error'][$lang];
$data['type'] = $file['type'][$lang];
$data['size'] = $file['size'][$lang];
$data['name'] = $file['name'][$lang];
$data['tmp_name'] = $file['tmp_name'][$lang];
$data['error'] = $file['error'][$lang];
$data['type'] = $file['type'][$lang];
$data['size'] = $file['size'][$lang];

$f[$lang] = $this->fileUpload($data);
}
Expand All @@ -575,9 +589,11 @@ public function save($val)
$f[] = $this->dataUpload($v);
}
}

return $f;
} elseif ($this->isDataUri($val)) {
$f = $this->dataUpload($val);

return $f;
}

Expand Down Expand Up @@ -661,6 +677,7 @@ public function fileUpload(array $fileData)

if ($ret === false) {
$this->logger->warning(sprintf('Could not upload file %s', $target));

return '';
} else {
$this->logger->notice(sprintf('File %s uploaded succesfully', $target));
Expand All @@ -687,7 +704,7 @@ public function uploadTarget($filename = null)
// @todo: Feedback
$this->logger->debug(
'Path does not exist. Attempting to create path '.$dir.'.',
[get_called_class().'::'.__FUNCTION__]
[ get_called_class().'::'.__FUNCTION__ ]
);
mkdir($dir, 0777, true);
}
Expand Down Expand Up @@ -776,8 +793,8 @@ protected function isAbsolutePath($file)
public function sanitizeFilename($filename)
{
// Remove blacklisted caharacters
$blacklist = ['/', '\\', '\0', '*', ':', '?', '"', '<', '>', '|', '#', '&', '!', '`', ' '];
$filename = str_replace($blacklist, '_', $filename);
$blacklist = [ '/', '\\', '\0', '*', ':', '?', '"', '<', '>', '|', '#', '&', '!', '`', ' ' ];
$filename = str_replace($blacklist, '_', $filename);

// Avoid hidden file
$filename = ltrim($filename, '.');
Expand Down Expand Up @@ -910,7 +927,7 @@ private function renamePatternArgs($path, $args = null)
*/
public function generateFilename()
{
$filename = $this->label().' '.date('Y-m-d H-i-s');
$filename = $this->label().' '.date('Y-m-d H-i-s');
$extension = $this->generateExtension();

if ($extension) {
Expand Down Expand Up @@ -971,4 +988,23 @@ public function generateExtension($file = null)

return '';
}

/**
* @return string
*/
public function filesystem()
{
return $this->filesystem;
}

/**
* @param string $filesystem The file system.
* @return self
*/
public function setFilesystem($filesystem)
{
$this->filesystem = $filesystem;

return $this;
}
}

0 comments on commit ffefedb

Please sign in to comment.