Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cms Kit - Convert BlogPost CoverImage to Media #7963

Merged
merged 11 commits into from
Mar 8, 2021

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

namespace Volo.CmsKit.Migrations
{
public partial class BlogPost_CoverImage : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "CmsContents");

migrationBuilder.AddColumn<Guid>(
name: "CoverImageMediaId",
table: "CmsBlogPosts",
type: "uniqueidentifier",
nullable: true);
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "CoverImageMediaId",
table: "CmsBlogPosts");

migrationBuilder.CreateTable(
name: "CmsContents",
columns: table => new
{
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
ConcurrencyStamp = table.Column<string>(type: "nvarchar(40)", maxLength: 40, nullable: true),
CreationTime = table.Column<DateTime>(type: "datetime2", nullable: false),
CreatorId = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
DeleterId = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
DeletionTime = table.Column<DateTime>(type: "datetime2", nullable: true),
EntityId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
EntityType = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
ExtraProperties = table.Column<string>(type: "nvarchar(max)", nullable: true),
IsDeleted = table.Column<bool>(type: "bit", nullable: false, defaultValue: false),
LastModificationTime = table.Column<DateTime>(type: "datetime2", nullable: true),
LastModifierId = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
TenantId = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
Value = table.Column<string>(type: "nvarchar(max)", maxLength: 2147483647, nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_CmsContents", x => x.Id);
});

migrationBuilder.CreateIndex(
name: "IX_CmsContents_TenantId_EntityType_EntityId",
table: "CmsContents",
columns: new[] { "TenantId", "EntityType", "EntityId" });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.HasMaxLength(2147483647)
.HasColumnType("nvarchar(max)");

b.Property<Guid?>("CoverImageMediaId")
.HasColumnType("uniqueidentifier");

b.Property<DateTime>("CreationTime")
.HasColumnType("datetime2")
.HasColumnName("CreationTime");
Expand Down Expand Up @@ -1379,78 +1382,6 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.ToTable("CmsComments");
});

modelBuilder.Entity("Volo.CmsKit.Contents.Content", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");

b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken()
.HasMaxLength(40)
.HasColumnType("nvarchar(40)")
.HasColumnName("ConcurrencyStamp");

b.Property<DateTime>("CreationTime")
.HasColumnType("datetime2")
.HasColumnName("CreationTime");

b.Property<Guid?>("CreatorId")
.HasColumnType("uniqueidentifier")
.HasColumnName("CreatorId");

b.Property<Guid?>("DeleterId")
.HasColumnType("uniqueidentifier")
.HasColumnName("DeleterId");

b.Property<DateTime?>("DeletionTime")
.HasColumnType("datetime2")
.HasColumnName("DeletionTime");

b.Property<string>("EntityId")
.IsRequired()
.HasMaxLength(64)
.HasColumnType("nvarchar(64)");

b.Property<string>("EntityType")
.IsRequired()
.HasMaxLength(64)
.HasColumnType("nvarchar(64)");

b.Property<string>("ExtraProperties")
.HasColumnType("nvarchar(max)")
.HasColumnName("ExtraProperties");

b.Property<bool>("IsDeleted")
.ValueGeneratedOnAdd()
.HasColumnType("bit")
.HasDefaultValue(false)
.HasColumnName("IsDeleted");

b.Property<DateTime?>("LastModificationTime")
.HasColumnType("datetime2")
.HasColumnName("LastModificationTime");

b.Property<Guid?>("LastModifierId")
.HasColumnType("uniqueidentifier")
.HasColumnName("LastModifierId");

b.Property<Guid?>("TenantId")
.HasColumnType("uniqueidentifier")
.HasColumnName("TenantId");

b.Property<string>("Value")
.IsRequired()
.HasMaxLength(2147483647)
.HasColumnType("nvarchar(max)");

b.HasKey("Id");

b.HasIndex("TenantId", "EntityType", "EntityId");

b.ToTable("CmsContents");
});

modelBuilder.Entity("Volo.CmsKit.MediaDescriptors.MediaDescriptor", b =>
{
b.Property<Guid>("Id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,4 @@
</None>
</ItemGroup>

<ItemGroup>
<Folder Include="Migrations" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ public class BlogPostDto : EntityDto<Guid>
public string ShortDescription { get; set; }

public string Content { get; set; }

public Guid? CoverImageMediaId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ public class CreateBlogPostDto

[DynamicMaxLength(typeof(BlogPostConsts), nameof(BlogPostConsts.MaxContentLength))]
public string Content { get; set; }

public Guid? CoverImageMediaId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,5 @@ public interface IBlogPostAdminAppService
CreateBlogPostDto,
UpdateBlogPostDto>
{
Task SetCoverImageAsync(Guid id, RemoteStreamContent streamContent);

Task<RemoteStreamContent> GetCoverImageAsync(Guid id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ public class UpdateBlogPostDto

[DynamicMaxLength(typeof(BlogPostConsts), nameof(BlogPostConsts.MaxContentLength))]
public string Content { get; set; }

public Guid? CoverImageMediaId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,23 @@ namespace Volo.CmsKit.Admin.Blogs
{
[RequiresGlobalFeature(typeof(BlogsFeature))]
[Authorize(CmsKitAdminPermissions.BlogPosts.Default)]
public class BlogPostAdminAppService: CmsKitAppServiceBase, IBlogPostAdminAppService
public class BlogPostAdminAppService : CmsKitAppServiceBase, IBlogPostAdminAppService
{
protected BlogPostManager BlogPostManager { get; }
protected IBlogPostRepository BlogPostRepository { get; }
protected IBlogRepository BlogRepository { get; }
protected IBlobContainer<BlogPostCoverImageContainer> BlobContainer { get; }
protected ICmsUserLookupService UserLookupService { get; }

public BlogPostAdminAppService(
BlogPostManager blogPostManager,
IBlogPostRepository blogPostRepository,
IBlogRepository blogRepository,
IBlobContainer<BlogPostCoverImageContainer> blobContainer,
ICmsUserLookupService userLookupService)
{
BlogPostManager = blogPostManager;
BlogPostRepository = blogPostRepository;
BlogRepository = blogRepository;
BlobContainer = blobContainer;
UserLookupService = userLookupService;

}

[Authorize(CmsKitAdminPermissions.BlogPosts.Create)]
Expand All @@ -54,7 +50,8 @@ public virtual async Task<BlogPostDto> CreateAsync(CreateBlogPostDto input)
input.Title,
input.Slug,
input.ShortDescription,
input.Content);
input.Content,
input.CoverImageMediaId);

await BlogPostRepository.InsertAsync(blogPost);

Expand All @@ -69,34 +66,16 @@ public virtual async Task<BlogPostDto> UpdateAsync(Guid id, UpdateBlogPostDto in
blogPost.SetTitle(input.Title);
blogPost.SetShortDescription(input.ShortDescription);
blogPost.SetContent(input.Content);

blogPost.CoverImageMediaId = input.CoverImageMediaId;

if (blogPost.Slug != input.Slug)
{
await BlogPostManager.SetSlugUrlAsync(blogPost, input.Slug);
}

await BlogPostRepository.UpdateAsync(blogPost);

return ObjectMapper.Map<BlogPost,BlogPostDto>(blogPost);
}

[Authorize(CmsKitAdminPermissions.BlogPosts.Update)]
public virtual async Task SetCoverImageAsync(Guid id, RemoteStreamContent streamContent)
{
await BlogPostRepository.GetAsync(id);

using (var stream = streamContent.GetStream())
{
await BlobContainer.SaveAsync(id.ToString(), stream, overrideExisting: true);
}
}

[Authorize(CmsKitAdminPermissions.BlogPosts.Default)]
public virtual async Task<RemoteStreamContent> GetCoverImageAsync(Guid id)
{
var stream = await BlobContainer.GetAsync(id.ToString());

return new RemoteStreamContent(stream);
return ObjectMapper.Map<BlogPost, BlogPostDto>(blogPost);
}

[Authorize(CmsKitAdminPermissions.BlogPosts.Default)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Content;
using Volo.Abp.GlobalFeatures;
using Volo.CmsKit.GlobalFeatures;
using Volo.CmsKit.Permissions;
Expand Down Expand Up @@ -52,50 +47,13 @@ public virtual Task<BlogPostDto> GetAsync(Guid id)
return BlogPostAdminAppService.GetAsync(id);
}

[HttpGet]
[Route("{id}/cover-image")]
[Authorize(CmsKitAdminPermissions.BlogPosts.Default)]
public virtual Task<RemoteStreamContent> GetCoverImageAsync(Guid id)
{
Response.Headers.Add("Content-Disposition", $"inline;filename=\"{id}\"");
Response.Headers.Add("Accept-Ranges", "bytes");
Response.Headers.Add("Cache-Control", "max-age=120");
Response.ContentType = "image";

return BlogPostAdminAppService.GetCoverImageAsync(id);
}

[HttpGet]
[Authorize(CmsKitAdminPermissions.BlogPosts.Default)]
public virtual Task<PagedResultDto<BlogPostDto>> GetListAsync(PagedAndSortedResultRequestDto input)
{
return BlogPostAdminAppService.GetListAsync(input);
}

[NonAction]
public virtual Task SetCoverImageAsync(Guid id, RemoteStreamContent streamContent)
{
return BlogPostAdminAppService.SetCoverImageAsync(id, streamContent);
}

[HttpPost]
[Route("{id}/cover-image")]
[Authorize(CmsKitAdminPermissions.BlogPosts.Update)]
public virtual async Task<IActionResult> UploadCoverImageAsync(Guid id, IFormFile file)
{
if (file == null)
{
return BadRequest();
}

using (var stream = file.OpenReadStream())
{
await BlogPostAdminAppService.SetCoverImageAsync(id, new RemoteStreamContent(stream));
}

return CreatedAtAction(nameof(GetCoverImageAsync), new { id });
}


[HttpPut]
[Route("{id}")]
[Authorize(CmsKitAdminPermissions.BlogPosts.Update)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public class CreateBlogPostViewModel
[HiddenInput]
[DynamicMaxLength(typeof(BlogPostConsts), nameof(BlogPostConsts.MaxContentLength))]
public string Content { get; set; }

[HiddenInput]
public Guid? CoverImageMediaId { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<abp-card-body>

<div class="form-group">
<img height="120" src="/api/cms-kit-admin/blogs/blog-posts/@Model.Id/cover-image" />
<img height="120" src="/api/cms-kit/media/@Model.ViewModel.CoverImageMediaId" />
<label>@L["CoverImage"]</label>
<input type="file" id="BlogPostCoverImage" class="form-control" />
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public class UpdateBlogPostViewModel
[HiddenInput]
[DynamicMaxLength(typeof(BlogPostConsts), nameof(BlogPostConsts.MaxContentLength))]
public string Content { get; set; }

[HiddenInput]
public Guid? CoverImageMediaId { get; set; }
}
}
}