/// <summary> /// Saves the /// </summary> /// <param name="file"></param> /// <param name="tx"></param> /// <param name="setdates"></param> /// <returns></returns> public virtual bool Save(MediaFileContent content, System.Data.IDbTransaction tx = null, bool setdates = true) { if (content != null) { // Set filename if (!String.IsNullOrEmpty(content.Filename)) { Filename = content.Filename; } // Set content type if (!String.IsNullOrEmpty(content.ContentType)) { Type = content.ContentType; } } if (base.Save(tx) && content != null) { if (File.Exists(PhysicalPath)) { DeleteFile(); DeleteCache(); } File.WriteAllBytes(PhysicalPath, content.Body); } return(base.Save(tx, setdates)); }
/// <summary> /// Saves the /// </summary> /// <param name="content"></param> /// <param name="tx"></param> /// <param name="setdates"></param> /// <param name="writefile"></param> /// <returns></returns> public virtual bool Save(MediaFileContent content, System.Data.IDbTransaction tx = null, bool setdates = true, bool writefile = true) { if (content != null) { // Set filename if (!String.IsNullOrEmpty(content.Filename)) { Filename = content.Filename; } // Set content type if (!String.IsNullOrEmpty(content.ContentType)) { Type = content.ContentType; } } if (base.Save(tx) && content != null) { // Delete the old data DeleteFile(); DeleteCache(); // Save the new if (writefile) { Application.Current.MediaProvider.Put(Id, content.Body); } } return(base.Save(tx, setdates)); }
/// <summary> /// Saves and publishes the current record and physical file. /// </summary> /// <param name="content">The physical file</param> /// <param name="tx">Optional transaction</param> /// <returns>Whether the operation succeeded or not</returns> public virtual bool SaveAndPublish(MediaFileContent content, System.Data.IDbTransaction tx = null) { //var user = HttpContext.Current != null ? HttpContext.Current.User : null ; if (Database.Identity != Guid.Empty || Application.Current.UserProvider.IsAuthenticated) { // Set file meta information SetFileMeta(content); // First get previously published record var self = Content.GetSingle(Id, false, tx); // Set up the dates. LastPublished = Updated = DateTime.Now; if (IsNew) { Created = Updated; } if (self == null) { Published = Updated; } // First save an up-to-date draft IsDraft = true; base.Save(content, tx, false, false); //var draftpath = PhysicalPath; // Now save a published version IsDraft = false; if (self == null) { IsNew = true; } base.Save(content, tx, false); // Check if we have have a drafted physical file Application.Current.MediaProvider.Publish(Id); DeleteCache(); // Now update all pages & posts which have a reference // to this media object. Page.Execute("UPDATE page SET page_last_modified = @0 WHERE page_attachments LIKE @1", tx, DateTime.Now, "%" + Id.ToString() + "%"); Post.Execute("UPDATE post SET post_last_modified = @0 WHERE post_attachments LIKE @1", tx, DateTime.Now, "%" + Id.ToString() + "%"); // Invalidate all pages & posts which have a reference // to this media object. Page.Get("page_attachments LIKE @1", tx, "%" + Id.ToString() + "%").ForEach(p => p.InvalidateRecord(p)); Post.Get("post_attachments LIKE @1", tx, "%" + Id.ToString() + "%").ForEach(p => p.InvalidateRecord(p)); return(true); } throw new AccessViolationException("User must be logged in to save data."); }
public JsonResult Upload() { // Get custom headers var filename = Request.Headers["X-File-Name"]; var type = Request.Headers["X-File-Type"]; var size = Convert.ToInt32(Request.Headers["X-File-Size"]); var parentId = !String.IsNullOrEmpty(Request.Headers["X-File-ParentId"]) ? new Guid(Request.Headers["X-File-ParentId"]) : Guid.Empty; var name = Request.Headers["X-File-DisplayName"]; var alt = Request.Headers["X-File-Alt"]; var desc = Request.Headers["X-File-Desc"]; using (var mem = new MemoryStream()) { var stream = Request.InputStream; stream.Seek(0, SeekOrigin.Begin); stream.CopyTo(mem); mem.Position = 0; using (var binary = new BinaryReader(mem)) { var media = new Piranha.Models.MediaFileContent() { ContentType = type, Filename = filename, Body = binary.ReadBytes(size) }; var content = new Piranha.Models.Content() { Id = Guid.NewGuid(), ParentId = parentId, IsFolder = false, Name = name, AlternateText = alt, Description = desc }; if (content.SaveAndPublish(media)) { return(Get(content.Id.ToString(), false)); } } } return(Json(new { Success = false })); }
/// <summary> /// Saves the given media content. /// </summary> /// <param name="content">The content</param> /// <param name="tx">The optional database transaction</param> /// <param name="setdates">If dates should be set automatically</param> /// <param name="writefile">If the file should be written or not</param> /// <returns></returns> public virtual bool Save(MediaFileContent content, System.Data.IDbTransaction tx = null, bool setdates = true, bool writefile = true) { if (content != null) { // Set filename if (!String.IsNullOrEmpty(content.Filename)) { Filename = content.Filename; } // Set content type if (!String.IsNullOrEmpty(content.ContentType)) { Type = content.ContentType; } } if (base.Save(tx) && content != null) { // Delete the old data if (!(this is Content) || !((Content)(object)this).IsDraft) { DeleteFile(); } DeleteCache(); // Save the new if (writefile) { // TODO: Dirty double cast, redesign! if (this is Content && ((Content)(object)this).IsDraft) { Application.Current.MediaProvider.PutDraft(Id, content.Body); } else { Application.Current.MediaProvider.Put(Id, content.Body); } } } return(base.Save(tx, setdates)); }
/// <summary> /// Updates the current records meta data from the given physical file. /// </summary> /// <param name="content">The physical content</param> private void SetFileMeta(MediaFileContent content) { if (content != null) { try { using (var mem = new MemoryStream(content.Body)) { using (var img = Image.FromStream(mem)) { Image resized = null; // Check if we need to resize the image try { int max = Convert.ToInt32(SysParam.GetByName("IMAGE_MAX_WIDTH").Value); if (max > 0) { resized = Drawing.ImageUtils.Resize(img, max); using (var writer = new MemoryStream()) { resized.Save(writer, img.RawFormat); content.Body = writer.ToArray(); } } } catch {} IsImage = true; Width = resized != null ? resized.Width : img.Width; Height = resized != null ? resized.Height : img.Height; if (resized != null) { resized.Dispose(); } } } } catch { IsImage = false; } Size = content.Body.Length; } }
/// <summary> /// Saves the current record together with the given physical file. /// </summary> /// <param name="content">The physical content</param> /// <param name="tx">Optional transaction</param> /// <returns>Whether the operation succeeded or not</returns> public bool Save(MediaFileContent content, System.Data.IDbTransaction tx = null) { SetFileMeta(content); return(base.Save(content, tx)); }
public JsonResult Upload() { // Get custom headers var filename = Request.Headers["X-File-Name"] ; var type = Request.Headers["X-File-Type"] ; var size = Convert.ToInt32(Request.Headers["X-File-Size"]) ; var parentId = !String.IsNullOrEmpty(Request.Headers["X-File-ParentId"]) ? new Guid(Request.Headers["X-File-ParentId"]) : Guid.Empty ; var name = Request.Headers["X-File-DisplayName"] ; var alt = Request.Headers["X-File-Alt"] ; var desc = Request.Headers["X-File-Desc"] ; using (var mem = new MemoryStream()) { var stream = Request.InputStream ; stream.Seek(0, SeekOrigin.Begin) ; stream.CopyTo(mem) ; mem.Position = 0 ; using (var binary = new BinaryReader(mem)) { var media = new Piranha.Models.MediaFileContent() { ContentType = type, Filename = filename, Body = binary.ReadBytes(size) } ; var content = new Piranha.Models.Content() { Id = Guid.NewGuid(), ParentId = parentId, IsFolder = false, Name = name, AlternateText = alt, Description = desc } ; if (content.SaveAndPublish(media)) return Get(content.Id, false) ; } } return Json(new { Success = false }) ; }