public override Response NodeAction(IRequest request, Node node, string action)
        {
            var textNode = node as TextNode;
            if(textNode == null)
                throw new InvalidOperationException("Node is not a TextNode");

            var arialUni = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF");
            //var bf = BaseFont.CreateFont(arialUni, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

            if (action.Equals("pdf"))
            {
                FontFactory.Register(arialUni, "Arial");
                using (var ms = new MemoryStream())
                using (var doc = new Document(PageSize.A4))
                using (var writer = PdfWriter.GetInstance(doc, ms))
                {
                    writer.InitialLeading = 12f;
                    doc.Open();

                    using (var htmlWorker = new HTMLWorker(doc))
                    using (var sr = new StringReader(textNode.Text))
                    {
                        htmlWorker.Parse(sr);
                    }
                    
                    doc.Close();
                    return new HttpResponse(ms.ToArray(), contenttype: "application/pdf");
                }
            }
            return base.ApiNodeAction(request, node, action);
        }
示例#2
0
 protected NodeDrop(Node node)
 {
     NodeId = node.NodeId.Id;
     Provider = node.NodeId.Provider;
     Link = string.Format("/{0}/{1}", Provider, WebUtility.UrlEncode(NodeId));
     if (node.User != null)
         UserId = node.User.Id;
 }
        public virtual ApiResponse ApiNodeViewDelete(IRequest request, Node node)
        {
            if (Equals(node.NodeId, new NodeIdentifier("text", request.User.Home.Id)))
                return new ForbiddenApiResponse("Cannot delete own home");

            Utilities.DeleteLinks(request.UnitOfWork, node);
            request.UnitOfWork.Nodes.Delete(node);
            return new ApiResponse(statusMessage: "Node deleted");
        }
示例#4
0
 public Link(Node start, Node end)
 {
     if (start != null && start.NodeId != null)
     {
         StartNode = start.NodeId.Id;
         StartNodeProvider = start.NodeId.Provider;
     }
     EndNode = end.NodeId.Id;
     EndNodeProvider = end.NodeId.Provider;
 }
        public override ApiResponse ApiNodeViewPut(IRequest request, Node node)
        {
            if (request.PostArgs == null)
                return new BadRequestApiResponse("No arguments");

            if (string.IsNullOrEmpty(request.PostArgs["text"]))
                return new BadRequestApiResponse();

            ((TextNode)node).Text = request.PostArgs["text"];
            request.UnitOfWork.Nodes.Save(node);
            return new ApiResponse(NodeDropFactory.Create(node), statusMessage: "Saved");
        }
        public override Response NodeView(IRequest request, Node node)
        {
            var textNode = node as TextNode;
            if (textNode == null)
                throw new InvalidOperationException("Node is not a TextNode");

            return new TemplatedResponse("text_node", new
            {
                Title = "Home",
                Node = new TextNodeDrop(textNode),
                Links = Utilities.GetGroupedLinks(request.UnitOfWork, node)
            });
        }
示例#7
0
        public override Response NodeView(IRequest request, Node node)
        {
            var urlNode = node as URLNode;
            if(urlNode == null)
                throw new InvalidOperationException("Node is not an URLNode");

            return new TemplatedResponse("url_node", new
            {
                Title = urlNode.Name,
                Node = new UrlNodeDrop(urlNode),
                Links = Utilities.GetGroupedLinks(request.UnitOfWork, node)
            });
        }
示例#8
0
        public override Response NodeAction(IRequest request, Node node, string action)
        {
            var baseFileNode = node as BaseFileNode;
            if(baseFileNode == null)
                throw new InvalidOperationException("Node is not a BaseFileNode");

            if (action.Equals("raw"))
            {
                if (baseFileNode.IsDirectory)
                    throw new InvalidOperationException("Not a file");
                var fileNode = node as FileNode;
                if (fileNode == null)
                    throw new InvalidOperationException("Not a file");
                
                return new StreamedHttpResponse(File.OpenRead(baseFileNode.Path), contenttype: fileNode.Mime,
                    headers: new Dictionary<string, string>
                    {
                        {"Content-Disposition", $"inline; filename=\"{Uri.EscapeDataString(fileNode.Name)}\""},
                        {"X-Sendfile", Uri.EscapeDataString(fileNode.Name)}
                    });
            }

            if (action.Equals("download"))
            {
                if (baseFileNode.IsDirectory)
                    throw new InvalidOperationException("Not a file");
                var fileNode = node as FileNode;
                if (fileNode == null)
                    throw new InvalidOperationException("Not a file");

                return new StreamedHttpResponse(File.OpenRead(baseFileNode.Path), contenttype: "application/force-download",
                    headers: new Dictionary<string, string>
                    {
                        {"Content-Disposition", $"attachment; filename=\"{Uri.EscapeDataString(fileNode.Name)}\""},
                        {"X-Sendfile", Uri.EscapeDataString(fileNode.Name)}
                    });
            }

            if (action.Equals("edit"))
            {
                return new TemplatedResponse("Files/text", new
                {
                    Title = baseFileNode.Name,
                    Node = new FileNodeDrop(baseFileNode),
                    Links = Utilities.GetGroupedLinks(request.UnitOfWork, node)
                });
            }

            return base.NodeAction(request, node, action);
        }
        public static NodeDrop Create(Node node)
        {
            if (node is TextNode)
                return new TextNodeDrop((TextNode) node);

            if (node is URLNode)
                return new UrlNodeDrop((URLNode) node);

            if (node is FileNode)
                return new FileNodeDrop((FileNode) node);

            if (node is DirectoryNode)
                return new DirectoryNodeDrop((DirectoryNode) node);

            return null;
        }
示例#10
0
        public static List<RenderedLinkDrop> GetRenderedLinkDrops(UnitOfWork unit, Node node)
        {
            var links =
                unit.Links.Where(l => l.StartNode == node.NodeId.Id && l.StartNodeProvider == node.NodeId.Provider)
                    .ToList();
            var linkDrops =
                links.Select(link => new RenderedLinkDrop(link, unit.Nodes.FindById(link.GetEndIdentifier()))).ToList();

            var dir = node as DirectoryNode;
            if (dir != null)
            {
                linkDrops.AddRange(dir.GetChild().Select(ch => new RenderedLinkDrop(new Link(node, ch)
                {
                    Comment = ch.IsDirectory ? "Folder" : "File"
                }, ch)));
            }
            return linkDrops;
        }
示例#11
0
        public static IEnumerable<LinksGroupDrop> GetGroupedLinks(UnitOfWork unit, Node node)
        {
            var gId = 0;
            var linkDrops = GetLinkDrops(unit, node);
            if (linkDrops.Count == 0)
                return new List<LinksGroupDrop> {new LinksGroupDrop(gId) };

            var groups = new List<LinksGroupDrop>();
            var unnamedGroup = new LinksGroupDrop(gId++);
            groups.Add(unnamedGroup);

            foreach (var link in linkDrops)
            {
                if (!string.IsNullOrWhiteSpace(link.Comment))
                {
                    var sameRealtionLink =
                        unnamedGroup.Items.FirstOrDefault(
                            l =>
                                l.Comment != null &&
                                l.Comment.Equals(link.Comment, StringComparison.CurrentCultureIgnoreCase));
                    if (sameRealtionLink != null)
                    {
                        unnamedGroup.Items.Remove(sameRealtionLink);
                        var group = new LinksGroupDrop(gId++, new List<LinkDrop> {link, sameRealtionLink});
                        groups.Add(group);
                        continue;
                    }

                    var groupWithSameRelation =
                        groups.FirstOrDefault(
                            g => g.Name.Equals(link.Comment, StringComparison.CurrentCultureIgnoreCase));
                    if (groupWithSameRelation != null)
                    {
                        groupWithSameRelation.Items.Add(link);
                        continue;
                    }
                }

                unnamedGroup.Items.Add(link);
            }

            return groups;
        }
示例#12
0
        public override Response NodeAction(IRequest request, Node node, string action)
        {
            var urlNode = node as URLNode;
            if (urlNode == null)
                throw new InvalidOperationException("Node is not an URLNode");

            if (action.Equals("download"))
            {
                using (var info = new UrlInfoParser(urlNode.URL))
                {
                    var dir = Path.Combine(Settings.Default.FileStorage, request.User.Username);
                    Directory.CreateDirectory(dir);
                    var path = Path.Combine(dir, info.FileName);
                    info.SaveContent(path);
                    var fileNode = new FileNode(path);
                    request.UnitOfWork.Files.Save(fileNode);
                    Utilities.CreateLinkForNode(request, urlNode, fileNode, "Downloaded");
                }
                return new RedirectResponse("/url/" + urlNode.Id);
            }

            return base.NodeAction(request, node, action);
        }
示例#13
0
        public override Response NodeView(IRequest request, Node node)
        {
            var baseFileNode = node as BaseFileNode;
            if (baseFileNode == null)
                throw new InvalidOperationException("Node is not a BaseFileNode");

            if (baseFileNode.IsDirectory)
            {
                return new TemplatedResponse("file_node", new
                {
                    Title = baseFileNode.Name,
                    Node = new DirectoryNodeDrop(baseFileNode),
                    Links = Utilities.GetGroupedLinks(request.UnitOfWork, node)
                });
            }

            var template = "file_node";
            var fileNode = node as FileNode;
            if (fileNode == null)
                throw new InvalidOperationException("Incorrect file node");

            if (EditableFileTypes.Contains(fileNode.Mime))
                template = "Files/text";
            else if (fileNode.Mime.Contains("image/"))
                template = "Files/image";
            else if (fileNode.Mime.Contains("video/"))
                template = "Files/video";
            else if (fileNode.Mime.Contains("audio/"))
                template = "Files/audio";

            return new TemplatedResponse(template, new
            {
                Title = baseFileNode.Name,
                Node = new FileNodeDrop(baseFileNode),
                Links = Utilities.GetGroupedLinks(request.UnitOfWork, node)
            });
        }
示例#14
0
 public virtual ApiResponse ApiNodeViewGet(IRequest request, Node node)
 {
     return new ApiResponse(NodeDropFactory.Create(node));
 }
示例#15
0
 public abstract Response NodeView(IRequest request, Node node);
 public NodeWithLink(Node node) : this(NodeDropFactory.Create(node), new AnonymousLinkDrop(node))
 {
 }
 public AnonymousRenderedLinkDrop(Node endnode) : base(endnode)
 {
     Rendered = TemplateUtilities.RenderTemplate("Blocks/link", new { link = this });
 }
示例#18
0
 public LinkDrop(Link link, Node endnode)
 {
     _link = link;
     Node = NodeDropFactory.Create(endnode);
 }
 public AnonymousLinkDrop(Node endnode, string comment = "")
 {
     Node = NodeDropFactory.Create(endnode);
     Comment = comment;
 }
 public NodeWithRenderedLink(Node node) : base(NodeDropFactory.Create(node), new AnonymousRenderedLinkDrop(node))
 {
 }
 public NodeWithRenderedLink(Node node, Link link) : this(NodeDropFactory.Create(node), new RenderedLinkDrop(link, node))
 {
 }
示例#22
0
 public virtual ApiResponse ApiNodeViewPut(IRequest request, Node node)
 {
     throw new System.NotImplementedException();
 }
示例#23
0
        public static void DeleteLinks(UnitOfWork unit, Node node)
        {
            var outLinks = unit.Links.Where(
                l => l.StartNodeProvider == node.NodeId.Provider && l.StartNode == node.NodeId.Id);

            var inLinks = unit.Links.Where(
                l => l.EndNodeProvider == node.NodeId.Provider && l.EndNode == node.NodeId.Id);

            foreach (var inLink in inLinks)
                unit.Links.Delete(inLink);

            foreach (var outLink in outLinks)
                unit.Links.Delete(outLink);
        }
示例#24
0
 public virtual ApiResponse ApiNodeAction(IRequest request, Node node, string action)
 {
     throw new System.NotImplementedException();
 }
示例#25
0
 public static Link CreateLinkForNode(IRequest request, Node parentNode, Node newNode, string comment)
 {
     var link = new Link(parentNode, newNode)
     {
         Comment = comment,
         DateAdded = DateTime.Now,
         User = request.User
     };
     request.UnitOfWork.Links.Save(link);
     return link;
 }
示例#26
0
 public RenderedNodeDrop(Node node) : base(node)
 {
     var link = new AnonymousRenderedLinkDrop(node);
     Rendered = link.Rendered;
 }
示例#27
0
 public RenderedLinkDrop(Link link, Node endnode) : base(link, endnode)
 {
     Rendered = TemplateUtilities.RenderTemplate("Blocks/link", new {link = this});
 }