Working task geting and stream download

This commit is contained in:
Arti Zirk 2017-05-24 21:34:02 +03:00
parent dc150f1363
commit 54472c5f0a
7 changed files with 120 additions and 9 deletions

1
.gitignore vendored
View File

@ -259,3 +259,4 @@ paket-files/
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
/TaskRunner/tmp

View File

@ -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<IDictionary<string,string>> Uris { get; set; }
}
}

View File

@ -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; }
}
}

View File

@ -33,12 +33,31 @@ namespace TaskRunner
{
Console.WriteLine($"Waiting for tasks from server: {options.server}");
var server = new ServerApi(new Uri(options.server));
try
{
var task = await server.GetTask();
Console.WriteLine(task);
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());
}
}

View File

@ -0,0 +1,8 @@
{
"profiles": {
"TaskRunner": {
"commandName": "Project",
"commandLineArgs": "--server http://10.42.0.1:8080/api/v1/"
}
}
}

View File

@ -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<string> GetTask()
public async Task<TaskDTO> GetTask()
{
var resp = await client.GetStringAsync("get");
return resp;
var resp = await client.GetAsync("/Task");
if (!resp.IsSuccessStatusCode)
{
throw new FailedToGetTaskException();
}
return JsonConvert.DeserializeObject<TaskDTO>(await resp.Content.ReadAsStringAsync());
}
public async Task<StreamDTO> GetStream(string VideoUuid, int StreamId)
{
var resp = await client.GetAsync($"/Videos/{VideoUuid}/Streams/{StreamId}");
if (!resp.IsSuccessStatusCode)
{
throw new FailedToGetStreamException();
}
return JsonConvert.DeserializeObject<StreamDTO>(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<string> 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;
}
}
}

View File

@ -7,6 +7,8 @@
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="1.9.71" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.2" />
<PackageReference Include="System.IO.FileSystem" Version="4.3.0" />
</ItemGroup>
</Project>