Пример #1
0
        /// <summary>
        /// Deletes and de-indexes a file.
        /// </summary>
        /// <param name="provider">The provider.</param>
        /// <param name="fullName">The full name of the file to delete.</param>
        /// <returns><c>true</c> if the file was deleted, <c>false</c> otherwise.</returns>
        public static bool DeleteFile(IFilesStorageProviderV60 provider, string fullName)
        {
            if (provider == null)
            {
                throw new ArgumentNullException("provider");
            }
            if (fullName == null)
            {
                throw new ArgumentNullException("fullName");
            }
            if (fullName.Length == 0)
            {
                throw new ArgumentException("Full Name cannot be empty", "fullName");
            }

            fullName = NormalizeFullName(fullName);

            bool done = provider.DeleteFile(fullName);

            if (!done)
            {
                return(false);
            }

            SearchClass.UnindexFile(provider.GetType().FullName + "|" + fullName, provider.CurrentWiki);

            Host.Instance.OnFileActivity(provider.GetType().FullName, fullName, null, FileActivity.FileDeleted);

            return(true);
        }
Пример #2
0
        /// <summary>
        /// Stores and indexes a file.
        /// </summary>
        /// <param name="provider">The destination provider.</param>
        /// <param name="fullName">The full name.</param>
        /// <param name="source">The source stream.</param>
        /// <param name="overwrite"><c>true</c> to overwrite the existing file, <c>false</c> otherwise.</param>
        /// <returns><c>true</c> if the file was stored, <c>false</c> otherwise.</returns>
        public static bool StoreFile(IFilesStorageProviderV60 provider, string fullName, Stream source, bool overwrite)
        {
            if (provider == null)
            {
                throw new ArgumentNullException("provider");
            }
            if (fullName == null)
            {
                throw new ArgumentNullException("fullName");
            }
            if (fullName.Length == 0)
            {
                throw new ArgumentException("Full Name cannot be empty", "fullName");
            }
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            fullName = NormalizeFullName(fullName);

            bool done = provider.StoreFile(fullName, source, overwrite);

            if (!done)
            {
                return(false);
            }

            if (overwrite)
            {
                SearchClass.UnindexFile(provider.GetType().FullName + "|" + fullName, provider.CurrentWiki);
            }

            // Index the attached file
            string tempDir = Path.Combine(Environment.GetEnvironmentVariable("TEMP"), Guid.NewGuid().ToString());

            if (!Directory.Exists(tempDir))
            {
                Directory.CreateDirectory(tempDir);
            }
            string tempFile = Path.Combine(tempDir, Path.GetFileName(fullName));

            source.Seek(0, SeekOrigin.Begin);
            using (FileStream temp = File.Create(tempFile)) {
                source.CopyTo(temp);
            }
            SearchClass.IndexFile(provider.GetType().FullName + "|" + fullName, tempFile, provider.CurrentWiki);
            Directory.Delete(tempDir, true);

            Host.Instance.OnFileActivity(provider.GetType().FullName, fullName, null, FileActivity.FileUploaded);

            return(true);
        }
Пример #3
0
        /// <summary>
        /// Deletes a directory and de-indexes all its contents.
        /// </summary>
        /// <param name="provider">The provider.</param>
        /// <param name="directory">The directory to delete.</param>
        public static bool DeleteDirectory(IFilesStorageProviderV60 provider, string directory)
        {
            if (provider == null)
            {
                throw new ArgumentNullException("provider");
            }
            if (directory == null)
            {
                throw new ArgumentNullException("directory");
            }
            if (directory.Length == 0 || directory == "/")
            {
                throw new ArgumentException("Cannot delete the root directory", "directory");
            }

            directory = NormalizeFullPath(directory);

            // This must be done BEFORE deleting the directory, otherwise there wouldn't be a way to list contents
            DeletePermissions(provider, directory);
            DeindexDirectory(provider, directory);

            // Delete Directory
            bool done = provider.DeleteDirectory(directory);

            if (!done)
            {
                return(false);
            }

            Host.Instance.OnDirectoryActivity(provider.GetType().FullName, directory, null, FileActivity.DirectoryDeleted);

            return(true);
        }
Пример #4
0
        /// <summary>
        /// Recursively re-indexes all the contents of the old (renamed) directory to the new one.
        /// </summary>
        /// <param name="provider">The provider.</param>
        /// <param name="oldDirectory">The old directory.</param>
        /// <param name="newDirectory">The new directory.</param>
        private static void ReindexDirectory(IFilesStorageProviderV60 provider, string oldDirectory, string newDirectory)
        {
            oldDirectory = NormalizeFullPath(oldDirectory);
            newDirectory = NormalizeFullPath(newDirectory);

            // At this point the directory has been already renamed,
            // thus we must list on the new directory and construct the old name
            // Example: /directory/one/ renamed to /directory/two-two/
            // List on /directory/two-two/
            //     dir1
            //     dir2
            // oldSub = /directory/one/dir1/

            foreach (string sub in provider.ListDirectories(newDirectory))
            {
                string oldSub = oldDirectory + sub.Substring(newDirectory.Length);
                ReindexDirectory(provider, oldSub, sub);
            }

            foreach (string file in provider.ListFiles(newDirectory))
            {
                string oldFile = oldDirectory + file.Substring(newDirectory.Length);
                SearchClass.RenameFile(provider.CurrentWiki, provider.GetType().FullName + "|" + oldFile, provider.GetType().FullName + "|" + file);
            }
        }
Пример #5
0
        /// <summary>
        /// Renames and re-indexes a file.
        /// </summary>
        /// <param name="provider">The provider.</param>
        /// <param name="fullName">The full name of the file to rename.</param>
        /// <param name="newName">The new name of the file (without namespace).</param>
        /// <returns><c>true</c> if the file was renamed, <c>false</c> otherwise.</returns>
        public static bool RenameFile(IFilesStorageProviderV60 provider, string fullName, string newName)
        {
            if (provider == null)
            {
                throw new ArgumentNullException("provider");
            }
            if (fullName == null)
            {
                throw new ArgumentNullException("fullName");
            }
            if (fullName.Length == 0)
            {
                throw new ArgumentException("Full Name cannot be empty", "fullName");
            }
            if (newName == null)
            {
                throw new ArgumentNullException("newName");
            }
            if (newName.Length == 0)
            {
                throw new ArgumentException("New Name cannot be empty", "newName");
            }

            fullName = NormalizeFullName(fullName);
            string newFullName = GetDirectory(fullName) + newName;

            newFullName = NormalizeFullName(newFullName);

            if (newFullName.ToLowerInvariant() == fullName.ToLowerInvariant())
            {
                return(false);
            }

            bool done = provider.RenameFile(fullName, newFullName);

            if (!done)
            {
                return(false);
            }

            SearchClass.RenameFile(provider.CurrentWiki, provider.GetType().FullName + "|" + fullName, provider.GetType().FullName + "|" + newFullName);

            Host.Instance.OnFileActivity(provider.GetType().FullName, fullName, newFullName, FileActivity.FileRenamed);

            return(true);
        }
Пример #6
0
        /// <summary>
        /// Renames and re-indexes a page attachment.
        /// </summary>
        /// <param name="provider">The provider.</param>
        /// <param name="pageFullName">The page full name.</param>
        /// <param name="name">The attachment name.</param>
        /// <param name="newName">The new attachment name.</param>
        /// <returns><c>true</c> if the attachment was rename, <c>false</c> otherwise.</returns>
        public static bool RenamePageAttachment(IFilesStorageProviderV60 provider, string pageFullName, string name, string newName)
        {
            if (provider == null)
            {
                throw new ArgumentNullException("provider");
            }
            if (pageFullName == null)
            {
                throw new ArgumentNullException("pageFullName");
            }
            if (pageFullName.Length == 0)
            {
                throw new ArgumentException("Page Full Name cannot be empty", "pageFullName");
            }
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (name.Length == 0)
            {
                throw new ArgumentException("Name cannot be empty", "name");
            }
            if (newName == null)
            {
                throw new ArgumentNullException("newName");
            }
            if (newName.Length == 0)
            {
                throw new ArgumentException("New Name cannot be empty", "newName");
            }

            if (name.ToLowerInvariant() == newName.ToLowerInvariant())
            {
                return(false);
            }

            PageContent page = Pages.FindPage(provider.CurrentWiki, pageFullName);

            if (page == null)
            {
                return(false);
            }

            bool done = provider.RenamePageAttachment(pageFullName, name, newName);

            if (!done)
            {
                return(false);
            }

            SearchClass.RenamePageAttachment(page, name, newName);

            Host.Instance.OnAttachmentActivity(provider.GetType().FullName, newName, pageFullName, name, FileActivity.AttachmentRenamed);

            return(true);
        }
Пример #7
0
        /// <summary>
        /// De-indexes all contents of a directory.
        /// </summary>
        /// <param name="provider">The provider.</param>
        /// <param name="directory">The directory.</param>
        private static void DeindexDirectory(IFilesStorageProviderV60 provider, string directory)
        {
            directory = NormalizeFullPath(directory);

            foreach (string sub in provider.ListDirectories(directory))
            {
                DeindexDirectory(provider, sub);
            }

            foreach (string file in provider.ListFiles(directory))
            {
                SearchClass.UnindexFile(provider.GetType().FullName + "|" + file, provider.CurrentWiki);
            }
        }
Пример #8
0
        /// <summary>
        /// Gets the proper full name for a directory.
        /// </summary>
        /// <param name="prov">The provider.</param>
        /// <param name="name">The directory name.</param>
        /// <returns>The full name (<b>not</b> prepended with <see cref="Actions.ForDirectories.ResourceMasterPrefix" />.</returns>
        public static string GetDirectoryName(IFilesStorageProviderV60 prov, string name)
        {
            if (prov == null)
            {
                throw new ArgumentNullException("prov");
            }
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (name.Length == 0)
            {
                throw new ArgumentException("Name cannot be empty", "name");
            }

            return("(" + prov.GetType().FullName + ")" + name);
        }
Пример #9
0
        /// <summary>
        /// Renames a directory and re-indexes all contents.
        /// </summary>
        /// <param name="provider">The provider.</param>
        /// <param name="fullPath">The full path of the directory to rename.</param>
        /// <param name="newName">The new name of the directory.</param>
        /// <returns><c>true</c> if the directory was renamed, <c>false</c> otherwise.</returns>
        public static bool RenameDirectory(IFilesStorageProviderV60 provider, string fullPath, string newName)
        {
            if (provider == null)
            {
                throw new ArgumentNullException("provider");
            }
            if (fullPath == null)
            {
                throw new ArgumentNullException("fullPath");
            }
            if (fullPath.Length == 0)
            {
                throw new ArgumentException("Cannot rename the root directory", "fullPath");
            }
            if (newName == null)
            {
                throw new ArgumentNullException("newName");
            }
            if (newName.Length == 0)
            {
                throw new ArgumentException("New Name cannot be empty", "newName");
            }

            fullPath = NormalizeFullPath(fullPath);
            string newFullPath = GetDirectory(fullPath) + newName;

            newFullPath = NormalizeFullPath(newFullPath);

            bool done = provider.RenameDirectory(fullPath, newFullPath);

            if (!done)
            {
                return(false);
            }

            MovePermissions(provider, fullPath, newFullPath);
            ReindexDirectory(provider, fullPath, newFullPath);

            Host.Instance.OnDirectoryActivity(provider.GetType().FullName, fullPath, newFullPath, FileActivity.DirectoryRenamed);

            return(true);
        }
Пример #10
0
        /// <summary>
        /// Creates a new directory.
        /// </summary>
        /// <param name="provider">The provider.</param>
        /// <param name="path">The path.</param>
        /// <param name="name">The name of the new directory.</param>
        /// <returns><c>true</c> if the directory was created, <c>false</c> otherwise.</returns>
        public static bool CreateDirectory(IFilesStorageProviderV60 provider, string path, string name)
        {
            if (provider == null)
            {
                throw new ArgumentNullException("provider");
            }
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }
            if (path.Length == 0)
            {
                path = "/";
            }
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (name.Length == 0)
            {
                throw new ArgumentException("Name cannot be empty", "name");
            }

            path = NormalizeFullPath(path);
            name = name.Trim('/', ' ');

            bool done = provider.CreateDirectory(path, name);

            if (!done)
            {
                return(false);
            }

            Host.Instance.OnDirectoryActivity(provider.GetType().FullName, path + name + "/", null, FileActivity.DirectoryCreated);

            return(true);
        }