Skip to content

Commit

Permalink
complete one
Browse files Browse the repository at this point in the history
  • Loading branch information
WangNingkai committed Jun 15, 2020
1 parent 3e3da01 commit 344dd02
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 12 deletions.
36 changes: 36 additions & 0 deletions app/Helpers/Tool.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,40 @@ public static function decodeShortUrl($code)
}
return $url->original_url;
}

/**
* 面包屑导航
* @param $key
* @param $path
* @return string
*/
public static function combineBreadcrumb($key, $path): string
{
$path = array_slice($path, 0, $key);
$url = '';
foreach ($path as $param) {
$url .= '/' . $param;
}

return trim($url, '/');
}

/**
* 面包屑返回上一级
* @param $path
* @return string
*/
public static function fetchGoBack($path): string
{
array_pop($path);
if (count($path) === 0) {
return '';
}
$url = '';
foreach ($path as $param) {
$url .= '/' . $param;
}

return trim($url, '/');
}
}
6 changes: 5 additions & 1 deletion app/Http/Controllers/DiskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public function __invoke($hash, $query = '/')
$root = array_get(setting($hash), 'root', '/');
$root = trim($root, '/');
$query = trim($query, '/');
$path = explode('/', $query);
$path = array_where($path, static function ($value) {
return !blank($value);
});
$query = "{$root}/$query";
$service = (new OneDrive($account_id));
// 缓存处理
Expand All @@ -44,7 +48,7 @@ public function __invoke($hash, $query = '/')
$doc = $this->filterDoc($list);
// 资源过滤
$list = $this->filter($list);
return view(config('olaindex.theme') . 'one', compact('accounts', 'hash', 'item', 'list', 'doc'));
return view(config('olaindex.theme') . 'one', compact('accounts', 'hash', 'path', 'item', 'list', 'doc'));
}

public function filter($list)
Expand Down
64 changes: 58 additions & 6 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function __invoke(Request $request)
->where('status', 1)->get();
});
$account_id = 0;
$hash = '';
if ($accounts) {
$account_id = array_get(array_first($accounts), 'id');
$hash = array_get(array_first($accounts), 'hash_id');
Expand All @@ -35,18 +36,69 @@ public function __invoke(Request $request)

// 资源处理
$root = array_get(setting($hash), 'root', '/');
$query = trim($root, '/');
$root = trim($root, '/');
$query = '/';
$path = explode('/', $query);
$path = array_where($path, static function ($value) {
return !blank($value);
});
$query = "{$root}/$query";
$service = (new OneDrive($account_id));
// 缓存处理
$item = Cache::remember('d:item:' . $query, setting('cache_expires'), static function () use ($service, $query) {
return $service->fetchItem($query);
});
$list = Cache::remember('d:list:' . $query, setting('cache_expires'), static function () use ($service, $query) {
return $service->fetchList($query);
});
$doc = [
'head' => '## HEAD',
'readme' => '## README'
];
return view(config('olaindex.theme') . 'one', compact('accounts', 'hash', 'item', 'list', 'doc'));
// 读取预设资源
$doc = $this->filterDoc($list);
// 资源过滤
$list = $this->filter($list);
return view(config('olaindex.theme') . 'one', compact('accounts', 'hash', 'path', 'item', 'list', 'doc'));
}

public function filter($list)
{
// 过滤微软内置无法读取的文件
$list = array_where($list, static function ($value) {
return !array_has($value, 'package.type');
});
// 过滤预留文件
$list = array_where($list, static function ($value) {
return !in_array($value['name'], ['README.md', 'HEAD.md', '.password', '.deny'], false);
});
// todo:过滤隐藏文件
return $list;
}

public function filterDoc($list)
{
$readme = array_where($list, static function ($value) {
return $value['name'] === 'README.md';
});
$head = array_where($list, static function ($value) {
return $value['name'] === 'HEAD.md';
});

if (!empty($readme)) {
$readme = array_first($readme);
$readme = Cache::remember('d:content:' . $readme['id'], setting('cache_expires'), static function () use ($readme) {
return file_get_contents($readme['@microsoft.graph.downloadUrl']);
});
} else {
$readme = '';
}
if (!empty($head)) {
$head = array_first($head);
$head = Cache::remember('d:content:' . $head['id'], setting('cache_expires'), static function () use ($head) {
return file_get_contents($head['@microsoft.graph.downloadUrl']);
});
} else {
$head = '';
}


return compact('head', 'readme');
}
}
67 changes: 63 additions & 4 deletions resources/views/default/one.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,40 @@
</div>
<nav aria-label="breadcrumb" class="d-none d-md-block d-md-none">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="#"><i class="ri-home-fill"></i> Home</a></li>
<li class="breadcrumb-item">Second</li>
<li class="breadcrumb-item">Third</li>
<li class="breadcrumb-item"><a href="{{ route('drive.query', ['hash' => $hash]) }}"><i
class="ri-home-fill"></i> Home</a></li>
@if(!blank($path))
@if (count($path) < 5)
@foreach ($path as $key => $value)
@if(end($path) === $value && $key === (count($path) - 1))
<li class="breadcrumb-item active">{{ str_limit($value,20) }}</li>
@else
@if (!blank($value))
<li class="breadcrumb-item ">
<a href="{{ route('drive.query', ['hash' => $hash, 'query' => url_encode(\App\Helpers\Tool::combineBreadcrumb($key + 1, $path))]) }}">
{{ str_limit($value,20) }}
</a>
</li>
@endif
@endif
@endforeach
@else
<li class="breadcrumb-item active"> ...</li>
@foreach ($path as $key => $value)
@if(end($path) === $value && $key === (count($path) - 1))
<li class="breadcrumb-item active">{{ str_limit($value,20) }}</li>
@else
@if (!blank($value) && $key === (count($path) - 2))
<li class="breadcrumb-item ">
<a href="{{ route('drive.query', ['hash' => $hash, 'query' => url_encode(\App\Helpers\Tool::combineBreadcrumb($key + 1, $path))]) }}">
{{ str_limit($value,20) }}
</a>
</li>
@endif
@endif
@endforeach
@endif
@endif
</ol>
</nav>
@if (!blank($doc['head']))
Expand All @@ -35,7 +66,35 @@
@endif
<div class="card border-light mb-3">
<div class="card-header"></div>
<div class="card-body"></div>
<div class="card-body">
<table class="table table-hover table-borderless">
<thead>
<tr>
<th scope="col">文件</th>
<th scope="col">修改日期</th>
<th scope="col">大小</th>
<th scope="col">操作</th>
</tr>
</thead>
<tbody>
@if(!blank($path))
<tr>
<td colspan="4">
<a href="{{ route('drive.query', ['hash' => $hash, 'query' => url_encode(\App\Helpers\Tool::fetchGoBack($path))]) }}">返回上一层</a>
</td>
</tr>
@endif
@foreach($list as $data)
<tr onclick="window.location.href='{{ route('drive.query', ['hash' => $hash, 'query' => url_encode(implode('/', array_add($path, key(array_slice($path, -1, 1, true)) + 1, $data['name']) ))]) }}'">
<td>{{ $data['name'] }}</td>
<td>{{ date('M d H:i', strtotime($data['lastModifiedDateTime'])) }}</td>
<td>{{ \App\Helpers\Tool::convertSize($data['size']) }}</td>
<td></td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
@if (!blank($doc['readme']))
<div class="card border-light mb-3">
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
Route::get('t/{code}', 'IndexController')->name('short');
// 多网盘支持
Route::get('d/{hash}', 'DiskController')->name('drive');
Route::get('d/{hash}/q/{query?}', 'DiskController')->name('drive.query');
Route::get('d/{hash}/q/{query?}', 'DiskController')->name('drive.query')->where('query', '.*');

// 首页
Route::get('/', 'HomeController')->name('home');
Expand Down

0 comments on commit 344dd02

Please sign in to comment.