using System.Drawing;
using Dpz.Core.Service.ObjectStorage.Services;
using Dpz.Core.Web.Models;
using Dpz.Core.Web.Models.Video;
namespace Dpz.Core.Web.Controllers;
public class VideoController(
IBarrageService barrageService,
IVideoService videoService,
IObjectStorageOperation objectStorageService,
ILogger<VideoController> logger)
: Controller
{
public async Task<IActionResult> Index()
{
this.SetTitle("视频");
var list = await videoService.GetVideosAsync();
var videoList = await objectStorageService.GetFolderListAsync("/Video");
var videos = videoList.Select(x =>
{
var video = list.FirstOrDefault(y => y.Id == x.Name.GenerateHashMd5());
if (video != null) return video;
return new VmVideo
{
Id = x.Name.GenerateHashMd5(),
Title = x.Name
};
}).ToList();
return View(videos);
}
[Route("video/watch/{id}.html")]
public async Task<IActionResult> Watch(string id)
{
var video = await videoService.GetVideoAsync(id);
if (video == null)
{
var folder =
(await objectStorageService.GetFolderListAsync("/Video")).FirstOrDefault(x =>
x.Name.GenerateHashMd5() == id);
if (folder == null)
return NotFound();
video = new VmVideo
{
Description = "",
Id = folder.Name.GenerateHashMd5(),
Name = "",
SubTitle = "",
Tags = Array.Empty<string>(),
Title = folder.Name,
VideoTitle = ""
};
}
this.SetTitle(video.VideoTitle);
return View(video);
}
[Route("history/chat/v3/"), HttpGet]
public async Task<IActionResult> HistoryChat(string id)
{
var dbDanmaku = await barrageService.GetGroupBarragesAsync(id);
var allDanmaku = dbDanmaku.Select(x =>
{
var result = int.TryParse(x.Color, out var color);
return new object[]
{
x.Time,
x.Position,
result ? color : x.Color,
"阿胖",
x.Text
};
});
return Json(new { code = 0, data = allDanmaku });
}
[Route("history/danmaku/{id}"), HttpGet]
public async Task<IActionResult> HistoryDanmaku(string id)
{
var dbDanmaku = await barrageService.GetGroupBarragesAsync(id);
var allDanmaku = dbDanmaku.OrderBy(x => x.Time).Select(x =>
{
var result = int.TryParse(x.Color, out var color);
var colorRgb = Color.FromArgb(color);
return new
{
color = result
? $"rgb({colorRgb.R},{colorRgb.G},{colorRgb.B})"
: x.Color,
text = x.Text,
time = x.Time,
type = x.Position switch
{
0 => "scroll",
1 => "top",
2 => "bottom",
_ => ""
}
};
}).ToList();
return Json(allDanmaku);
}
[Route("history/danmaku"), HttpPost]
public async Task<IActionResult> HistoryDanmaku([FromBody] VideoDanmaku2 danmaku)
{
var color = ColorTranslator.FromHtml("#FF0000");
var vmBarrage = new VmBarrage
{
Color = color.ToArgb().ToString(),
Group = danmaku.Id,
Position = danmaku.Type switch
{
"scroll" => 0,
"top" => 1,
"bottom" => 2,
_ => 0
},
SendTime = DateTime.Now,
Size = 0,
Text = danmaku.Text,
Time = danmaku.Time
};
await barrageService.AddBarrageAsync(vmBarrage);
return Json(new ResultInfo(true));
}
[Route("history/chat/v3/"), HttpPost]
public async Task<IActionResult> HistoryChat([FromBody] VideoDanmaku danmaku)
{
var vmBarrage = new VmBarrage
{
Color = danmaku.Color.ToString(),
Group = danmaku.Id,
Position = (int)danmaku.Type,
SendTime = DateTime.Now,
Size = 0,
Text = danmaku.Text,
Time = danmaku.Time
};
await barrageService.AddBarrageAsync(vmBarrage);
return Json(new { code = 0, data = danmaku });
}
[Route("history/danmaku/v2/{id}"), HttpGet]
public async Task<IActionResult> HistoryDanmakuV2(string id)
{
var dbDanmaku = await barrageService.GetGroupBarragesAsync(id);
var data = dbDanmaku
.Select(x =>
{
var result = int.TryParse(x.Color, out var color);
var colorRgb = Color.FromArgb(color);
return new
{
text = x.Text,
time = x.Time,
color = result ? $"rgb({colorRgb.R},{colorRgb.G},{colorRgb.B})" : x.Color,
border = false,
mode = x.Position switch
{
0 => 0,
1 => 1,
2 => 1,
_ => 0
}
};
})
.ToList();
return Json(data);
}
[Route("send/danmaku/v2"), HttpPost]
public async Task<IActionResult> SendDanmaku([FromBody] SendDanmaku danmaku)
{
if (!ModelState.IsValid)
{
return Json(new ResultInfo(false, ModelState));
}
Color color;
try
{
color = ColorTranslator.FromHtml(danmaku.Color);
}
catch (Exception e)
{
color = ColorTranslator.FromHtml("#FFFFFF");
logger.LogError(e, "color translator fail,in color:{Color}", danmaku.Color);
}
var vmBarrage = new VmBarrage
{
Color = color.ToArgb().ToString(),
Group = danmaku.Id,
Position = danmaku.Mode,
SendTime = DateTime.Now,
Size = 0,
Text = danmaku.Text,
Time = danmaku.Time
};
await barrageService.AddBarrageAsync(vmBarrage);
return Json(new ResultInfo(true));
}
}