-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
removed system calls and glob function in FileManager #14
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,125 +1,97 @@ | ||
<?php | ||
|
||
namespace PunkAve\FileUploaderBundle\Services; | ||
|
||
class FileManager | ||
{ | ||
protected $options; | ||
|
||
public function __construct($options) | ||
{ | ||
$this->options = $options; | ||
} | ||
|
||
/** | ||
* Get a list of files already present. The 'folder' option is required. | ||
* If you pass consistent options to this method and handleFileUpload with | ||
* regard to paths, then you will get consistent results. | ||
*/ | ||
public function getFiles($options = array()) | ||
{ | ||
$options = array_merge($this->options, $options); | ||
|
||
$folder = $options['file_base_path'] . '/' . $options['folder']; | ||
if (file_exists($folder)) | ||
{ | ||
$dirs = glob("$folder/originals/*"); | ||
$fullPath = isset($options['full_path']) ? $options['full_path'] : false; | ||
if ($fullPath) | ||
{ | ||
return $dirs; | ||
} | ||
$result = array_map(function($s) { return basename($s); }, $dirs); | ||
return $result; | ||
} | ||
else | ||
{ | ||
return array(); | ||
} | ||
} | ||
|
||
/** | ||
* Remove the folder specified by 'folder' and its contents. | ||
* If you pass consistent options to this method and handleFileUpload with | ||
* regard to paths, then you will get consistent results. | ||
*/ | ||
public function removeFiles($options = array()) | ||
{ | ||
$options = array_merge($this->options, $options); | ||
|
||
|
||
$folder = $options['file_base_path'] . '/' . $options['folder']; | ||
|
||
if (!strlen(trim($options['file_base_path']))) | ||
{ | ||
throw \Exception("file_base_path option looks empty, bailing out"); | ||
} | ||
|
||
if (!strlen(trim($options['folder']))) | ||
{ | ||
throw \Exception("folder option looks empty, bailing out"); | ||
} | ||
system("rm -rf " . escapeshellarg($folder)); | ||
} | ||
|
||
/** | ||
* Sync existing files from one folder to another. The 'fromFolder' and 'toFolder' | ||
* options are required. As with the 'folder' option elsewhere, these are appended | ||
* to the file_base_path for you, missing parent folders are created, etc. If | ||
* 'fromFolder' does not exist no error is reported as this is common if no files | ||
* have been uploaded. If there are files and the sync reports errors an exception | ||
* is thrown. | ||
* | ||
* If you pass consistent options to this method and handleFileUpload with | ||
* regard to paths, then you will get consistent results. | ||
*/ | ||
public function syncFiles($options = array()) | ||
{ | ||
$options = array_merge($this->options, $options); | ||
|
||
// We're syncing and potentially deleting folders, so make sure | ||
// we were passed something - make it a little harder to accidentally | ||
// trash your site | ||
if (!strlen(trim($options['file_base_path']))) | ||
{ | ||
throw \Exception("file_base_path option looks empty, bailing out"); | ||
} | ||
if (!strlen(trim($options['from_folder']))) | ||
{ | ||
throw \Exception("from_folder option looks empty, bailing out"); | ||
} | ||
if (!strlen(trim($options['to_folder']))) | ||
{ | ||
throw \Exception("to_folder option looks empty, bailing out"); | ||
} | ||
|
||
$from = $options['file_base_path'] . '/' . $options['from_folder']; | ||
$to = $options['file_base_path'] . '/' . $options['to_folder']; | ||
$slashes = substr_count($from, '/'); | ||
if (file_exists($from)) | ||
{ | ||
if (isset($options['create_to_folder']) && $options['create_to_folder']) | ||
{ | ||
@mkdir($to, 0777, true); | ||
} | ||
elseif (!file_exists($to)) | ||
{ | ||
throw new \Exception("to_folder does not exist"); | ||
} | ||
system("rsync -a --delete " . escapeshellarg($from . '/') . " " . escapeshellarg($to), $result); | ||
if ($result !== 0) | ||
{ | ||
throw new \Exception("Sync failed"); | ||
} | ||
if (isset($options['remove_from_folder']) && $options['remove_from_folder']) | ||
{ | ||
system("rm -rf " . escapeshellarg($from)); | ||
} | ||
} | ||
else | ||
{ | ||
// A missing from_folder is not an error. This is commonly the case | ||
// when syncing from something that has nothing attached to it yet, etc. | ||
} | ||
} | ||
} | ||
<?php | ||
|
||
namespace PunkAve\FileUploaderBundle\Services; | ||
|
||
use Symfony\Component\Filesystem\Filesystem; | ||
use \Symfony\Component\Finder\Finder; | ||
use Symfony\Component\Finder\SplFileInfo; | ||
|
||
class FileManager extends Filesystem | ||
{ | ||
protected $options; | ||
|
||
public function __construct($options) | ||
{ | ||
if (!strlen(trim($options['file_base_path']))) { | ||
throw \Exception("file_base_path option looks empty, bailing out"); | ||
} | ||
$this->options = $options; | ||
} | ||
/** | ||
* Get a list of files already present. The 'folder' option is required. | ||
* If you pass consistent options to this method and handleFileUpload with | ||
* regard to paths, then you will get consistent results. | ||
*/ | ||
public function getFiles($options = array()) | ||
{ | ||
$options = array_merge($this->options, $options); | ||
$ret = array(); | ||
$folder = $options['file_base_path'] . DIRECTORY_SEPARATOR . $options['folder']; | ||
if ($this->exists($folder)) { | ||
$finder = new Finder(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. replaced glob with Finder instance |
||
$finder->in($folder.DIRECTORY_SEPARATOR.'originals'); | ||
$fullPath = isset($options['full_path']) ? $options['full_path'] : false; | ||
foreach($finder as $entry) { | ||
/** @var $entry SplFileInfo */ | ||
array_push($ret, $fullPath?$entry->getRealPath():$entry->getBasename()); | ||
} | ||
} | ||
return $ret; | ||
} | ||
|
||
/** | ||
* Remove the folder specified by 'folder' and its contents. | ||
* If you pass consistent options to this method and handleFileUpload with | ||
* regard to paths, then you will get consistent results. | ||
* @param array $options | ||
* @throws IOException | ||
*/ | ||
public function removeFiles($options = array()) | ||
{ | ||
$folder = $options['folder']; | ||
if (!strlen(trim($folder))) { | ||
throw \Exception("folder option looks empty, bailing out"); | ||
} | ||
|
||
// Remove folder, let the caller deal with an IO exception in case of error | ||
$this->remove($this->options['file_base_path'] . DIRECTORY_SEPARATOR . $folder); | ||
} | ||
|
||
/** | ||
* Sync existing files from one folder to another. The 'fromFolder' and 'toFolder' | ||
* options are required. As with the 'folder' option elsewhere, these are appended | ||
* to the file_base_path for you, missing parent folders are created, etc. If | ||
* 'fromFolder' does not exist no error is reported as this is common if no files | ||
* have been uploaded. If there are files and the sync reports errors an exception | ||
* is thrown. | ||
* | ||
* If you pass consistent options to this method and handleFileUpload with | ||
* regard to paths, then you will get consistent results. | ||
*/ | ||
public function syncFiles($options = array()) | ||
{ | ||
$options = array_merge($this->options, $options); | ||
|
||
// We're syncing and potentially deleting folders, so make sure | ||
// we were passed something - make it a little harder to accidentally | ||
// trash your site | ||
if (!strlen(trim($options['from_folder']))) { | ||
throw \Exception("from_folder option looks empty, bailing out"); | ||
} | ||
if (!strlen(trim($options['to_folder']))) { | ||
throw \Exception("to_folder option looks empty, bailing out"); | ||
} | ||
|
||
$from = $options['file_base_path'] . DIRECTORY_SEPARATOR . $options['from_folder']; | ||
$to = $options['file_base_path'] . DIRECTORY_SEPARATOR . $options['to_folder']; | ||
if ($this->exists($from)) { | ||
$this->mirror($from, $to); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the mirror function does not remove files present in $to but not in $from, but who cares? if you do, I can alaways augment the mirror functino by extending the Filesystem class. The folder is quite always created. There is also no need to create the target folder as it is done in the mirror function |
||
if (isset($options['remove_from_folder']) && $options['remove_from_folder']) { | ||
$this->remove($from); | ||
} | ||
} else { | ||
// A missing from_folder is not an error. This is commonly the case | ||
// when syncing from something that has nothing attached to it yet, etc. | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to change the checking for conf parameter in constructor as it is always called before the other functions