Exemplo n.º 1
0
        public override ApiResponse ApiProviderViewPost(IRequest request)
        {
            var parentNodeId = new NodeIdentifier(
                   WebUtility.UrlDecode(request.PostArgs["parent_provider"]),
                   WebUtility.UrlDecode(request.PostArgs["parent_id"]));
            var parentNode = request.UnitOfWork.Nodes.FindById(parentNodeId);
            if (parentNode == null)
                return new BadRequestApiResponse();

            if (parentNode.User.Id != request.UserId)
                return new ForbiddenApiResponse();

            var results = new List<NodeWithRenderedLink>();
            if (string.IsNullOrEmpty(request.PostArgs["text"]))
                return new BadRequestApiResponse("Text is not specified");

            var newNode = new TextNode
            {
                DateAdded = DateTime.Now,
                Text = request.PostArgs["text"],
                User = request.User
            };

            request.UnitOfWork.Text.Save(newNode);
            results.Add(new NodeWithRenderedLink(newNode,
                Utilities.CreateLinkForNode(request, parentNode, newNode)));

            return new ApiResponse(results, 201, "Nodes added");
        }
Exemplo n.º 2
0
        public override ApiResponse ApiProviderViewPost(IRequest request)
        {
            var parentNodeId = new NodeIdentifier(
                  WebUtility.UrlDecode(request.PostArgs["parent_provider"]),
                  WebUtility.UrlDecode(request.PostArgs["parent_id"]));
            var parentNode = request.UnitOfWork.Nodes.FindById(parentNodeId);
            if (parentNode == null)
                return new BadRequestApiResponse();

            if (parentNode.User.Id != request.UserId)
                return new ForbiddenApiResponse();

            var results = new List<NodeWithRenderedLink>();
            
            if (string.IsNullOrEmpty(request.PostArgs["url"]))
                return new BadRequestApiResponse("Url is not specified");

            using (var info = new UrlInfoParser(request.PostArgs["url"]))
            {
                string imageFileName = null;
                if (!string.IsNullOrEmpty(info.ImageUrl))
                {
                    var dir = Path.Combine(Settings.Default.FileStorage, "thumbnails");
                    Directory.CreateDirectory(dir);
                    imageFileName = Path.GetRandomFileName() + info.ImageFileName;
                    info.SaveImage(Path.Combine(dir, imageFileName));
                }

                var newNode = new URLNode
                {
                    DateAdded = DateTime.Now,
                    URL = request.PostArgs["url"],
                    User = request.User,
                    Name = info.Title,
                };

                if (!string.IsNullOrEmpty(imageFileName))
                    newNode.Image = "/media/thumbnails/" + imageFileName;

                request.UnitOfWork.URL.Save(newNode);
                results.Add(new NodeWithRenderedLink(newNode,
                    Utilities.CreateLinkForNode(request, parentNode, newNode)));

                if (!string.IsNullOrEmpty(request.PostArgs["download"]))
                {
                    var dir = Path.Combine(Settings.Default.FileStorage, request.User.Username);
                    Directory.CreateDirectory(dir);
                    var fileNode = request.UnitOfWork.Files.CreateFileFromStream(Path.Combine(dir, info.FileName), info.ContentStream);
                    Utilities.CreateLinkForNode(request, newNode, fileNode);
                }
            }

            return new ApiResponse(results, 201, "Nodes added");
        }
Exemplo n.º 3
0
        public override ApiResponse ApiProviderViewPost(IRequest request)
        {
            var parentNodeId = new NodeIdentifier(
                    WebUtility.UrlDecode(request.PostArgs["parent_provider"]),
                    WebUtility.UrlDecode(request.PostArgs["parent_id"]));
            var parentNode = request.UnitOfWork.Nodes.FindById(parentNodeId);

            if (parentNode == null)
                return new BadRequestApiResponse();

            if (parentNode.User.Id != request.UserId)
                return new ForbiddenApiResponse();

            var results = new List<NodeWithRenderedLink>();

            if (!request.Files.Any())
                return new BadRequestApiResponse("No files passed");

            var dir = Path.Combine(Settings.Default.FileStorage, request.User.Username);
            Directory.CreateDirectory(dir);

            foreach (var file in request.Files)
            {
                var filePath = Path.Combine(dir, file.FileName);
                try
                {
                    var fileNode = request.UnitOfWork.Files.CreateFileFromStream(filePath, file.Data);
                    results.Add(new NodeWithRenderedLink(fileNode,
                        Utilities.CreateLinkForNode(request, parentNode, fileNode)));
                }
                catch (Exception ex)
                {
                    return new BadRequestApiResponse(ex.Message);
                }
            }

            return new ApiResponse(results, 201, "Nodes added");
        }
Exemplo n.º 4
0
        private static Response LinksView(IRequest request)
        {
            if (request.UserId == null)
                return new NonAuthorizedApiResponse();
            
            if (request.Method == "POST")
            {
                var parentNodeId = new NodeIdentifier(
                    WebUtility.UrlDecode(request.PostArgs["parent_provider"]), 
                    WebUtility.UrlDecode(request.PostArgs["parent_id"]));
                var parentNode = request.UnitOfWork.Nodes.FindById(parentNodeId);

                if (parentNode == null)
                    return new BadRequestApiResponse();

                if (parentNode.User.Id != request.UserId)
                    return new ForbiddenApiResponse();

                var results = new List<NodeWithRenderedLink>();
               
                var endNodeId = new NodeIdentifier(
                        WebUtility.UrlDecode(request.PostArgs["end_provider"]),
                        WebUtility.UrlDecode(request.PostArgs["end_id"]));
                var endNode = request.UnitOfWork.Nodes.FindById(endNodeId);

                if(endNode == null)
                    return new BadRequestApiResponse();

                results.Add(new NodeWithRenderedLink(endNode,
                    Utilities.CreateLinkForNode(request, parentNode, endNode)));

                return new ApiResponse(results, 201, "Nodes added");
            }

            return new BadRequestApiResponse();
        }