コード例 #1
0
        public async Task InvokeAsync(HttpContext context)
        {
            var query = pathResolver.Parse(context.Request.Path);

            if (query.IsHandled && context.Request.Method.Equals("GET"))
            {
                context.Response.ContentType = "application/json";
                context.Response.StatusCode  = 200;

                string authToken = GetAuthorizarionToken(context);


                if (query.NodeType != null)
                {
                    nodeSerializer.StreamSchema(query.NodeType, authToken, context.Response.Body);
                }
                else
                {
                    nodeSerializer.WriteValue(context.Response.Body, authToken, factory.RegisterdTypeNames());
                }

                await context.Response.Body.FlushAsync();
            }
            else
            {
                await _next(context);
            }
        }
コード例 #2
0
 private void StreamResult(HttpContext context, string authToken, int depth, object respone)
 {
     if (respone is INode node)
     {
         nodeSerializer.StreamSerialize(node, authToken, context.Response.Body, depth, 0);
     }
     else
     {
         nodeSerializer.WriteValue(context.Response.Body, authToken, respone);
     }
 }
コード例 #3
0
    private void SendNodeArray(IEnumerable <INode> result)
    {
        if (IsOpen)
        {
            var stream = new MemoryStream();
            serializer.WriteValue(stream, CurrentToken, result);
            var dataToSend = new ArraySegment <byte>();
            stream.TryGetBuffer(out dataToSend);

            socket.SendAsync(dataToSend, WebSocketMessageType.Text, true, CancellationToken.None);
        }
    }
コード例 #4
0
 public void GetAll()
 {
     SendOkJson();
     _serializer.WriteValue(Response.Body, GetToken(), _factory.RegisterdTypeNames());
 }
コード例 #5
0
ファイル: FileMiddleware.cs プロジェクト: matst80/TinyCMS
        public async Task InvokeAsync(HttpContext context)
        {
            var pathValue = context.Request.Path.Value.TrimEnd('/').TrimStart('/');
            var pathParts = context.Request.Path.Value.Split('/');

            if (pathParts.Length > 0 && context.Request.Path.StartsWithSegments("/files"))
            {
                var token  = GetAuthorizarionToken(context);
                var isFile = pathParts.Last().Contains(".");

                if (isFile)
                {
                    var file = fileStorage.GetFile(pathValue);
                    if (context.Request.Method == "POST")
                    {
                        using (var fileStream = file.OpenWrite())
                        {
                            context.Response.ContentType = "application/json";

                            var frm = context.Request.Form;
                            foreach (var formFile in frm.Files)
                            {
                                formFile.CopyTo(fileStream);
                                break;
                            }

                            await fileStream.FlushAsync();

                            context.Response.StatusCode = 200;
                            serializer.WriteValue(context.Response.Body, token, file);
                        }
                    }
                    else
                    {
                        if (file != null && file.Exists)
                        {
                            using (var fileStream = file.OpenRead())
                            {
                                await fileStream.CopyToAsync(context.Response.Body);

                                await context.Response.Body.FlushAsync();
                            }
                        }
                        else
                        {
                            await _next(context);
                        }
                    }
                }
                else
                {
                    var directory = fileStorage.GetDirectory(pathValue);
                    if (directory != null)
                    {
                        if (context.Request.Method == "POST" && !directory.Exists)
                        {
                            directory.Create();
                        }
                        context.Response.ContentType = "application/json";
                        context.Response.StatusCode  = 200;
                        var files       = directory.GetFiles().ToArray();
                        var directories = directory.GetDirectories().ToArray();
                        serializer.WriteValue(context.Response.Body, token, new JsonResult()
                        {
                            Directories = directories,
                            Files       = files
                        });
                        await context.Response.WriteAsync("\n");

                        await context.Response.Body.FlushAsync();
                    }
                    else
                    {
                        await _next(context);
                    }
                }
            }
            else
            {
                await _next(context);
            }
        }