private void WriteRetrieve(HttpContext context)
        {
            IList <Directory> dirs;
            IList <File>      files;
            var selected = Selection.SelectedItem;

            var host     = Engine.Resolve <IHost>();
            var rootItem = Engine.Persister.Get(host.DefaultSite.RootItemID);
            var root     = new HierarchyNode <ContentItem>(rootItem);

            var selectionTrail = Find.EnumerateParents(selected, null, true).ToList().Where(a => a is AbstractNode).Reverse().ToList();
            var selectedPath   = selected.Path;

            FS = Engine.Resolve <IFileSystem>();

            var dir = selected as Directory;

            if (dir == null)
            {
                var uploadDirectories = MediaBrowserUtils.GetAvailableUploadFoldersForAllSites(context, root, selectionTrail, Engine, FS);
                dirs  = new List <Directory>();
                files = new List <File>();
                foreach (var updDir in uploadDirectories)
                {
                    dirs.Add(updDir.Current as Directory);
                }
                selectedPath = "/";
            }
            else
            {
                dirs  = dir.GetDirectories();
                files = dir.GetFiles();
            }

            var directory  = FS.GetDirectory(selectedPath);
            var fsRootPath = directory != null && !string.IsNullOrWhiteSpace(directory.RootPath) ? directory.RootPath : "";

            var selectableExtensions = context.Request["exts"];

            context.Response.WriteJson(new
            {
                Path  = selectedPath,
                Total = dirs.Count + files.Count,
                Trail = selectionTrail.Select(d => new { d.Title, Url = d.Url }).ToList(),
                Dirs  = dirs.Select(d => new { d.Title, Url = VirtualPathUtility.ToAppRelative(d.LocalUrl).Trim('~') }).ToList(),
                Files = GetFileReducedList(files.ToList(), ImageSizes, selectableExtensions, fsRootPath)
            });
        }
Exemplo n.º 2
0
        protected override void OnInit(EventArgs e)
        {
            FS     = Engine.Resolve <IFileSystem>();
            Config = Engine.Resolve <ConfigurationManagerWrapper>().Sections.Management;
            var selected = Selection.SelectedItem;

            var host           = Engine.Resolve <IHost>();
            var rootItem       = Engine.Persister.Get(host.DefaultSite.RootItemID);
            var root           = new HierarchyNode <ContentItem>(rootItem);
            var selectionTrail = new List <ContentItem>();

            var ckEditor             = Request["ckEditor"];
            var ckEditorFuncNum      = Request["ckEditorFuncNum"];
            var mediaCtrl            = Request["mediaControl"];
            var preferredSize        = Request["preferredSize"];
            var selectableExtensions = Request["selectableExtensions"];
            var selectedUrl          = Request["selectedUrl"];
            var defaultDirectoryPath = Request["defaultDirectoryPath"];
            var mediaBrowserHandler  = new MediaBrowserHandler();

            mediaBrowserModel = new MediaBrowserModel
            {
                CkEditor             = ckEditor,
                CkEditorFuncNum      = ckEditorFuncNum,
                MediaControl         = mediaCtrl,
                HandlerUrl           = AjaxMediaBrowserRetriever,
                PreferredSize        = preferredSize,
                Breadcrumb           = new string[] { },
                Path                 = "",
                RootIsSelectable     = false,
                DefaultDirectoryPath = defaultDirectoryPath
            };


            if (selected is AbstractNode)
            {
                selectionTrail = new List <ContentItem>(Find.EnumerateParents(selected, null, true));
            }
            else
            {
                mediaBrowserHandler.TrySelectingPrevious(ref selected, ref selectionTrail);
            }

            var uploadDirectories = MediaBrowserUtils.GetAvailableUploadFoldersForAllSites(Context, root, selectionTrail, Engine, FS);

            if (uploadDirectories.Count > 0)
            {
                var imageSizes = Engine.Resolve <Management.Files.FileSystem.Pages.ImageSizeCache>();
                if (uploadDirectories.Count == 1 || selectionTrail.Count > 1)
                {
                    var directory  = FS.GetDirectory(mediaBrowserModel.Path);
                    var fsRootPath = directory != null && !string.IsNullOrWhiteSpace(directory.RootPath) ? directory.RootPath : "";

                    var dir = (selectionTrail.Count > 0 ? selectionTrail.ElementAt(0) : uploadDirectories[0].Current) as Directory;


                    //Find and/or create default upload directory for templated content.
                    if (!string.IsNullOrWhiteSpace(mediaBrowserModel.DefaultDirectoryPath))
                    {
                        var segments = (mediaBrowserModel.DefaultDirectoryPath.Split(new[] { '/' }, 3, StringSplitOptions.RemoveEmptyEntries));
                        if (segments.Length == 3)
                        {
                            var siteName         = segments[0];
                            var uploadSiteFolder = dir.GetDirectories().FirstOrDefault(d => d.Name == siteName.ToLower());
                            if (uploadSiteFolder == null)
                            {
                                var newDir = VirtualPathUtility.AppendTrailingSlash(Request.ApplicationPath + dir.LocalUrl) + siteName.ToLower();
                                FS.CreateDirectory(newDir);
                                uploadSiteFolder = dir.GetDirectories().FirstOrDefault(d => d.Name == siteName.ToLower());
                            }

                            var contentName             = segments[1];
                            var uploadSiteContentFolder = uploadSiteFolder.GetDirectories().FirstOrDefault(d => d.Name == contentName.ToLower());
                            if (uploadSiteContentFolder == null)
                            {
                                var newDir = VirtualPathUtility.AppendTrailingSlash(Request.ApplicationPath + uploadSiteFolder.LocalUrl) + contentName.ToLower();
                                FS.CreateDirectory(newDir);
                                uploadSiteContentFolder = uploadSiteFolder.GetDirectories().FirstOrDefault(d => d.Name == contentName.ToLower());
                            }

                            var templateName = segments[2];
                            var uploadSiteContentTemplateFolder = uploadSiteContentFolder.GetDirectories().FirstOrDefault(d => d.Name == templateName.ToLower());
                            if (uploadSiteContentTemplateFolder == null)
                            {
                                var newDir = VirtualPathUtility.AppendTrailingSlash(Request.ApplicationPath + uploadSiteContentFolder.LocalUrl) + templateName.ToLower();
                                FS.CreateDirectory(newDir);
                                uploadSiteContentTemplateFolder = uploadSiteContentFolder.GetDirectories().FirstOrDefault(d => d.Name == templateName.ToLower());

                                dir = uploadSiteContentTemplateFolder;
                            }
                            else
                            {
                                dir = uploadSiteContentTemplateFolder;
                            }
                        }
                    }


                    var files = dir.GetFiles().ToList();
                    mediaBrowserModel.Dirs  = dir.GetDirectories();
                    mediaBrowserModel.Files = MediaBrowserHandler.GetFileReducedList(files, imageSizes, selectableExtensions, fsRootPath);
                    mediaBrowserModel.Path  = System.Web.VirtualPathUtility.ToAppRelative(dir.LocalUrl).Trim('~');
                }
                else
                {
                    mediaBrowserModel.Path             = System.Web.VirtualPathUtility.ToAppRelative("/").Trim('~');
                    mediaBrowserModel.RootIsSelectable = true;
                    mediaBrowserModel.Dirs             = new List <Directory>();
                    foreach (var updDir in uploadDirectories)
                    {
                        mediaBrowserModel.Dirs.Add(updDir.Current as Directory);
                    }
                }

                var breadcrumb = mediaBrowserModel.Path.Split(new[] { "/" }, StringSplitOptions.RemoveEmptyEntries).ToList();
                breadcrumb.Insert(0, "[root]");
                mediaBrowserModel.Breadcrumb = breadcrumb.ToArray();
            }
        }
        private void WriteSearch(HttpContext context)
        {
            var query = context.Request["query"];

            if (string.IsNullOrWhiteSpace(query))
            {
                context.Response.WriteJson(new { Status = "Error", Total = 0, Message = "Please provide a search term." });
                return;
            }
            query = query.TrimStart().TrimEnd();

            var host     = Engine.Resolve <IHost>();
            var rootItem = Engine.Persister.Get(host.DefaultSite.RootItemID);
            var root     = new HierarchyNode <ContentItem>(rootItem);

            FS = Engine.Resolve <IFileSystem>();
            var selectionTrail    = Find.EnumerateParents(Selection.SelectedItem, null, true).ToList().Where(a => a is AbstractNode).Reverse().ToList();
            var uploadDirectories = MediaBrowserUtils.GetAvailableUploadFoldersForAllSites(context, root, selectionTrail, Engine, FS);

            if (uploadDirectories.Count == 0)
            {
                context.Response.WriteJson(new { Status = "Error", Total = 0, Message = "No available directories in this site." });
                return;
            }

            //Do the search using the IFileSystem
            var resultsFileData = FS.SearchFiles(query, uploadDirectories);

            if (!resultsFileData.Any())
            {
                context.Response.WriteJson(new { Status = "Error", Total = 0, Message = "0 files found." });
                return;
            }

            var files                = new List <File>();
            var fileMap              = new Dictionary <string, File>(StringComparer.OrdinalIgnoreCase);
            var lastParent           = string.Empty;
            AbstractDirectory parent = null;

            foreach (var fd in resultsFileData.OrderBy(s => s.VirtualPath))
            {
                var parentDirectory = fd.VirtualPath.Substring(0, fd.VirtualPath.LastIndexOf('/'));
                if (lastParent != parentDirectory)
                {
                    parent     = new Directory(DirectoryData.Virtual(parentDirectory), null);
                    lastParent = parentDirectory;
                }

                var file = new File(fd, parent);
                file.Set(FS);
                file.Set(ImageSizes);

                var unresizedFileName = ImageSizes.RemoveImageSize(file.Name);
                if (unresizedFileName != null && fileMap.ContainsKey(unresizedFileName))
                {
                    fileMap[unresizedFileName].Add(file);

                    if (ImageSizes.GetSizeName(file.Name) == "icon")
                    {
                        file.IsIcon = true;
                    }
                }
                else
                {
                    if (unresizedFileName == null)
                    {
                        files.Add(file);
                        fileMap[file.Name] = file;
                    }
                }
            }
            files.Sort(new TitleComparer <File>());

            var directory  = FS.GetDirectory("/");
            var fsRootPath = directory != null && !string.IsNullOrWhiteSpace(directory.RootPath) ? directory.RootPath : "";

            var selectableExtensions = context.Request["exts"];

            context.Response.WriteJson(new
            {
                Path  = "",
                Total = files.Count,
                Files = GetFileReducedList(files, ImageSizes, selectableExtensions, fsRootPath)
            });
        }
Exemplo n.º 4
0
        protected override void OnInit(EventArgs e)
        {
            FS     = Engine.Resolve <IFileSystem>();
            Config = Engine.Resolve <ConfigurationManagerWrapper>().Sections.Management;
            var selected = Selection.SelectedItem;

            var host           = Engine.Resolve <IHost>();
            var rootItem       = Engine.Persister.Get(host.DefaultSite.RootItemID);
            var root           = new HierarchyNode <ContentItem>(rootItem);
            var selectionTrail = new List <ContentItem>();

            var ckEditor             = Request["ckEditor"];
            var ckEditorFuncNum      = Request["ckEditorFuncNum"];
            var mediaCtrl            = Request["mediaControl"];
            var preferredSize        = Request["preferredSize"];
            var selectableExtensions = Request["selectableExtensions"];
            var selectedUrl          = Request["selectedUrl"];
            var mediaBrowserHandler  = new MediaBrowserHandler();

            mediaBrowserModel = new MediaBrowserModel
            {
                CkEditor         = ckEditor,
                CkEditorFuncNum  = ckEditorFuncNum,
                MediaControl     = mediaCtrl,
                HandlerUrl       = AjaxMediaBrowserRetriever,
                PreferredSize    = preferredSize,
                Breadcrumb       = new string[] { },
                Path             = "",
                RootIsSelectable = false
            };


            if (selected is AbstractNode)
            {
                selectionTrail = new List <ContentItem>(Find.EnumerateParents(selected, null, true));
            }
            else
            {
                mediaBrowserHandler.TrySelectingPrevious(ref selected, ref selectionTrail);
            }

            var uploadDirectories = MediaBrowserUtils.GetAvailableUploadFoldersForAllSites(Context, root, selectionTrail, Engine, FS);

            if (uploadDirectories.Count > 0)
            {
                var imageSizes = Engine.Resolve <Management.Files.FileSystem.Pages.ImageSizeCache>();
                if (uploadDirectories.Count == 1 || selectionTrail.Count > 1)
                {
                    var dir   = (selectionTrail.Count > 0 ? selectionTrail.ElementAt(0) : uploadDirectories[0].Current) as Directory;
                    var files = dir.GetFiles().ToList();
                    mediaBrowserModel.Dirs  = dir.GetDirectories();
                    mediaBrowserModel.Files = MediaBrowserHandler.GetFileReducedList(files, imageSizes, selectableExtensions);
                    mediaBrowserModel.Path  = dir.LocalUrl;
                }
                else
                {
                    mediaBrowserModel.Path             = "/";
                    mediaBrowserModel.RootIsSelectable = true;
                    mediaBrowserModel.Dirs             = new List <Directory>();
                    foreach (var updDir in uploadDirectories)
                    {
                        mediaBrowserModel.Dirs.Add(updDir.Current as Directory);
                    }
                }

                var breadcrumb = mediaBrowserModel.Path.Split(new[] { "/" }, StringSplitOptions.RemoveEmptyEntries).ToList();
                breadcrumb.Insert(0, "[root]");
                mediaBrowserModel.Breadcrumb = breadcrumb.ToArray();
            }
        }
Exemplo n.º 5
0
        private void WriteSearch(HttpContext context)
        {
            var query = context.Request["query"];

            if (string.IsNullOrWhiteSpace(query))
            {
                context.Response.WriteJson(new { Total = 0, Message = "Please provide a search term" });
                return;
            }
            query = query.TrimStart().TrimEnd();

            var host     = Engine.Resolve <IHost>();
            var rootItem = Engine.Persister.Get(host.DefaultSite.RootItemID);
            var root     = new HierarchyNode <ContentItem>(rootItem);

            FS = Engine.Resolve <IFileSystem>();
            var selectionTrail    = Find.EnumerateParents(Selection.SelectedItem, null, true).ToList().Where(a => a is AbstractNode).Reverse().ToList();
            var uploadDirectories = MediaBrowserUtils.GetAvailableUploadFoldersForAllSites(context, root, selectionTrail, Engine, FS);

            if (uploadDirectories.Count == 0)
            {
                context.Response.WriteJson(new { Total = 0, Message = "No available directories" });
                return;
            }

            if (query.IndexOf('*') < 0)
            {
                query = "*" + query + "*";
            }

            var resultFilenames = new List <string>();

            foreach (var dir in uploadDirectories)
            {
                //Search, returns Absolute Paths
                resultFilenames.AddRange(
                    System.IO.Directory.GetFiles(
                        System.Web.Hosting.HostingEnvironment.MapPath("~" + dir.Current.Url),
                        query,
                        System.IO.SearchOption.AllDirectories
                        )
                    );
            }

            if (resultFilenames.Count == 0)
            {
                context.Response.WriteJson(new { Total = 0, Message = "0 files found" });
                return;
            }

            List <File>       files      = new List <File>();
            var               fileMap    = new Dictionary <string, File>(StringComparer.OrdinalIgnoreCase);
            string            lastParent = null;
            AbstractDirectory parent     = null;

            foreach (var nm in resultFilenames.OrderBy(s => s))
            {
                var fd = FS.GetFile(AbsolutePathToVirtual(nm));
                var parentDirectory = fd.VirtualPath.Substring(0, fd.VirtualPath.LastIndexOf('/'));
                if (lastParent != parentDirectory)
                {
                    parent     = new Directory(DirectoryData.Virtual(parentDirectory), null);
                    lastParent = parentDirectory;
                }

                var file = new File(fd, parent);
                file.Set(FS);
                file.Set(ImageSizes);

                var unresizedFileName = ImageSizes.RemoveImageSize(file.Name);
                if (unresizedFileName != null && fileMap.ContainsKey(unresizedFileName))
                {
                    fileMap[unresizedFileName].Add(file);

                    if (ImageSizes.GetSizeName(file.Name) == "icon")
                    {
                        file.IsIcon = true;
                    }
                }
                else
                {
                    if (unresizedFileName == null)
                    {
                        files.Add(file);
                        fileMap[file.Name] = file;
                    }
                }
            }
            files.Sort(new TitleComparer <File>());

            var selectableExtensions = context.Request["exts"];

            context.Response.WriteJson(new
            {
                Path  = "",
                Total = files.Count,
                Files = GetFileReducedList(files, ImageSizes, selectableExtensions)
            });
        }