Skip to content

Commit

Permalink
Remove external use of file service driver and container, opt for enh…
Browse files Browse the repository at this point in the history
…anced interface
  • Loading branch information
Lee Hicks committed Dec 21, 2017
1 parent 459bab5 commit 1c89bd3
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 52 deletions.
12 changes: 5 additions & 7 deletions src/Components/Package/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,16 +459,15 @@ protected function addStorageFiles()
throw new InternalServerErrorException("Can not find storage service $service.");
}

$container = $storage->getContainerId();
if ($storage->driver()->folderExists($container, $resource)) {
if ($storage->folderExists($resource)) {
$zippedResource = $this->getStorageFolderZip($storage, $resource);
if ($zippedResource !== false) {
$newFileName = $service . '/' . rtrim($resource, '/') . '/' . md5($resource) . '.zip';
$this->package->zipFile($zippedResource, $newFileName);
$this->destructible[] = $zippedResource;
}
} elseif ($storage->driver()->fileExists($container, $resource)) {
$content = $storage->driver()->getFileContent($container, $resource, null, false);
} elseif ($storage->fileExists($resource)) {
$content = $storage->getFileContent($resource, null, false);
$this->package->zipContent($service . '/' . $resource, $content);
}
}
Expand Down Expand Up @@ -497,9 +496,8 @@ protected function getStorageFolderZip($storage, $resource)
throw new InternalServerErrorException('Could not create zip file for extracting ' . $resource);
}

$container = $storage->getContainerId();
if ($storage->driver()->folderExists($container, $resource)) {
$storage->driver()->getFolderAsZip($container, $resource, $zip, $zipFileName, true);
if ($storage->folderExists($resource)) {
$storage->getFolderAsZip($resource, $zip, $zipFileName, true);
} else {
return false;
}
Expand Down
6 changes: 2 additions & 4 deletions src/Components/Package/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -917,14 +917,12 @@ protected function storeFiles()
$resourcePath = $service . '/' . ltrim($resource, '/');
$file = $this->package->getFileFromZip($resourcePath);
if (!empty($file)) {
$storage->driver()
->moveFile($storage->getContainerId(), ltrim($resource, '/'), $file, true);
$storage->moveFile(ltrim($resource, '/'), $file, true);
} else {
$resourcePath = $service . '/' . trim($resource, '/') . '/' . md5($resource) . '.zip';
$zip = $this->package->getZipFromZip($resourcePath);
if (!empty($zip)) {
$storage->driver()->extractZipFile(
$storage->getContainerId(),
$storage->extractZipFile(
rtrim($resource, '/') . '/',
$zip,
false,
Expand Down
18 changes: 7 additions & 11 deletions src/Components/Package/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,9 @@ public function getExportStorageService($default = null)
*/
public function getExportStorageFolder($default = null)
{
$folder = array_get($this->manifest, 'storage.folder', array_get($this->manifest, 'storage.path', $default));
return (empty($folder))? $default : $folder;
$folder = array_get($this->manifest, 'storage.folder', array_get($this->manifest, 'storage.path', $default));

return (empty($folder)) ? $default : $folder;
}

/**
Expand All @@ -202,7 +203,7 @@ public function getExportFilename()
array_get($this->manifest, 'storage.file', $default)
);

$filename = (empty($filename))? $default : $filename;
$filename = (empty($filename)) ? $default : $filename;

if (strpos($filename, static::FILE_EXTENSION) === false) {
$filename .= '.' . static::FILE_EXTENSION;
Expand Down Expand Up @@ -669,16 +670,11 @@ public function saveZipFile($storageService, $storageFolder)

/** @type FileServiceInterface $storage */
$storage = ServiceManager::getService($storageService);
$container = $storage->getContainerId();
if (!$storage->driver()->folderExists($container, $storageFolder)) {
$storage->driver()->createFolder($container, $storageFolder);
if (!$storage->folderExists($storageFolder)) {
$storage->createFolder($storageFolder);
}

$storage->driver()->moveFile(
$container,
$storageFolder . '/' . basename($this->zipFile),
$this->zipFile
);
$storage->moveFile($storageFolder . '/' . basename($this->zipFile), $this->zipFile);

$url = Environment::getURI() .
'/api/v2/' .
Expand Down
156 changes: 144 additions & 12 deletions src/Contracts/FileServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,176 @@
interface FileServiceInterface
{
/**
* Get File System driver
* @param $path
* @return boolean
*/
public function isPublicPath($path);

/**
* @param $path
* @return boolean
*/
public function folderExists($path);

/**
* Gets all resources with properties of a particular folder
*
* @param string $path
* @param bool $include_files
* @param bool $include_folders
* @param bool $full_tree
*
* @return FileSystemInterface
* @return array
*/
public function driver();
public function getFolder($path, $include_files = true, $include_folders = true, $full_tree = false);

/**
* @return string
* Gets all properties of a particular folder
*
* @param string $path
*
* @return array
*/
public function getContainerId();
public function getFolderProperties($path);

/**
* @return string
* @param string $path
* @param array $properties
*
* @return void
*/
public function createFolder($path, $properties = []);

/**
* @param string $path
* @param array|string $properties
*
* @return void
*/
public function updateFolderProperties($path, $properties = []);

/**
* @param string $dest_path
* @param string $src_container
* @param string $src_path
* @param bool $check_exist
*
* @return void
*/
public function getFilePath();
public function copyFolder($dest_path, $src_container, $src_path, $check_exist = false);

/**
* @param array $folders
* @param string $root
* @param bool $force If true, delete folder content as well,
* otherwise return error when content present.
*
* @return array
*/
public function deleteFolders($folders, $root = '', $force = false);

/**
* @param string $path
* @param bool $force
* @param bool $content_only
* @return void
*/
public function deleteFolder($path, $force = false, $content_only = false);

/**
* @param string $path
*
* @return bool
*/
public function fileExists($path);

/**
* @param string $path
* @param string $local_file
* @param bool $content_as_base
*
* @return string
*/
public function getFolderPath();
public function getFileContent($path, $local_file = null, $content_as_base = true);

/**
* @param string $path
* @param bool $include_content
* @param bool $content_as_base
*
* @return array
*/
public function getPublicPaths();
public function getFileProperties($path, $include_content = false, $content_as_base = true);

/**
* @param $path
* @return boolean
* @param string $path
* @param array $properties
*
* @return void
*/
public function isPublicPath($path);
public function updateFileProperties($path, $properties = []);

/**
* @param string $path
* @param string $content
* @param boolean $content_is_base
* @param bool $check_exist
*
* @return void
*/
public function writeFile($path, $content, $content_is_base = true, $check_exist = false);

/**
* @param string $path
* @param string $local_path
* @param bool $check_exist
*
* @return void
*/
public function moveFile($path, $local_path, $check_exist = false);

/**
* @param string $dest_path
* @param string $sc_container
* @param string $src_path
* @param bool $check_exist
*
* @return void
*/
public function copyFile($dest_path, $sc_container, $src_path, $check_exist = false);

/**
* @param string $path File path relative to the service root directory
* @param bool $noCheck Set true to avoid checking file existence
*
* @return void
*/
public function deleteFile($path, $noCheck = false);

/**
* @param array $files Array of file paths relative to root
* @param string $root
*
* @return array
*/
public function deleteFiles($files, $root = null);

/**
* @param string $path
* @param bool $download
*/
public function streamFile($path, $download = false);

/**
* @param string $path
* @param null|\ZipArchive $zip
* @param string $zipFileName
* @param bool $overwrite
*
* @return string Zip File Name created/updated
*/
public function getFolderAsZip($path, $zip = null, $zipFileName = null, $overwrite = false);

/**
* @param $path
* @param $zip
Expand Down
3 changes: 2 additions & 1 deletion src/Contracts/FileSystemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,11 @@ public function deleteFolders($container, $folders, $root = '', $force = false);
* @param string $container
* @param string $path Folder path relative to the service root directory
* @param bool $force
* @param bool $content_only
*
* @return void
*/
public function deleteFolder($container, $path, $force = false);
public function deleteFolder($container, $path, $force = false, $content_only = false);

/**
* @param string $container
Expand Down
23 changes: 6 additions & 17 deletions src/Utility/Packager.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,10 @@ private function sanitizeAppRecord(& $record)
}

if ($record['type'] === AppTypes::STORAGE_SERVICE) {
if (!empty(array_get($record, 'storage_service_id'))) {
$serviceRecord = Service::whereId($record['storage_service_id'])->first();
if (empty($serviceRecord)) {
throw new BadRequestException('Invalid Storage Service provided.');
}
if (null === $type = ServiceManager::getServiceType($serviceRecord->type)) {
throw new BadRequestException('Invalid Storage Service provided.');
}
if (ServiceTypeGroups::FILE !== $type->getGroup()) {
if (!empty($serviceId = array_get($record, 'storage_service_id'))) {
$fileServiceNames = ServiceManager::getServiceNamesByGroup(ServiceTypeGroups::FILE);
$serviceName = ServiceManager::getServiceNameById($serviceId);
if (!in_array($serviceName, $fileServiceNames)) {
throw new BadRequestException('Invalid Storage Service provided.');
}
} else {
Expand Down Expand Up @@ -609,14 +604,8 @@ private function packageAppFiles($app)
throw new InternalServerErrorException("Can not find storage service by identifier '$storageServiceId''.");
}

if (empty($storageFolder)) {
if ($storage->driver()->containerExists($appName)) {
$storage->driver()->getFolderAsZip($appName, '', $this->zip, $zipFileName, true);
}
} else {
if ($storage->driver()->folderExists($storageFolder, $appName)) {
$storage->driver()->getFolderAsZip($storageFolder, $appName, $this->zip, $zipFileName, true);
}
if ($storage->folderExists($storageFolder)) {
$storage->getFolderAsZip($storageFolder, $this->zip, $zipFileName, true);
}

return true;
Expand Down

0 comments on commit 1c89bd3

Please sign in to comment.