Skip to content

Commit

Permalink
optimize error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
peinhu committed Mar 28, 2019
1 parent 64b07d0 commit 33778e7
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 117 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,提高头文件读写效率。
Expand All @@ -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生效)。
Expand Down
35 changes: 21 additions & 14 deletions src/Console/BuildRedisHashesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}
}
64 changes: 35 additions & 29 deletions src/Console/CleanUpDirectoryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}
}
53 changes: 31 additions & 22 deletions src/Console/ListGroupDirectoryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}



}
}
11 changes: 6 additions & 5 deletions src/RedisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
46 changes: 13 additions & 33 deletions src/RedisSavedPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}


Expand Down
4 changes: 2 additions & 2 deletions src/UploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 33778e7

Please sign in to comment.