Fixing destination file @streaming

This commit is contained in:
Paul Schneider 2020-10-18 23:00:17 +01:00
commit 1517b48741
11 changed files with 193 additions and 10 deletions

View file

@ -180,6 +180,8 @@ namespace Yavsc.ApiControllers
}
return Ok(new { deleted=id });
}
}
}

View file

@ -0,0 +1,67 @@
using System.IO;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
using Microsoft.Extensions.Logging;
using Yavsc.Attributes.Validation;
using Yavsc.Helpers;
using Yavsc.Models;
using Yavsc.Models.Messaging;
using Yavsc.Services;
namespace Yavsc.ApiControllers
{
[Authorize, Route("api/stream")]
public partial class FileSystemStreamController : Controller
{
private readonly ILogger logger;
private readonly ILiveProcessor liveProcessor;
readonly ApplicationDbContext dbContext;
public FileSystemStreamController(ApplicationDbContext context, ILiveProcessor liveProcessor, ILoggerFactory loggerFactory)
{
this.dbContext = context;
this.logger = loggerFactory.CreateLogger<FileSystemStreamController>();
this.liveProcessor = liveProcessor;
}
public async Task<IActionResult> Put([ValidRemoteUserFilePath] string filename)
{
if (!HttpContext.WebSockets.IsWebSocketRequest)
return HttpBadRequest("not a web socket");
if (!HttpContext.User.Identity.IsAuthenticated)
return new HttpUnauthorizedResult();
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())
return HttpBadRequest("invalid file name");
var userName = User.GetUserName();
var hubContext = Microsoft.AspNet.SignalR.GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
string url = string.Format(
"{0}/{1}/{2}",
Startup.UserFilesOptions.RequestPath.ToUriComponent(),
userName,
filename
);
hubContext.Clients.All.addPublicStream(new PublicStreamInfo
{
sender = userName,
url = url,
}, $"{userName} is starting a stream!");
string destDir = HttpContext.User.InitPostToFileSystem(filePath);
logger.LogInformation($"Saving flow to {destDir}");
var userId = User.GetUserId();
var user = await dbContext.Users.FirstAsync(u => u.Id == userId);
await liveProcessor.AcceptStream(HttpContext, user, destDir, shortFileName);
return Ok();
}
}
}

View file

@ -40,8 +40,6 @@ namespace Yavsc.Services
public async Task<bool> AcceptStream(HttpContext context, ApplicationUser user, string destDir, string fileName)
{
// TODO defer request handling
string uname = user.UserName;
LiveCastHandler liveHandler = null;
if (Casters.ContainsKey(uname))
@ -94,6 +92,7 @@ namespace Yavsc.Services
var fsInputQueue = new Queue<ArraySegment<byte>>();
bool endOfInput = false;
sBuffer = new ArraySegment<byte>(buffer,0,received.Count);
fsInputQueue.Enqueue(sBuffer);
var taskWritingToFs = liveHandler.ReceiveUserFile(user, _logger, destDir, fsInputQueue, fileName, () => endOfInput);
@ -128,18 +127,20 @@ namespace Yavsc.Services
_logger.LogInformation("try and receive new bytes");
buffer = new byte[Constants.WebSocketsMaxBufLen];
sBuffer = new ArraySegment<byte>(buffer);
received = await liveHandler.Socket.ReceiveAsync(sBuffer, liveHandler.TokenSource.Token);
_logger.LogInformation($"Received bytes : {received.Count}");
sBuffer = new ArraySegment<byte>(buffer,0,received.Count);
_logger.LogInformation($"segment : offset: {sBuffer.Offset} count: {sBuffer.Count}");
_logger.LogInformation($"Is the end : {received.EndOfMessage}");
fsInputQueue.Enqueue(sBuffer);
if (received.CloseStatus.HasValue)
{
endOfInput=true;
_logger.LogInformation($"received a close status: {received.CloseStatus.Value}: {received.CloseStatusDescription}");
}
else fsInputQueue.Enqueue(sBuffer);
}
else endOfInput=true;
while (ToClose.Count > 0)

View file

@ -16,8 +16,8 @@ namespace Yavsc
var webSocketOptions = new WebSocketOptions()
{
KeepAliveInterval = TimeSpan.FromSeconds(30),
ReceiveBufferSize = Constants.WebSocketsMaxBufLen+4*sizeof(int),
ReplaceFeature = true
ReceiveBufferSize = Constants.WebSocketsMaxBufLen,
ReplaceFeature = false
};
app.UseWebSockets(webSocketOptions);

View file

@ -64,7 +64,7 @@ namespace Yavsc.ViewModels.Streaming
{
var buffer = queue.Dequeue();
logger.LogInformation($"writing {buffer.Array.Length} bytes...");
logger.LogInformation($"writing {buffer.Count} bytes...");
await dest.WriteAsync(buffer.Array, buffer.Offset, buffer.Count);
logger.LogInformation($"done.");