示例#1
0
        /// <summary>
        /// Rename item
        /// </summary>
        /// <param name="target">Target</param>
        /// <param name="name">Name</param>
        /// <returns>Response result</returns>
        RenameCommandResult IConnectorDriver.Rename(string target, string name)
        {
            // Create command result
            var commandResult = new RenameCommandResult();

            // Parse target path
            var fullPath = ParsePath(target);

            // Add item to removed entries list
            commandResult.Removed.Add(target);

            // Proceed if it's a file or directory
            if (fullPath.IsDirectory())
            {
                // Directory

                // Remove thumbnails
                fullPath.RemoveThumbs();

                // Get parent path info
                var directoryInfo = fullPath.GetParentDirectory();
                if (directoryInfo != null)
                {
                    // Get new path
                    var newPath = Path.Combine(directoryInfo.FullName, name);

                    // Move path
                    Directory.Move(((FileSystemVolumeDirectoryInfo)fullPath).Directory.FullName, newPath);

                    // Add it to added entries list
                    commandResult.Added.Add(
                        DirectoryEntryObjectModel.Create(new DirectoryInfo(newPath), fullPath.Root));
                }
            }
            else if (fullPath.IsFile())
            {
                // File

                // Remove thumbnails
                fullPath.RemoveThumbs();

                // Get new path
                var newPath = Path.Combine(((FileSystemVolumeFileInfo)fullPath).File.DirectoryName ?? string.Empty, name);

                // Move file
                File.Move(((FileSystemVolumeFileInfo)fullPath).File.FullName, newPath);

                // Add it to added entries list
                commandResult.Added.Add(
                    FileEntryObjectModel.Create(new FileInfo(newPath), fullPath.Root));
            }
            else
            {
                // Not supported
                throw new NotSupportedException();
            }

            // Return result
            return(commandResult);
        }
示例#2
0
        /// <summary>
        /// Get parents
        /// </summary>
        /// <param name="target">Target</param>
        /// <returns>Response results</returns>
        ParentsCommandResult IConnectorDriver.Parents(string target)
        {
            // Create command result
            var commandResult = new ParentsCommandResult();

            // Parse target path
            var fullPath = ParseDirectoryPath(target);

            // Add directories
            if (fullPath.IsDirectorySameAsRoot)
            {
                // Root level
                commandResult.Tree.Add(
                    DirectoryEntryObjectModel.Create(
                        fullPath.Directory,
                        fullPath.Root));
            }
            else
            {
                // Not root level

                // Check that directory has a parent
                if (fullPath.Directory.Parent != null)
                {
                    // Add directories in the parent
                    foreach (var directoryItem in fullPath.Directory.Parent.GetDirectories())
                    {
                        commandResult.Tree.Add(
                            DirectoryEntryObjectModel.Create(
                                directoryItem, fullPath.Root));
                    }

                    // Go back to root
                    var parent = fullPath.Directory;

                    while (parent != null && parent.FullName != fullPath.Root.Directory.FullName)
                    {
                        // Update parent
                        parent = parent.Parent;

                        // Ensure it's a child of the root
                        if (parent != null &&
                            parent.FullName.Length > fullPath.Root.Directory.FullName.Length)
                        {
                            commandResult.Tree.Add(
                                DirectoryEntryObjectModel.Create(
                                    parent, fullPath.Root));
                        }
                    }
                }
            }

            // Return it
            return(commandResult);
        }
        /// <summary>
        /// Create target entry object model for given directory
        /// </summary>
        /// <param name="fullPath">Target directory path info</param>
        /// <returns>Result target entry object model</returns>
        public static BaseDirectoryEntryObjectModel CreateTargetEntryObjectModel(
            this FileSystemVolumeDirectoryInfo fullPath)
        {
            // Check if we are targeting the root directory
            if (fullPath.IsDirectorySameAsRoot)
            {
                // Result instance as root
                return(RootDirectoryEntryObjectModel.Create(fullPath.Directory, fullPath.Root));
            }

            // Result instance as directory
            return(DirectoryEntryObjectModel.Create(fullPath.Directory, fullPath.Root));
        }
示例#4
0
        /// <summary>
        /// Make directory
        /// </summary>
        /// <param name="target">Target</param>
        /// <param name="name">Name</param>
        /// <returns>Response results</returns>
        MakeDirCommandResult IConnectorDriver.MakeDir(string target, string name)
        {
            // Create command result
            var commandResult = new MakeDirCommandResult();

            // Parse target path
            var fullPath = ParseDirectoryPath(target);

            // Create directory
            var newDirectory = Directory.CreateDirectory(Path.Combine(fullPath.Directory.FullName, name));

            // Add new directory to response
            commandResult.Added.Add(DirectoryEntryObjectModel.Create(newDirectory, fullPath.Root));

            // Return result
            return(commandResult);
        }
示例#5
0
        /// <summary>
        /// Get tree
        /// </summary>
        /// <param name="target">Target</param>
        /// <returns>Response results</returns>
        TreeCommandResult IConnectorDriver.Tree(string target)
        {
            // Create command result
            var commandResult = new TreeCommandResult();

            // Parse target path
            var fullPath = ParseDirectoryPath(target);

            // Add visible directories
            foreach (var directoryInfo in fullPath.Directory.GetVisibleDirectories())
            {
                commandResult.Tree.Add(
                    DirectoryEntryObjectModel.Create(directoryInfo, fullPath.Root));
            }

            // Return it
            return(commandResult);
        }
        /// <summary>
        /// Search accessible files and directories in given volume
        /// </summary>
        /// <typeparam name="TVolume">Volume type</typeparam>
        /// <param name="volume">Volume</param>
        /// <param name="root">Root path</param>
        /// <param name="searchTerm">Search term</param>
        /// <returns>Result list</returns>
        public static IEnumerable <EntryObjectModel> SearchAccessibleFilesAndDirectories <TVolume>(this TVolume volume, string root,
                                                                                                   string searchTerm)
            where TVolume : FileSystemRootVolume
        {
            var entries = new List <EntryObjectModel>();

            foreach (var directory in Directory.EnumerateDirectories(root).Where(m => m.Contains(searchTerm)))
            {
                entries.Add(DirectoryEntryObjectModel.Create(new DirectoryInfo(directory), volume));
            }
            foreach (var file in Directory.EnumerateFiles(root).Where(m => m.Contains(searchTerm)))
            {
                // Check if file refers to an image or not
                if (ImagingUtils.CanProcessFile(file))
                {
                    // Add image to added entries
                    entries.Add(
                        ImageEntryObjectModel.Create(new FileInfo(file), volume));
                }
                else
                {
                    // Add file to added entries
                    entries.Add(
                        FileEntryObjectModel.Create(new FileInfo(file), volume));
                }
            }
            foreach (var subDir in Directory.EnumerateDirectories(root))
            {
                try
                {
                    entries.AddRange(volume.SearchAccessibleFilesAndDirectories(subDir, searchTerm));
                }
                catch (UnauthorizedAccessException)
                {
                    // Do nothing
                }
            }

            return(entries);
        }
示例#7
0
        /// <summary>
        /// Open file
        /// </summary>
        /// <param name="target">Target</param>
        /// <param name="includeRootSubFolders">Include subfolders of root directories</param>
        /// <returns>Response result</returns>
        OpenCommandResult IConnectorDriver.Open(string target, bool includeRootSubFolders)
        {
            // Get directory target path info
            var fullPath = ParseDirectoryPath(target);

            // Create command result
            var commandResult = new OpenCommandResult(
                fullPath.CreateTargetEntryObjectModel(),
                new OptionsResultData(fullPath));

            // Add visible files
            foreach (var fileItem in fullPath.Directory.GetVisibleFiles())
            {
                // Check if file is supported for imaging
                if (ImagingUtils.CanProcessFile(fileItem))
                {
                    // Is supported
                    // Add file item as image entry
                    commandResult.Files.Add(
                        ImageEntryObjectModel.Create(fileItem, fullPath.Root));
                }
                else
                {
                    // Not supported
                    // Add file item as standard entry
                    commandResult.Files.Add(
                        FileEntryObjectModel.Create(fileItem, fullPath.Root));
                }
            }

            // Add visible directories
            foreach (var directoryItem in fullPath.Directory.GetVisibleDirectories())
            {
                commandResult.Files.Add(DirectoryEntryObjectModel.Create(directoryItem, fullPath.Root));
            }

            // Return it
            return(commandResult);
        }
示例#8
0
        /// <summary>
        /// Paste items
        /// </summary>
        /// <param name="source">Source</param>
        /// <param name="dest">Destination</param>
        /// <param name="targets">Targets</param>
        /// <param name="isCut">Is cut</param>
        /// <returns>Response result</returns>
        PasteCommandResult IConnectorDriver.Paste(string source, string dest, IEnumerable <string> targets, bool isCut)
        {
            // Parse destination path
            var destPath = ParsePath(dest);

            // Create command result
            var commandResult = new PasteCommandResult();

            // Loop target items
            foreach (var item in targets)
            {
                // Parse target item path, which will be the source
                var src = ParsePath(item);

                // Proceed if it's a file or directory
                if (src.IsDirectory())
                {
                    // Directory

                    // Compute new directory
                    var newDir = new DirectoryInfo(Path.Combine(destPath.Info.FullName, src.Info.Name));

                    // Check if it already exists
                    if (newDir.Exists)
                    {
                        // Exists
                        Directory.Delete(newDir.FullName, true);
                    }

                    // Check if content is being cut
                    if (isCut)
                    {
                        // Remove thumbnails
                        src.RemoveThumbs();

                        // Move directory
                        src.GetDirectory().MoveTo(newDir.FullName);

                        // Add to removed entries
                        commandResult.Removed.Add(item);
                    }
                    else
                    {
                        // Copy directory
                        FileSystemUtils.DirectoryCopy(src.GetDirectory(), newDir.FullName, true);
                    }

                    // Add it to added items
                    commandResult.Added.Add(
                        DirectoryEntryObjectModel.Create(newDir, destPath.Root));
                }
                else if (src.IsFile())
                {
                    // File

                    // Compute new file path
                    var newFilePath = Path.Combine(destPath.Info.FullName, src.Info.Name);

                    // Check if file exists
                    if (File.Exists(newFilePath))
                    {
                        // Exists
                        File.Delete(newFilePath);
                    }

                    // Check if content is being cut
                    if (isCut)
                    {
                        // Remove thumbnails
                        src.RemoveThumbs();

                        // Move file
                        File.Move(src.Info.FullName, newFilePath);

                        // Add it to removed items
                        commandResult.Removed.Add(item);
                    }
                    else
                    {
                        // Copy file
                        File.Copy(src.Info.FullName, newFilePath);
                    }

                    // Check if file refers to an image or not
                    if (ImagingUtils.CanProcessFile(newFilePath))
                    {
                        // Add image to added entries
                        commandResult.Added.Add(
                            ImageEntryObjectModel.Create(new FileInfo(newFilePath), destPath.Root));
                    }
                    else
                    {
                        // Add file to added entries
                        commandResult.Added.Add(
                            FileEntryObjectModel.Create(new FileInfo(newFilePath), destPath.Root));
                    }
                }
                else
                {
                    // Not supported
                    throw new NotSupportedException();
                }
            }

            // Return result
            return(commandResult);
        }
示例#9
0
        /// <summary>
        /// Duplicate items
        /// </summary>
        /// <param name="targets">Targets</param>
        /// <returns>Response result</returns>
        DuplicateCommandResult IConnectorDriver.Duplicate(IEnumerable <string> targets)
        {
            // Create command result
            var commandResult = new DuplicateCommandResult();

            // Loop targets
            foreach (var targetItem in targets)
            {
                // Parse target path
                var fullPath = ParsePath(targetItem);

                // Proceed if it's a file or directory
                if (fullPath.IsDirectory())
                {
                    // Directory

                    // Remove thumbnails
                    fullPath.RemoveThumbs();

                    // Get path info
                    var parentPath = fullPath.GetParentDirectory().FullName;
                    var name       = fullPath.GetDirectory().Name;

                    // Compute new name
                    var newName = $@"{parentPath}\{name} copy";

                    // Check if directory already exists
                    if (!Directory.Exists(newName))
                    {
                        // Doesn't exist
                        FileSystemUtils.DirectoryCopy(fullPath.GetDirectory(), newName, true);
                    }
                    else
                    {
                        // Already exists, create numbered copy
                        var newNameFound = false;
                        for (var i = 1; i < 100; i++)
                        {
                            // Compute new name
                            newName = $@"{parentPath}\{name} copy {i}";

                            // Test that it doesn't exist
                            if (!Directory.Exists(newName))
                            {
                                FileSystemUtils.DirectoryCopy(fullPath.GetDirectory(), newName, true);
                                newNameFound = true;
                                break;
                            }
                        }

                        // Check if new name was found
                        if (!newNameFound)
                        {
                            throw new ELFinderNewNameSelectionException($@"{parentPath}\{name} copy");
                        }
                    }

                    // Add entry to added items
                    commandResult.Added.Add(
                        DirectoryEntryObjectModel.Create(new DirectoryInfo(newName), fullPath.Root));
                }
                else if (fullPath.IsFile())
                {
                    // File

                    // Remove thumbnails
                    fullPath.RemoveThumbs();

                    // Get path info
                    var parentPath = fullPath.GetDirectory().FullName;
                    var name       = fullPath.Info.Name.Substring(0, fullPath.Info.Name.Length - fullPath.Info.Extension.Length);
                    var ext        = fullPath.Info.Extension;

                    // Compute new name
                    var newName = $@"{parentPath}\{name} copy{ext}";

                    // Check if file already exists
                    if (!File.Exists(newName))
                    {
                        // Doesn't exist
                        ((FileSystemVolumeFileInfo)fullPath).File.CopyTo(newName);
                    }
                    else
                    {
                        // Already exists, create numbered copy
                        var newNameFound = false;
                        for (var i = 1; i < 100; i++)
                        {
                            // Compute new name
                            newName = $@"{parentPath}\{name} copy {i}{ext}";

                            // Test that it doesn't exist
                            if (!File.Exists(newName))
                            {
                                ((FileSystemVolumeFileInfo)fullPath).File.CopyTo(newName);
                                newNameFound = true;
                                break;
                            }
                        }

                        // Check if new name was found
                        if (!newNameFound)
                        {
                            throw new ELFinderNewNameSelectionException($@"{parentPath}\{name} copy{ext}");
                        }
                    }

                    // Check if file refers to an image or not
                    if (ImagingUtils.CanProcessFile(newName))
                    {
                        // Add image to added entries
                        commandResult.Added.Add(
                            ImageEntryObjectModel.Create(new FileInfo(newName), fullPath.Root));
                    }
                    else
                    {
                        // Add file to added entries
                        commandResult.Added.Add(
                            FileEntryObjectModel.Create(new FileInfo(newName), fullPath.Root));
                    }
                }
                else
                {
                    // Not supported
                    throw new NotSupportedException();
                }
            }

            // Return result
            return(commandResult);
        }
示例#10
0
        /// <summary>
        /// Initialize file/directory open
        /// </summary>
        /// <param name="target">Target</param>
        /// <returns>Response result</returns>
        InitCommandResult IConnectorDriver.Init(string target)
        {
            // Declare target directory path info
            FileSystemVolumeDirectoryInfo fullPath;

            // Check if target was defined
            if (string.IsNullOrEmpty(target))
            {
                // Target not defined

                // Get startup volume
                var startupVolume = RootVolumes.GetStartupVolume();

                // Get directory target path info
                fullPath = new FileSystemVolumeDirectoryInfo(
                    startupVolume, startupVolume.StartDirectory ?? startupVolume.Directory);
            }
            else
            {
                // Target defined

                // Get directory target path info
                fullPath = ParseDirectoryPath(target);
            }

            // Create command result
            var commandResult = new InitCommandResult(
                fullPath.CreateTargetEntryObjectModel(),
                new OptionsResultData(fullPath));

            // Add visible files
            foreach (var fileItem in fullPath.Directory.GetVisibleFiles())
            {
                // Check if file is supported for imaging
                if (ImagingUtils.CanProcessFile(fileItem))
                {
                    // Is supported
                    // Add file item as image entry
                    commandResult.Files.Add(
                        ImageEntryObjectModel.Create(fileItem, fullPath.Root));
                }
                else
                {
                    // Not supported
                    // Add file item as standard entry
                    commandResult.Files.Add(
                        FileEntryObjectModel.Create(fileItem, fullPath.Root));
                }
            }

            // Add visible directories
            foreach (var directoryItem in fullPath.Directory.GetVisibleDirectories())
            {
                commandResult.Files.Add(DirectoryEntryObjectModel.Create(directoryItem, fullPath.Root));
            }

            // Add root directories
            foreach (var rootVolume in RootVolumes)
            {
                commandResult.Files.Add(RootDirectoryEntryObjectModel.Create(rootVolume.Directory, rootVolume));
            }

            // Add root directories, if different from root
            if (!fullPath.IsDirectorySameAsRoot)
            {
                foreach (var directoryItem in fullPath.Root.Directory.GetVisibleDirectories())
                {
                    commandResult.Files.Add(DirectoryEntryObjectModel.Create(directoryItem, fullPath.Root));
                }
            }

            // Set max upload size
            if (fullPath.Root.MaxUploadSizeKb.HasValue)
            {
                commandResult.UploadMaxSize = $"{fullPath.Root.MaxUploadSizeKb.Value}K";
            }

            // Return it
            return(commandResult);
        }