Verified Commit 5f3f5fcd authored by Cyril Vazquez's avatar Cyril Vazquez
Browse files

Manage compressed folders

parent 35189ef9
......@@ -358,18 +358,28 @@ class Repository
$filename = $this->root . DIRECTORY_SEPARATOR . $path;
if (!file_exists($filename)) {
return false;
return $this->checkCompressedFile($path);
}
return true;
}
protected function checkCompressedFile($path)
{
list ($zipfile, $filename) = $this->getCompressedPathinfo($path);
$zip = \laabs::newService('dependency/fileSystem/plugins/zip');
$compressedFiles = $zip->contents($zipfile);
return in_array($filename, $compressedFiles);
}
protected function readFile($path)
{
$filename = $this->root . DIRECTORY_SEPARATOR . $path;
if (!file_exists($filename)) {
throw new \Exception("Can not find resource at path $path");
return $this->readCompressedFile($path);
}
if (!$data = file_get_contents($filename)) {
......@@ -379,6 +389,51 @@ class Repository
return $data;
}
protected function readCompressedFile($path)
{
list ($zipfile, $filename) = $this->getCompressedPathinfo($path);
$tmpdir = \laabs::getTmpDir().DIRECTORY_SEPARATOR.str_replace(',', '.', microtime(true)).'.'.mt_rand();
$tmpfile = $tmpdir.DIRECTORY_SEPARATOR.basename($filename);
$zip = \laabs::newService('dependency/fileSystem/plugins/zip');
$zip->extract($zipfile, $tmpdir, $filename, null, 'e');
if (!$data = file_get_contents($tmpfile)) {
throw new \Exception("Can not read at path $path");
}
unlink($tmpfile);
return $data;
}
protected function getCompressedPathinfo($path)
{
$steps = \laabs\explode(DIRECTORY_SEPARATOR, $path);
$filename = $this->root;
while ($step = array_shift($steps)) {
$filename .= DIRECTORY_SEPARATOR.$step;
switch (true) {
case is_dir($filename) :
continue;
case is_file($filename.'.7z') :
return [$filename.'.7z', $step.DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $steps)];
case is_file($filename.'.zip') :
return [$filename.'.zip', $step.DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $steps)];
default:
throw new \Exception("Can not find resource at path $path");
}
}
throw new \Exception("Can not find resource at path $path");
}
protected function updateFile($path, $data)
{
$filename = $this->root . DIRECTORY_SEPARATOR . $path;
......@@ -435,49 +490,16 @@ class Repository
}
private function deleteFile($path)
{
{
$filename = $this->root . DIRECTORY_SEPARATOR . $path;
if (!file_exists($filename)) {
throw new \Exception("No resource found at path $path");
return $this->deleteCompressedFile($path);
}
if ($this->shred) {
$fp = fopen($filename, 'w');
$fileSize = filesize($filename);
$pass = 0;
//while ($pass != 6) {
$randNum = rand(0, 255);
$compNum = 255 - $randNum;
$lastNum = -1;
do {
$lastNum = rand(0, 255);
} while ($lastNum == $compNum);
// First pass with random num
$randContents = str_repeat(chr($randNum), $fileSize);
fseek($fp, 0);
fwrite($fp, $randContents);
// Second pass with complement of random num
$compContents = str_repeat(chr($compNum), $fileSize);
fseek($fp, 0);
fwrite($fp, $compContents);
// Third pass with a new random num
$lastContents = str_repeat(chr($lastNum), $fileSize);
fseek($fp, 0);
fwrite($fp, $lastContents);
$pass++;
//}
fclose($fp);
$this->shred($filename);
}
if (!unlink($filename)) {
throw new \Exception("Can not delete at path $path");
......@@ -486,6 +508,52 @@ class Repository
return true;
}
protected function deleteCompressedFile($path)
{
list ($zipfile, $filename) = $this->getCompressedPathinfo($path);
$zip = \laabs::newService('dependency/fileSystem/plugins/zip');
return $zip->delete($zipfile, $filename);
}
protected function shred($filename)
{
$fp = fopen($filename, 'w');
$fileSize = filesize($filename);
$pass = 0;
//while ($pass != 6) {
$randNum = rand(0, 255);
$compNum = 255 - $randNum;
$lastNum = -1;
do {
$lastNum = rand(0, 255);
} while ($lastNum == $compNum);
// First pass with random num
$randContents = str_repeat(chr($randNum), $fileSize);
fseek($fp, 0);
fwrite($fp, $randContents);
// Second pass with complement of random num
$compContents = str_repeat(chr($compNum), $fileSize);
fseek($fp, 0);
fwrite($fp, $compContents);
// Third pass with a new random num
$lastContents = str_repeat(chr($lastNum), $fileSize);
fseek($fp, 0);
fwrite($fp, $lastContents);
$pass++;
//}
fclose($fp);
}
protected function writeMetadata($path, $metadata)
{
$dir = $this->root . DIRECTORY_SEPARATOR . dirname($path);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment