Skip to content

Commit

Permalink
修复上传文件原始命名保存名称错误问题。
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Nov 19, 2022
1 parent e0cdb86 commit 2d78483
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/Form/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ public function saveFullUrl(bool $value = true)
/**
* 设置类型
*
* 类型包括:blend、image、xls、word、ppt、pdf、code、zip、text、audio、video
* 类型包括:blend、image、xls、word、
* ppt、pdf、code、zip、text、audio、video
* 其中 blend 为全部类型
*
* @param string $type
Expand Down
28 changes: 21 additions & 7 deletions src/MediaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Illuminate\Support\Facades\Storage;

/**
* Class MediaManager.
* 文件管理
*/
class MediaManager
{
Expand Down Expand Up @@ -248,19 +248,22 @@ public function setNametype($type = 'uniqid')
public function getPutFileName($file)
{
switch ($this->nametype) {
// 时间命名
case 'datetime':
return $this->generateDatetimeName($file);
break;

// 原始命名,不覆盖已传同名文件
case 'sequence':
return $this->generateSequenceName($file);
break;

// 原始命名
// 原始命名,将覆盖已传同名文件
case 'original':
return $this->generateClientOriginalName($file);
break;

// 哈希命名
case 'uniqid':
default:
return $this->generateUniqueName($file);
Expand All @@ -269,7 +272,7 @@ public function getPutFileName($file)
}

/**
* 时间文件名
* 时间命名
*/
public function generateDatetimeName($file)
{
Expand All @@ -284,7 +287,7 @@ public function generateDatetimeName($file)
}

/**
* uniqid文件名
* 哈希命名
*/
public function generateUniqueName($file)
{
Expand All @@ -299,7 +302,7 @@ public function generateUniqueName($file)
}

/**
* sequence 命名
* 原始命名,不覆盖已传同名文件
*/
public function generateSequenceName($file)
{
Expand All @@ -308,21 +311,29 @@ public function generateSequenceName($file)
$extension = $file->getClientOriginalExtension();

if (! empty($extension)) {
$nameLen = strlen($original) - strlen($extension) - 1;
$original = substr($original, 0, $nameLen);

$new = sprintf('%s_%s.%s', $original, $index, $extension);
} else {
$new = sprintf('%s_%s', $original, $index);
}

while ($this->storage->exists($this->formatPath($this->path, $new))) {
$index++;
$new = sprintf('%s_%s.%s', $original, $index, $extension);

if (! empty($extension)) {
$new = sprintf('%s_%s.%s', $original, $index, $extension);
} else {
$new = sprintf('%s_%s', $original, $index);
}
}

return $new;
}

/**
* 原始命名
* 原始命名,将覆盖已传同名文件
*/
public function generateClientOriginalName($file)
{
Expand All @@ -333,6 +344,9 @@ public function generateClientOriginalName($file)
return $name;
}

$nameLen = strlen($name) - strlen($extension) - 1;
$name = substr($name, 0, $nameLen);

return $name.'.'.$extension;
}

Expand Down
6 changes: 3 additions & 3 deletions version.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<?php

return [
'1.0.27' => [
'增加原始名称命名方式。',
],
'1.0.28' => [
'修复 Laravel9 版本附件获取全部路径问题。',
],
Expand All @@ -16,4 +13,7 @@
'1.0.32' => [
'修复弹出选择框标题显示问题。',
],
'1.0.33' => [
'修复上传文件原始命名保存名称错误问题。',
],
];

0 comments on commit 2d78483

Please sign in to comment.