Skip to content

Commit

Permalink
implementation of unzip action for S3 plugin of PHP connector #22
Browse files Browse the repository at this point in the history
  • Loading branch information
psolom committed Apr 8, 2017
1 parent 95af4a9 commit 1151f32
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 4 deletions.
8 changes: 4 additions & 4 deletions connectors/php/LocalFilemanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -997,8 +997,8 @@ public function actionExtract()
$this->error(sprintf($this->lang('ERROR_EXTRACTING_FILE')));
}

$root_files = [];
$response_data = [];
$first_level_items = [];

// make all the folders
for($i = 0; $i < $zip->numFiles; $i++) {
Expand All @@ -1010,7 +1010,7 @@ public function actionExtract()
$created = mkdir($dir_name, 0700, true);

if ($created && substr_count($file_name, '/') === 1) {
$root_files[] = $file_name;
$first_level_items[] = $file_name;
}
}
}
Expand All @@ -1026,15 +1026,15 @@ public function actionExtract()
$copied = copy('zip://'. $source_fullpath .'#'. $file_name, $dir_name);

if($copied && strpos($file_name, '/') === false) {
$root_files[] = $file_name;
$first_level_items[] = $file_name;
}
}
}
}

$zip->close();

foreach ($root_files as $file_name) {
foreach ($first_level_items as $file_name) {
$relative_path = $this->cleanPath($target_path . '/' . $file_name);
$item = $this->get_file_info($relative_path);
$response_data[] = $item;
Expand Down
86 changes: 86 additions & 0 deletions connectors/php/plugins/s3/S3Filemanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ public function actionGetFolder()
$this->error(sprintf($this->lang('DIRECTORY_NOT_EXIST'), $target_path));
}

//$res = $this->getFilesList($target_fullpath);

if(!$handle = @opendir($target_fullpath)) {
$this->error(sprintf($this->lang('UNABLE_TO_OPEN_DIRECTORY'), $target_path));
} else {
Expand Down Expand Up @@ -962,6 +964,90 @@ public function actionSummarize()
];
}

/**
* @inheritdoc
*/
public function actionExtract()
{
if (!extension_loaded('zip')) {
$this->error(sprintf($this->lang('NOT_FOUND_SYSTEM_MODULE'), 'zip'));
}

$source_path = $this->post['source'];
$target_path = $this->post['target'];
$source_fullpath = $this->getFullPath($source_path, true);
$target_fullpath = $this->getFullPath($target_path, true);

if(is_dir($source_fullpath)) {
$this->error(sprintf($this->lang('FORBIDDEN_ACTION_DIR')));
}

$temp_archive = sys_get_temp_dir() . '/' . uniqid();
if (!copy($source_fullpath, $temp_archive)) {
$this->error(sprintf($this->lang('ERROR_SERVER')));
}

$zip = new ZipArchive();
if ($zip->open($temp_archive) !== true) {
$this->error(sprintf($this->lang('ERROR_EXTRACTING_FILE')));
}

$folders = [];
$response_data = [];

// make all the folders
for($i = 0; $i < $zip->numFiles; $i++) {
$file_stat = $zip->statIndex($i);

if ($file_stat['name'][strlen($file_stat['name'])-1] === "/") {
$dir_name = $target_fullpath . $file_stat['name'];
$created = mkdir($dir_name, 0755, true);

if ($created) {
$folders[] = $file_stat['name'];
}
}
}

// since there is no concept of "folder" in S3 we have to extract first level folders manually
$root_folders = [];
foreach($folders as $name) {
$name = ltrim($name, '/');
$root = substr($name, 0, strpos($name, '/') + 1);
$root_folders[$root] = $root;
}
$first_level_items = array_values($root_folders);

// unzip into the folders
for($i = 0; $i < $zip->numFiles; $i++) {
$file_name = $zip->getNameIndex($i);
$file_stat = $zip->statIndex($i);

if ($file_stat['name'][strlen($file_stat['name'])-1] !== "/") {
if ($this->is_allowed_file_type($file_name)) {
$dir_name = $target_fullpath . $file_stat['name'];
$copied = copy('zip://'. $temp_archive .'#'. $file_name, $dir_name);

if($copied && strpos($file_name, '/') === false) {
$first_level_items[] = $file_name;
}
}
}
}

$zip->close();

foreach ($first_level_items as $file_name) {
$relative_path = $this->cleanPath($target_path . '/' . $file_name);
$item = $this->get_file_info($relative_path);
$response_data[] = $item;
}

unlink($temp_archive);

return $response_data;
}


/*******************************************************************************
* Utility
Expand Down

0 comments on commit 1151f32

Please sign in to comment.