Esempio n. 1
0
        private Dictionary <int, Download> CopyDownloads(int productId, int cloneId, string entityName = "")
        {
            var downloads       = _downloadService.GetDownloadsFor(productId, entityName);
            var clonedDownloads = new Dictionary <int, Download>();

            if (!downloads.Any())
            {
                return(null);
            }

            using (var scope = new DbContextScope(ctx: _productRepository.Context, autoCommit: true))
            {
                foreach (var download in downloads)
                {
                    download.EntityId = cloneId;
                    var clone = CopyDownload(download);
                    clonedDownloads[clone.Id] = clone;
                }
            }

            return(clonedDownloads);
        }
        public ActionResult GetDownload(Guid id, bool agree = false, string fileVersion = "")
        {
            if (id == Guid.Empty)
            {
                return(HttpNotFound());
            }

            var orderItem = _orderService.GetOrderItemByGuid(id);

            if (orderItem == null)
            {
                return(HttpNotFound());
            }

            var order           = orderItem.Order;
            var product         = orderItem.Product;
            var hasNotification = false;

            if (!_downloadService.IsDownloadAllowed(orderItem))
            {
                hasNotification = true;
                NotifyError(T("Common.Download.NotAllowed"));
            }

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

                if (order.CustomerId != _workContext.CurrentCustomer.Id)
                {
                    hasNotification = true;
                    NotifyError(T("Account.CustomerOrders.NotYourOrder"));
                }
            }

            Download download;

            if (fileVersion.HasValue())
            {
                download = _downloadService.GetDownloadByVersion(product.Id, "Product", fileVersion);
            }
            else
            {
                download = _downloadService.GetDownloadsFor(product).FirstOrDefault();
            }

            if (download == null)
            {
                hasNotification = true;
                NotifyError(T("Common.Download.NoDataAvailable"));
            }

            if (product.HasUserAgreement && !agree)
            {
                hasNotification = true;
            }

            if (!product.UnlimitedDownloads && orderItem.DownloadCount >= product.MaxNumberOfDownloads)
            {
                hasNotification = true;
                NotifyError(T("Common.Download.MaxNumberReached", product.MaxNumberOfDownloads));
            }

            if (hasNotification)
            {
                return(RedirectToAction("UserAgreement", "Customer", new { id, fileVersion }));
            }

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

                return(new RedirectResult(download.DownloadUrl));
            }
            else
            {
                var data = _downloadService.LoadDownloadBinary(download);

                if (data == null || data.LongLength == 0)
                {
                    NotifyError(T("Common.Download.NoDataAvailable"));
                    return(RedirectToAction("UserAgreement", "Customer", new { id = id }));
                }

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

                return(GetFileContentResultFor(download, product, data));
            }
        }