yavsc/src/Yavsc.Blogs/Controllers/FileSystemStreamController.cs

73 lines
2.8 KiB
C#
Raw Normal View History

2023-03-19 17:57:55 +00:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
2020-10-18 23:00:17 +01:00
using Yavsc.Attributes.Validation;
using Yavsc.Models;
using Yavsc.Models.Messaging;
using Yavsc.Services;
2023-03-19 17:57:55 +00:00
using Microsoft.AspNetCore.SignalR;
2025-02-17 23:56:28 +00:00
using Yavsc.Server.Helpers;
2026-06-20 18:43:01 +01:00
using static Yavsc.Blogs.Constants;
2026-07-04 15:28:07 +01:00
using Yavsc.Server.Hubs;
2020-10-18 23:00:17 +01:00
2026-06-10 16:59:23 +01:00
namespace Yavsc.Blogs.Controllers
2020-10-18 23:00:17 +01:00
{
2026-06-20 18:43:01 +01:00
[Authorize, Route(APIPrefix + "/stream")]
2020-10-18 23:00:17 +01:00
public partial class FileSystemStreamController : Controller
{
private readonly ILogger logger;
private readonly ILiveProcessor liveProcessor;
2023-03-19 17:57:55 +00:00
private readonly IHubContext<ChatHub> hubContext;
2020-10-18 23:00:17 +01:00
readonly ApplicationDbContext dbContext;
2023-03-19 17:57:55 +00:00
public FileSystemStreamController(ApplicationDbContext context, ILiveProcessor liveProcessor, ILoggerFactory loggerFactory,
IHubContext<ChatHub> hubContext)
2020-10-18 23:00:17 +01:00
{
this.dbContext = context;
this.logger = loggerFactory.CreateLogger<FileSystemStreamController>();
this.liveProcessor = liveProcessor;
2023-03-19 17:57:55 +00:00
this.hubContext = hubContext;
2020-10-18 23:00:17 +01:00
}
[Authorize, Route("put/{filename}")]
2020-10-18 23:00:17 +01:00
public async Task<IActionResult> Put([ValidRemoteUserFilePath] string filename)
{
logger.LogInformation("Put : " + filename);
2020-10-18 23:00:17 +01:00
if (!HttpContext.WebSockets.IsWebSocketRequest)
2023-03-19 17:57:55 +00:00
return BadRequest("not a web socket");
2020-10-18 23:00:17 +01:00
var subdirs = filename.Split('/');
var filePath = subdirs.Length > 1 ? string.Join("/", subdirs.Take(subdirs.Length-1)) : null;
var shortFileName = subdirs[subdirs.Length-1];
if (!shortFileName.IsValidShortFileName())
{
logger.LogInformation("invalid file name : " + filename);
2023-03-19 17:57:55 +00:00
return BadRequest("invalid file name");
}
logger.LogInformation("validated: api/stream/Put: "+filename);
2020-10-18 23:00:17 +01:00
var userName = User.GetUserName();
2026-07-04 15:28:07 +01:00
2020-10-18 23:00:17 +01:00
string url = string.Format(
"{0}/{1}/{2}",
2024-02-25 18:05:10 +00:00
Config.UserFilesOptions.RequestPath.ToUriComponent(),
2020-10-18 23:00:17 +01:00
userName,
filename
);
2026-07-04 15:28:07 +01:00
2025-11-16 15:53:13 +00:00
string destDir = HttpContext.User.EnsureDestinationDirectory(filePath);
logger.LogInformation($"Saving flow to {destDir}");
var userId = User.GetUserId();
var user = await dbContext.Users.FirstAsync(u => u.Id == userId);
logger.LogInformation("Accepting stream ...");
_ = hubContext.Clients.All.SendAsync("addPublicStream", new PublicStreamInfo
2020-10-18 23:00:17 +01:00
{
sender = userName,
url = url,
}, $"{userName} is starting a stream!");
2026-07-04 15:28:07 +01:00
2020-10-18 23:00:17 +01:00
await liveProcessor.AcceptStream(HttpContext, user, destDir, shortFileName);
return Ok();
}
}
}