Skip to content

Commit da653e7

Browse files
Closes smartstore#2072 & smartstore#2013 Media: completely remove transiency handling for download entities and leave it up to the automatic media tracker.
1 parent 74b92ca commit da653e7

15 files changed

Lines changed: 233 additions & 256 deletions

File tree

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,19 @@ where downloadIds.Contains(dl.Id)
4848
return downloads.OrderBySequence(downloadIds).ToList();
4949
}
5050

51-
public virtual IList<Download> GetDownloadsFor<TEntity>(TEntity entity) where TEntity : BaseEntity
51+
public virtual IList<Download> GetDownloadsFor<TEntity>(TEntity entity, bool versionedFilesOnly = false) where TEntity : BaseEntity
5252
{
5353
Guard.NotNull(entity, nameof(entity));
5454

55-
return GetDownloadsFor(entity.Id, entity.GetUnproxiedType().Name);
55+
return GetDownloadsFor(entity.Id, entity.GetUnproxiedType().Name, versionedFilesOnly);
5656
}
5757

58-
public virtual IList<Download> GetDownloadsFor(int entityId, string entityName)
58+
public virtual IList<Download> GetDownloadsFor(int entityId, string entityName, bool versionedFilesOnly = false)
5959
{
6060
if (entityId > 0)
6161
{
6262
var downloads = (from x in _downloadRepository.Table.Expand(x => x.MediaFile)
63-
where x.EntityId == entityId && x.EntityName == entityName
63+
where x.EntityId == entityId && x.EntityName == entityName && (!string.IsNullOrEmpty(x.FileVersion) && versionedFilesOnly)
6464
select x).ToList();
6565

6666
if (downloads.Any())
@@ -131,11 +131,13 @@ public virtual void DeleteDownload(Download download)
131131
_downloadRepository.Delete(download);
132132
}
133133

134-
public virtual void InsertDownload(Download download)
134+
public virtual void InsertDownload(Download download, out int? id)
135135
{
136136
Guard.NotNull(download, nameof(download));
137137

138138
_downloadRepository.Insert(download);
139+
140+
id = download.Id;
139141
}
140142

141143
public virtual MediaFileInfo InsertDownload(Download download, Stream stream, string fileName)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ public partial interface IDownloadService
2929
/// </summary>
3030
/// <param name="entity">Entity to get download for</param>
3131
/// <returns>List of download entities sorted by FileVersion</returns>
32-
IList<Download> GetDownloadsFor<TEntity>(TEntity entity) where TEntity : BaseEntity;
32+
IList<Download> GetDownloadsFor<TEntity>(TEntity entity, bool versionedFilesOnly = false) where TEntity : BaseEntity;
3333

3434
/// <summary>
3535
/// Gets downloads by entity identifier
3636
/// </summary>
3737
/// <param name="entityId">Entity identifier</param>
3838
/// <param name="entityName">Entity name</param>
3939
/// <returns>List of download entities</returns>
40-
IList<Download> GetDownloadsFor(int entityId, string entityName);
40+
IList<Download> GetDownloadsFor(int entityId, string entityName, bool versionedFilesOnly = false);
4141

4242
/// <summary>
4343
/// Gets downloads by entity identifier & fileversion
@@ -73,7 +73,7 @@ public partial interface IDownloadService
7373
/// Inserts a download
7474
/// </summary>
7575
/// <param name="download">Download</param>
76-
void InsertDownload(Download download);
76+
void InsertDownload(Download download, out int? id);
7777

7878
/// <summary>
7979
/// Inserts a download.

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

Lines changed: 0 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5-
using System.Linq.Expressions;
6-
using SmartStore.ComponentModel;
7-
using SmartStore.Core;
8-
using SmartStore.Core.Data;
9-
using SmartStore.Core.Domain.Media;
10-
using SmartStore.Core.Infrastructure;
115

126
namespace SmartStore.Services.Media
137
{
@@ -88,127 +82,5 @@ public bool CheckUniqueFileName(string title, string ext, HashSet<string> destFi
8882
i++;
8983
}
9084
}
91-
92-
#region Legacy (remove later)
93-
94-
// TODO: (mm) (mc) remove this stuff
95-
96-
public static void UpdateDownloadTransientStateFor<TEntity>(TEntity entity, Expression<Func<TEntity, int>> downloadIdProp, bool save = false) where TEntity : BaseEntity
97-
{
98-
Guard.NotNull(entity, nameof(entity));
99-
Guard.NotNull(downloadIdProp, nameof(downloadIdProp));
100-
101-
var propName = downloadIdProp.ExtractMemberInfo().Name;
102-
int currentDownloadId = downloadIdProp.CompileFast(PropertyCachingStrategy.EagerCached).Invoke(entity);
103-
var rs = EngineContext.Current.Resolve<IRepository<Download>>();
104-
105-
UpdateTransientStateForEntityInternal(entity, propName, currentDownloadId, rs, null, save);
106-
}
107-
108-
public static void UpdateDownloadTransientStateFor<TEntity>(TEntity entity, Expression<Func<TEntity, int?>> downloadIdProp, bool save = false) where TEntity : BaseEntity
109-
{
110-
Guard.NotNull(entity, nameof(entity));
111-
Guard.NotNull(downloadIdProp, nameof(downloadIdProp));
112-
113-
var propName = downloadIdProp.ExtractMemberInfo().Name;
114-
int currentDownloadId = downloadIdProp.CompileFast(PropertyCachingStrategy.EagerCached).Invoke(entity).GetValueOrDefault();
115-
var rs = EngineContext.Current.Resolve<IRepository<Download>>();
116-
117-
UpdateTransientStateForEntityInternal(entity, propName, currentDownloadId, rs, null, save);
118-
}
119-
120-
//public static void UpdateDownloadTransientState(int? prevDownloadId, int? currentDownloadId, bool save = false)
121-
//{
122-
// var rs = EngineContext.Current.Resolve<IRepository<Download>>();
123-
// UpdateTransientStateCore(prevDownloadId.GetValueOrDefault(), currentDownloadId.GetValueOrDefault(), rs, null, save);
124-
//}
125-
126-
//public static void UpdateDownloadTransientState(int prevDownloadId, int currentDownloadId, bool save = false)
127-
//{
128-
// var rs = EngineContext.Current.Resolve<IRepository<Download>>();
129-
// UpdateTransientStateCore(prevDownloadId, currentDownloadId, rs, null, save);
130-
//}
131-
132-
internal static void UpdateTransientStateForEntityInternal<TEntity, TMedia>(
133-
TEntity entity,
134-
string propName,
135-
int currentMediaId,
136-
IRepository<TMedia> rs,
137-
Action<object> deleteAction,
138-
bool save) where TEntity : BaseEntity where TMedia : BaseEntity
139-
{
140-
object obj = null;
141-
int prevMediaId = 0;
142-
if (rs.Context.TryGetModifiedProperty(entity, propName, out obj))
143-
{
144-
prevMediaId = ((int?)obj).GetValueOrDefault();
145-
}
146-
147-
UpdateTransientStateCore(prevMediaId, currentMediaId, rs, deleteAction, save);
148-
}
149-
150-
internal static void UpdateTransientStateCore<TMedia>(
151-
int prevMediaId,
152-
int currentMediaId,
153-
IRepository<TMedia> rs,
154-
Action<object> deleteAction,
155-
bool save)
156-
where TMedia : BaseEntity
157-
{
158-
var autoCommit = rs.AutoCommitEnabled;
159-
rs.AutoCommitEnabled = false;
160-
161-
try
162-
{
163-
TMedia media = null;
164-
bool shouldSave = false;
165-
166-
bool isModified = prevMediaId != currentMediaId;
167-
168-
if (currentMediaId > 0 && isModified)
169-
{
170-
// new entity with a media or media replaced
171-
media = rs.GetById(currentMediaId);
172-
if (media != null)
173-
{
174-
var transient = media as ITransient;
175-
if (transient != null)
176-
{
177-
transient.IsTransient = false;
178-
shouldSave = true;
179-
}
180-
}
181-
}
182-
183-
if (prevMediaId > 0 && isModified)
184-
{
185-
// ID changed, so delete old record
186-
media = rs.GetById(prevMediaId);
187-
if (media != null)
188-
{
189-
if (deleteAction == null)
190-
{
191-
rs.Delete(media);
192-
}
193-
else
194-
{
195-
deleteAction(media);
196-
}
197-
shouldSave = true;
198-
}
199-
}
200-
201-
if (save && shouldSave)
202-
{
203-
rs.Context.SaveChanges();
204-
}
205-
}
206-
finally
207-
{
208-
rs.AutoCommitEnabled = autoCommit;
209-
}
210-
}
211-
212-
#endregion
21385
}
21486
}

src/Plugins/SmartStore.WebApi/Controllers/OData/DownloadsController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public IHttpActionResult GetProperty(int key, string propertyName)
3535
[WebApiAuthenticate(Permission = Permissions.Media.Download.Create)]
3636
public IHttpActionResult Post(Download entity)
3737
{
38-
var result = Insert(entity, () => Service.InsertDownload(entity));
38+
var result = Insert(entity, () => Service.InsertDownload(entity, out _));
3939
return result;
4040
}
4141

src/Presentation/SmartStore.Web/Administration/Controllers/DownloadController.cs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public ActionResult SaveDownloadUrl(string downloadUrl, bool minimalMode = false
7272
UpdatedOnUtc = DateTime.UtcNow
7373
};
7474

75-
_downloadService.InsertDownload(download);
75+
_downloadService.InsertDownload(download, out _);
7676

7777
return Json(new
7878
{
@@ -98,7 +98,7 @@ public ActionResult CreateDownloadFromMediaFile(int mediaFileId, int entityId =
9898
UpdatedOnUtc = DateTime.UtcNow
9999
};
100100

101-
_downloadService.InsertDownload(download);
101+
_downloadService.InsertDownload(download, out _);
102102

103103
return Json(new
104104
{
@@ -117,28 +117,17 @@ public ActionResult AsyncUpload(string clientCtrlId, bool minimalMode = false, s
117117
throw new ArgumentException(T("Common.NoFileUploaded"));
118118
}
119119

120-
var download = new Download
121-
{
122-
EntityId = entityId,
123-
EntityName = entityName,
124-
DownloadGuid = Guid.NewGuid(),
125-
UseDownloadUrl = false,
126-
DownloadUrl = string.Empty,
127-
UpdatedOnUtc = DateTime.UtcNow
128-
};
129-
130-
var mediaFile = _downloadService.InsertDownload(download, postedFile.Stream, postedFile.FileName);
120+
var path = _mediaService.CombinePaths(SystemAlbumProvider.Downloads, postedFile.FileName);
121+
var file = _mediaService.SaveFile(path, postedFile.Stream, dupeFileHandling: DuplicateFileHandling.Rename);
131122

132123
return Json(new
133124
{
134125
success = true,
135126
clientCtrlId,
136-
downloadId = download.Id,
137-
id = download.MediaFileId,
138-
name = mediaFile.Name,
139-
type = mediaFile.MediaType,
140-
thumbUrl = _mediaService.GetUrl(download.MediaFileId, _mediaSettings.ProductThumbPictureSize, host: string.Empty),
141-
html = this.RenderPartialViewToString(DOWNLOAD_TEMPLATE, download.Id, new { minimalMode, fieldName, entityId, entityName }) // OBSOLETE
127+
id = file.Id,
128+
name = file.Name,
129+
type = file.MediaType,
130+
thumbUrl = _mediaService.GetUrl(file.Id, _mediaSettings.ProductThumbPictureSize, host: string.Empty)
142131
});
143132
}
144133

src/Presentation/SmartStore.Web/Administration/Controllers/OrderController.cs

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public partial class OrderController : AdminControllerBase
100100
private readonly SearchSettings _searchSettings;
101101
private readonly ShoppingCartSettings _shoppingCartSettings;
102102
private readonly Lazy<MediaSettings> _mediaSettings;
103+
private readonly IMediaService _mediaService;
103104

104105
#endregion
105106

@@ -147,7 +148,8 @@ public OrderController(
147148
AdminAreaSettings adminAreaSettings,
148149
SearchSettings searchSettings,
149150
ShoppingCartSettings shoppingCartSettings,
150-
Lazy<MediaSettings> mediaSettings)
151+
Lazy<MediaSettings> mediaSettings,
152+
IMediaService mediaService)
151153
{
152154
_orderService = orderService;
153155
_orderReportService = orderReportService;
@@ -192,6 +194,7 @@ public OrderController(
192194
_searchSettings = searchSettings;
193195
_shoppingCartSettings = shoppingCartSettings;
194196
_mediaSettings = mediaSettings;
197+
_mediaService = mediaService;
195198
}
196199

197200
#endregion
@@ -1908,6 +1911,7 @@ public ActionResult UploadLicenseFilePopup(int id, int orderItemId)
19081911
var model = new OrderModel.UploadLicenseModel
19091912
{
19101913
LicenseDownloadId = orderItem.LicenseDownloadId ?? 0,
1914+
OldLicenseDownloadId = orderItem.LicenseDownloadId ?? 0,
19111915
OrderId = order.Id,
19121916
OrderItemId = orderItem.Id
19131917
};
@@ -1932,13 +1936,49 @@ public ActionResult UploadLicenseFilePopup(string btnId, string formId, OrderMod
19321936
return HttpNotFound();
19331937
}
19341938

1935-
// Attach license.
1936-
if (model.LicenseDownloadId > 0)
1939+
var isUrlDownload = Request.Form["is-url-download-" + model.LicenseDownloadId] == "true";
1940+
var setOldFileToTransient = false;
1941+
1942+
if (model.LicenseDownloadId != model.OldLicenseDownloadId && model.LicenseDownloadId != 0 && !isUrlDownload)
1943+
{
1944+
// Insert download if a new file was uploaded.
1945+
var mediaFileInfo = _mediaService.GetFileById(model.LicenseDownloadId);
1946+
var download = new Download
1947+
{
1948+
MediaFile = mediaFileInfo.File,
1949+
EntityId = model.OrderId,
1950+
EntityName = "LicenseDownloadId",
1951+
DownloadGuid = Guid.NewGuid(),
1952+
UseDownloadUrl = false,
1953+
DownloadUrl = string.Empty,
1954+
UpdatedOnUtc = DateTime.UtcNow,
1955+
IsTransient = false
1956+
};
1957+
1958+
_downloadService.InsertDownload(download, out var downloadId);
1959+
orderItem.LicenseDownloadId = downloadId;
1960+
1961+
setOldFileToTransient = true;
1962+
}
1963+
else if (isUrlDownload)
1964+
{
1965+
var download = _downloadService.GetDownloadById(model.LicenseDownloadId);
1966+
download.IsTransient = false;
1967+
_downloadService.UpdateDownload(download);
1968+
19371969
orderItem.LicenseDownloadId = model.LicenseDownloadId;
1938-
else
1939-
orderItem.LicenseDownloadId = null;
19401970

1941-
MediaHelper.UpdateDownloadTransientStateFor(orderItem, x => x.LicenseDownloadId);
1971+
setOldFileToTransient = true;
1972+
}
1973+
1974+
if (setOldFileToTransient && model.OldLicenseDownloadId > 0)
1975+
{
1976+
// Set old download to transient if LicenseDownloadId is 0.
1977+
var oldDownload = _downloadService.GetDownloadById(model.OldLicenseDownloadId);
1978+
oldDownload.IsTransient = true;
1979+
_downloadService.UpdateDownload(oldDownload);
1980+
}
1981+
19421982
_orderService.UpdateOrder(order);
19431983

19441984
ViewBag.RefreshPage = true;
@@ -1965,9 +2005,13 @@ public ActionResult DeleteLicenseFilePopup(string btnId, string formId, OrderMod
19652005
return HttpNotFound();
19662006
}
19672007

1968-
// Attach license.
2008+
// Set deleted file to transient.
2009+
var download = _downloadService.GetDownloadById((int)model.OldLicenseDownloadId);
2010+
download.IsTransient = true;
2011+
_downloadService.UpdateDownload(download);
2012+
2013+
// Detach license.
19692014
orderItem.LicenseDownloadId = null;
1970-
MediaHelper.UpdateDownloadTransientStateFor(orderItem, x => x.LicenseDownloadId);
19712015
_orderService.UpdateOrder(order);
19722016

19732017
ViewBag.RefreshPage = true;

0 commit comments

Comments
 (0)