Skip to content
This repository was archived by the owner on Aug 22, 2021. It is now read-only.

Commit 21d1ce7

Browse files
committed
Add events.
1 parent b80f739 commit 21d1ce7

File tree

5 files changed

+148
-1
lines changed

5 files changed

+148
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
sftp-config.json
55
.subsplit
66
.php_cs.cache
7+
composer.lock

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,45 @@ $ composer require "overtrue/laravel-ueditor:~1.0"
7676

7777
> 七牛的 `access_key``secret_key` 可以在这里找到:https://portal.qiniu.com/user/key ,在创建 `bucket` (空间)的时候,推荐大家都使用公开的空间。
7878
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+
79118
# License
80119

81120
MIT

src/Events/Uploaded.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
}

src/Events/Uploading.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
}

src/StorageManager.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Illuminate\Contracts\Filesystem\Filesystem;
1313
use Illuminate\Http\Request;
1414
use Illuminate\Support\Facades\Storage;
15+
use Overtrue\LaravelUEditor\Events\Uploaded;
16+
use Overtrue\LaravelUEditor\Events\Uploading;
1517
use Symfony\Component\HttpFoundation\File\UploadedFile;
1618

1719
/**
@@ -59,7 +61,9 @@ public function upload(Request $request)
5961

6062
$filename = $this->getFilename($file, $config);
6163

62-
$this->store($file, $filename);
64+
$modifiedFilename = event(new Uploading($file, $filename, $config));
65+
66+
$this->store($file, !is_null($modifiedFilename) ? $modifiedFilename : $filename);
6367

6468
$response = [
6569
'state' => 'SUCCESS',
@@ -70,6 +74,8 @@ public function upload(Request $request)
7074
'size' => $file->getSize(),
7175
];
7276

77+
event(new Uploaded($file, $response));
78+
7379
return response()->json($response);
7480
}
7581

0 commit comments

Comments
 (0)