From 3cb14c964807ae81133153ff59e3b446ffc9325b Mon Sep 17 00:00:00 2001 From: YDKK Date: Wed, 5 Sep 2018 18:50:00 +0900 Subject: [PATCH] Support verify action Remove dependency on DynamicJson Bump version to v1.0 --- .gitignore | 3 ++- LFS2S3Proxy/Program.cs | 20 ++++++++++++------ LFS2S3Proxy/S3Proxy.cs | 48 +++++++++++++++++++++++++++++++----------- 3 files changed, 52 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 3c4efe2..bdbb63f 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 +/LFS2S3Proxy/config.json diff --git a/LFS2S3Proxy/Program.cs b/LFS2S3Proxy/Program.cs index 2f2485e..85b2ef7 100644 --- a/LFS2S3Proxy/Program.cs +++ b/LFS2S3Proxy/Program.cs @@ -5,17 +5,19 @@ using System.Net; using System.Reactive.Linq; using System.Reactive.Subjects; +using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using Amazon.Runtime.Internal; -using Codeplex.Data; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using Topshelf; namespace LFS2S3Proxy { class Program { - const string version = "LFS2S3Proxy v0.3"; + const string version = "LFS2S3Proxy v1.0"; static void Main(string[] args) { HostFactory.Run(x => @@ -68,14 +70,14 @@ public void Start() return; } - var config = DynamicJson.Parse(File.ReadAllText("config.json")); - _proxy = new S3Proxy(config.s3bucketName); + dynamic config = JObject.Parse(File.ReadAllText("config.json")); + _proxy = new S3Proxy((string)config.s3bucketName); _token = config.token; - _server = new HttpServer(config.listen); + _server = new HttpServer((string)config.listen); - _list.Add(_server.Subscribe(Event)); _list.Add(_server.Subscribe(ctx => Console.WriteLine("Request to: " + ctx.Request.RawUrl))); + _list.Add(_server.Subscribe(Event)); Console.WriteLine($"{version} started at {config.listen}"); } @@ -97,6 +99,12 @@ private static void Event(RequestContext ctx) return; } + if (ctx.Request.RawUrl.EndsWith("/verify") && ctx.Request.RawUrl.Count(x => x == '/') == 3) + { + var repositoryName = ctx.Request.RawUrl.Split('/')[2]; + _proxy.Verify(ctx, repositoryName); + return; + } } ctx.Respond(new StringResponse("Not Found.", 404)); } diff --git a/LFS2S3Proxy/S3Proxy.cs b/LFS2S3Proxy/S3Proxy.cs index 3872f0b..91dab07 100644 --- a/LFS2S3Proxy/S3Proxy.cs +++ b/LFS2S3Proxy/S3Proxy.cs @@ -5,10 +5,11 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Web.UI.WebControls; using Amazon.S3; using Amazon.S3.Model; -using Codeplex.Data; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; namespace LFS2S3Proxy { @@ -54,20 +55,41 @@ private string GeneratePreSignedUrl(string repositoryName, string key, HttpVerb } } - public void Request(RequestContext context, string repositoryName) + private dynamic GetJson(RequestContext context) { - var stream = new MemoryStream(); - context.Request.InputStream.CopyTo(stream); + using (var stream = new MemoryStream()) + { + context.Request.InputStream.CopyTo(stream); - var json = Encoding.UTF8.GetString(stream.ToArray()); + var json = Encoding.UTF8.GetString(stream.ToArray()); + + return JObject.Parse(json); + } + } - var data = DynamicJson.Parse(json); + public void Verify(RequestContext context, string repositoryName) + { + var data = GetJson(context); + + var (exist, metadata) = CheckExists(repositoryName, (string)data.oid); + if (exist) + { + context.Respond(new Response()); + return; + } + + context.Respond(new EmptyResponse(404)); + } + + public void Request(RequestContext context, string repositoryName) + { + var data = GetJson(context); dynamic response = new ExpandoObject(); response.objects = new List(); - Parallel.ForEach((IEnumerable)(object[])data.objects, o => + Parallel.ForEach((IEnumerable)(JArray)data.objects, o => { dynamic eo = new ExpandoObject(); eo.oid = o.oid; @@ -78,7 +100,7 @@ public void Request(RequestContext context, string repositoryName) } }); - switch (data.operation) + switch ((string)data.operation) { case "upload": Parallel.ForEach((IEnumerable)response.objects, o => @@ -90,8 +112,12 @@ public void Request(RequestContext context, string repositoryName) { upload = new { - href = GeneratePreSignedUrl(repositoryName, o.oid, HttpVerb.PUT), + href = GeneratePreSignedUrl(repositoryName, (string)o.oid, HttpVerb.PUT), expires_at = DateTime.Now + _urlExpiresAt + }, + verify = new + { + href = $"https://{context.Request.Headers["Host"].First()}{context.Request.RawUrl.Replace("/objects/batch", "/verify")}" } }; } @@ -107,7 +133,7 @@ public void Request(RequestContext context, string repositoryName) { download = new { - href = GeneratePreSignedUrl(repositoryName, o.oid, HttpVerb.GET), + href = GeneratePreSignedUrl(repositoryName, (string)o.oid, HttpVerb.GET), expires_at = DateTime.Now + _urlExpiresAt } }; @@ -122,8 +148,6 @@ public void Request(RequestContext context, string repositoryName) } }); break; - case "verify": - break;//not supported yet. } var res = new StringResponse(JsonConvert.SerializeObject(response));