示例#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["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");
        }
示例#2
0
 public UrlNodeDrop(URLNode node) : base(node)
 {
     _node = node;
 }
示例#3
0
 public static string GetKnownExtension(URLNode node)
 {
     var uri = new Uri(node.URL);
     var ext = Path.GetExtension(uri.LocalPath);
     return FileProvider.KnownExtensions.Contains(ext) ? ext?.Substring(1) : "_page";
 }