/// <summary> /// Stores and 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="source">The source stream.</param> /// <param name="overwrite"><c>true</c> to overwrite an existing attachment with the same name, <c>false</c> otherwise.</param> /// <returns><c>true</c> if the attachment was stored, <c>false</c> otherwise.</returns> public static bool StorePageAttachment(IFilesStorageProviderV30 provider, string pageFullName, string name, Stream source, bool overwrite) { 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 (source == null) { throw new ArgumentNullException("source"); } PageInfo pageInfo = Pages.FindPage(pageFullName); if (pageInfo == null) { return(false); } PageContent page = Content.GetPageContent(pageInfo, false); if (page == null) { return(false); } bool done = provider.StorePageAttachment(pageInfo, name, source, overwrite); if (!done) { return(false); } if (overwrite) { SearchClass.UnindexPageAttachment(name, page); } // 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, name); source.Seek(0, SeekOrigin.Begin); using (FileStream temp = File.Create(tempFile)) { source.CopyTo(temp); } SearchClass.IndexPageAttachment(name, tempFile, page); Directory.Delete(tempDir, true); Host.Instance.OnAttachmentActivity(provider.GetType().FullName, name, pageFullName, null, FileActivity.AttachmentUploaded); return(true); }
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); }