Skip to content

Commit 4605a39

Browse files
committed
DownloadService saving binary data like PictureService through current storage provider
1 parent 79651d9 commit 4605a39

22 files changed

Lines changed: 240 additions & 200 deletions

File tree

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,8 @@ namespace SmartStore.Core.Domain.Media
88
/// Represents a download
99
/// </summary>
1010
[DataContract]
11-
public partial class Download : BaseEntity, ITransient
12-
{
13-
public Download()
14-
{
15-
this.UpdatedOnUtc = DateTime.UtcNow;
16-
}
17-
11+
public partial class Download : BaseEntity, ITransient, IMediaStorageSupported
12+
{
1813
/// <summary>
1914
/// Gets or sets a GUID
2015
/// </summary>

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,24 @@ public void MigrateLocaleResources(LocaleResourcesBuilder builder)
191191
"Specifies the new storage location for media file like images.",
192192
"Legt den neuen Speicherort für Mediendateien wie z.B. Bilder fest.");
193193

194+
builder.AddOrUpdate("Admin.Configuration.Settings.Media.MediaIsStoredIn",
195+
"Store media in",
196+
"Medien speichern in");
197+
198+
builder.AddOrUpdate("Admin.Configuration.Settings.Media.MoveMediaNote",
199+
"Do not forget to backup your database before changing this option. Please bear in mind that this operation can take several minutes depending on the amount of media files.",
200+
"Bitte sichern Sie Ihre Datenbank, ehe Sie Mediendateien verschieben. Dieser Vorgang kann je nach Dateimenge mehrere Minuten in Anspruch nehmen.");
201+
194202
builder.AddOrUpdate("Admin.Common.MoveNow", "Move now", "Jetzt verschieben");
195203

196204

197205
builder.Delete(
198206
"Admin.Configuration.Settings.Media.PicturesStoredIntoDatabase.Database",
199207
"Admin.Configuration.Settings.Media.PicturesStoredIntoDatabase.FileSystem",
200208
"Admin.Configuration.Settings.Media.MoveToFs",
201-
"Admin.Configuration.Settings.Media.MoveToDb");
209+
"Admin.Configuration.Settings.Media.MoveToDb",
210+
"Admin.Configuration.Settings.Media.PicturesStoredIntoDatabase",
211+
"Admin.Configuration.Settings.Media.MovePicturesNote");
202212
}
203213
}
204214
}

src/Libraries/SmartStore.Data/Setup/SeedData/InvariantSeedData.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7949,6 +7949,7 @@ public IList<Product> Products()
79497949
Extension = ".mp3",
79507950
Filename = "vivaldi-four-seasons-spring",
79517951
IsNew = true,
7952+
UpdatedOnUtc = DateTime.UtcNow
79527953
}
79537954
};
79547955

@@ -8025,7 +8026,8 @@ public IList<Product> Products()
80258026
},
80268027
Extension = ".mp3",
80278028
Filename = "beethoven-fur-elise.mp3",
8028-
IsNew = true
8029+
IsNew = true,
8030+
UpdatedOnUtc = DateTime.UtcNow
80298031
}
80308032
};
80318033

src/Libraries/SmartStore.Services/Catalog/CopyProductService.cs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,13 @@ public virtual Product CopyProduct(Product product, string newName, bool isPubli
101101
Filename = download.Filename,
102102
Extension = download.Extension,
103103
IsNew = download.IsNew,
104+
UpdatedOnUtc = utcNow
104105
};
105106

106-
if ((download.BinaryDataId ?? 0) != 0)
107-
{
108-
downloadCopy.BinaryData = new BinaryData
109-
{
110-
Data = download.BinaryData.Data
111-
};
112-
}
107+
var data = ((download.BinaryDataId ?? 0) != 0 ? download.BinaryData.Data : null);
108+
109+
_downloadService.InsertDownload(downloadCopy, data);
113110

114-
_downloadService.InsertDownload(downloadCopy);
115111
downloadId = downloadCopy.Id;
116112
}
117113

@@ -128,18 +124,14 @@ public virtual Product CopyProduct(Product product, string newName, bool isPubli
128124
ContentType = sampleDownload.ContentType,
129125
Filename = sampleDownload.Filename,
130126
Extension = sampleDownload.Extension,
131-
IsNew = sampleDownload.IsNew
127+
IsNew = sampleDownload.IsNew,
128+
UpdatedOnUtc = utcNow
132129
};
133130

134-
if ((sampleDownload.BinaryDataId ?? 0) != 0)
135-
{
136-
sampleDownloadCopy.BinaryData = new BinaryData
137-
{
138-
Data = sampleDownload.BinaryData.Data
139-
};
140-
}
131+
var data = ((sampleDownload.BinaryDataId ?? 0) != 0 ? sampleDownload.BinaryData.Data : null);
132+
133+
_downloadService.InsertDownload(sampleDownloadCopy, data);
141134

142-
_downloadService.InsertDownload(sampleDownloadCopy);
143135
sampleDownloadId = sampleDownloadCopy.Id;
144136
}
145137
}

src/Libraries/SmartStore.Services/Extensions/NameValueCollectionExtensions.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,12 @@ public static string CreateSelectedAttributesXml(this NameValueCollection collec
175175
if (download != null)
176176
{
177177
download.IsTransient = false;
178-
downloadService.UpdateDownload(download);
178+
179+
if ((download.BinaryDataId ?? 0) != 0 && download.BinaryData != null)
180+
downloadService.UpdateDownload(download, download.BinaryData.Data);
181+
else
182+
downloadService.UpdateDownload(download, null);
183+
179184
selectedAttributes = productAttributeParser.AddProductAttribute(selectedAttributes, attribute, download.DownloadGuid.ToString());
180185
}
181186
}
@@ -200,18 +205,14 @@ public static string CreateSelectedAttributesXml(this NameValueCollection collec
200205
ContentType = postedFile.ContentType,
201206
Filename = System.IO.Path.GetFileNameWithoutExtension(postedFile.FileName),
202207
Extension = System.IO.Path.GetExtension(postedFile.FileName),
203-
IsNew = true
208+
IsNew = true,
209+
UpdatedOnUtc = DateTime.UtcNow
204210
};
205211

206-
if (postedFile.InputStream != null)
207-
{
208-
download.BinaryData = new BinaryData
209-
{
210-
Data = postedFile.InputStream.ToByteArray()
211-
};
212-
}
212+
var data = (postedFile.InputStream != null ? postedFile.InputStream.ToByteArray() : null);
213+
214+
downloadService.InsertDownload(download, data);
213215

214-
downloadService.InsertDownload(download);
215216
//save attribute
216217
selectedAttributes = productAttributeParser.AddProductAttribute(selectedAttributes, attribute, download.DownloadGuid.ToString());
217218
}

src/Libraries/SmartStore.Services/Media/DownloadService.cs

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,31 @@
77
using SmartStore.Core.Domain.Orders;
88
using SmartStore.Core.Domain.Payments;
99
using SmartStore.Core.Events;
10+
using SmartStore.Core.Plugins;
11+
using SmartStore.Services.Configuration;
12+
using SmartStore.Services.Media.Storage;
1013

1114
namespace SmartStore.Services.Media
1215
{
13-
/// <summary>
14-
/// Download service
15-
/// </summary>
1616
public partial class DownloadService : IDownloadService
1717
{
1818
private readonly IRepository<Download> _downloadRepository;
1919
private readonly IEventPublisher _eventPubisher;
20-
private readonly IBinaryDataService _binaryDataService;
20+
private readonly Provider<IMediaStorageProvider> _storageProvider;
2121

22-
public DownloadService(
22+
public DownloadService(
2323
IRepository<Download> downloadRepository,
2424
IEventPublisher eventPubisher,
25-
IBinaryDataService binaryDataService)
25+
ISettingService settingService,
26+
IProviderManager providerManager)
2627
{
2728
_downloadRepository = downloadRepository;
2829
_eventPubisher = eventPubisher;
29-
_binaryDataService = binaryDataService;
30-
}
30+
31+
var systemName = settingService.GetSettingByKey("Media.Storage.Provider", DatabaseMediaStorageProvider.SystemName);
32+
33+
_storageProvider = providerManager.GetProvider<IMediaStorageProvider>(systemName);
34+
}
3135

3236
public virtual Download GetDownloadById(int downloadId)
3337
{
@@ -71,39 +75,44 @@ public virtual Download GetDownloadByGuid(Guid downloadGuid)
7175

7276
public virtual void DeleteDownload(Download download)
7377
{
74-
if (download == null)
75-
throw new ArgumentNullException("download");
78+
Guard.ArgumentNotNull(() => download);
7679

77-
if ((download.BinaryDataId ?? 0) != 0)
78-
{
79-
_binaryDataService.DeleteBinaryData(download.BinaryData);
80-
}
80+
// delete from storage
81+
_storageProvider.Value.Remove(download.ToMedia());
8182

82-
_downloadRepository.Delete(download);
83+
// delete entity
84+
_downloadRepository.Delete(download);
8385

84-
_eventPubisher.EntityDeleted(download);
86+
// event notification
87+
_eventPubisher.EntityDeleted(download);
8588
}
8689

87-
public virtual void InsertDownload(Download download)
90+
public virtual void InsertDownload(Download download, byte[] downloadBinary)
8891
{
89-
if (download == null)
90-
throw new ArgumentNullException("download");
92+
Guard.ArgumentNotNull(() => download);
9193

92-
download.UpdatedOnUtc = DateTime.UtcNow;
9394
_downloadRepository.Insert(download);
9495

95-
_eventPubisher.EntityInserted(download);
96+
// save to storage
97+
_storageProvider.Value.Save(download.ToMedia(), downloadBinary);
98+
99+
// event notification
100+
_eventPubisher.EntityInserted(download);
96101
}
97102

98-
public virtual void UpdateDownload(Download download)
103+
public virtual void UpdateDownload(Download download, byte[] downloadBinary)
99104
{
100-
if (download == null)
101-
throw new ArgumentNullException("download");
105+
Guard.ArgumentNotNull(() => download);
102106

103107
download.UpdatedOnUtc = DateTime.UtcNow;
108+
104109
_downloadRepository.Update(download);
105110

106-
_eventPubisher.EntityUpdated(download);
111+
// save to storage
112+
_storageProvider.Value.Save(download.ToMedia(), downloadBinary);
113+
114+
// event notification
115+
_eventPubisher.EntityUpdated(download);
107116
}
108117

109118
public virtual bool IsDownloadAllowed(OrderItem orderItem)

src/Libraries/SmartStore.Services/Media/IDownloadService.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ public partial interface IDownloadService
4343
/// Inserts a download
4444
/// </summary>
4545
/// <param name="download">Download</param>
46-
void InsertDownload(Download download);
46+
/// <param name="downloadBinary">Download binary, can be <c>null</c></param>
47+
void InsertDownload(Download download, byte[] downloadBinary);
4748

48-
/// <summary>
49-
/// Updates the download
50-
/// </summary>
51-
/// <param name="download">Download</param>
52-
void UpdateDownload(Download download);
49+
/// <summary>
50+
/// Updates the download
51+
/// </summary>
52+
/// <param name="download">Download</param>
53+
/// <param name="downloadBinary">Download binary, can be <c>null</c></param>
54+
void UpdateDownload(Download download, byte[] downloadBinary);
5355

5456
/// <summary>
5557
/// Gets a value indicating whether download is allowed

src/Libraries/SmartStore.Services/Media/IPictureService.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public partial interface IPictureService
3131
/// <returns>Picture SEO name</returns>
3232
string GetPictureSeName(string name);
3333

34+
/// <summary>
35+
/// Updates a SEO filename of a picture
36+
/// </summary>
37+
/// <param name="pictureId">The picture identifier</param>
38+
/// <param name="seoFilename">The SEO filename</param>
39+
/// <returns>Picture</returns>
40+
Picture SetSeoFilename(int pictureId, string seoFilename);
41+
3442
/// <summary>
3543
/// Gets the loaded picture binary depending on picture storage settings
3644
/// </summary>
@@ -145,13 +153,5 @@ string GetDefaultPictureUrl(int targetSize = 0,
145153
/// <param name="validateBinary">A value indicating whether to validated provided picture binary</param>
146154
/// <returns>Picture</returns>
147155
Picture UpdatePicture(int pictureId, byte[] pictureBinary, string mimeType, string seoFilename, bool isNew, bool validateBinary = true);
148-
149-
/// <summary>
150-
/// Updates a SEO filename of a picture
151-
/// </summary>
152-
/// <param name="pictureId">The picture identifier</param>
153-
/// <param name="seoFilename">The SEO filename</param>
154-
/// <returns>Picture</returns>
155-
Picture SetSeoFilename(int pictureId, string seoFilename);
156156
}
157157
}

src/Libraries/SmartStore.Services/Media/PictureService.cs

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -246,15 +246,24 @@ public virtual string GetPictureSeName(string name)
246246
return SeoHelper.GetSeName(name, true, false);
247247
}
248248

249-
public virtual byte[] LoadPictureBinary(Picture picture)
249+
public virtual Picture SetSeoFilename(int pictureId, string seoFilename)
250+
{
251+
var picture = GetPictureById(pictureId);
252+
253+
// update if it has been changed
254+
if (picture != null && seoFilename != picture.SeoFilename)
255+
{
256+
picture = UpdatePicture(picture.Id, LoadPictureBinary(picture), picture.MimeType, seoFilename, true, false);
257+
}
258+
259+
return picture;
260+
}
261+
262+
public virtual byte[] LoadPictureBinary(Picture picture)
250263
{
251264
Guard.ArgumentNotNull(() => picture);
252265

253-
return _storageProvider.Value.Load(new MediaStorageItem
254-
{
255-
Entity = picture,
256-
MimeType = picture.MimeType
257-
});
266+
return _storageProvider.Value.Load(picture.ToMedia());
258267
}
259268

260269
public virtual Size GetPictureSize(Picture picture)
@@ -401,11 +410,7 @@ public virtual void DeletePicture(Picture picture)
401410
_imageCache.DeleteCachedImages(picture);
402411

403412
// delete from storage
404-
_storageProvider.Value.Remove(new MediaStorageItem
405-
{
406-
Entity = picture,
407-
MimeType = picture.MimeType
408-
});
413+
_storageProvider.Value.Remove(picture.ToMedia());
409414

410415
// delete entity
411416
_pictureRepository.Delete(picture);
@@ -442,12 +447,7 @@ public virtual Picture InsertPicture(
442447
_pictureRepository.Insert(picture);
443448

444449
// save to storage
445-
_storageProvider.Value.Save(new MediaStorageItem
446-
{
447-
Entity = picture,
448-
MimeType = picture.MimeType,
449-
NewData = pictureBinary
450-
});
450+
_storageProvider.Value.Save(picture.ToMedia(), pictureBinary);
451451

452452
// event notification
453453
_eventPublisher.EntityInserted(picture);
@@ -489,32 +489,14 @@ public virtual Picture UpdatePicture(
489489
_pictureRepository.Update(picture);
490490

491491
// save to storage
492-
_storageProvider.Value.Save(new MediaStorageItem
493-
{
494-
Entity = picture,
495-
MimeType = picture.MimeType,
496-
NewData = pictureBinary
497-
});
492+
_storageProvider.Value.Save(picture.ToMedia(), pictureBinary);
498493

499494
// event notification
500495
_eventPublisher.EntityUpdated(picture);
501496

502497
return picture;
503498
}
504499

505-
public virtual Picture SetSeoFilename(int pictureId, string seoFilename)
506-
{
507-
var picture = GetPictureById(pictureId);
508-
509-
// update if it has been changed
510-
if (picture != null && seoFilename != picture.SeoFilename)
511-
{
512-
picture = UpdatePicture(picture.Id, LoadPictureBinary(picture), picture.MimeType, seoFilename, true, false);
513-
}
514-
515-
return picture;
516-
}
517-
518500
#endregion
519501
}
520502
}

0 commit comments

Comments
 (0)