/// <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(MediaContent content) { if (!App.MediaTypes.IsSupported(content.Filename)) { throw new ValidationException("Filetype not supported."); } Media model = null; if (content.Id.HasValue) { model = await GetByIdAsync(content.Id.Value).ConfigureAwait(false); } if (model == null) { model = new Media() { Id = model != null || content.Id.HasValue ? content.Id.Value : Guid.NewGuid(), Created = DateTime.Now }; content.Id = model.Id; } else { using (var session = await _storage.OpenAsync().ConfigureAwait(false)) { // 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(model, GetResourceName(model, version.Width, version.Height, version.FileExtension)).ConfigureAwait(false); } model.Versions.Clear(); } // Delete the old file because we might have a different filename await session.DeleteAsync(model, GetResourceName(model)).ConfigureAwait(false); } } var type = App.MediaTypes.GetItem(content.Filename); model.Filename = content.Filename.Replace(" ", "_"); model.FolderId = content.FolderId; model.Type = App.MediaTypes.GetMediaType(content.Filename); model.ContentType = type.ContentType; model.LastModified = DateTime.Now; Stream stream = null; if (content is BinaryMediaContent binaryContent) { stream = new MemoryStream(binaryContent.Data); } else if (content is StreamMediaContent streamContent) { stream = streamContent.Data; } // Pre-process if this is an image if (_processor != null && type.AllowProcessing && model.Type == MediaType.Image) { // Make sure to apply auto orientation according to exif var memStream = new MemoryStream(); _processor.AutoOrient(stream, memStream); // Get the image size _processor.GetSize(memStream, out var width, out var height); model.Width = width; model.Height = height; stream = memStream; } // Upload to storage using (var session = await _storage.OpenAsync().ConfigureAwait(false)) { model.Size = stream.Length; await session.PutAsync(model, model.Filename, model.ContentType, stream).ConfigureAwait(false); } App.Hooks.OnBeforeSave(model); await _repo.Save(model).ConfigureAwait(false); App.Hooks.OnAfterSave(model); RemoveFromCache(model); RemoveStructureFromCache(); }
/// <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(MediaContent content) { if (!App.MediaTypes.IsSupported(content.Filename)) { throw new ValidationException("Filetype not supported."); } Media model = null; if (content.Id.HasValue) { model = await GetByIdAsync(content.Id.Value).ConfigureAwait(false); } if (model == null) { model = new Media() { Id = model != null || content.Id.HasValue ? content.Id.Value : Guid.NewGuid(), Created = DateTime.Now }; content.Id = model.Id; } else { using (var session = await _storage.OpenAsync().ConfigureAwait(false)) { // 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, version.FileExtension)).ConfigureAwait(false); } model.Versions.Clear(); } // Delete the old file because we might have a different filename await session.DeleteAsync(GetResourceName(model)).ConfigureAwait(false); } } var type = App.MediaTypes.GetItem(content.Filename); model.Filename = content.Filename; model.FolderId = content.FolderId; model.Type = App.MediaTypes.GetMediaType(content.Filename); model.ContentType = type.ContentType; model.LastModified = DateTime.Now; // Pre-process if this is an image if (_processor != null && type.AllowProcessing && model.Type == MediaType.Image) { byte[] bytes; if (content is BinaryMediaContent) { bytes = ((BinaryMediaContent)content).Data; } else { var reader = new BinaryReader(((StreamMediaContent)content).Data); bytes = reader.ReadBytes((int)reader.BaseStream.Length); ((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().ConfigureAwait(false)) { if (content is BinaryMediaContent) { var bc = (BinaryMediaContent)content; model.Size = bc.Data.Length; await session.PutAsync(model.Id + "-" + model.Filename, model.ContentType, bc.Data).ConfigureAwait(false); } else if (content is StreamMediaContent) { var sc = (StreamMediaContent)content; var stream = sc.Data; model.Size = sc.Data.Length; await session.PutAsync(model.Id + "-" + model.Filename, model.ContentType, stream).ConfigureAwait(false); } } App.Hooks.OnBeforeSave(model); await _repo.Save(model).ConfigureAwait(false); App.Hooks.OnAfterSave(model); RemoveFromCache(model); RemoveStructureFromCache(); }
/// <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); }