Skip to content

Commit

Permalink
增加原始名称命名方式
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Mar 8, 2022
1 parent 94cf378 commit d2da13f
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) : 是否有删除按钮
Expand Down
32 changes: 27 additions & 5 deletions src/Form/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
* 设置每页数量
*
Expand Down
111 changes: 96 additions & 15 deletions src/MediaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public function setPath($path = '/')
return $this;
}

/**
* 列出数据
*/
public function ls($type = 'image', $order = 'time')
{
$files = $this->storage->files($this->path);
Expand Down Expand Up @@ -121,7 +124,7 @@ public function ls($type = 'image', $order = 'time')
}

/**
* Get full path for a giving fiel path.
* 获取完整路径
*
* @param string $path
*
Expand Down Expand Up @@ -149,6 +152,9 @@ public function download()
return response('', 404);
}

/**
* 删除
*/
public function delete($path)
{
$paths = is_array($path) ? $path : func_get_args();
Expand All @@ -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
*
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -202,49 +221,102 @@ 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文件名
*/
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);
}

/**
Expand Down Expand Up @@ -437,4 +509,13 @@ public function getFileChangeTime($file)
return '';
}
}

/**
* 格式化路径
*/
public function formatPath($path, $name)
{
return rtrim($path, '/') . '/' . trim($name, '/');
}

}
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.21' => [
'优化js翻译使用方式。',
],
'1.0.22' => [
'修复模态表单不能使用问题。',
],
Expand All @@ -16,4 +13,7 @@
'1.0.26' => [
'修复帮助函数没有适配自定义存储磁盘问题。',
],
'1.0.27' => [
'增加原始名称命名方式。',
],
];

0 comments on commit d2da13f

Please sign in to comment.