Skip to content

Commit 1b9fe83

Browse files
authored
Merge pull request #2 from edwinhuish/main
local 文件路径支持用闭包
2 parents 1e78482 + f7e3cc1 commit 1b9fe83

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

src/Adapter/LocalAdapter.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace Tinywan\Storage\Adapter;
1111

12+
use Tinywan\Storage\Exception\StorageException;
13+
1214
class LocalAdapter extends AdapterAbstract
1315
{
1416
/**
@@ -20,19 +22,28 @@ public function uploadFile(array $options = []): array
2022
{
2123
$result = [];
2224
$config = config('plugin.tinywan.storage.app.storage.local');
23-
if (is_callable($config['root'])) {
24-
$config['root'] = (string) $config['root']();
25+
$dirname = $config['dirname'];
26+
if (is_callable($dirname)) {
27+
$dirname = (string) $dirname() ?: '';
28+
}
29+
if (!empty($dirname)){
30+
$dirname = DIRECTORY_SEPARATOR.ltrim($dirname, DIRECTORY_SEPARATOR); // 避免没有加前置 “/”
31+
}
32+
$basePath = $config['root'].$dirname.DIRECTORY_SEPARATOR;
33+
if (!$this->createDir($basePath)) {
34+
throw new StorageException('文件夹创建失败,请核查是否有对应权限。');
2535
}
36+
$baseUrl = $config['domain'].$config['uri'].str_replace(DIRECTORY_SEPARATOR, '/', $dirname).'/';
2637
foreach ($this->files as $key => $file) {
2738
$uniqueId = hash_file('sha1', $file->getPathname());
2839
$saveFilename = $uniqueId.'.'.$file->getUploadExtension();
29-
$savePath = $config['root'].$this->dirSeparator.$saveFilename;
40+
$savePath = $basePath.$saveFilename;
3041
$temp = [
3142
'key' => $key,
3243
'origin_name' => $file->getUploadName(),
3344
'save_name' => $saveFilename,
3445
'save_path' => $savePath,
35-
'url' => $config['domain'].$config['dirname'].$this->dirSeparator.$saveFilename,
46+
'url' => $baseUrl.$saveFilename,
3647
'unique_id' => $uniqueId,
3748
'size' => $file->getSize(),
3849
'mime_type' => $file->getUploadMineType(),
@@ -44,4 +55,25 @@ public function uploadFile(array $options = []): array
4455

4556
return $result;
4657
}
58+
59+
60+
protected function createDir(string $path): bool
61+
{
62+
// 判断传过来的$path是否已是目录,若是,则直接返回true
63+
if (is_dir($path)) {
64+
return true;
65+
}
66+
67+
// 走到这步,说明传过来的$path不是目录
68+
// 判断其上级是否为目录,是,则直接创建$path目录
69+
if (is_dir(dirname($path))) {
70+
return mkdir($path);
71+
}
72+
73+
// 走到这说明其上级目录也不是目录,则继续判断其上上...级目录
74+
$this->createDir(dirname($path));
75+
76+
// 走到这步,说明上级目录已创建成功,则直接接着创建当前目录,并把创建的结果返回
77+
return mkdir($path);
78+
}
4779
}

src/config/plugin/tinywan/storage/app.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@
1919
'local' => [
2020
'adapter' => \Tinywan\Storage\Adapter\LocalAdapter::class,
2121
'root' => runtime_path().'/storage',
22-
'dirname' => '/storage',
22+
// 'dirname' => 'subdir',
23+
'dirname' => function(){
24+
return date('Y-m');
25+
},
2326
'domain' => 'http://127.0.0.1:8787',
27+
'uri'=> '/runtime', // 如果 domain + uri 不在 public 目录下,请做好软链接,否则生成的url无法访问
2428
],
2529
// 阿里云对象存储
2630
'oss' => [

0 commit comments

Comments
 (0)