diff --git a/app/Http/Controllers/DanmakuConstructController.php b/app/Http/Controllers/DanmakuConstructController.php new file mode 100644 index 0000000..6852eef --- /dev/null +++ b/app/Http/Controllers/DanmakuConstructController.php @@ -0,0 +1,71 @@ +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'] + ]); + $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->getFileInfo()); + 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"]); + } +} diff --git a/app/Models/VideoDanmakus.php b/app/Models/VideoDanmakus.php index 41af5ad..851fb77 100644 --- a/app/Models/VideoDanmakus.php +++ b/app/Models/VideoDanmakus.php @@ -6,8 +6,14 @@ use Illuminate\Database\Eloquent\Model; class VideoDanmakus extends Model { + protected $guarded = []; protected $table = "video_danmakus"; protected $dateFormat = 'U'; + public $timestamps = false; + protected $casts = [ + 'created_at' => 'datetime:Y-m-d H:i:s', + ]; + protected $fillable = ["from", "from_mid", "content"]; public function video(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Videos::class, "video_bvid", "bvid"); diff --git a/app/Util/DanmakuUtil.php b/app/Util/DanmakuUtil.php new file mode 100644 index 0000000..5b59ac2 --- /dev/null +++ b/app/Util/DanmakuUtil.php @@ -0,0 +1,32 @@ +load($file->getRealPath()); + $danmaku_items = $document->getElementsByTagName("d"); + $result = []; + /** @var \DOMNode $item */ + foreach ($danmaku_items as $item) { + $paramsNode = $item->attributes->getNamedItem("p"); + $param_list = mb_split(",", $paramsNode->value); + if (sizeof($param_list) < 7) { + throw new \Exception("弹幕格式异常"); + } + $userNode = $item->attributes->getNamedItem("user"); + $result[] = [ + "from" => $userNode->value, + "from_mid" => $param_list[6], + "content" => $item->textContent, + "created_at" => intval($param_list[4])/1000, + ]; + } + return $result; + } +} diff --git a/composer.json b/composer.json index afcb5ef..a7c93d6 100644 --- a/composer.json +++ b/composer.json @@ -6,6 +6,7 @@ "license": "MIT", "require": { "php": "^7.3|^8.0", + "ext-dom": "*", "ext-json": "*", "ext-mbstring": "*", "fruitcake/laravel-cors": "^2.0", diff --git a/resources/views/common/header.blade.php b/resources/views/common/header.blade.php index 534d931..16c71ca 100644 --- a/resources/views/common/header.blade.php +++ b/resources/views/common/header.blade.php @@ -21,7 +21,7 @@ 稿件查询 节目查询 @auth("web") - 节目建设 + 节目建设 @endauth @@ -50,7 +50,7 @@ 稿件查询 节目查询 @auth("web") - 节目建设 + 节目建设 @endauth diff --git a/resources/views/danmaku/construct/batch_import.blade.php b/resources/views/danmaku/construct/batch_import.blade.php new file mode 100644 index 0000000..b03a2ea --- /dev/null +++ b/resources/views/danmaku/construct/batch_import.blade.php @@ -0,0 +1,35 @@ + +
+ +