public void Update(Models.MediaContent @new, Models.MediaContent old) { var contentPath = new MediaContentPath(@new); if ([email protected](old.FileName, StringComparison.OrdinalIgnoreCase)) { Kooboo.IO.IOUtility.RenameFile(old.PhysicalPath, @new.FileName); } if (@new.ContentFile != null) { @new.FileName = @new.ContentFile.FileName; @new.UserKey = @new.FileName; @new.UUID = @new.FileName; locker.EnterWriteLock(); try { @new.ContentFile.Stream.SaveAs(contentPath.PhysicalPath); } finally { locker.ExitWriteLock(); } } SetFilePublished(contentPath.PhysicalPath, @new.Published); metadataStorage.SaveMetadata(@new); }
public void Delete(Models.MediaContent content) { string sql = string.Format("DELETE FROM {0} WHERE UUID=@UUID", content.GetRepository().GetMediaContentTableName()); SqlCommand command = new SqlCommand(); command.CommandText = sql; command.Parameters.Add(new SqlParameter("UUID", content.UUID)); SQLServerHelper.BatchExecuteNonQuery(content.GetRepository().GetConnectionString(), command); }
/// <summary> /// Adds or updates the given model in the database /// depending on its state. /// </summary> /// <param name="model">The model</param> /// <param name="data">The binary data</param> public void Save(Models.MediaContent content) { if (!App.MediaTypes.IsSupported(content.Filename)) { throw new NotSupportedException("Filetype not supported."); } var model = db.Media .FirstOrDefault(m => m.Id == content.Id); if (model == null) { model = new Media() { Id = model != null || content.Id.HasValue ? content.Id.Value : Guid.NewGuid(), Created = DateTime.Now }; content.Id = model.Id; db.Media.Add(model); } model.Filename = content.Filename; model.FolderId = content.FolderId; model.Type = App.MediaTypes.GetMediaType(content.Filename); model.ContentType = App.MediaTypes.GetContentType(content.Filename); model.LastModified = DateTime.Now; // Upload to storage using (var session = storage.Open()) { if (content is Models.BinaryMediaContent) { var bc = (Models.BinaryMediaContent)content; model.Size = bc.Data.Length; session.Put(model.Id + "-" + model.Filename, model.ContentType, bc.Data); } else if (content is Models.StreamMediaContent) { var sc = (Models.StreamMediaContent)content; var stream = sc.Data; model.Size = sc.Data.Length; session.Put(model.Id + "-" + model.Filename, model.ContentType, ref stream); } } db.SaveChanges(); RemoveFromCache(model); }
/// <summary> /// Adds or updates the given model in the database /// depending on its state. /// </summary> /// <param name="model">The model</param> /// <param name="data">The binary data</param> /// <param name="transaction">The optional transaction</param> public void Save(Models.MediaContent content, IDbTransaction transaction = null) { var media = GetById(content.Id, transaction); var insert = false; if (media == null) { media = new Media() { Id = !string.IsNullOrWhiteSpace(content.Id) ? content.Id : Guid.NewGuid().ToString() }; insert = true; } media.Filename = content.Filename; media.FolderId = content.FolderId; media.ContentType = content.ContentType; // Upload to storage using (var session = storage.Open()) { if (content is Models.BinaryMediaContent) { var bc = (Models.BinaryMediaContent)content; media.Size = bc.Data.Length; media.PublicUrl = session.Put(media.Id + "-" + media.Filename, media.ContentType, bc.Data); } else if (content is Models.StreamMediaContent) { var sc = (Models.StreamMediaContent)content; var stream = sc.Data; media.Size = sc.Data.Length; media.PublicUrl = session.Put(media.Id + "-" + media.Filename, media.ContentType, ref stream); } } if (insert) { db.Execute($"INSERT INTO [{TABLE}] ([Id],[FolderId],[Filename],[ContentType],[Size],[PublicUrl],[Created],[LastModified]) VALUES(@Id,@FolderId,@Filename,@ContentType,@Size,@PublicUrl,@Created,@LastModified)", media, transaction: transaction); } else { db.Execute($"UPDATE [{TABLE}] SET [Id]=@Id,[FolderId]=@FolderId,[Filename]=@Filename,[ContentType]=@ContentType,[Size]=@Size,[PublicUrl]=@PublicUrl,[Created]=@Created,[LastModified]=@LastModified", media, transaction: transaction); } }
public void Update(Models.MediaContent @new, Models.MediaContent old) { ((IPersistable)@new).OnSaving(); string sql = string.Format("UPDATE {0} SET FolderName = @FolderName,FileName = @FileName,VirtualPath=@VirtualPath) WHERE UUID=@UUID" , @new.GetRepository().GetMediaContentTableName()); SqlCommand command = new SqlCommand(); command.CommandText = sql; command.Parameters.Add(new SqlParameter("UUID", @new.UUID)); command.Parameters.Add(new SqlParameter("FolderName", @new.FolderName)); command.Parameters.Add(new SqlParameter("FileName", @new.FileName)); command.Parameters.Add(new SqlParameter("VirtualPath", @new.VirtualPath)); SQLServerHelper.BatchExecuteNonQuery(@new.GetRepository().GetConnectionString(), command); ((IPersistable)@new).OnSaved(); }
public void Delete(Models.MediaContent content) { locker.EnterWriteLock(); try { if (File.Exists(content.PhysicalPath)) { File.Delete(content.PhysicalPath); } metadataStorage.DeleteMetadata(content); } finally { locker.ExitWriteLock(); } }
public void Add(Models.MediaContent content) { ((IPersistable)content).OnSaving(); string sql = string.Format("INSERT INTO {0}(UUID,FolderName,FileName,VirtualPath,UserId) VALUES(@UUID,@FolderName,@FileName,@VirtualPath,@UserId)" , content.GetRepository().GetMediaContentTableName()); SqlCommand command = new SqlCommand(); command.CommandText = sql; command.Parameters.Add(new SqlParameter("UUID", content.UUID)); command.Parameters.Add(new SqlParameter("FolderName", content.FolderName)); command.Parameters.Add(new SqlParameter("FileName", content.FileName)); command.Parameters.Add(new SqlParameter("VirtualPath", content.VirtualPath)); command.Parameters.Add(new SqlParameter("@UserId", content.UserId)); SQLServerHelper.BatchExecuteNonQuery(content.GetRepository().GetConnectionString(), command); ((IPersistable)content).OnSaved(); }
/// <summary> /// Saves the media content. /// </summary> /// <param name="content">The media content</param> public void Save(Models.MediaContent content) { var media = Query().FirstOrDefault(m => m.Id == content.Id); if (media == null) { media = new Data.Media() { Id = content.Id.HasValue ? content.Id.Value : Guid.NewGuid() }; db.Media.Add(media); } media.Type = content.Type; media.FileName = content.Filename; media.FolderId = content.FolderId; media.ContentType = content.ContentType; media.Description = content.Description; // Upload to storage using (var session = storage.Open()) { if (content is Models.BinaryMediaContent) { var bc = (Models.BinaryMediaContent)content; media.FileSize = bc.Data.Length; media.PublicUrl = session.Put(media.Id.ToString() + "-" + media.FileName, media.ContentType, bc.Data); } else if (content is Models.StreamMediaContent) { var sc = (Models.StreamMediaContent)content; var stream = sc.Data; media.FileSize = sc.Data.Length; media.PublicUrl = session.Put(media.Id.ToString() + "-" + media.FileName, media.ContentType, ref stream); } } db.SaveChanges(); }
public void Add(Models.MediaContent content) { //throw new NotImplementedException(); this.Add(content, true); }
/// <summary> /// Adds or updates the given model in the database /// depending on its state. /// </summary> /// <param name="content">The content to save</param> public static void Save(this IMediaService service, Models.MediaContent content) { service.SaveAsync(content).GetAwaiter().GetResult(); }
/// <summary> /// Adds or updates the given model in the database /// depending on its state. /// </summary> /// <param name="model">The model</param> /// <param name="data">The binary data</param> /// <param name="transaction">The optional transaction</param> public void Save(Models.MediaContent content, IDbTransaction transaction = null) { if (!App.MediaTypes.IsSupported(content.Filename)) { throw new NotSupportedException("Filetype not supported."); } var model = GetById(content.Id, transaction); var insert = false; if (model == null) { model = new Media() { Id = model != null || !string.IsNullOrWhiteSpace(content.Id) ? content.Id : Guid.NewGuid().ToString() }; content.Id = model.Id; insert = true; } model.Filename = content.Filename; model.FolderId = content.FolderId; model.Type = App.MediaTypes.GetMediaType(content.Filename); model.ContentType = App.MediaTypes.GetContentType(content.Filename); // Upload to storage using (var session = storage.Open()) { if (content is Models.BinaryMediaContent) { var bc = (Models.BinaryMediaContent)content; model.Size = bc.Data.Length; session.Put(model.Id + "-" + model.Filename, model.ContentType, bc.Data); } else if (content is Models.StreamMediaContent) { var sc = (Models.StreamMediaContent)content; var stream = sc.Data; model.Size = sc.Data.Length; session.Put(model.Id + "-" + model.Filename, model.ContentType, ref stream); } } if (insert) { // Prepare model.Created = DateTime.Now; model.LastModified = DateTime.Now; db.Execute($"INSERT INTO [{TABLE}] ([Id],[FolderId],[Type],[Filename],[ContentType],[Size],[Created],[LastModified]) VALUES(@Id,@FolderId,@Type,@Filename,@ContentType,@Size,@Created,@LastModified)", model, transaction: transaction); } else { model.LastModified = DateTime.Now; db.Execute($"UPDATE [{TABLE}] SET [Id]=@Id,[FolderId]=@FolderId,[Type]=@Type,[Filename]=@Filename,[ContentType]=@ContentType,[Size]=@Size,[LastModified]=@LastModified WHERE [Id]=@Id", model, transaction: transaction); RemoveFromCache(model); } }
/// <summary> /// Adds or updates the given model in the database /// depending on its state. /// </summary> /// <param name="content">The content to save</param> public async Task SaveAsync(Models.MediaContent content) { if (!App.MediaTypes.IsSupported(content.Filename)) { throw new NotSupportedException("Filetype not supported."); } var model = db.Media .Include(m => m.Versions) .FirstOrDefault(m => m.Id == content.Id); if (model == null) { model = new Media() { Id = model != null || content.Id.HasValue ? content.Id.Value : Guid.NewGuid(), Created = DateTime.Now }; content.Id = model.Id; db.Media.Add(model); } else { using (var session = await storage.OpenAsync()) { // Delete all versions as we're updating the image if (model.Versions.Count > 0) { foreach (var version in model.Versions) { // Delete version from storage await session.DeleteAsync(GetResourceName(model, version.Width, version.Height, ".jpg")); } db.MediaVersions.RemoveRange(model.Versions); } // Delete the old file because we might have a different filename await session.DeleteAsync(GetResourceName(model)); } } model.Filename = content.Filename; model.FolderId = content.FolderId; model.Type = App.MediaTypes.GetMediaType(content.Filename); model.ContentType = App.MediaTypes.GetContentType(content.Filename); model.LastModified = DateTime.Now; // Pre-process if this is an image if (processor != null && model.Type == Models.MediaType.Image) { byte[] bytes; if (content is Models.BinaryMediaContent) { bytes = ((Models.BinaryMediaContent)content).Data; } else { var reader = new BinaryReader(((Models.StreamMediaContent)content).Data); bytes = reader.ReadBytes((int)reader.BaseStream.Length); ((Models.StreamMediaContent)content).Data.Position = 0; } int width, height; processor.GetSize(bytes, out width, out height); model.Width = width; model.Height = height; } // Upload to storage using (var session = await storage.OpenAsync()) { if (content is Models.BinaryMediaContent) { var bc = (Models.BinaryMediaContent)content; model.Size = bc.Data.Length; await session.PutAsync(model.Id + "-" + model.Filename, model.ContentType, bc.Data); } else if (content is Models.StreamMediaContent) { var sc = (Models.StreamMediaContent)content; var stream = sc.Data; model.Size = sc.Data.Length; await session.PutAsync(model.Id + "-" + model.Filename, model.ContentType, stream); } } App.Hooks.OnBeforeSave <Media>(model); db.SaveChanges(); App.Hooks.OnAfterSave <Media>(model); RemoveFromCache(model); }
/// <summary> /// Adds or updates the given model in the database depending on its state. /// Please note that this method is not really synchronous, it's just a /// wrapper for the async version. /// </summary> /// <param name="content">The content to save</param> public void Save(Models.MediaContent content) { Task.Run(() => SaveAsync(content)).Wait(); }