Skip to content

Commit

Permalink
feat(admin): 增加本地文件系统设置面板
Browse files Browse the repository at this point in the history
  • Loading branch information
medz committed Sep 5, 2018
1 parent d78647d commit a1301ad
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 2 deletions.
69 changes: 69 additions & 0 deletions app/Admin/Controllers/FileStorage/LocalFilesystem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

/*
* +----------------------------------------------------------------------+
* | ThinkSNS Plus |
* +----------------------------------------------------------------------+
* | Copyright (c) 2018 Chengdu ZhiYiChuangXiang Technology Co., Ltd. |
* +----------------------------------------------------------------------+
* | This source file is subject to version 2.0 of the Apache license, |
* | that is bundled with this package in the file LICENSE, and is |
* | available through the world-wide-web at the following url: |
* | http://www.apache.org/licenses/LICENSE-2.0.html |
* +----------------------------------------------------------------------+
* | Author: Slim Kit Group <master@zhiyicx.com> |
* | Homepage: www.thinksns.com |
* +----------------------------------------------------------------------+
*/

namespace Zhiyi\Plus\Admin\Controllers\FileStorage;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use function Zhiyi\Plus\setting;
use Illuminate\Http\JsonResponse;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;

class LocalFilesystem
{
/**
* Get local filesystem.
* @return \Illuminate\Http\JsonResponse
*/
public function show(): JsonResponse
{
$configure = setting('file-storage', 'filesystems.local', []);
$result = [
'disk' => $configure['disk'] ?? 'local',
'timeout' => $configure['timeout'] ?? 3360,
'disks' => array_keys(config('filesystems.disks')),
];

return new JsonResponse($result, Response::HTTP_OK);
}

/**
* Update local filesystem.
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function update(Request $request): Response
{
$disk = $request->input('disk', 'local');
$timeout = (int) $request->input('timeout');

if (! in_array($disk, array_keys(config('filesystems.disks')))) {
throw new UnprocessableEntityHttpException('选择的磁盘不存在!');
}

$setting = setting('file-storage');
$setting->set('filesystems.local', [
'disk' => $disk,
'timeout' => $timeout,
]);

return new Response('', Response::HTTP_NO_CONTENT);
}
}
2 changes: 1 addition & 1 deletion app/FileStorage/Filesystems/LocalFilesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function delete(string $path): bool
public function createTask(Request $request, ResourceInterface $resource): TaskInterface
{
$expiresAt = (new Carbon)->addSeconds(
setting('file-storage', 'filesystems.local', ['timeout' => 3360])['timeout']
setting('file-storage', 'filesystems.local', [])['timeout'] ?? 3600
);
$uri = url()->temporarySignedRoute('storage:local-put', $expiresAt, [
'channel' => $resource->getChannel(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<template>
<div class="panel panel-default">
<div class="panel-heading">本地存储</div>
<div class="panel-body">
<sb-ui-loading v-if="loading" />

<div class="form-horizontal" v-else>

<!-- 磁盘选择 -->
<div class="form-group">
<label class="col-sm-2 control-label">使用磁盘</label>
<div class="col-sm-3">
<select class="form-control" v-model="disk">
<option
v-for="disk in disks"
:key="disk"
:value="disk"
>
{{ disk }}
</option>
</select>
</div>
<div class="col-sm-7 help-block">
磁盘选择取决于你应用中 <code>config/filesystems.php</code> 中的 <code>disks</code>
所允许的磁盘,如果选择的磁盘没有在其磁盘配置中正确的配置,会影响到所选系统的正常运行。
如果你没有自定义过磁盘,那么选择 <code>local</code> 磁盘是最为保险且有效的设置。
其他磁盘选择如果你的应用自定义过磁盘,请按照你自定义的设置进行选择。
</div>
</div>

<!-- 过期时间 -->
<div class="form-group">
<label class="col-sm-2 control-label">过期时间</label>
<div class="col-sm-3">
<input
type="number"
class="form-control"
placeholder="请输入过期时长..."
v-model.number="timeout"
min="0"
>
</div>
<div class="col-sm-7 help-block">
过期时间,适用于创建本地上传任务开始,上传链接的生命时间,如果你的应用没有特殊的大型文件上传,设置在一小时或者半小时较为合理。
默认是 <code>3600 Second</code> 即一小时,过期时间设置单位为<code>秒</code>。
</div>
</div>

<!-- 提交按钮 -->
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<ui-button type="button" class="btn btn-primary" @click="submitHandler" />
</div>
</div>
</div>

</div>
</div>
</template>

<script>
import request, { createRequestURI } from '../../../util/request';
export default {
data: () => ({
disk: 'local',
timeout: 3600,
loading: true,
disks: [],
}),
created() {
this.loading = true;
request.get(
createRequestURI('file-storage/filesystems/local'),
{
validateStatus: status => status === 200
}
)
.then(({ data: { disk, timeout, disks } }) => {
this.disk = disk;
this.disks = disks;
this.timeout = timeout;
this.loading = false;
})
.catch(({ response: { data: message = "获取默认本地存储设置失败,请检查网络或者刷新页面重试" } }) => {
this.$store.dispatch("alert-open", { type: "danger", message });
});
},
methods: {
submitHandler({ stopProcessing }) {
request.patch(
createRequestURI('file-storage/filesystems/local'),
{
disk: this.disk,
timeout: this.timeout,
},
{
validateStatus: status => status === 204,
}
)
.then(() => {
this.$store.dispatch("alert-open", {
type: "success",
message: "更新成功!"
});
})
.catch(({ response: { data: message = "提交失败!" } }) => {
this.$store.dispatch("alert-open", { type: "danger", message });
})
.finally(stopProcessing);
}
}
}
</script>

Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<template>
<div class="container-fluid">
<default-filesystem-panel />
<local-filesystem-panel />
</div>
</template>

<script>
import DefaultFilesystemPanel from './default';
import LocalFilesystemPanel from './local';
export default {
components: {
DefaultFilesystemPanel
DefaultFilesystemPanel,
LocalFilesystemPanel
}
}
</script>
Expand Down
3 changes: 3 additions & 0 deletions routes/new-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@

$route->get('file-storage/default-filesystem', AdminControllers\FileStorage\DefaultFilesystem::class.'@show');
$route->patch('file-storage/default-filesystem', AdminControllers\FileStorage\DefaultFilesystem::class.'@update');

$route->get('file-storage/filesystems/local', AdminControllers\FileStorage\LocalFilesystem::class.'@show');
$route->patch('file-storage/filesystems/local', AdminControllers\FileStorage\LocalFilesystem::class.'@update');
});

0 comments on commit a1301ad

Please sign in to comment.