Skip to content

Commit

Permalink
Bypass Storage for local storage
Browse files Browse the repository at this point in the history
This allow file uploads to be stored outside of the storage/app folder, which was previously not possible
This should also provide a performance gain
  • Loading branch information
daftspunk committed Apr 5, 2016
1 parent 72ab00a commit 71ce583
Showing 1 changed file with 78 additions and 30 deletions.
108 changes: 78 additions & 30 deletions src/Database/Attach/File.php
Expand Up @@ -230,7 +230,7 @@ public function getContents($fileName = null)
$fileName = $this->disk_name;
}

return Storage::get($this->getStorageDirectory() . $this->getPartitionDirectory() . $fileName);
return $this->storageCmd('get', $this->getStorageDirectory() . $this->getPartitionDirectory() . $fileName);
}

/**
Expand Down Expand Up @@ -497,16 +497,24 @@ protected function deleteThumbs()
$pattern = 'thumb_'.$this->id.'_';

$directory = $this->getStorageDirectory() . $this->getPartitionDirectory();
$allFiles = Storage::files($directory);
$allFiles = $this->storageCmd('files', $directory);
$collection = [];
foreach ($allFiles as $file) {
if (starts_with(basename($file), $pattern)) {
$collection[] = $file;
}
}

/*
* Delete the collection of files
*/
if (!empty($collection)) {
Storage::delete($collection);
if ($this->isLocalStorage()) {
FileHelper::delete($collection);
}
else {
Storage::delete($collection);
}
}
}

Expand Down Expand Up @@ -540,22 +548,6 @@ protected function getLocalTempPath($path = null)
return $this->getTempPath() . '/' . $path;
}

/**
* Copy the Storage to local file
*/
protected function copyStorageToLocal($storagePath, $localPath)
{
return FileHelper::put($localPath, Storage::get($storagePath));
}

/**
* Copy the local file to Storage
*/
protected function copyLocalToStorage($localPath, $storagePath)
{
return Storage::put($storagePath, FileHelper::get($localPath), ($this->isPublic()) ? 'public' : null);
}

/**
* Saves a file
* @param string $sourcePath An absolute local path to a file name to read from.
Expand Down Expand Up @@ -597,6 +589,7 @@ protected function putFile($sourcePath, $destinationFileName = null)

/**
* Delete file contents from storage device.
* @return void
*/
protected function deleteFile($fileName = null)
{
Expand All @@ -606,54 +599,109 @@ protected function deleteFile($fileName = null)
$directory = $this->getStorageDirectory() . $this->getPartitionDirectory();
$filePath = $directory . $fileName;

if (Storage::exists($filePath)) {
Storage::delete($filePath);
if ($this->storageCmd('exists', $filePath)) {
$this->storageCmd('delete', $filePath);
}

$this->deleteEmptyDirectory($directory);
}

/**
* Check file exists on storage device.
* @return void
*/
protected function hasFile($fileName = null)
{
$filePath = $this->getStorageDirectory() . $this->getPartitionDirectory() . $fileName;
return Storage::exists($filePath);
return $this->storageCmd('exists', $filePath);
}

/**
* Checks if directory is empty then deletes it,
* three levels up to match the partition directory.
* @return void
*/
protected function deleteEmptyDirectory($dir = null)
{
if (!$this->isDirectoryEmpty($dir))
if (!$this->isDirectoryEmpty($dir)) {
return;
}

Storage::deleteDirectory($dir);
$this->storageCmd('deleteDirectory', $dir);

$dir = dirname($dir);
if (!$this->isDirectoryEmpty($dir))
if (!$this->isDirectoryEmpty($dir)) {
return;
}

Storage::deleteDirectory($dir);
$this->storageCmd('deleteDirectory', $dir);

$dir = dirname($dir);
if (!$this->isDirectoryEmpty($dir))
if (!$this->isDirectoryEmpty($dir)) {
return;
}

Storage::deleteDirectory($dir);
$this->storageCmd('deleteDirectory', $dir);
}

/**
* Returns true if a directory contains no files.
* @return void
*/
protected function isDirectoryEmpty($dir)
{
if (!$dir) return null;
if (!$dir) {
return null;
}

return count($this->storageCmd('allFiles', $dir)) === 0;
}

//
// Storage interface
//

/**
* Calls a method against File or Storage depending on local storage.
* This allows local storage outside the storage/app folder and is
* also good for performance. For local storage, *every* argument
* is prefixed with the local root path. Props to Laravel for
* the unified interface.
* @return mixed
*/
protected function storageCmd()
{
$args = func_get_args();
$command = array_shift($args);

if ($this->isLocalStorage()) {
$interface = 'File';
$path = $this->getLocalRootPath();
$args = array_map(function($value) use ($path) {
return $path . '/' . $value;
}, $args);
}
else {
$interface = 'Storage';
}

return forward_static_call_array([$interface, $command], $args);
}

/**
* Copy the Storage to local file
*/
protected function copyStorageToLocal($storagePath, $localPath)
{
return FileHelper::put($localPath, Storage::get($storagePath));
}

return count(Storage::allFiles($dir)) === 0;
/**
* Copy the local file to Storage
*/
protected function copyLocalToStorage($localPath, $storagePath)
{
return Storage::put($storagePath, FileHelper::get($localPath), ($this->isPublic()) ? 'public' : null);
}

//
Expand Down

0 comments on commit 71ce583

Please sign in to comment.