72 lines
2.3 KiB
PHP
72 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\VideoDanmakus;
|
|
use App\Models\Videos;
|
|
use App\Util\DanmakuUtil;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller as BaseController;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class DanmakuConstructController extends BaseController
|
|
{
|
|
public function page(Request $request)
|
|
{
|
|
$view = view("danmaku.construct.batch_import");
|
|
if ($request->has("video_bvid")) {
|
|
$bvid = $request->get("video_bvid");
|
|
$video = Videos::query()->where("bvid", "=", $bvid)->first();
|
|
if ($video == null) {
|
|
$view->withErrors([
|
|
"video_bvid" => "系统无此对应视频",
|
|
]);
|
|
} else {
|
|
$request->session()->flashInput([
|
|
"video_bvid" => $bvid
|
|
]);
|
|
}
|
|
}
|
|
return $view;
|
|
}
|
|
|
|
public function do_import(Request $request)
|
|
{
|
|
$request->validate([
|
|
'video_bvid' => ['required'],
|
|
'platform_id' => ['required', 'int'],
|
|
'file.*' => ['required', 'file']
|
|
]);
|
|
$payload = $request->only(["video_bvid", "platform_id"]);
|
|
$files = $request->file("file");
|
|
if (!is_array($files)) {
|
|
$files = [$files];
|
|
}
|
|
$video = Videos::query()->where("bvid", "=", $payload["video_bvid"])->first();
|
|
if ($video == null) {
|
|
return back()->withInput()->withErrors([
|
|
"video_bvid" => "系统无此对应视频",
|
|
]);
|
|
}
|
|
foreach ($files as $file) {
|
|
$danmakus = DanmakuUtil::parse_danmaku($file->getRealPath());
|
|
DB::beginTransaction();
|
|
try {
|
|
foreach ($danmakus as &$danmaku) {
|
|
$danmaku['video_bvid'] = $video->bvid;
|
|
$danmaku['platform_id'] = $payload["platform_id"];
|
|
unset($danmaku);
|
|
}
|
|
VideoDanmakus::insert($danmakus);
|
|
DB::commit();
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return back()->withInput()->withErrors([
|
|
"file" => "文件导入异常:" . $e->getMessage(),
|
|
]);
|
|
}
|
|
}
|
|
return redirect("/danmakus/" . $payload["video_bvid"]);
|
|
}
|
|
}
|