public async Task <ActionResult> PlayMedia(string mediaFileIdStr, double startPositionInSec) { if (!MediaFileId.TryParse(mediaFileIdStr, out var mediaFileId)) { return(BadRequest($"{nameof(mediaFileIdStr)} is invalid")); } if (startPositionInSec < 0) { return(BadRequest($"{nameof(startPositionInSec)} must be > 0")); } var settings = _settingsService.Get(); if (string.IsNullOrWhiteSpace(settings.System.MpvPath)) { return(BadRequest("mpv path must be set in the settings tab")); } if (!_processManager.IsMpvRunning()) { var arguments = new[] { $"--input-ipc-server={NamedPipeFactory.GetPipeNameForCurrentOs()}" }; _processManager.StartProcess(settings.System.MpvPath, arguments); } if (!_processManager.IsSvpRunning() && !string.IsNullOrWhiteSpace(settings.System.SvpPath)) { _processManager.StartProcess(settings.System.SvpPath); } var fileInfo = await _fileLookupProvider.GetFileInfoForId(mediaFileId.FileId); _queue.AddToQueue(new Item { Path = fileInfo.Path, MediaFileId = mediaFileId, StartPosition = TimeSpan.FromSeconds(startPositionInSec), }); return(StatusCode(202)); }
public override async Task Download ( DownloadFileRequest request, IServerStreamWriter <ResponseDownloadFile> responseStream, ServerCallContext context ) { if (!MediaFileId.TryParse(request.MediaFileId, out var mediaFileId)) { throw new Exception("Invalid mediaFileId"); } var info = await _episodeFileLookupProvider.GetFileInfoForId(mediaFileId.FileId); var backSlashIndex = info.Path.LastIndexOf("\\", StringComparison.CurrentCultureIgnoreCase); var index = backSlashIndex > -1 ? backSlashIndex : info.Path.LastIndexOf("/", StringComparison.CurrentCultureIgnoreCase); var fileName = info.Path.Substring(index + 1); var chunkSize = 2048; using (Stream source = File.OpenRead(info.Path)) { byte[] buffer = new byte[chunkSize]; int bytesRead; await responseStream.WriteAsync(new ResponseDownloadFile { Started = new Started { FileName = fileName, Lenght = source.Length, SizePerChunk = chunkSize, } }); while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0) { await responseStream.WriteAsync(new ResponseDownloadFile { Progress = new Chunk { Content = ByteString.CopyFrom(buffer, 0, bytesRead) } }); } source.Seek(0, SeekOrigin.Begin); string hash; using (var md5 = MD5.Create()) { var byteHash = md5.ComputeHash(source); hash = BitConverter.ToString(byteHash).Replace("-", "").ToLowerInvariant(); } await responseStream.WriteAsync(new ResponseDownloadFile { Done = new Done { Hash = hash } }); } }