From 33778e7dffa8b1da309f21ad008361357350fd9c Mon Sep 17 00:00:00 2001 From: peinhu Date: Thu, 28 Mar 2019 17:15:55 +0800 Subject: [PATCH] optimize error handling --- README.md | 8 +-- src/Console/BuildRedisHashesCommand.php | 35 ++++++++----- src/Console/CleanUpDirectoryCommand.php | 64 +++++++++++++---------- src/Console/ListGroupDirectoryCommand.php | 53 +++++++++++-------- src/RedisClient.php | 11 ++-- src/RedisSavedPath.php | 46 +++++----------- src/UploadController.php | 4 +- src/Util.php | 9 ++-- 8 files changed, 113 insertions(+), 117 deletions(-) diff --git a/README.md b/README.md index daee3bb..12ebfa4 100644 --- a/README.md +++ b/README.md @@ -93,9 +93,7 @@ `* * * * * php /项目根目录的绝对路径/artisan schedule:run 1>> /dev/null 2>&1` 在`app/Console/Kernel.php`中的`schedule`方法中添加以下代码: ```php - $schedule->call(function () { - \Illuminate\Support\Facades\Artisan::call('aetherupload:clean 2'); - })->daily(); + $schedule->command('aetherupload:clean 2')->daily(); ``` * (推荐)提高头文件读写效率。 通过将头文件的文件系统由本地硬盘改为Redis,提高头文件读写效率。 @@ -118,9 +116,7 @@ `* * * * * php /项目根目录的绝对路径/artisan schedule:run 1>> /dev/null 2>&1` 在`app/Console/Kernel.php`中的`schedule`方法中添加以下代码: ```php - $schedule->call(function () { - \Illuminate\Support\Facades\Artisan::call('aetherupload:build'); - })->daily(); + $schedule->command('aetherupload:build')->daily(); ``` * 提高上传临时文件读写速度(仅对PHP生效)。 diff --git a/src/Console/BuildRedisHashesCommand.php b/src/Console/BuildRedisHashesCommand.php index ba02bf3..3659e7d 100644 --- a/src/Console/BuildRedisHashesCommand.php +++ b/src/Console/BuildRedisHashesCommand.php @@ -37,29 +37,36 @@ public function handle() { $savedPathArr = []; - RedisSavedPath::deleteAll(); + try{ - foreach ( Config::get('aetherupload.groups') as $groupName => $group ) { - $subDirNames = Storage::directories(ConfigMapper::get('root_dir') . DIRECTORY_SEPARATOR . $group['group_dir']); + RedisSavedPath::deleteAll(); - foreach ( $subDirNames as $subDirName ) { - $fileNames = Storage::files($subDirName); + foreach( Config::get('aetherupload.groups') as $groupName => $group ) { + $subDirNames = Storage::directories(ConfigMapper::get('root_dir') . DIRECTORY_SEPARATOR . $group['group_dir']); - foreach ( $fileNames as $fileName ) { - if ( pathinfo($fileName, PATHINFO_EXTENSION) === 'part' ) { - continue; - } + foreach ( $subDirNames as $subDirName ) { + $fileNames = Storage::files($subDirName); + + foreach ( $fileNames as $fileName ) { + if ( pathinfo($fileName, PATHINFO_EXTENSION) === 'part' ) { + continue; + } - $savedPathArr[Util::getSavedPathKey($groupName,pathinfo($fileName, PATHINFO_FILENAME))] = $group['group_dir'] . '_' . basename($subDirName) . '_' . basename($fileName); + $savedPathArr[Util::getSavedPathKey($groupName,pathinfo($fileName, PATHINFO_FILENAME))] = $group['group_dir'] . '_' . basename($subDirName) . '_' . basename($fileName); + } } } - } - RedisSavedPath::setMulti($savedPathArr); + RedisSavedPath::setMulti($savedPathArr); - $this->info(count($savedPathArr) . ' items have been set in Redis.'); - $this->info('Done.'); + $this->info(count($savedPathArr) . ' items have been set in Redis.'); + $this->info('Done.'); + + }catch(\Exception $e){ + + $this->error($e->getMessage()); + } } } diff --git a/src/Console/CleanUpDirectoryCommand.php b/src/Console/CleanUpDirectoryCommand.php index 1ee5ade..8228212 100644 --- a/src/Console/CleanUpDirectoryCommand.php +++ b/src/Console/CleanUpDirectoryCommand.php @@ -38,54 +38,60 @@ public function handle() $dueTime = strtotime('-' . $this->argument('days') . ' day'); $rootDir = ConfigMapper::get('root_dir'); - $headers = Storage::disk(ConfigMapper::get('header_storage_disk'))->files($rootDir . DIRECTORY_SEPARATOR . '_header'); + try { + $headers = Storage::disk(ConfigMapper::get('header_storage_disk'))->files($rootDir . DIRECTORY_SEPARATOR . '_header'); - foreach ( $headers as $header ) { + foreach ( $headers as $header ) { - if ( pathinfo($header, PATHINFO_EXTENSION) !== '' ) { - continue; - } + if ( pathinfo($header, PATHINFO_EXTENSION) !== '' ) { + continue; + } - $createTime = substr(basename($header), 0, 10); + $createTime = substr(basename($header), 0, 10); - if ( $createTime < $dueTime ) { - $invalidHeaders[] = $header; + if ( $createTime < $dueTime ) { + $invalidHeaders[] = $header; + } } - } - Storage::disk(ConfigMapper::get('header_storage_disk'))->delete($invalidHeaders); + Storage::disk(ConfigMapper::get('header_storage_disk'))->delete($invalidHeaders); - $this->info(count($invalidHeaders) . ' invalid headers have been deleted.'); + $this->info(count($invalidHeaders) . ' invalid headers have been deleted.'); - $groupDirs = array_map(function ($v) { - return $v['group_dir']; - }, Config::get('aetherupload.groups')); + $groupDirs = array_map(function ($v) { + return $v['group_dir']; + }, Config::get('aetherupload.groups')); - foreach ( $groupDirs as $groupDir ) { - $subDirNames = Storage::directories($rootDir . DIRECTORY_SEPARATOR . $groupDir); + foreach ( $groupDirs as $groupDir ) { + $subDirNames = Storage::directories($rootDir . DIRECTORY_SEPARATOR . $groupDir); - foreach ( $subDirNames as $subDirName ) { - $files = Storage::files($subDirName); + foreach ( $subDirNames as $subDirName ) { + $files = Storage::files($subDirName); - foreach ( $files as $file ) { + foreach ( $files as $file ) { - if ( pathinfo($file, PATHINFO_EXTENSION) !== 'part' ) { - continue; - } + if ( pathinfo($file, PATHINFO_EXTENSION) !== 'part' ) { + continue; + } - $createTime = substr($fileName = basename($file, '.part'), 0, 10); + $createTime = substr($fileName = basename($file, '.part'), 0, 10); - if ( $createTime < $dueTime ) { - $invalidFiles[] = $file; + if ( $createTime < $dueTime ) { + $invalidFiles[] = $file; + } } } } - } - Storage::delete($invalidFiles); + Storage::delete($invalidFiles); + + $this->info(count($invalidFiles) . ' invalid files have been deleted.'); + $this->info('Done.'); - $this->info(count($invalidFiles) . ' invalid files have been deleted.'); - $this->info('Done.'); + }catch(\Exception $e){ + + $this->error($e->getMessage()); + } } } diff --git a/src/Console/ListGroupDirectoryCommand.php b/src/Console/ListGroupDirectoryCommand.php index 626ffaf..91b4d3f 100644 --- a/src/Console/ListGroupDirectoryCommand.php +++ b/src/Console/ListGroupDirectoryCommand.php @@ -36,38 +36,47 @@ public function handle() { $rootDir = Config::get('aetherupload.root_dir'); - if ( ! Storage::exists($rootDir) ) { - Storage::makeDirectory($rootDir . DIRECTORY_SEPARATOR . '_header'); - $this->info('Root directory "' . $rootDir . '" has been created.'); - } + try { + + if ( ! Storage::exists($rootDir) ) { + Storage::makeDirectory($rootDir . DIRECTORY_SEPARATOR . '_header'); + $this->info('Root directory "' . $rootDir . '" has been created.'); + } - $directories = array_map(function ($directory) { - return basename($directory); - }, Storage::directories($rootDir)); + $directories = array_map(function ($directory) { + return basename($directory); + }, Storage::directories($rootDir)); - $groupDirs = array_map(function ($v) { - return $v['group_dir']; - }, Config::get('aetherupload.groups')); + $groupDirs = array_map(function ($v) { + return $v['group_dir']; + }, Config::get('aetherupload.groups')); - foreach ( $groupDirs as $groupDir ) { - if ( in_array($groupDir, $directories) ) { - continue; - } else { - if ( Storage::makeDirectory($rootDir . DIRECTORY_SEPARATOR . $groupDir) ) { - $this->info('Directory "' . $rootDir . DIRECTORY_SEPARATOR . $groupDir . '" has been created.'); + foreach ( $groupDirs as $groupDir ) { + if ( in_array($groupDir, $directories) ) { + continue; } else { - $this->error('Fail to create directory "' . $rootDir . DIRECTORY_SEPARATOR . $groupDir . '".'); + if ( Storage::makeDirectory($rootDir . DIRECTORY_SEPARATOR . $groupDir) ) { + $this->info('Directory "' . $rootDir . DIRECTORY_SEPARATOR . $groupDir . '" has been created.'); + } else { + $this->error('Fail to create directory "' . $rootDir . DIRECTORY_SEPARATOR . $groupDir . '".'); + } } } - } - $this->info('Group-Directory List:'); + $this->info('Group-Directory List:'); - foreach ( Config::get('aetherupload.groups') as $groupName => $groupArr ) { - if ( Storage::exists($rootDir . DIRECTORY_SEPARATOR . $groupArr['group_dir']) ) { - $this->info($groupName . '-' . $groupArr['group_dir']); + foreach ( Config::get('aetherupload.groups') as $groupName => $groupArr ) { + if ( Storage::exists($rootDir . DIRECTORY_SEPARATOR . $groupArr['group_dir']) ) { + $this->info($groupName . '-' . $groupArr['group_dir']); + } } + + }catch(\Exception $e){ + + $this->error($e->getMessage()); } + + } } diff --git a/src/RedisClient.php b/src/RedisClient.php index 3282276..b6a153a 100644 --- a/src/RedisClient.php +++ b/src/RedisClient.php @@ -34,21 +34,22 @@ public function delete($field) { $result = Redis::hdel('aetherupload_header', $field); - if ( $result !== 1 ) { + if ( $result === 0 ) { throw new \Exception('delete error'); } } - public function exists($field) { $result = Redis::hexists('aetherupload_header', $field); - if ( $result !== 1 && $result !== 0 ) { + if ( $result === 1 ) { + return true; + } elseif ( $result === 0 ) { + return false; + } else { throw new \Exception('exists error'); } - - return $result; } public function listContents($directory) diff --git a/src/RedisSavedPath.php b/src/RedisSavedPath.php index 2316fa0..0d0581b 100644 --- a/src/RedisSavedPath.php +++ b/src/RedisSavedPath.php @@ -7,42 +7,30 @@ class RedisSavedPath { - /** - * @param $key - * @return bool - */ public static function exists($key) { $result = Redis::hexists('aetherupload_resource', $key); - if ( $result !== 1 && $result !== 0 ) { + if ( $result === 1 ) { + return true; + } elseif ( $result === 0 ) { + return false; + } else { throw new \Exception('exists error'); } - - return $result; } - /** - * @param $key - * @return string - */ public static function get($key) { $result = Redis::hget('aetherupload_resource', $key); - if ( $result === null) { + if ( $result === null ) { throw new \Exception('read error'); } return $result; } - /** - * set or overwrite a hash - * @param $key - * @param $savedPath - * @return bool - */ public static function set($key, $savedPath) { $result = Redis::hset('aetherupload_resource', $key, $savedPath); @@ -51,40 +39,32 @@ public static function set($key, $savedPath) throw new \Exception('write error'); } - return $result; + return true; } - /** - * @param $keyArr - * @return bool - */ public static function setMulti($keyArr) { - $result = Redis::hmset('aetherupload_resource', $keyArr); + Redis::hmset('aetherupload_resource', $keyArr); - return $result; + return true; } - /** - * @param $key - * @return bool - */ public static function delete($key) { $result = Redis::hdel('aetherupload_resource', $key); - if ( $result !== 1 ) { + if ( $result === 0 ) { throw new \Exception('delete error'); } - return $result; + return true; } public static function deleteAll() { - $result = Redis::del('aetherupload_resource'); + Redis::del('aetherupload_resource'); - return $result; + return true; } diff --git a/src/UploadController.php b/src/UploadController.php index 7c0db1b..2270af4 100644 --- a/src/UploadController.php +++ b/src/UploadController.php @@ -62,7 +62,7 @@ public function preprocess() $partialResource->filterByExtension($resourceExt); // determine if this upload meets the condition of instant completion - if ( $resourceHash !== false && ConfigMapper::get('instant_completion') === true && RedisSavedPath::exists($savedPathKey = Util::getSavedPathKey($group, $resourceHash)) ) { + if ( $resourceHash !== false && ConfigMapper::get('instant_completion') === true && RedisSavedPath::exists($savedPathKey = Util::getSavedPathKey($group, $resourceHash)) === true ) { $result['savedPath'] = RedisSavedPath::get($savedPathKey); return Responser::returnResult($result); @@ -119,7 +119,7 @@ public function saveChunk() } // determine if this upload meets the condition of instant completion - if ( $resourceHash !== false && ConfigMapper::get('instant_completion') === true && RedisSavedPath::exists($savedPathKey) ) { + if ( $resourceHash !== false && ConfigMapper::get('instant_completion') === true && RedisSavedPath::exists($savedPathKey) === true ) { $partialResource->delete(); unset($partialResource->chunkIndex); $result['savedPath'] = RedisSavedPath::get($savedPathKey); diff --git a/src/Util.php b/src/Util.php index ee05dc1..2e26a0c 100644 --- a/src/Util.php +++ b/src/Util.php @@ -87,12 +87,11 @@ public static function deleteResource($savedPath) try { ConfigMapper::instance()->applyGroupConfig($group); $resource = new Resource($group, $groupSubDir, $name); - $resource->delete($resource->path); + + return $resource->delete($resource->path); }catch (\Exception $e){ return false; } - - return true; } public static function deleteRedisSavedPath($savedPath) @@ -101,12 +100,10 @@ public static function deleteRedisSavedPath($savedPath) $savedPathKey = $savedPathArr[0].'_'.pathinfo($savedPathArr[2],PATHINFO_FILENAME); try { - RedisSavedPath::delete($savedPathKey); + return RedisSavedPath::delete($savedPathKey); }catch (\Exception $e){ return false; } - - return true; }