Skip to content

Commit 608794d

Browse files
committed
Resolves smartstore#669 Picture entity: put PictureBinary in a separate table
1 parent 4328f49 commit 608794d

33 files changed

Lines changed: 714 additions & 141 deletions
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace SmartStore.Core.Domain.Media
2+
{
3+
public partial class BinaryData : BaseEntity
4+
{
5+
/// <summary>
6+
/// Binary data
7+
/// </summary>
8+
public byte[] Data { get; set; }
9+
}
10+
}

src/Libraries/SmartStore.Core/Domain/Media/Download.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ public Download()
3434
[DataMember]
3535
public string DownloadUrl { get; set; }
3636

37-
/// <summary>
38-
/// Gets or sets the download binary
39-
/// </summary>
40-
public byte[] DownloadBinary { get; set; }
37+
/// <summary>
38+
/// Gets or sets the download binary
39+
/// </summary>
40+
[Obsolete("Use property BinaryData instead")]
41+
public byte[] DownloadBinary { get; set; }
4142

4243
/// <summary>
4344
/// The mime-type of the download
@@ -76,5 +77,16 @@ public Download()
7677
[DataMember]
7778
[Index("IX_UpdatedOn_IsTransient", 0)]
7879
public DateTime UpdatedOnUtc { get; set; }
79-
}
80+
81+
/// <summary>
82+
/// Gets or sets the binary data identifier
83+
/// </summary>
84+
[DataMember]
85+
public int? BinaryDataId { get; set; }
86+
87+
/// <summary>
88+
/// Gets or sets the binary data
89+
/// </summary>
90+
public virtual BinaryData BinaryData { get; set; }
91+
}
8092
}

src/Libraries/SmartStore.Core/Domain/Media/Picture.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@ namespace SmartStore.Core.Domain.Media
1212
[DataContract]
1313
public partial class Picture : BaseEntity, ITransient
1414
{
15+
private ICollection<ProductPicture> _productPictures;
16+
1517
public Picture()
1618
{
1719
this.UpdatedOnUtc = DateTime.UtcNow;
1820
}
19-
20-
private ICollection<ProductPicture> _productPictures;
21-
/// <summary>
22-
/// Gets or sets the picture binary
23-
/// </summary>
24-
public byte[] PictureBinary { get; set; }
21+
22+
/// <summary>
23+
/// Gets or sets the picture binary
24+
/// </summary>
25+
[Obsolete("Use property BinaryData instead")]
26+
public byte[] PictureBinary { get; set; }
2527

2628
/// <summary>
2729
/// Gets or sets the picture mime type
@@ -55,9 +57,20 @@ public Picture()
5557
[Index("IX_UpdatedOn_IsTransient", 0)]
5658
public DateTime UpdatedOnUtc { get; set; }
5759

58-
/// <summary>
59-
/// Gets or sets the product pictures
60-
/// </summary>
60+
/// <summary>
61+
/// Gets or sets the binary data identifier
62+
/// </summary>
63+
[DataMember]
64+
public int? BinaryDataId { get; set; }
65+
66+
/// <summary>
67+
/// Gets or sets the binary data
68+
/// </summary>
69+
public virtual BinaryData BinaryData { get; set; }
70+
71+
/// <summary>
72+
/// Gets or sets the product pictures
73+
/// </summary>
6174
[DataMember]
6275
public virtual ICollection<ProductPicture> ProductPictures
6376
{

src/Libraries/SmartStore.Core/SmartStore.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@
183183
<Compile Include="Domain\DataExchange\ExportEnums.cs" />
184184
<Compile Include="Domain\DataExchange\ExportFilter.cs" />
185185
<Compile Include="Domain\DataExchange\ExportProfile.cs" />
186+
<Compile Include="Domain\Media\BinaryData.cs" />
186187
<Compile Include="Domain\Messages\EmailAttachmentStorageLocation.cs" />
187188
<Compile Include="Domain\Messages\QueuedEmailAttachment.cs" />
188189
<Compile Include="Domain\Orders\CheckoutEnums.cs" />

src/Libraries/SmartStore.Core/Utilities/SmartSyndicationFeed.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void AddNamespaces(bool purlContent)
2424
if (purlContent)
2525
{
2626
this.AttributeExtensions.Add(new XmlQualifiedName("content", XNamespace.Xmlns.ToString()), UrlPurlContent);
27-
}
27+
}
2828
}
2929

3030
public void Init(string selfLink, Language language = null)
@@ -62,10 +62,13 @@ public bool AddEnclosue(SyndicationItem item, Picture picture, string pictureUrl
6262
{
6363
if (picture != null && pictureUrl.HasValue())
6464
{
65-
long pictureLength = 10000; // 0 omits the length attribute but that invalidates the feed
65+
// 0 omits the length attribute but that invalidates the feed
66+
long pictureLength = 10000;
6667

67-
if (picture.PictureBinary != null)
68-
pictureLength = picture.PictureBinary.LongLength;
68+
if ((picture.BinaryDataId ?? 0) != 0)
69+
{
70+
pictureLength = picture.BinaryData.Data.LongLength;
71+
}
6972

7073
var enclosure = SyndicationLink.CreateMediaEnclosureLink(new Uri(pictureUrl), picture.MimeType.EmptyNull(), pictureLength);
7174

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Data.Entity.ModelConfiguration;
2+
using SmartStore.Core.Domain.Media;
3+
4+
namespace SmartStore.Data.Mapping.Media
5+
{
6+
public partial class BinaryDataMap : EntityTypeConfiguration<BinaryData>
7+
{
8+
public BinaryDataMap()
9+
{
10+
ToTable("BinaryData");
11+
HasKey(x => x.Id);
12+
Property(x => x.Data).IsRequired().IsMaxLength();
13+
}
14+
}
15+
}

src/Libraries/SmartStore.Data/Mapping/Media/DownloadMap.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@ public DownloadMap()
99
{
1010
this.ToTable("Download");
1111
this.HasKey(p => p.Id);
12-
this.Property(p => p.DownloadBinary).IsMaxLength();
13-
}
12+
13+
#pragma warning disable 612, 618
14+
this.Property(p => p.DownloadBinary).IsMaxLength();
15+
#pragma warning restore 612, 618
16+
17+
HasOptional(x => x.BinaryData)
18+
.WithMany()
19+
.HasForeignKey(x => x.BinaryDataId)
20+
.WillCascadeOnDelete(false);
21+
}
1422
}
1523
}

src/Libraries/SmartStore.Data/Mapping/Media/PictureMap.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,18 @@ public PictureMap()
99
{
1010
this.ToTable("Picture");
1111
this.HasKey(p => p.Id);
12-
this.Property(p => p.PictureBinary).IsMaxLength();
13-
this.Property(p => p.MimeType).IsRequired().HasMaxLength(40);
12+
13+
#pragma warning disable 612, 618
14+
this.Property(p => p.PictureBinary).IsMaxLength();
15+
#pragma warning restore 612, 618
16+
17+
this.Property(p => p.MimeType).IsRequired().HasMaxLength(40);
1418
this.Property(p => p.SeoFilename).HasMaxLength(300);
19+
20+
HasOptional(x => x.BinaryData)
21+
.WithMany()
22+
.HasForeignKey(x => x.BinaryDataId)
23+
.WillCascadeOnDelete(false);
1524
}
1625
}
1726
}

src/Libraries/SmartStore.Data/Migrations/201607171240407_MediaBinaryData.Designer.cs

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
namespace SmartStore.Data.Migrations
2+
{
3+
using System.Data.Entity;
4+
using System.Data.Entity.Migrations;
5+
using System.Linq;
6+
using Core;
7+
using Core.Domain.Media;
8+
using Setup;
9+
10+
public partial class MediaBinaryData : DbMigration, ILocaleResourcesProvider, IDataSeeder<SmartObjectContext>
11+
{
12+
private const int PAGESIZE = 50;
13+
14+
private void MovePictureBinaryToBinaryDataTable(SmartObjectContext context, DbSet<BinaryData> binaryDatas)
15+
{
16+
var pageIndex = 0;
17+
IPagedList<Picture> pictures = null;
18+
19+
// no where clause here!
20+
var pictureQuery = context.Set<Picture>().OrderBy(x => x.Id);
21+
22+
do
23+
{
24+
if (pictures != null)
25+
{
26+
context.DetachEntities(pictures);
27+
pictures.Clear();
28+
pictures = null;
29+
}
30+
31+
pictures = new PagedList<Picture>(pictureQuery, pageIndex++, PAGESIZE);
32+
33+
#pragma warning disable 612, 618
34+
foreach (var picture in pictures)
35+
{
36+
if (picture.PictureBinary != null && picture.PictureBinary.Length > 0)
37+
{
38+
var binaryData = new BinaryData { Data = picture.PictureBinary };
39+
binaryDatas.AddOrUpdate(binaryData);
40+
context.SaveChanges();
41+
42+
picture.PictureBinary = null;
43+
picture.BinaryDataId = binaryData.Id;
44+
}
45+
}
46+
#pragma warning restore 612, 618
47+
48+
context.SaveChanges();
49+
}
50+
while (pictures.HasNextPage);
51+
}
52+
53+
private void MoveDownloadBinaryToBinaryDataTable(SmartObjectContext context, DbSet<BinaryData> binaryDatas)
54+
{
55+
var pageIndex = 0;
56+
IPagedList<Download> downloads = null;
57+
58+
// no where clause here!
59+
var downloadQuery = context.Set<Download>().OrderBy(x => x.Id);
60+
61+
do
62+
{
63+
if (downloads != null)
64+
{
65+
context.DetachEntities(downloads);
66+
downloads.Clear();
67+
downloads = null;
68+
}
69+
70+
downloads = new PagedList<Download>(downloadQuery, pageIndex++, PAGESIZE);
71+
72+
#pragma warning disable 612, 618
73+
foreach (var download in downloads)
74+
{
75+
if (download.DownloadBinary != null && download.DownloadBinary.Length > 0)
76+
{
77+
var binaryData = new BinaryData { Data = download.DownloadBinary };
78+
binaryDatas.AddOrUpdate(binaryData);
79+
context.SaveChanges();
80+
81+
download.DownloadBinary = null;
82+
download.BinaryDataId = binaryData.Id;
83+
}
84+
}
85+
#pragma warning restore 612, 618
86+
87+
context.SaveChanges();
88+
}
89+
while (downloads.HasNextPage);
90+
}
91+
92+
public override void Up()
93+
{
94+
CreateTable(
95+
"dbo.BinaryData",
96+
c => new
97+
{
98+
Id = c.Int(nullable: false, identity: true),
99+
Data = c.Binary(nullable: false),
100+
})
101+
.PrimaryKey(t => t.Id);
102+
103+
AddColumn("dbo.Picture", "BinaryDataId", c => c.Int());
104+
AddColumn("dbo.Download", "BinaryDataId", c => c.Int());
105+
CreateIndex("dbo.Picture", "BinaryDataId");
106+
CreateIndex("dbo.Download", "BinaryDataId");
107+
AddForeignKey("dbo.Picture", "BinaryDataId", "dbo.BinaryData", "Id");
108+
AddForeignKey("dbo.Download", "BinaryDataId", "dbo.BinaryData", "Id");
109+
}
110+
111+
public override void Down()
112+
{
113+
DropForeignKey("dbo.Download", "BinaryDataId", "dbo.BinaryData");
114+
DropForeignKey("dbo.Picture", "BinaryDataId", "dbo.BinaryData");
115+
DropIndex("dbo.Download", new[] { "BinaryDataId" });
116+
DropIndex("dbo.Picture", new[] { "BinaryDataId" });
117+
DropColumn("dbo.Download", "BinaryDataId");
118+
DropColumn("dbo.Picture", "BinaryDataId");
119+
DropTable("dbo.BinaryData");
120+
}
121+
122+
public bool RollbackOnFailure
123+
{
124+
get { return false; }
125+
}
126+
127+
public void Seed(SmartObjectContext context)
128+
{
129+
context.MigrateLocaleResources(MigrateLocaleResources);
130+
131+
var binaryDatas = context.Set<BinaryData>();
132+
133+
MovePictureBinaryToBinaryDataTable(context, binaryDatas);
134+
MoveDownloadBinaryToBinaryDataTable(context, binaryDatas);
135+
}
136+
137+
public void MigrateLocaleResources(LocaleResourcesBuilder builder)
138+
{
139+
}
140+
}
141+
}

0 commit comments

Comments
 (0)