/// <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(IFilesStorageProviderV40 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); }
/// <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(IFilesStorageProviderV40 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); }
/// <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(IFilesStorageProviderV40 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); }
/// <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(IFilesStorageProviderV40 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); } }
/// <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(IFilesStorageProviderV40 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); }
/// <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(IFilesStorageProviderV40 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); }
/// <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(IFilesStorageProviderV40 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); } }
private List <TreeElement> BuildImagesSubTree(IFilesStorageProviderV40 provider, string path) { string[] dirs = new string[0]; string[] files = new string[0]; if (chkImageAttachments.Checked) { // Load page attachments files = provider.ListPageAttachments(currentPage.FullName); } else { // Load files dirs = provider.ListDirectories(path); files = provider.ListFiles(path); } List <TreeElement> result = new List <TreeElement>(100); foreach (string d in dirs) { TreeElement item = new TreeElement(d, Tools.ExtractDirectoryName(d), BuildImagesSubTree(provider, d)); // Do not display empty folders to reduce "noise" if (item.SubItems.Count > 0) { result.Add(item); } } foreach (string f in files) { if (IsImage(f)) { string name = provider.GetType().ToString() + "|" + f; TreeElement item = new TreeElement(name, @"<img src=""Thumb.aspx?Provider=" + provider.GetType().ToString() + @"&Size=Small&File=" + Tools.UrlEncode(f) + @"&Page=" + (chkImageAttachments.Checked ? Tools.UrlEncode(currentPage.FullName) : "") + @""" alt=""" + name + @""" /><span class=""imageinfo"">" + f.Substring(f.LastIndexOf("/") + 1) + "</span>", "javascript:return SelectImage('" + (chkImageAttachments.Checked ? "(" + Tools.UrlEncode(currentPage.FullName) + ")" : "") + "', '" + f.Replace("'", "\\\\\\'") + "', '" + (chkImageAttachments.Checked ? currentPage.FullName : "") + "');"); result.Add(item); } } return(result); }
protected void rptItems_DataBinding(object sender, EventArgs e) { provider = Collectors.CollectorsBox.FilesProviderCollector.GetProvider(lstProviders.SelectedValue, Tools.DetectCurrentWiki()); if (provider == null || CurrentPage == null) { return; } // Build a DataTable containing the proper information DataTable table = new DataTable("Items"); table.Columns.Add("Name"); table.Columns.Add("Size"); table.Columns.Add("Editable", typeof(bool)); table.Columns.Add("Page"); table.Columns.Add("Link"); table.Columns.Add("CanDelete", typeof(bool)); table.Columns.Add("CanDownload", typeof(bool)); string[] attachments = provider.ListPageAttachments(CurrentPage.FullName); foreach (string s in attachments) { FileDetails details = provider.GetPageAttachmentDetails(CurrentPage.FullName, s); DataRow row = table.NewRow(); string ext = Path.GetExtension(s).ToLowerInvariant(); row["Name"] = s; row["Size"] = Tools.BytesToString(details.Size); row["Editable"] = canUpload && canDelete && (ext == ".jpg" || ext == ".jpeg" || ext == ".png"); row["Page"] = CurrentPage.FullName; if (canDownload) { row["Link"] = "GetFile.aspx?File=" + Tools.UrlEncode(s).Replace("'", "'") + "&AsStreamAttachment=1&Provider=" + provider.GetType().FullName + "&IsPageAttachment=1&Page=" + Tools.UrlEncode(CurrentPage.FullName) + "&NoHit=1"; } else { row["Link"] = ""; } row["CanDelete"] = canDelete; row["CanDownload"] = canDownload; table.Rows.Add(row); } rptItems.DataSource = table; }
/// <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(IFilesStorageProviderV40 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); }
/// <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(IFilesStorageProviderV40 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); }
/// <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(IFilesStorageProviderV40 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); }
protected void rptItems_DataBinding(object sender, EventArgs e) { permissionsManager.CurrentResourceName = CurrentDirectory; permissionsManager.CurrentFilesProvider = lstProviders.SelectedValue; // Build a DataTable containing the proper information DataTable table = new DataTable("Items"); table.Columns.Add("Type"); table.Columns.Add("Name"); table.Columns.Add("Size"); table.Columns.Add("WikiMarkupLink"); table.Columns.Add("Link"); table.Columns.Add("Editable", typeof(bool)); table.Columns.Add("FullPath"); table.Columns.Add("CanDelete", typeof(bool)); table.Columns.Add("CanDownload", typeof(bool)); if (!canList) { lblNoList.Visible = true; rptItems.DataSource = table; // This is empty return; } lblNoList.Visible = false; string currDir = CurrentDirectory; string[] dirs = provider.ListDirectories(currDir); string currentUser = SessionFacade.GetCurrentUsername(); string[] currentGroups = SessionFacade.GetCurrentGroupNames(currentWiki); AuthChecker authChecker = new AuthChecker(Collectors.CollectorsBox.GetSettingsProvider(currentWiki)); foreach (string s in dirs) { bool canListThisSubDir = authChecker.CheckActionForDirectory(provider, s, Actions.ForDirectories.List, currentUser, currentGroups); DataRow row = table.NewRow(); row["Type"] = "D"; row["Name"] = GetItemName(s) /* + "/"*/; row["Size"] = "(" + ((int)(provider.ListFiles(s).Length + provider.ListDirectories(s).Length)).ToString() + ")"; row["WikiMarkupLink"] = " "; row["Link"] = ""; row["Editable"] = false; row["FullPath"] = s; row["CanDelete"] = canDeleteDirs; row["CanDownload"] = canListThisSubDir; table.Rows.Add(row); } string[] files = provider.ListFiles(currDir); foreach (string s in files) { FileDetails details = provider.GetFileDetails(s); DataRow row = table.NewRow(); string ext = Path.GetExtension(s).ToLowerInvariant(); row["Type"] = "F"; row["Name"] = GetItemName(s); row["Size"] = Tools.BytesToString(details.Size); row["WikiMarkupLink"] = "{UP}" + s; if (canDownload) { row["Link"] = "GetFile.aspx?File=" + Tools.UrlEncode(s).Replace("'", "'") + "&AsStreamAttachment=1&Provider=" + provider.GetType().FullName + "&NoHit=1"; } else { row["Link"] = ""; } row["Editable"] = canUpload && canDeleteFiles && (ext == ".jpg" || ext == ".jpeg" || ext == ".png"); row["FullPath"] = s; row["CanDelete"] = canDeleteFiles; row["CanDownload"] = canDownload; table.Rows.Add(row); } rptItems.DataSource = table; }
protected void rptIndex_ItemCommand(object sender, CommandEventArgs e) { Log.LogEntry("Index rebuild requested for " + e.CommandArgument as string, EntryType.General, SessionFacade.GetCurrentUsername(), currentWiki); if (e.CommandName == "PagesRebuild") { // Clear the pages search index for the current wiki SearchClass.ClearIndex(currentWiki); IPagesStorageProviderV40 pagesProvider = Collectors.CollectorsBox.PagesProviderCollector.GetProvider(e.CommandArgument as string, currentWiki); // Index all pages of the wiki List <NamespaceInfo> namespaces = new List <NamespaceInfo>(pagesProvider.GetNamespaces()); namespaces.Add(null); foreach (NamespaceInfo nspace in namespaces) { // Index pages of the namespace PageContent[] pages = pagesProvider.GetPages(nspace); foreach (PageContent page in pages) { // Index page SearchClass.IndexPage(page); // Index page messages Message[] messages = pagesProvider.GetMessages(page.FullName); foreach (Message message in messages) { SearchClass.IndexMessage(message, page); // Search for replies Message[] replies = message.Replies; foreach (Message reply in replies) { // Index reply SearchClass.IndexMessage(reply, page); } } } } } else if (e.CommandName == "FilesRebuild") { // Clear the files search index for the current wiki SearchClass.ClearFilesIndex(currentWiki); IFilesStorageProviderV40 filesProvider = Collectors.CollectorsBox.FilesProviderCollector.GetProvider(e.CommandArgument as string, currentWiki); // Index all files of the wiki // 1. List all directories (add the root directory: null) // 2. List all files in each directory // 3. Index each file List <string> directories = new List <string>(filesProvider.ListDirectories(null)); directories.Add(null); foreach (string directory in directories) { string[] files = filesProvider.ListFiles(directory); foreach (string file in files) { byte[] fileContent; using (MemoryStream stream = new MemoryStream()) { filesProvider.RetrieveFile(file, stream); fileContent = new byte[stream.Length]; stream.Seek(0, SeekOrigin.Begin); stream.Read(fileContent, 0, (int)stream.Length); } // Index the file string tempDir = Path.Combine(Environment.GetEnvironmentVariable("TEMP"), Guid.NewGuid().ToString()); if (!Directory.Exists(tempDir)) { Directory.CreateDirectory(tempDir); } string tempFile = Path.Combine(tempDir, file.Substring(file.LastIndexOf('/') + 1)); using (FileStream writer = File.Create(tempFile)) { writer.Write(fileContent, 0, fileContent.Length); } SearchClass.IndexFile(filesProvider.GetType().FullName + "|" + file, tempFile, currentWiki); Directory.Delete(tempDir, true); } } // Index all attachment of the wiki string[] pagesWithAttachments = filesProvider.GetPagesWithAttachments(); foreach (string page in pagesWithAttachments) { string[] attachments = filesProvider.ListPageAttachments(page); foreach (string attachment in attachments) { byte[] fileContent; using (MemoryStream stream = new MemoryStream()) { filesProvider.RetrievePageAttachment(page, attachment, stream); fileContent = new byte[stream.Length]; stream.Seek(0, SeekOrigin.Begin); stream.Read(fileContent, 0, (int)stream.Length); } // 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, attachment); using (FileStream writer = File.Create(tempFile)) { writer.Write(fileContent, 0, fileContent.Length); } SearchClass.IndexPageAttachment(attachment, tempFile, Pages.FindPage(currentWiki, page)); Directory.Delete(tempDir, true); } } } Log.LogEntry("Index rebuild completed for " + e.CommandArgument as string, EntryType.General, SessionFacade.GetCurrentUsername(), currentWiki); }