190 lines
6.4 KiB
C#
190 lines
6.4 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Newtonsoft.Json;
|
|
using Shared.Engine.Utilities;
|
|
using Shared.Models.SQL;
|
|
using System.Web;
|
|
|
|
namespace SISI
|
|
{
|
|
public class HistoryController : BaseController
|
|
{
|
|
[HttpGet]
|
|
[Route("sisi/historys")]
|
|
async public Task<ActionResult> List(int pg = 1, int pageSize = 36)
|
|
{
|
|
string md5user = getuser();
|
|
if (md5user == null || !AppInit.conf.sisi.history.enable)
|
|
return StatusCode(403, "access denied");
|
|
|
|
#region historys
|
|
int total_pages = 0;
|
|
var historys = new List<PlaylistItem>(pageSize);
|
|
|
|
using (var sqlDb = SisiContext.Factory != null
|
|
? SisiContext.Factory.CreateDbContext()
|
|
: new SisiContext())
|
|
{
|
|
var historysQuery = sqlDb.historys
|
|
.AsNoTracking()
|
|
.Where(i => i.user == md5user)
|
|
.Take(pageSize * 20);
|
|
|
|
total_pages = Math.Max(0, await historysQuery.CountAsync() / pageSize) + 1;
|
|
|
|
var items = historysQuery
|
|
.OrderByDescending(i => i.created)
|
|
.Skip((pg * pageSize) - pageSize)
|
|
.Take(pageSize);
|
|
|
|
if (items.Any())
|
|
{
|
|
foreach (var item in items)
|
|
{
|
|
if (string.IsNullOrEmpty(item.json))
|
|
continue;
|
|
|
|
try
|
|
{
|
|
var pl = JsonConvert.DeserializeObject<PlaylistItem>(item.json);
|
|
if (pl != null)
|
|
{
|
|
historys.Add(new PlaylistItem()
|
|
{
|
|
name = pl.name,
|
|
video = getvideLink(pl),
|
|
picture = pl.bookmark.image != null ? HostImgProxy(pl.bookmark.image, plugin: pl.bookmark.site) : null,
|
|
time = pl.time,
|
|
json = pl.json,
|
|
related = pl.related || Regex.IsMatch(pl.bookmark.site, "^(elo|epr|fph|phub|sbg|xmr|xnx|xds)"),
|
|
quality = pl.quality,
|
|
preview = pl.preview,
|
|
model = pl.model,
|
|
bookmark = pl.bookmark,
|
|
history_uid = pl.history_uid
|
|
});
|
|
}
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region getvideLink
|
|
string getvideLink(PlaylistItem pl)
|
|
{
|
|
if (pl.bookmark.site is "phub" or "phubprem")
|
|
return $"{host}/{pl.bookmark.site}/vidosik?vkey={HttpUtility.UrlEncode(pl.bookmark.href)}";
|
|
|
|
return $"{host}/{pl.bookmark.site}/vidosik?uri={HttpUtility.UrlEncode(pl.bookmark.href)}";
|
|
}
|
|
#endregion
|
|
|
|
string localhost = $"http://{AppInit.conf.listen.localhost}:{AppInit.conf.listen.port}";
|
|
|
|
return new JsonResult(new Channel()
|
|
{
|
|
list = historys,
|
|
total_pages = total_pages
|
|
});
|
|
}
|
|
|
|
|
|
[HttpPost]
|
|
[Route("sisi/history/add")]
|
|
async public Task<ActionResult> Add([FromBody] PlaylistItem data)
|
|
{
|
|
string md5user = getuser();
|
|
if (md5user == null || !AppInit.conf.sisi.history.enable || data == null || string.IsNullOrEmpty(data?.bookmark?.site) || string.IsNullOrEmpty(data?.bookmark?.href))
|
|
return StatusCode(403, "access denied");
|
|
|
|
string uid = CrypTo.md5($"{data.bookmark.site}:{data.bookmark.href}");
|
|
|
|
using (var sqlDb = SisiContext.Factory != null
|
|
? SisiContext.Factory.CreateDbContext()
|
|
: new SisiContext())
|
|
{
|
|
bool any = await sqlDb.historys.AsNoTracking().AnyAsync(i => i.user == md5user && i.uid == uid);
|
|
|
|
if (any == false)
|
|
{
|
|
data.history_uid = uid;
|
|
|
|
sqlDb.historys.Add(new SisiHistorySqlModel
|
|
{
|
|
user = md5user,
|
|
uid = uid,
|
|
created = DateTime.UtcNow,
|
|
json = JsonConvertPool.SerializeObject(data)
|
|
});
|
|
|
|
await sqlDb.SaveChangesLocks();
|
|
}
|
|
}
|
|
|
|
return Json(new
|
|
{
|
|
result = true
|
|
});
|
|
}
|
|
|
|
|
|
[HttpGet]
|
|
[Route("sisi/history/remove")]
|
|
async public Task<ActionResult> Remove(string id)
|
|
{
|
|
string md5user = getuser();
|
|
if (md5user == null || !AppInit.conf.sisi.history.enable || string.IsNullOrEmpty(id))
|
|
return StatusCode(403, "access denied");
|
|
|
|
try
|
|
{
|
|
await SisiContext.semaphore.WaitAsync(TimeSpan.FromSeconds(30));
|
|
|
|
using (var sqlDb = SisiContext.Factory != null
|
|
? SisiContext.Factory.CreateDbContext()
|
|
: new SisiContext())
|
|
{
|
|
await sqlDb.historys
|
|
.Where(i => i.user == md5user && i.uid == id)
|
|
.ExecuteDeleteAsync();
|
|
}
|
|
}
|
|
catch { }
|
|
finally
|
|
{
|
|
SisiContext.semaphore.Release();
|
|
}
|
|
|
|
return Json(new
|
|
{
|
|
result = true,
|
|
});
|
|
}
|
|
|
|
|
|
|
|
string getuser()
|
|
{
|
|
string user_id = requestInfo.user_uid;
|
|
if (string.IsNullOrEmpty(user_id))
|
|
return null;
|
|
|
|
string profile_id = getProfileid();
|
|
if (!string.IsNullOrEmpty(profile_id))
|
|
return CrypTo.md5($"{user_id}_{profile_id}");
|
|
|
|
return CrypTo.md5(user_id);
|
|
}
|
|
|
|
string getProfileid()
|
|
{
|
|
if (HttpContext.Request.Query.TryGetValue("profile_id", out var profile_id) && !string.IsNullOrEmpty(profile_id) && profile_id != "0")
|
|
return profile_id;
|
|
|
|
return string.Empty;
|
|
}
|
|
}
|
|
}
|