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"; } }
/// <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(IFilesStorageProviderV40 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"); } PageContent page = Pages.FindPage(provider.CurrentWiki, pageFullName); if (page == null) { return(false); } bool done = provider.StorePageAttachment(pageFullName, 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); }