Skip to content

Commit

Permalink
Support verify action
Browse files Browse the repository at this point in the history
Remove dependency on DynamicJson
Bump version to v1.0
  • Loading branch information
YDKK committed Sep 5, 2018
1 parent 6acf2e5 commit 3cb14c9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,5 @@ paket-files/

# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
*.pyc
/LFS2S3Proxy/config.json
20 changes: 14 additions & 6 deletions LFS2S3Proxy/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down Expand Up @@ -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}");
}
Expand All @@ -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));
}
Expand Down
48 changes: 36 additions & 12 deletions LFS2S3Proxy/S3Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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<ExpandoObject>();

Parallel.ForEach((IEnumerable<dynamic>)(object[])data.objects, o =>
Parallel.ForEach((IEnumerable<dynamic>)(JArray)data.objects, o =>
{
dynamic eo = new ExpandoObject();
eo.oid = o.oid;
Expand All @@ -78,7 +100,7 @@ public void Request(RequestContext context, string repositoryName)
}
});

switch (data.operation)
switch ((string)data.operation)
{
case "upload":
Parallel.ForEach((IEnumerable<dynamic>)response.objects, o =>
Expand All @@ -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")}"
}
};
}
Expand All @@ -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
}
};
Expand All @@ -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));
Expand Down

0 comments on commit 3cb14c9

Please sign in to comment.