-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
44cd860
commit cec9586
Showing
6 changed files
with
583 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Oops, something went wrong.