Repository navigation
Expand file tree
/
Copy pathDownloadController.cs
More file actions
287 lines (238 loc) · 13.5 KB
/
Copy pathDownloadController.cs
File metadata and controls
287 lines (238 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using Grand.Core;
using Grand.Core.Domain.Catalog;
using Grand.Core.Domain.Customers;
using Grand.Services.Catalog;
using Grand.Services.Customers;
using Grand.Services.Localization;
using Grand.Services.Media;
using Grand.Services.Orders;
using Microsoft.AspNetCore.Mvc;
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Grand.Web.Controllers
{
public partial class DownloadController : BasePublicController
{
private readonly IDownloadService _downloadService;
private readonly IProductService _productService;
private readonly IOrderService _orderService;
private readonly IWorkContext _workContext;
private readonly ILocalizationService _localizationService;
private readonly CustomerSettings _customerSettings;
public DownloadController(IDownloadService downloadService,
IProductService productService,
IOrderService orderService,
IWorkContext workContext,
ILocalizationService localizationService,
CustomerSettings customerSettings)
{
this._downloadService = downloadService;
this._productService = productService;
this._orderService = orderService;
this._workContext = workContext;
this._localizationService = localizationService;
this._customerSettings = customerSettings;
}
public virtual async Task<IActionResult> Sample(string productId)
{
var product = await _productService.GetProductById(productId);
if (product == null)
return InvokeHttp404();
if (!product.HasSampleDownload)
return Content("Product doesn't have a sample download.");
var download = await _downloadService.GetDownloadById(product.SampleDownloadId);
if (download == null)
return Content("Sample download is not available any more.");
if (download.UseDownloadUrl)
return new RedirectResult(download.DownloadUrl);
if (download.DownloadBinary == null)
return Content("Download data is not available any more.");
string fileName = !String.IsNullOrWhiteSpace(download.Filename) ? download.Filename : product.Id.ToString();
string contentType = !String.IsNullOrWhiteSpace(download.ContentType) ? download.ContentType : "application/octet-stream";
return new FileContentResult(download.DownloadBinary, contentType) { FileDownloadName = fileName + download.Extension };
}
public virtual async Task<IActionResult> GetDownload(Guid orderItemId, bool agree = false)
{
var orderItem = await _orderService.GetOrderItemByGuid(orderItemId);
if (orderItem == null)
return InvokeHttp404();
var order = await _orderService.GetOrderByOrderItemId(orderItem.Id);
var product = await _productService.GetProductById(orderItem.ProductId);
if (!await _downloadService.IsDownloadAllowed(orderItem))
return Content("Downloads are not allowed");
if (_customerSettings.DownloadableProductsValidateUser)
{
if (_workContext.CurrentCustomer == null)
return Challenge();
if (order.CustomerId != _workContext.CurrentCustomer.Id)
return Content("This is not your order");
}
var download = await _downloadService.GetDownloadById(product.DownloadId);
if (download == null)
return Content("Download is not available any more.");
if (product.HasUserAgreement)
{
if (!agree)
return RedirectToRoute("DownloadUserAgreement", new { orderItemId = orderItemId });
}
if (!product.UnlimitedDownloads && orderItem.DownloadCount >= product.MaxNumberOfDownloads)
return Content(string.Format(_localizationService.GetResource("DownloadableProducts.ReachedMaximumNumber"), product.MaxNumberOfDownloads));
if (download.UseDownloadUrl)
{
//increase download
order.OrderItems.FirstOrDefault(x => x.Id == orderItem.Id).DownloadCount++;
await _orderService.UpdateOrder(order);
//return result
return new RedirectResult(download.DownloadUrl);
}
//binary download
if (download.DownloadBinary == null)
return Content("Download data is not available any more.");
//increase download
order.OrderItems.FirstOrDefault(x => x.Id == orderItem.Id).DownloadCount++;
await _orderService.UpdateOrder(order);
if (product.ProductType != ProductType.BundledProduct)
{
//return result
string fileName = !String.IsNullOrWhiteSpace(download.Filename) ? download.Filename : product.Id.ToString();
string contentType = !String.IsNullOrWhiteSpace(download.ContentType) ? download.ContentType : "application/octet-stream";
return new FileContentResult(download.DownloadBinary, contentType) { FileDownloadName = fileName + download.Extension };
}
else
{
using (var memoryStream = new MemoryStream())
{
using (var ziparchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
string fileName = (!String.IsNullOrWhiteSpace(download.Filename) ? download.Filename : product.Id.ToString()) + download.Extension;
if (Grand.Core.OperatingSystem.IsWindows())
{
System.IO.File.WriteAllBytes(@"App_Data\Download\" + fileName, download.DownloadBinary);
ziparchive.CreateEntryFromFile(@"App_Data\Download\" + fileName, fileName);
}
else
{
System.IO.File.WriteAllBytes(@"App_Data/Download/" + fileName, download.DownloadBinary);
ziparchive.CreateEntryFromFile(@"App_Data/Download/" + fileName, fileName);
}
foreach (var bundle in product.BundleProducts)
{
var p1 = await _productService.GetProductById(bundle.ProductId);
if (p1 != null && p1.IsDownload)
{
var d1 = await _downloadService.GetDownloadById(p1.DownloadId);
if (d1 != null && !d1.UseDownloadUrl)
{
fileName = (!String.IsNullOrWhiteSpace(d1.Filename) ? d1.Filename : p1.Id.ToString()) + d1.Extension;
if (Grand.Core.OperatingSystem.IsWindows())
{
System.IO.File.WriteAllBytes(@"App_Data\Download\" + fileName, d1.DownloadBinary);
ziparchive.CreateEntryFromFile(@"App_Data\Download\" + fileName, fileName);
}
else
{
System.IO.File.WriteAllBytes(@"App_Data/Download/" + fileName, d1.DownloadBinary);
ziparchive.CreateEntryFromFile(@"App_Data/Download/" + fileName, fileName);
}
}
}
}
}
return File(memoryStream.ToArray(), "application/zip", $"{Regex.Replace(product.Name, "[^A-Za-z0-9 _]", "")}.zip");
}
}
}
public virtual async Task<IActionResult> GetLicense(Guid orderItemId)
{
var orderItem = await _orderService.GetOrderItemByGuid(orderItemId);
if (orderItem == null)
return InvokeHttp404();
var order = await _orderService.GetOrderByOrderItemId(orderItem.Id);
var product = await _productService.GetProductById(orderItem.ProductId);
if (!await _downloadService.IsLicenseDownloadAllowed(orderItem))
return Content("Downloads are not allowed");
if (_customerSettings.DownloadableProductsValidateUser)
{
if (_workContext.CurrentCustomer == null || order.CustomerId != _workContext.CurrentCustomer.Id)
return Challenge();
}
var download = await _downloadService.GetDownloadById(!String.IsNullOrEmpty(orderItem.LicenseDownloadId) ? orderItem.LicenseDownloadId : "");
if (download == null)
return Content("Download is not available any more.");
if (download.UseDownloadUrl)
return new RedirectResult(download.DownloadUrl);
//binary download
if (download.DownloadBinary == null)
return Content("Download data is not available any more.");
//return result
string fileName = !String.IsNullOrWhiteSpace(download.Filename) ? download.Filename : product.Id.ToString();
string contentType = !String.IsNullOrWhiteSpace(download.ContentType) ? download.ContentType : "application/octet-stream";
return new FileContentResult(download.DownloadBinary, contentType) { FileDownloadName = fileName + download.Extension };
}
public virtual async Task<IActionResult> GetFileUpload(Guid downloadId)
{
var download = await _downloadService.GetDownloadByGuid(downloadId);
if (download == null)
return Content("Download is not available any more.");
if (download.UseDownloadUrl)
return new RedirectResult(download.DownloadUrl);
//binary download
if (download.DownloadBinary == null)
return Content("Download data is not available any more.");
//return result
string fileName = !String.IsNullOrWhiteSpace(download.Filename) ? download.Filename : downloadId.ToString();
string contentType = !String.IsNullOrWhiteSpace(download.ContentType) ? download.ContentType : "application/octet-stream";
return new FileContentResult(download.DownloadBinary, contentType) { FileDownloadName = fileName + download.Extension };
}
public virtual async Task<IActionResult> GetOrderNoteFile(string orderNoteId)
{
var orderNote = await _orderService.GetOrderNote(orderNoteId);
if (orderNote == null)
return InvokeHttp404();
var order = await _orderService.GetOrderById(orderNote.OrderId);
if (order == null)
return InvokeHttp404();
if (_workContext.CurrentCustomer == null || order.CustomerId != _workContext.CurrentCustomer.Id)
return Challenge();
var download = await _downloadService.GetDownloadById(orderNote.DownloadId);
if (download == null)
return Content("Download is not available any more.");
if (download.UseDownloadUrl)
return new RedirectResult(download.DownloadUrl);
//binary download
if (download.DownloadBinary == null)
return Content("Download data is not available any more.");
//return result
string fileName = !String.IsNullOrWhiteSpace(download.Filename) ? download.Filename : orderNote.Id.ToString();
string contentType = !String.IsNullOrWhiteSpace(download.ContentType) ? download.ContentType : "application/octet-stream";
return new FileContentResult(download.DownloadBinary, contentType) { FileDownloadName = fileName + download.Extension };
}
public virtual async Task<IActionResult> GetCustomerNoteFile(string customerNoteId,
[FromServices] ICustomerService customerService)
{
if (string.IsNullOrEmpty(customerNoteId))
return Content("Download is not available.");
var customerNote = await customerService.GetCustomerNote(customerNoteId);
if (customerNote == null)
return InvokeHttp404();
if (_workContext.CurrentCustomer == null || customerNote.CustomerId != _workContext.CurrentCustomer.Id)
return Challenge();
var download = await _downloadService.GetDownloadById(customerNote.DownloadId);
if (download == null)
return Content("Download is not available any more.");
if (download.UseDownloadUrl)
return new RedirectResult(download.DownloadUrl);
//binary download
if (download.DownloadBinary == null)
return Content("Download data is not available any more.");
//return result
string fileName = !String.IsNullOrWhiteSpace(download.Filename) ? download.Filename : customerNote.Id.ToString();
string contentType = !String.IsNullOrWhiteSpace(download.ContentType) ? download.ContentType : "application/octet-stream";
return new FileContentResult(download.DownloadBinary, contentType) { FileDownloadName = fileName + download.Extension };
}
}
}