Remove class App\Services\Storage

This commit is contained in:
printempw 2017-08-05 15:03:58 +08:00
parent 01a0d58c7d
commit b06051dd91
3 changed files with 16 additions and 179 deletions

View File

@ -5,9 +5,9 @@ namespace App\Http\Controllers;
use Arr;
use Log;
use Utils;
use File;
use Option;
use ZipArchive;
use App\Services\Storage;
use App\Services\OptionForm;
use Illuminate\Http\Request;
@ -135,7 +135,7 @@ class UpdateController extends Controller
Utils::download($release_url, $tmp_path);
} catch (\Exception $e) {
Storage::remove($tmp_path);
File::delete($tmp_path);
exit(trans('admin.update.errors.prefix').$e->getMessage());
}
@ -177,17 +177,19 @@ class UpdateController extends Controller
}
$zip->close();
if (Storage::copyDir($extract_dir, base_path()) !== true) {
try {
File::copyDirectory($extract_dir, base_path());
Storage::removeDir(storage_path('update_cache'));
Log::info("[Extracter] Covering files");
File::deleteDirectory(storage_path('update_cache'));
Log::info("[Extracter] Cleaning cache");
} catch (\Exception $e) {
Log::error("[Extracter] Error occured when covering files", $e);
File::deleteDirectory(storage_path('update_cache'));
exit(trans('admin.update.errors.overwrite'));
} else {
Log::info("[Extracter] Covering files");
Storage::removeDir(storage_path('update_cache'));
Log::info("[Extracter] Cleaning cache");
}
return json(trans('admin.update.complete'), 0);

View File

@ -1,165 +0,0 @@
<?php
namespace App\Services;
class Storage
{
/**
* Read a file and return bin data
*
* @param string $filename
* @return string|bool
*/
public static function get($filename)
{
$result = file_get_contents($filename, 'r');
if (false === $result) {
throw new \Exception("Failed to read $filename.");
}
return $result;
}
public static function put($filename, $data)
{
return file_put_contents($filename, $data);
}
public static function exists($filename)
{
return file_exists($filename);
}
public static function hash($filename, $type = 'sha256')
{
return hash_file('sha256', $filename);
}
public static function rename($fname, $new_fname)
{
if (false === rename($fname, $new_fname)) {
throw new \Exception("Failed to rename $fname to $new_fname.");
}
return $new_fname;
}
public static function size($filename)
{
if (self::exists($filename)) {
return filesize($filename);
} else {
return 0;
}
}
/**
* Remove a file
*
* @param $filename
* @return $bool
*/
public static function remove($filename)
{
if (self::exists($filename)) {
return unlink($filename);
}
}
public static function removeDir($dir)
{
$resource = opendir($dir);
$size = 0;
while($filename = @readdir($resource)) {
if ($filename != "." && $filename != "..") {
$path = "$dir/$filename";
if (is_dir($path)) {
// recursion
self::removeDir($path."/");
} else {
unlink($path);
}
}
}
closedir($resource);
return rmdir($dir);
}
/**
* Recursively count the size of specified directory
*
* @param string $dir
* @return int, total size in bytes
*/
public static function getDirSize($dir)
{
$resource = opendir($dir);
$size = 0;
while($filename = @readdir($resource)) {
if ($filename != "." && $filename != "..") {
$path = "$dir/$filename";
if (is_dir($path)) {
// recursion
$size += self::getDirSize($path);
} else if (is_file($path)) {
$size += filesize($path);
}
}
}
closedir($resource);
return $size;
}
/**
* Recursively count files of specified directory
*
* @param string $dir
* @param $file_num
* @return int, total size in bytes
*/
public static function getFileNum($dir, $file_num = 0)
{
$resource = opendir($dir);
while($filename = readdir($resource)) {
if ($filename != "." && $filename != "..") {
$path = "$dir/$filename";
if (is_dir($path)) {
// recursion
$file_num = self::getFileNum($path, $file_num);
} else {
$file_num++;
}
}
}
closedir($resource);
return $file_num;
}
/**
* Copy directory recursively
*
* @param string $source
* @param string $dest
* @return bool
*/
public static function copyDir($source, $dest)
{
if(!is_dir($source))
return false;
if(!is_dir($dest))
mkdir($dest, 0777, true);
$handle = dir($source);
while($entry = $handle->read()) {
if ($entry != "." && $entry != "..") {
if (is_dir($source.'/'.$entry)) {
// recursion
self::copyDir($source.'/'.$entry, $dest.'/'.$entry);
} else {
@copy($source.'/'.$entry, $dest.'/'.$entry);
}
}
}
return true;
}
}

View File

@ -3,9 +3,9 @@
namespace App\Services;
use Log;
use Storage;
use Carbon\Carbon;
use Illuminate\Support\Str;
use Storage as LaravelStorage;
use App\Exceptions\PrettyPageException;
class Utils
@ -138,8 +138,8 @@ class Utils
if (file_exists($absolute_path)) {
$hash = hash_file('sha256', $absolute_path);
if (!LaravelStorage::disk('textures')->has($hash)) {
LaravelStorage::disk('textures')->move($path, $hash);
if (! Storage::disk('textures')->has($hash)) {
Storage::disk('textures')->move($path, $hash);
} else {
// delete the temp file
unlink($absolute_path);