/// <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); }
private void AddRating(string fullPageName, int rate) { IFilesStorageProviderV40 filesStorageProvider = GetDefaultFilesStorageProvider(); MemoryStream stream = new MemoryStream(); if (FileExists(filesStorageProvider, DefaultDirectoryName(), ratingFileName)) { filesStorageProvider.RetrieveFile(DefaultDirectoryName() + ratingFileName, stream); stream.Seek(0, SeekOrigin.Begin); } string fileContent = Encoding.UTF8.GetString(stream.ToArray()); string[] plugins = fileContent.Split(new String[] { "||" }, StringSplitOptions.RemoveEmptyEntries); StringBuilder sb = new StringBuilder(); // If the plugin is found return the posizion in the plugins array // otherwise return -1 int pluginIndex = SearchPlugin(plugins, fullPageName); if (pluginIndex != -1) { int numRates = int.Parse(plugins[pluginIndex].Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries)[1]); int average = int.Parse(plugins[pluginIndex].Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries)[2]); int newAverage = ((average * numRates) + (rate * 100)) / (numRates + 1); numRates++; plugins[pluginIndex] = fullPageName + "|" + numRates + "|" + newAverage; foreach (string plugin in plugins) { sb.Append(plugin + "||"); } } else { foreach (string plugin in plugins) { sb.Append(plugin + "||"); } sb.Append(fullPageName + "|1|" + (rate * 100)); } stream = new MemoryStream(Encoding.UTF8.GetBytes(sb.ToString())); filesStorageProvider.StoreFile(DefaultDirectoryName() + ratingFileName, stream, true); }
protected void btnSave_Click(object sender, EventArgs e) { ResizeImage(); bool done = false; txtNewName.Text = txtNewName.Text.Trim(); string targetName = chkNewName.Checked ? txtNewName.Text : Path.GetFileName(file); bool overwrite = !chkNewName.Checked; if (targetName.Length == 0 || Path.GetFileNameWithoutExtension(targetName).Length == 0) { lblResult.CssClass = "resulterror"; lblResult.Text = Properties.Messages.InvalidFileName; return; } if (!overwrite) { // Force extension targetName = Path.ChangeExtension(targetName, rdoPng.Checked ? "png" : "jpg"); } if (string.IsNullOrEmpty(page)) { string path = (file.LastIndexOf('/') > 0) ? file.Substring(0, file.LastIndexOf('/') + 1) : ""; done = provider.StoreFile(path + targetName, resultMemStream, overwrite); } else { done = provider.StorePageAttachment(page, targetName, resultMemStream, overwrite); } if (done) { lblResult.Text = Properties.Messages.FileSaved; lblResult.CssClass = "resultok"; } else { lblResult.Text = Properties.Messages.CouldNotSaveFile; lblResult.CssClass = "resulterror"; } }