This repository was archived by the owner on Aug 22, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 5 files changed +148
-1
lines changed Expand file tree Collapse file tree 5 files changed +148
-1
lines changed Original file line number Diff line number Diff line change 4
4
sftp-config.json
5
5
.subsplit
6
6
.php_cs.cache
7
+ composer.lock
Original file line number Diff line number Diff line change @@ -76,6 +76,45 @@ $ composer require "overtrue/laravel-ueditor:~1.0"
76
76
77
77
> 七牛的 ` access_key ` 和 ` secret_key ` 可以在这里找到:https://portal.qiniu.com/user/key ,在创建 ` bucket ` (空间)的时候,推荐大家都使用公开的空间。
78
78
79
+ # 事件
80
+
81
+ 你肯定有一些朋友肯定会有一些比较特殊的场景,那么你可以使用本插件提供的事件来支持:
82
+
83
+ > 请按照 Laravel 事件的文档来使用:
84
+ > https://laravel.com/docs/5.4/events#registering-events-and-listeners
85
+
86
+ ## 上传中事件
87
+
88
+ > Overtrue\LaravelUEditor\Events\Uploading
89
+
90
+ 在保存文件之前,你可以拿到一些信息:
91
+
92
+ - ` $event->file ` 这是请求的已经上传的文件对象,` Symfony\Component\HttpFoundation\File\UploadedFile ` 实例。
93
+ - ` $event->filename ` 这是即将存储时用的新文件名
94
+ - ` $event->config ` 上传配置,数组。
95
+
96
+ 你可以在本事件监听器返回值,返回值将替换 ` $filename ` 作为存储文件名。
97
+
98
+ ## 上传完成事件
99
+
100
+ > Overtrue\LaravelUEditor\Events\Uploaded
101
+
102
+ 它有两个属性:
103
+
104
+ - ` $event->file ` 与 Uploading 一样,上传的文件
105
+ - ` $event->result ` 上传结构,数组,包含以下信息:
106
+
107
+ ``` php
108
+ 'state' => 'SUCCESS',
109
+ 'url' => 'http://xxxxxx.qiniucdn.com/xxx/xxx.jpg',
110
+ 'title' => '文件名.jpg',
111
+ 'original' => '上传时的源文件名.jpg',
112
+ 'type' => 'jpg',
113
+ 'size' => 17283,
114
+ ```
115
+
116
+ 你可以监听此事件用于一些后续处理任务,比如记录到数据库。
117
+
79
118
# License
80
119
81
120
MIT
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ /*
4
+ * This file is part of the overtrue/laravel-ueditor.
5
+ * (c) overtrue <i@overtrue.me>
6
+ * This source file is subject to the MIT license that is bundled
7
+ * with this source code in the file LICENSE.
8
+ */
9
+
10
+ namespace Overtrue \LaravelUEditor \Events ;
11
+
12
+ use Illuminate \Broadcasting \InteractsWithSockets ;
13
+ use Illuminate \Foundation \Events \Dispatchable ;
14
+ use Illuminate \Queue \SerializesModels ;
15
+ use Symfony \Component \HttpFoundation \File \UploadedFile ;
16
+
17
+ /**
18
+ * Class Uploading.
19
+ *
20
+ * @author overtrue <i@overtrue.me>
21
+ */
22
+ class Uploaded
23
+ {
24
+ use Dispatchable, InteractsWithSockets, SerializesModels;
25
+
26
+ /**
27
+ * @var \Symfony\Component\HttpFoundation\File\UploadedFile
28
+ */
29
+ public $ file ;
30
+
31
+ /**
32
+ * @var array
33
+ */
34
+ public $ result ;
35
+
36
+ /**
37
+ * Uploaded constructor.
38
+ *
39
+ * @param \Symfony\Component\HttpFoundation\File\UploadedFile $file
40
+ * @param array $result
41
+ */
42
+ public function __construct (UploadedFile $ file , array $ result )
43
+ {
44
+ $ this ->file = $ file ;
45
+ $ this ->result = $ result ;
46
+ }
47
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ /*
4
+ * This file is part of the overtrue/laravel-ueditor.
5
+ * (c) overtrue <i@overtrue.me>
6
+ * This source file is subject to the MIT license that is bundled
7
+ * with this source code in the file LICENSE.
8
+ */
9
+
10
+ namespace Overtrue \LaravelUEditor \Events ;
11
+
12
+ use Illuminate \Broadcasting \InteractsWithSockets ;
13
+ use Illuminate \Foundation \Events \Dispatchable ;
14
+ use Illuminate \Queue \SerializesModels ;
15
+ use Symfony \Component \HttpFoundation \File \UploadedFile ;
16
+
17
+ /**
18
+ * Class Uploading.
19
+ *
20
+ * @author overtrue <i@overtrue.me>
21
+ */
22
+ class Uploading
23
+ {
24
+ use Dispatchable, InteractsWithSockets, SerializesModels;
25
+
26
+ /**
27
+ * @var \Symfony\Component\HttpFoundation\File\UploadedFile
28
+ */
29
+ protected $ file ;
30
+
31
+ /**
32
+ * @var string
33
+ */
34
+ protected $ filename ;
35
+
36
+ /**
37
+ * @var array
38
+ */
39
+ protected $ config ;
40
+
41
+ /**
42
+ * Uploading constructor.
43
+ *
44
+ * @param \Symfony\Component\HttpFoundation\File\UploadedFile $file
45
+ * @param $filename
46
+ * @param array $config
47
+ */
48
+ public function __construct (UploadedFile $ file , $ filename , array $ config )
49
+ {
50
+ $ this ->file = $ file ;
51
+ $ this ->filename = $ filename ;
52
+ $ this ->config = $ config ;
53
+ }
54
+ }
Original file line number Diff line number Diff line change 12
12
use Illuminate \Contracts \Filesystem \Filesystem ;
13
13
use Illuminate \Http \Request ;
14
14
use Illuminate \Support \Facades \Storage ;
15
+ use Overtrue \LaravelUEditor \Events \Uploaded ;
16
+ use Overtrue \LaravelUEditor \Events \Uploading ;
15
17
use Symfony \Component \HttpFoundation \File \UploadedFile ;
16
18
17
19
/**
@@ -59,7 +61,9 @@ public function upload(Request $request)
59
61
60
62
$ filename = $ this ->getFilename ($ file , $ config );
61
63
62
- $ this ->store ($ file , $ filename );
64
+ $ modifiedFilename = event (new Uploading ($ file , $ filename , $ config ));
65
+
66
+ $ this ->store ($ file , !is_null ($ modifiedFilename ) ? $ modifiedFilename : $ filename );
63
67
64
68
$ response = [
65
69
'state ' => 'SUCCESS ' ,
@@ -70,6 +74,8 @@ public function upload(Request $request)
70
74
'size ' => $ file ->getSize (),
71
75
];
72
76
77
+ event (new Uploaded ($ file , $ response ));
78
+
73
79
return response ()->json ($ response );
74
80
}
75
81
You can’t perform that action at this time.
0 commit comments