Skip to content

Commit

Permalink
Allow uploading file to Aliyun OSS (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
baijunyao committed Jun 20, 2020
1 parent 9bdbf82 commit 86fda81
Show file tree
Hide file tree
Showing 17 changed files with 323 additions and 27 deletions.
61 changes: 61 additions & 0 deletions app/Console/Commands/Upgrade/V11_0_0.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Console\Commands\Upgrade;

use Artisan;
use DB;
use File;
use Illuminate\Console\Command;

Expand All @@ -29,5 +30,65 @@ public function handle()

Artisan::call('storage:link --relative');
}

$updatedConfigs = [
160 => 'filesystems.disks.oss_backups.access_key',
161 => 'filesystems.disks.oss_backups.secret_key',
162 => 'filesystems.disks.oss_backups.bucket',
163 => 'filesystems.disks.oss_backups.endpoint',
];

foreach ($updatedConfigs as $id => $name) {
DB::table('configs')->where('id', $id)->update([
'name' => $name,
]);
}

DB::table('configs')->where('id', 164)->update([
'value' => str_replace('"oss"', '"oss_backups"', DB::table('configs')->where('id', 164)->value('value'))
]);

DB::table('configs')->insertOrIgnore([
[
'id' => 200,
'name' => 'filesystems.disks.oss_uploads.access_key',
'value' => '',
'created_at' => '2020-06-26 23:29:52',
'updated_at' => '2020-06-26 23:29:52',
'deleted_at' => null,
],
[
'id' => 201,
'name' => 'filesystems.disks.oss_uploads.secret_key',
'value' => '',
'created_at' => '2020-06-26 23:29:52',
'updated_at' => '2020-06-26 23:29:52',
'deleted_at' => null,
],
[
'id' => 202,
'name' => 'filesystems.disks.oss_uploads.bucket',
'value' => '',
'created_at' => '2020-06-26 23:29:52',
'updated_at' => '2020-06-26 23:29:52',
'deleted_at' => null,
],
[
'id' => 203,
'name' => 'filesystems.disks.oss_uploads.endpoint',
'value' => '',
'created_at' => '2020-06-26 23:29:52',
'updated_at' => '2020-06-26 23:29:52',
'deleted_at' => null,
],
[
'id' => 204,
'name' => 'bjyblog.upload_disks',
'value' => '["public"]',
'created_at' => '2018-12-04 22:29:52',
'updated_at' => '2018-12-04 22:29:52',
'deleted_at' => null,
],
]);
}
}
14 changes: 11 additions & 3 deletions app/Http/Controllers/Admin/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ public function create()

public function uploadImage(Request $request)
{
$imagePath = $request->file('editormd-image-file')->store('uploads/article/' . Date::now()->format('Ymd'), 'public');
foreach (config('bjyblog.upload_disks') as $disk) {
$imagePath = $request->file('editormd-image-file')->store('uploads/article/' . Date::now()->format('Ymd'), $disk);
}

return response()->json([
'success' => 1,
Expand All @@ -65,7 +67,10 @@ public function store(Store $request)
$article = $request->except('_token');

if ($request->hasFile('cover')) {
$imagePath = $request->file('cover')->store('uploads/article/' . Date::now()->format('Ymd'), 'public');
foreach (config('bjyblog.upload_disks') as $disk) {
$imagePath = $request->file('cover')->store('uploads/article/' . Date::now()->format('Ymd'), $disk);
}

$article['cover'] = '/' . $imagePath;
}

Expand Down Expand Up @@ -95,7 +100,10 @@ public function update(Store $request, ArticleTag $articleTagModel, $id)

// 上传封面图
if ($request->hasFile('cover')) {
$imagePath = $request->file('cover')->store('uploads/article/' . Date::now()->format('Ymd'), 'public');
foreach (config('bjyblog.upload_disks') as $disk) {
$imagePath = $request->file('cover')->store('uploads/article/' . Date::now()->format('Ymd'), $disk);
}

$article['cover'] = '/' . $imagePath;
}

Expand Down
10 changes: 9 additions & 1 deletion app/Http/Controllers/Admin/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public function backup()
return view('admin.config.backup');
}

public function upload()
{
return view('admin.config.upload');
}

public function seo()
{
return view('admin.config.seo');
Expand Down Expand Up @@ -71,7 +76,10 @@ public function update(Request $request)
$configs = $request->except('_token');

if ($request->hasFile('153')) {
$imagePath = $request->file('153')->store('uploads/images', 'public');
foreach (config('bjyblog.upload_disks') as $disk) {
$imagePath = $request->file('153')->store('uploads/images', $disk);
}

$configs['153'] = '/' . $imagePath;
}

Expand Down
4 changes: 3 additions & 1 deletion app/Http/Controllers/Resources/ArticleCoverController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class ArticleCoverController extends Controller
{
public function store(Store $request)
{
$imagePath = $request->file('cover')->store('uploads/article' . Date::now()->format('Ymd'), 'public');
foreach (config('bjyblog.upload_disks') as $disk) {
$imagePath = $request->file('cover')->store('uploads/article/' . Date::now()->format('Ymd'), $disk);
}

return response()->json([
'success' => 1,
Expand Down
4 changes: 3 additions & 1 deletion app/Http/Controllers/Resources/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ class ConfigController extends Controller

public function uploadQqQunOrCode(QqQunOrCode $request)
{
$imagePath = $request->file('file')->store('uploads/images', 'public');
foreach (config('bjyblog.upload_disks') as $disk) {
$imagePath = $request->file('file')->store('uploads/images', $disk);
}

return response()->json([
'url' => $imagePath,
Expand Down
1 change: 1 addition & 0 deletions config/bjyblog.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@
'language' => '',
],
'theme' => '',
'upload_disks' => '',
];
22 changes: 16 additions & 6 deletions config/filesystems.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,24 @@
'url' => env('AWS_URL'),
],

'oss' => [
'oss_backups' => [
'driver' => 'oss',
'root' => '',
'access_key' => env('OSS_ACCESS_KEY'),
'secret_key' => env('OSS_SECRET_KEY'),
'endpoint' => env('OSS_ENDPOINT'),
'bucket' => env('OSS_BUCKET'),
'isCName' => env('OSS_IS_CNAME', false),
'access_key' => '',
'secret_key' => '',
'endpoint' => '',
'bucket' => '',
'isCName' => false,
],

'oss_uploads' => [
'driver' => 'oss',
'root' => '',
'access_key' => '',
'secret_key' => '',
'endpoint' => '',
'bucket' => '',
'isCName' => false,
],
],

Expand Down
48 changes: 44 additions & 4 deletions database/seeds/ConfigsTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,31 +250,31 @@ public function run()
],
[
'id' => 160,
'name' => 'filesystems.disks.oss.access_key',
'name' => 'filesystems.disks.oss_backups.access_key',
'value' => '',
'created_at' => '2018-12-04 22:29:52',
'updated_at' => '2018-12-04 22:29:52',
'deleted_at' => null,
],
[
'id' => 161,
'name' => 'filesystems.disks.oss.secret_key',
'name' => 'filesystems.disks.oss_backups.secret_key',
'value' => '',
'created_at' => '2018-12-04 22:29:52',
'updated_at' => '2018-12-04 22:29:52',
'deleted_at' => null,
],
[
'id' => 162,
'name' => 'filesystems.disks.oss.bucket',
'name' => 'filesystems.disks.oss_backups.bucket',
'value' => '',
'created_at' => '2018-12-04 22:29:52',
'updated_at' => '2018-12-04 22:29:52',
'deleted_at' => null,
],
[
'id' => 163,
'name' => 'filesystems.disks.oss.endpoint',
'name' => 'filesystems.disks.oss_backups.endpoint',
'value' => '',
'created_at' => '2018-12-04 22:29:52',
'updated_at' => '2018-12-04 22:29:52',
Expand Down Expand Up @@ -575,6 +575,46 @@ public function run()
'updated_at' => '2020-05-12 23:06:00',
'deleted_at' => null,
],
[
'id' => 200,
'name' => 'filesystems.disks.oss_uploads.access_key',
'value' => '',
'created_at' => '2020-06-26 23:29:52',
'updated_at' => '2020-06-26 23:29:52',
'deleted_at' => null,
],
[
'id' => 201,
'name' => 'filesystems.disks.oss_uploads.secret_key',
'value' => '',
'created_at' => '2020-06-26 23:29:52',
'updated_at' => '2020-06-26 23:29:52',
'deleted_at' => null,
],
[
'id' => 202,
'name' => 'filesystems.disks.oss_uploads.bucket',
'value' => '',
'created_at' => '2020-06-26 23:29:52',
'updated_at' => '2020-06-26 23:29:52',
'deleted_at' => null,
],
[
'id' => 203,
'name' => 'filesystems.disks.oss_uploads.endpoint',
'value' => '',
'created_at' => '2020-06-26 23:29:52',
'updated_at' => '2020-06-26 23:29:52',
'deleted_at' => null,
],
[
'id' => 204,
'name' => 'bjyblog.upload_disks',
'value' => '["public"]',
'created_at' => '2018-12-04 22:29:52',
'updated_at' => '2018-12-04 22:29:52',
'deleted_at' => null,
],
]);
}
}
1 change: 1 addition & 0 deletions resources/lang/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
"URL": "URL",
"Update Success": "Mise à jour réussie",
"Upgrade": "Améliorer",
"Upload": "Télécharger",
"Use Slug": "Utilisation Slug",
"User": "Utilisateur",
"User Counts": "Nombre utilisateurs",
Expand Down
1 change: 1 addition & 0 deletions resources/lang/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
"URL": "URL",
"Update Success": "Успешно обновлено",
"Upgrade": "Обновление",
"Upload": "Загрузить",
"Use Slug": "Использовать слаги",
"User": "Пользователи",
"User Counts": "Количество пользователей",
Expand Down
1 change: 1 addition & 0 deletions resources/lang/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
"URL": "链接",
"Update Success": "修改成功",
"Upgrade": "升级",
"Upload": "上传",
"Use Slug": "使用 Slug",
"User": "用户",
"User Counts": "登录用户",
Expand Down
10 changes: 5 additions & 5 deletions resources/views/admin/config/backup.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<td>
<input class="bjy-icheck" type="checkbox" name="164[]" value="local" @if(in_array('local', $config['backup.backup.destination.disks'])) checked @endif>{{ __('Local') }}
&emsp;
<input class="bjy-icheck" type="checkbox" name="164[]" value="oss" @if(in_array('oss', $config['backup.backup.destination.disks'])) checked @endif>{{ __('Aliyun') }} OSS
<input class="bjy-icheck" type="checkbox" name="164[]" value="oss_backups" @if(in_array('oss_backups', $config['backup.backup.destination.disks'])) checked @endif>{{ __('Aliyun') }} OSS
</td>
</tr>
<tr>
Expand All @@ -31,25 +31,25 @@
<tr>
<th>{{ __('Aliyun') }} AccessKeyID:</th>
<td>
<input class="form-control modal-sm" type="text" name="160" value="{{ $config['filesystems.disks.oss.access_key'] }}" >
<input class="form-control modal-sm" type="text" name="160" value="{{ $config['filesystems.disks.oss_backups.access_key'] }}" >
</td>
</tr>
<tr>
<th>{{ __('Aliyun') }} AccessKeySecret:</th>
<td>
<input class="form-control modal-sm" type="text" name="161" value="{{ $config['filesystems.disks.oss.secret_key'] }}" >
<input class="form-control modal-sm" type="text" name="161" value="{{ $config['filesystems.disks.oss_backups.secret_key'] }}" >
</td>
</tr>
<tr>
<th>{{ __('Aliyun') }} BUCKET:</th>
<td>
<input class="form-control modal-sm" type="text" name="162" value="{{ $config['filesystems.disks.oss.bucket'] }}" >
<input class="form-control modal-sm" type="text" name="162" value="{{ $config['filesystems.disks.oss_backups.bucket'] }}" >
</td>
</tr>
<tr>
<th>{{ __('Aliyun') }} ENDPOINT:</th>
<td>
<input class="form-control modal-sm" type="text" name="163" value="{{ $config['filesystems.disks.oss.endpoint'] }}" >
<input class="form-control modal-sm" type="text" name="163" value="{{ $config['filesystems.disks.oss_backups.endpoint'] }}" >
</td>
</tr>
<tr>
Expand Down
51 changes: 51 additions & 0 deletions resources/views/admin/config/upload.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
@extends('admin.layouts.admin')

@section('title', __('Upload'))

@section('nav', __('Upload'))

@section('content')
<form class="form-inline" enctype="multipart/form-data" action="{{ url('admin/config/update') }}" method="post">
{{ csrf_field() }}
<table class="table table-striped table-bordered table-hover">
<tr>
<th>{{ __('Type') }}:</th>
<td>
<input class="bjy-icheck" type="checkbox" name="204[]" value="public" @if(in_array('public', $config['bjyblog.upload_disks'])) checked @endif>{{ __('Local') }}
&emsp;
<input class="bjy-icheck" type="checkbox" name="204[]" value="oss_uploads" @if(in_array('oss_uploads', $config['bjyblog.upload_disks'])) checked @endif>{{ __('Aliyun') }} OSS
</td>
</tr>
<tr>
<th>{{ __('Aliyun') }} AccessKeyID:</th>
<td>
<input class="form-control modal-sm" type="text" name="200" value="{{ $config['filesystems.disks.oss_uploads.access_key'] }}" >
</td>
</tr>
<tr>
<th>{{ __('Aliyun') }} AccessKeySecret:</th>
<td>
<input class="form-control modal-sm" type="text" name="201" value="{{ $config['filesystems.disks.oss_uploads.secret_key'] }}" >
</td>
</tr>
<tr>
<th>{{ __('Aliyun') }} BUCKET:</th>
<td>
<input class="form-control modal-sm" type="text" name="202" value="{{ $config['filesystems.disks.oss_uploads.bucket'] }}" >
</td>
</tr>
<tr>
<th>{{ __('Aliyun') }} ENDPOINT:</th>
<td>
<input class="form-control modal-sm" type="text" name="203" value="{{ $config['filesystems.disks.oss_uploads.endpoint'] }}" >
</td>
</tr>
<tr>
<th></th>
<td>
<input class="btn btn-success" type="submit" value="{{ __('Submit') }}">
</td>
</tr>
</table>
</form>
@endsection
1 change: 1 addition & 0 deletions resources/views/admin/layouts/admin.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<li><a href="{{ url('admin/config/qqQun') }}">QQ群</a></li>
@endif
<li><a href="{{ url('admin/config/backup') }}">{{ __('Backup') }}</a></li>
<li><a href="{{ url('admin/config/upload') }}">{{ __('Upload') }}</a></li>
<li><a href="{{ url('admin/config/seo') }}">{{ __('SEO') }}</a></li>
<li><a href="{{ url('admin/config/socialShare') }}">{{ __('Social Share') }}</a></li>
<li><a href="{{ url('admin/config/socialLinks') }}">{{ __('Social Links') }}</a></li>
Expand Down
Loading

0 comments on commit 86fda81

Please sign in to comment.