From d2da13f3ae05116db43e871c4831f8c8a19a0875 Mon Sep 17 00:00:00 2001 From: deatil <2217957370@qq.com> Date: Tue, 8 Mar 2022 23:14:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8E=9F=E5=A7=8B=E5=90=8D?= =?UTF-8?q?=E7=A7=B0=E5=91=BD=E5=90=8D=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/Form/Field.php | 32 +++++++++++-- src/MediaManager.php | 111 +++++++++++++++++++++++++++++++++++++------ version.php | 6 +-- 4 files changed, 127 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index a2c7df3..acdc3dc 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ $form->video('video','视频') ### 参数说明 ``` path(string) : 快速定位目录,默认为根目录 -nametype(string): 文件重命名方式 uniqid|datetime,默认 uniqid +nametype(string): 文件重命名方式 uniqid|datetime|sequence|original,默认 uniqid pageSize(int) : 弹出层列表每页显示数量 limit(int) : 限制条数 remove(boolean) : 是否有删除按钮 diff --git a/src/Form/Field.php b/src/Form/Field.php index e53bae9..e6489db 100644 --- a/src/Form/Field.php +++ b/src/Form/Field.php @@ -232,15 +232,13 @@ public function remove($remove = false) /** * 设置上传保存文件名类型 * - * @param string $type uniqid|datetime + * @param string $type uniqid|datetime|sequence|original * @return $this */ public function nametype($type = 'uniqid') { - if ($type == 'datetime') { - $type = 'datetime'; - } else { - $type = 'uniqid'; + if (! in_array($type, ['uniqid', 'datetime', 'sequence', 'original'])) { + $type = 'original'; } $this->nametype = $type; @@ -271,6 +269,30 @@ public function datetimeName() return $this; } + /** + * sequence 格式命名 + * + * @return $this + */ + public function sequenceName() + { + $this->nametype("sequence"); + + return $this; + } + + /** + * 原始名称命名 + * + * @return $this + */ + public function originalName() + { + $this->nametype("original"); + + return $this; + } + /** * 设置每页数量 * diff --git a/src/MediaManager.php b/src/MediaManager.php index 6533213..d6f449a 100644 --- a/src/MediaManager.php +++ b/src/MediaManager.php @@ -80,6 +80,9 @@ public function setPath($path = '/') return $this; } + /** + * 列出数据 + */ public function ls($type = 'image', $order = 'time') { $files = $this->storage->files($this->path); @@ -121,7 +124,7 @@ public function ls($type = 'image', $order = 'time') } /** - * Get full path for a giving fiel path. + * 获取完整路径 * * @param string $path * @@ -149,6 +152,9 @@ public function download() return response('', 404); } + /** + * 删除 + */ public function delete($path) { $paths = is_array($path) ? $path : func_get_args(); @@ -166,12 +172,17 @@ public function delete($path) return true; } + /** + * 移动 + */ public function move($new) { return $this->storage->move($this->path, $new); } /** + * 上传 + * * @param UploadedFile[] $files * @param string $dir * @@ -180,7 +191,11 @@ public function move($new) public function upload($files = []) { foreach ($files as $file) { - $this->storage->putFileAs($this->path, $file, $this->getPutFileName($file)); + $this->storage->putFileAs( + $this->path, + $file, + $this->getPutFileName($file) + ); } return true; @@ -191,6 +206,10 @@ public function upload($files = []) */ public function checkType($files = [], $type = null) { + if (empty($files)) { + return false; + } + foreach ($files as $file) { $fileExtension = $file->getClientOriginalExtension(); $fileType = $this->detectFileType('file.'.$fileExtension); @@ -202,21 +221,50 @@ public function checkType($files = [], $type = null) return true; } + /** + * 设置命名方式 + */ public function setNametype($type = 'uniqid') { $this->nametype = $type; + return $this; } + /** + * 获取最后的命名 + */ public function getPutFileName($file) { - if ($this->nametype == 'datetime') { - return $this->generateDatetimeName($file); - } else { - return $this->generateUniqueName($file); + switch ($this->nametype) { + case 'datetime': + return $this->generateDatetimeName($file); + break; + + case 'sequence': + return $this->generateSequenceName($file); + break; + + // 原始命名 + case 'original': + return $this->generateClientName($file); + break; + + case 'uniqid': + default: + return $this->generateUniqueName($file); + break; } } - + + /** + * 时间文件名 + */ + public function generateDatetimeName($file) + { + return date('YmdHis').mt_rand(10000, 99999).'.'.$file->getClientOriginalExtension(); + } + /** * uniqid文件名 */ @@ -224,27 +272,51 @@ public function generateUniqueName($file) { return md5(uniqid().microtime()).'.'.$file->getClientOriginalExtension(); } - + /** - * 时间文件名 + * sequence 命名 */ - public function generateDatetimeName($file) + public function generateSequenceName($file) { - return date('YmdHis').mt_rand(10000, 99999).'.'.$file->getClientOriginalExtension(); + $index = 1; + $original = $file->getClientOriginalName(); + $extension = $file->getClientOriginalExtension(); + $new = sprintf('%s_%s.%s', $original, $index, $extension); + + while ($this->storage->exists($this->formatPath($this->path, $new))) { + $index++; + $new = sprintf('%s_%s.%s', $original, $index, $extension); + } + + return $new; } + /** + * 原始命名 + */ + public function generateClientName($file) + { + return $file->getClientOriginalName() . '.' . $file->getClientOriginalExtension(); + } + + /** + * 创建文件夹 + */ public function createFolder($name) { - $path = rtrim($this->path, '/').'/'.trim($name, '/'); + $path = $this->formatPath($this->path, $name); return $this->storage->makeDirectory($path); } - public function exists() + /** + * 是否存在 + */ + public function exists($name) { - $path = $this->getFullPath($this->path); + $path = $this->formatPath($this->path, $name); - return file_exists($path); + return $this->storage->exists($path); } /** @@ -437,4 +509,13 @@ public function getFileChangeTime($file) return ''; } } + + /** + * 格式化路径 + */ + public function formatPath($path, $name) + { + return rtrim($path, '/') . '/' . trim($name, '/'); + } + } diff --git a/version.php b/version.php index 5a3c5aa..782da60 100644 --- a/version.php +++ b/version.php @@ -1,9 +1,6 @@ [ - '优化js翻译使用方式。', - ], '1.0.22' => [ '修复模态表单不能使用问题。', ], @@ -16,4 +13,7 @@ '1.0.26' => [ '修复帮助函数没有适配自定义存储磁盘问题。', ], + '1.0.27' => [ + '增加原始名称命名方式。', + ], ];