Skip to content

Commit eb4b37c

Browse files
committed
v0.4 uploads表多态化。 加入了upload_model配置,可更灵活的选择Eloquent类。
1 parent 8461d58 commit eb4b37c

File tree

5 files changed

+79
-9
lines changed

5 files changed

+79
-9
lines changed

config/upload.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,31 @@
77
*/
88

99
return [
10-
'base_storage_disk' => 'local', // 请在filesystems.php 的 disks 数组里挑一个
10+
11+
/**
12+
* 请在filesystems.php 的 disks 数组里挑一个
13+
**/
14+
'base_storage_disk' => 'local',
15+
16+
/**
17+
* 对应的数据模型类
18+
**/
19+
'upload_model' => App\Upload::class,
20+
21+
/**
22+
* 上传策略类
23+
**/
1124
'upload_strategy' => zgldh\UploadManager\UploadStrategy::class,
25+
26+
/**
27+
* validator group 用于 withValidator()函数 common是默认的。
28+
**/
1229
'validator_groups' => [
13-
// validator group 用于 withValidator()函数 common是默认的。
1430
'common' => [
15-
//validators
16-
'min' => 0, //kilobytes 请参考 http://laravel.com/docs/5.1/validation
31+
/**
32+
* 请参考 http://laravel.com/docs/5.1/validation
33+
**/
34+
'min' => 0, //kilobytes
1735
'max' => 4096, //kilobytes
1836
],
1937
'image' => [
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class AddPolymorphicColumnsToUploads extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::table('uploads', function (Blueprint $table) {
16+
$table->integer('uploadable_id')->nullable();
17+
$table->string('uploadable_type')->nullable();
18+
19+
$table->index([
20+
'uploadable_id',
21+
'uploadable_type'
22+
],'uploadable_index');
23+
});
24+
}
25+
26+
/**
27+
* Reverse the migrations.
28+
*
29+
* @return void
30+
*/
31+
public function down()
32+
{
33+
Schema::table('uploads', function (Blueprint $table) {
34+
$table->dropColumn('uploadable_id');
35+
$table->dropColumn('uploadable_type');
36+
37+
$table->dropIndex('uploadable_index');
38+
});
39+
}
40+
}

model/Upload.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public function user()
2121
{
2222
return $this->belongsTo('App\User', 'user_id', 'id');
2323
}
24+
25+
public function uploadable()
26+
{
27+
return $this->morphTo();
28+
}
2429

2530
public function getUrlAttribute()
2631
{

src/UploadManager.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php namespace zgldh\UploadManager;
22

3-
use App\Upload;
43
use Illuminate\Support\Str;
54
use Symfony\Component\HttpFoundation\File\UploadedFile;
65

@@ -116,6 +115,13 @@ private function coreUpload($upload, $uploadedFilePath, $file, $preCallback)
116115
return $upload;
117116
}
118117

118+
private function newUploadModel()
119+
{
120+
$modelClassName = config('upload.upload_model');
121+
$model = app($modelClassName);
122+
return $model;
123+
}
124+
119125
/**
120126
* 保存上传文件,生成上传对象
121127
* @param $file
@@ -128,7 +134,7 @@ public function upload($file, $preCallback = null)
128134
return $this->uploadByUrl($file, $preCallback);
129135
}
130136

131-
$upload = new Upload();
137+
$upload = $this->newUploadModel();
132138
$upload->disk = \Config::get('upload.base_storage_disk');
133139

134140
$uploadedFilePath = $file->getPathname();
@@ -145,7 +151,7 @@ public function upload($file, $preCallback = null)
145151
*/
146152
public function uploadByUrl($url, $preCallback = null)
147153
{
148-
$upload = new Upload();
154+
$upload = $this->newUploadModel();
149155
$upload->disk = \Config::get('upload.base_storage_disk');
150156

151157
$uploadedFilePath = $url;

src/UploadManagerServiceProvider.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ public function boot()
3737
]);
3838

3939

40-
if (class_exists('\App\Upload')) {
41-
\App\Upload::deleted(
40+
$modelClassName = config('upload.upload_model');
41+
if (class_exists($modelClassName)) {
42+
$modelClassName::deleted(
4243
function ($upload) {
4344
//
4445
$upload->deleteFile();

0 commit comments

Comments
 (0)