Skip to content

Commit

Permalink
feat: 更新获取文件元数据信息的方式
Browse files Browse the repository at this point in the history
  • Loading branch information
mouyong committed Mar 14, 2024
1 parent 917003b commit 59e012b
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 107 deletions.
2 changes: 2 additions & 0 deletions FileStorage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ $fileInfo = $resp->getData();
```php
$fileId = 1;
$filepath = null;
$temporary = false;

$resp = \FresnsCmdWord::plugin('FileStorage')->getFileInfo([
'fileId' => $fileId,
'filepath' => $filepath,
'temporary' => false,
]);

$fileinfo = $resp->getData('fileinfo');
Expand Down
215 changes: 116 additions & 99 deletions FileStorage/app/Models/Traits/FileServiceTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,107 +4,124 @@

use Plugins\FileStorage\Models\File;
use Plugins\FileStorage\Models\FileUsage;
use Plugins\FileStorage\Utilities\FileUtility;
use Plugins\FileStorage\Utilities\CosUtility;
use Plugins\FileStorage\Utilities\OssUtility;

/**
* @mixin File
*/

trait FileServiceTrait
{
public function addFileUsageRecord(array $fileInfo)
{
$file = $this;

// 必须外部传入的参数
$usageType = $fileInfo['usage_type'];
$tableName = $fileInfo['table_name'];
$tableColumn = $fileInfo['table_column'];
$tableId = $fileInfo['table_id'];
$tableValue = $fileInfo['table_value'] ?? null;

$params = [
'file_id' => $file['id'],
'file_type' => $file['type'],
'usage_type' => $usageType,
'table_name' => $tableName,
'table_column' => $tableColumn,
'table_id' => $tableId,
'table_value' => $tableValue,
];

$data = $params + [
'rating' => $fileInfo['rating'] ?? 0,
'remark' => $fileInfo['remark'] ?? null,
];

$usage = FileUsage::updateOrCreate($params, $data);

return $usage;
}

public function getFileInfo()
{
$file = $this;

$fileInfo['id'] = $file['id'];
$fileInfo['fid'] = $file['fid'];
$fileInfo['type'] = $file['type'];
$fileInfo['name'] = $file['name'];
$fileInfo['mime'] = $file['mime'];
$fileInfo['extension'] = $file['extension'];
$fileInfo['size'] = $file['size'];
$fileInfo['md5'] = $file['md5'];
$fileInfo['sha'] = $file['sha'];
$fileInfo['sha_type'] = $file['sha_type'];
$fileInfo['path'] = $file['path'];
$fileInfo['url'] = null;
switch ($fileInfo['type']) {
case File::TYPE_IMAGE:
$fileInfo['image_width'] = $file['image_width'];
$fileInfo['image_height'] = $file['image_height'];
break;

case File::TYPE_IMAGE:
$fileInfo['audio_time'] = $file['audio_time'];
break;

case File::TYPE_IMAGE:
$fileInfo['video_time'] = $file['video_time'];
$fileInfo['video_poster_path'] = $file['video_poster_path'];
break;
}
$fileInfo['more_json'] = $file['more_json'];
$fileInfo['transcoding_state'] = $file['transcoding_state'];
$fileInfo['transcoding_reason'] = $file['transcoding_reason'];
$fileInfo['original_path'] = $file['original_path'];
$fileInfo['is_physical_delete'] = $file['is_physical_delete'];

$fileInfo['usages'] = [];
foreach ($file['usages'] as $usage) {
$usageInfo['usage_type'] = $usage['usage_type'];
$usageInfo['table_name'] = $usage['table_name'];
$usageInfo['table_column'] = $usage['table_column'];
$usageInfo['table_id'] = $usage['table_id'];
$usageInfo['table_value'] = $usage['table_value'];
$usageInfo['rating'] = $usage['rating'];
$usageInfo['remark'] = $usage['remark'];

$fileInfo['usages'][] = $usageInfo;
}

return $fileInfo;
}

public function getFileInfoWithUsages()
{
$fileInfo = $this->getFileInfo();

$usages = [];
foreach ($fileInfo['usages'] as $usage) {
$fileInfoData = collect($fileInfo)->except('usages')->all();
$usages[] = array_merge($fileInfoData, $usage);
}

return $usages;
}
}
trait FileServiceTrait
{
public function addFileUsageRecord(array $fileInfo)
{
$file = $this;

// 必须外部传入的参数
$usageType = $fileInfo['usage_type'];
$tableName = $fileInfo['table_name'];
$tableColumn = $fileInfo['table_column'];
$tableId = $fileInfo['table_id'];
$tableValue = $fileInfo['table_value'] ?? null;

$params = [
'file_id' => $file['id'],
'file_type' => $file['type'],
'usage_type' => $usageType,
'table_name' => $tableName,
'table_column' => $tableColumn,
'table_id' => $tableId,
'table_value' => $tableValue,
];

$data = $params + [
'rating' => $fileInfo['rating'] ?? 0,
'remark' => $fileInfo['remark'] ?? null,
];

$usage = FileUsage::updateOrCreate($params, $data);

return $usage;
}

public function getFileInfo($temporary = false)
{
$file = $this;

$fileInfo['id'] = $file['id'];
$fileInfo['fid'] = $file['fid'];
$fileInfo['type'] = $file['type'];
$fileInfo['name'] = $file['name'];
$fileInfo['mime'] = $file['mime'];
$fileInfo['extension'] = $file['extension'];
$fileInfo['size'] = $file['size'];
$fileInfo['md5'] = $file['md5'];
$fileInfo['sha'] = $file['sha'];
$fileInfo['sha_type'] = $file['sha_type'];
$fileInfo['path'] = $file['path'];
$fileInfo['url'] = null;
switch ($fileInfo['type']) {
case File::TYPE_IMAGE:
$fileInfo['image_width'] = $file['image_width'];
$fileInfo['image_height'] = $file['image_height'];
break;

case File::TYPE_IMAGE:
$fileInfo['audio_time'] = $file['audio_time'];
break;

case File::TYPE_IMAGE:
$fileInfo['video_time'] = $file['video_time'];
$fileInfo['video_poster_path'] = $file['video_poster_path'];
break;
}
$fileInfo['more_json'] = $file['more_json'];
$fileInfo['transcoding_state'] = $file['transcoding_state'];
$fileInfo['transcoding_reason'] = $file['transcoding_reason'];
$fileInfo['original_path'] = $file['original_path'];
$fileInfo['is_physical_delete'] = $file['is_physical_delete'];

$fileInfo['usages'] = [];
foreach ($file['usages'] as $usage) {
$usageInfo['usage_type'] = $usage['usage_type'];
$usageInfo['table_name'] = $usage['table_name'];
$usageInfo['table_column'] = $usage['table_column'];
$usageInfo['table_id'] = $usage['table_id'];
$usageInfo['table_value'] = $usage['table_value'];
$usageInfo['rating'] = $usage['rating'];
$usageInfo['remark'] = $usage['remark'];

$fileInfo['usages'][] = $usageInfo;
}


// 获取 URL
if ($temporary == false) {
$url = FileUtility::getStorage()->url($fileInfo['path']);
} else {
$expiresMinutes = match ($driver) {
default => now()->addMinutes(20),
CosUtility::DISK_KEY => now()->addMinutes(20),
OssUtility::DISK_KEY => 20 * 60,
};
$url = FileUtility::getStorage()->temporaryUrl($fileInfo['path'], $expiresMinutes);
}
$fileInfo['url'] = $url;

return $fileInfo;
}

public function getFileInfoWithUsages()
{
$fileInfo = $this->getFileInfo();

$usages = [];
foreach ($fileInfo['usages'] as $usage) {
$fileInfoData = collect($fileInfo)->except('usages')->all();
$usages[] = array_merge($fileInfoData, $usage);
}

return $usages;
}
}
3 changes: 2 additions & 1 deletion FileStorage/app/Services/CmdWordService.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ public function getFileInfo(array $wordBody)
{
$fileId = $wordBody['fileId'];
$filepath = $wordBody['filepath'];
$temporary = $wordBody['temporary'] ?? false;

$fileInfo = FileUtility::getFileInfo($fileId, $filepath);
$fileInfo = FileUtility::getFileInfo($fileId, $filepath, $temporary);

return $this->success([
'fileinfo' => $fileInfo,
Expand Down
18 changes: 11 additions & 7 deletions FileStorage/app/Utilities/FileUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,14 +355,18 @@ public static function create($params): File
return $file;
}

public static function getFileInfo(?string $fileId = null, ?string $filepath = null)
public static function getFileInfo(?string $fileId = null, ?string $filepath = null, $temporary = false)
{
if (!$fileId && !$filepath) {
return null;
}

if ($filepath && filter_var($filepath, FILTER_VALIDATE_URL)) {
return $filepath;
$fileInfo = [];
$fileInfo['name'] = pathinfo($filepath, PATHINFO_BASENAME);
$fileInfo['path'] = parse_url($filepath, PATHINFO_BASENAME)['path'] ?? null;
$fileInfo['url'] = $filepath;
return $fileInfo;
}

if ($fileId) {
Expand All @@ -377,7 +381,7 @@ public static function getFileInfo(?string $fileId = null, ?string $filepath = n
return null;
}

$fileInfo = $file->getFileInfo();
$fileInfo = $file->getFileInfo($temporary);

return $fileInfo;
}
Expand All @@ -389,8 +393,8 @@ public static function getFileUrl(?string $fileId = null, ?string $filepath = nu
return null;
}

if ($fileInfo && filter_var($fileInfo, FILTER_VALIDATE_URL)) {
$url = $fileInfo;
if (empty($fileInfo['fid'])) {
$url = $fileInfo['url'];
} else {
$url = FileUtility::getStorage()->url($fileInfo['path']);
}
Expand All @@ -405,8 +409,8 @@ public static function getFileTemporaryUrl(?string $fileId = null, ?string $file
return null;
}

if ($fileInfo && filter_var($fileInfo, FILTER_VALIDATE_URL)) {
$url = $fileInfo;
if (empty($fileInfo['fid'])) {
$url = $fileInfo['url'];
} else {
$driver = FileUtility::getFileStorageDriver();

Expand Down

0 comments on commit 59e012b

Please sign in to comment.