From 54472c5f0a8dac5fb237378b9e1be1cc90d4fc10 Mon Sep 17 00:00:00 2001 From: Arti Zirk Date: Wed, 24 May 2017 21:34:02 +0300 Subject: [PATCH] Working task geting and stream download --- .gitignore | 3 +- TaskRunner/DTOs/StreamDTO.cs | 13 ++++++ TaskRunner/DTOs/TaskDTO.cs | 23 ++++++++++ TaskRunner/Program.cs | 27 ++++++++++-- TaskRunner/Properties/launchSettings.json | 8 ++++ TaskRunner/ServerAPI.cs | 53 +++++++++++++++++++++-- TaskRunner/TaskRunner.csproj | 2 + 7 files changed, 120 insertions(+), 9 deletions(-) create mode 100644 TaskRunner/DTOs/StreamDTO.cs create mode 100644 TaskRunner/DTOs/TaskDTO.cs create mode 100644 TaskRunner/Properties/launchSettings.json diff --git a/.gitignore b/.gitignore index 3c4efe2..6dd2419 100644 --- a/.gitignore +++ b/.gitignore @@ -258,4 +258,5 @@ paket-files/ # Python Tools for Visual Studio (PTVS) __pycache__/ -*.pyc \ No newline at end of file +*.pyc +/TaskRunner/tmp diff --git a/TaskRunner/DTOs/StreamDTO.cs b/TaskRunner/DTOs/StreamDTO.cs new file mode 100644 index 0000000..b42e41d --- /dev/null +++ b/TaskRunner/DTOs/StreamDTO.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace TaskRunner.DTOs +{ + public class StreamDTO + { + public int StreamId { get; set; } + public string MimeType { get; set; } + public IList> Uris { get; set; } + } +} diff --git a/TaskRunner/DTOs/TaskDTO.cs b/TaskRunner/DTOs/TaskDTO.cs new file mode 100644 index 0000000..567ffa8 --- /dev/null +++ b/TaskRunner/DTOs/TaskDTO.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace TaskRunner.DTOs +{ + public enum TaskStatus + { + Created, + Starting, + Running, + Finished, + Error + } + + public class TaskDTO + { + public int TaskId { get; set; } + public TaskStatus TaskStatus { get; set; } + public int SourceStreamId { get; set; } + public int DestinationStreamId { get; set; } + } +} diff --git a/TaskRunner/Program.cs b/TaskRunner/Program.cs index 7aefd74..b3bb919 100644 --- a/TaskRunner/Program.cs +++ b/TaskRunner/Program.cs @@ -22,7 +22,7 @@ namespace TaskRunner public class Program { public static void Main(string[] args) - { + { MainAsync(args).Wait(); } @@ -33,12 +33,31 @@ namespace TaskRunner { Console.WriteLine($"Waiting for tasks from server: {options.server}"); var server = new ServerApi(new Uri(options.server)); - var task = await server.GetTask(); - Console.WriteLine(task); + try + { + var task = await server.GetTask(); + var src = await server.GetStream("asdf", task.SourceStreamId); + var dest = await server.GetStream("asdf", task.DestinationStreamId); + Console.WriteLine($"Got task {task.TaskId}"); + Console.WriteLine($"Source: {src.MimeType}"); + Console.WriteLine($"Dest: {dest.MimeType}"); + + foreach (var strUri in src.Uris) + { + var uri = new Uri(strUri["Uri"]); + var fs = new FileStorage(@"\Users\Arti.Zirk\Documents\files"); + var path = await fs.DownloadFile(uri); + Console.WriteLine(path); + } + } catch (FailedToGetTaskException) { + Console.WriteLine("No tasks available"); + return; + } + var taskManager = new TaskManager(); await taskManager.RunFfmpeg(); } else { - Console.WriteLine(options.GetUsage()); + //Console.WriteLine(options.GetUsage()); } } diff --git a/TaskRunner/Properties/launchSettings.json b/TaskRunner/Properties/launchSettings.json new file mode 100644 index 0000000..4bcf1fd --- /dev/null +++ b/TaskRunner/Properties/launchSettings.json @@ -0,0 +1,8 @@ +{ + "profiles": { + "TaskRunner": { + "commandName": "Project", + "commandLineArgs": "--server http://10.42.0.1:8080/api/v1/" + } + } +} \ No newline at end of file diff --git a/TaskRunner/ServerAPI.cs b/TaskRunner/ServerAPI.cs index 1084d42..e9cf166 100644 --- a/TaskRunner/ServerAPI.cs +++ b/TaskRunner/ServerAPI.cs @@ -3,10 +3,15 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Net.Http; - +using TaskRunner.DTOs; +using Newtonsoft.Json; +using System.IO; namespace TaskRunner { + public class TaskRunnerException:Exception { } + public class FailedToGetTaskException:TaskRunnerException { } + public class FailedToGetStreamException:TaskRunnerException { } public class ServerApi { public Uri serverUri; @@ -18,10 +23,50 @@ namespace TaskRunner client.BaseAddress = serverUri; } - public async Task GetTask() + public async Task GetTask() { - var resp = await client.GetStringAsync("get"); - return resp; + var resp = await client.GetAsync("/Task"); + if (!resp.IsSuccessStatusCode) + { + throw new FailedToGetTaskException(); + } + return JsonConvert.DeserializeObject(await resp.Content.ReadAsStringAsync()); + } + + public async Task GetStream(string VideoUuid, int StreamId) + { + var resp = await client.GetAsync($"/Videos/{VideoUuid}/Streams/{StreamId}"); + if (!resp.IsSuccessStatusCode) + { + throw new FailedToGetStreamException(); + } + return JsonConvert.DeserializeObject(await resp.Content.ReadAsStringAsync()); + } + } + + public class FileStorage + { + private string baseDir = Path.GetFullPath("files"); + public FileStorage() { } + public FileStorage(string dir) { + baseDir = Path.GetFullPath(dir); + } + public async Task DownloadFile(Uri uri) + { + var entity = await (new HttpClient()).GetAsync(uri); + if (!entity.IsSuccessStatusCode) + { + throw new TaskRunnerException(); + } + var fileName = uri.Segments.Last(); + var fullPath = Path.Combine(baseDir, fileName); + Console.WriteLine(fileName); + Directory.CreateDirectory(baseDir); + //File.Create(fullPath); + using (var fos = File.OpenWrite(fullPath)) { + await entity.Content.CopyToAsync(fos); + } + return fullPath; } } } diff --git a/TaskRunner/TaskRunner.csproj b/TaskRunner/TaskRunner.csproj index ef6462a..61d1db6 100644 --- a/TaskRunner/TaskRunner.csproj +++ b/TaskRunner/TaskRunner.csproj @@ -7,6 +7,8 @@ + +