Пример #1
0
        public ActionResult GetDownload(Guid opvId, bool agree = false)
        {
            var orderProductVariant = _orderService.GetOrderProductVariantByGuid(opvId);

            if (orderProductVariant == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            var order          = orderProductVariant.Order;
            var productVariant = orderProductVariant.ProductVariant;

            if (!_orderProcessingService.IsDownloadAllowed(orderProductVariant))
            {
                return(Content("Downloads are not allowed"));
            }

            if (_customerSettings.DownloadableProductsValidateUser)
            {
                if (_workContext.CurrentCustomer == null)
                {
                    return(new HttpUnauthorizedResult());
                }

                if (order.CustomerId != _workContext.CurrentCustomer.Id)
                {
                    return(Content("This is not your order"));
                }
            }

            var download = _downloadService.GetDownloadById(productVariant.DownloadId);

            if (download == null)
            {
                return(Content("Download is not available any more."));
            }

            if (productVariant.HasUserAgreement)
            {
                if (!agree)
                {
                    return(RedirectToAction("useragreement", "customer", new { opvId = opvId }));
                }
            }


            if (!productVariant.UnlimitedDownloads && orderProductVariant.DownloadCount >= productVariant.MaxNumberOfDownloads)
            {
                return(Content(string.Format("You have reached maximum number of downloads {0}", productVariant.MaxNumberOfDownloads)));
            }


            if (download.UseDownloadUrl)
            {
                orderProductVariant.DownloadCount++;
                _orderService.UpdateOrder(order);


                return(new RedirectResult(download.DownloadUrl));
            }
            else
            {
                if (download.DownloadBinary == null)
                {
                    return(Content("Download data is not available any more."));
                }

                string fileName = download.Filename ?? productVariant.Id.ToString();


                orderProductVariant.DownloadCount++;
                _orderService.UpdateOrder(order);


                return(new FileContentResult(download.DownloadBinary, download.ContentType)
                {
                    FileDownloadName = fileName + download.Extension
                });
            }
        }