/// <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(IFilesStorageProviderV30 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); } // 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); Directory.Delete(tempDir, true); Host.Instance.OnFileActivity(provider.GetType().FullName, fullName, null, FileActivity.FileUploaded); 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); }