DownloadManager.cs :  » Web-Frameworks » nopCommerce » NopSolutions » NopCommerce » BusinessLogic » Media » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Web Frameworks » nopCommerce 
nopCommerce » NopSolutions » NopCommerce » BusinessLogic » Media » DownloadManager.cs
//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.IO;
using NopSolutions.NopCommerce.BusinessLogic.Orders;
using NopSolutions.NopCommerce.BusinessLogic.Payment;
using NopSolutions.NopCommerce.BusinessLogic.Products;
using NopSolutions.NopCommerce.BusinessLogic.Utils;
using NopSolutions.NopCommerce.Common.Utils;
using NopSolutions.NopCommerce.DataAccess;
using NopSolutions.NopCommerce.DataAccess.Media;

namespace NopSolutions.NopCommerce.BusinessLogic.Media{
    /// <summary>
    /// Download manager
    /// </summary>
    public partial class DownloadManager
    {
        #region Utilities
        private static DownloadCollection DBMapping(DBDownloadCollection dbCollection)
        {
            if (dbCollection == null)
                return null;

            var collection = new DownloadCollection();
            foreach (var dbItem in dbCollection)
            {
                var item = DBMapping(dbItem);
                collection.Add(item);
            }

            return collection;
        }

        private static Download DBMapping(DBDownload dbItem)
        {
            if (dbItem == null)
                return null;

            var item = new Download();
            item.DownloadId = dbItem.DownloadId;
            item.UseDownloadUrl = dbItem.UseDownloadUrl;
            item.DownloadUrl = dbItem.DownloadUrl;
            item.DownloadBinary = dbItem.DownloadBinary;
            item.ContentType = dbItem.ContentType;
            item.Filename = dbItem.Filename;
            item.Extension = dbItem.Extension;
            item.IsNew = dbItem.IsNew;

            return item;
        }
        #endregion

        #region Methods
        /// <summary>
        /// Gets a download url for an admin area
        /// </summary>
        /// <param name="download">Download instance</param>
        /// <returns>Download url</returns>
        public static string GetAdminDownloadUrl(Download download)
        {
            if (download == null)
                throw new ArgumentNullException("download");
            string url = CommonHelper.GetStoreAdminLocation() + "GetDownloadAdmin.ashx?DownloadID=" + download.DownloadId;
            return url.ToLowerInvariant();
        }

        /// <summary>
        /// Gets a download url for a product variant
        /// </summary>
        /// <param name="orderProductVariant">Order product variant instance</param>
        /// <returns>Download url</returns>
        public static string GetDownloadUrl(OrderProductVariant orderProductVariant)
        {
            if (orderProductVariant == null)
                throw new ArgumentNullException("orderProductVariant");

            string url = string.Empty;
            var productVariant = orderProductVariant.ProductVariant;
            if (productVariant != null && productVariant.IsDownload)
            {
                url = string.Format("{0}GetDownload.ashx?OrderProductVariantGuid={1}", CommonHelper.GetStoreLocation(), orderProductVariant.OrderProductVariantGuid);
            }
            return url.ToLowerInvariant();
        }

        /// <summary>
        /// Gets a license download url for a product variant
        /// </summary>
        /// <param name="orderProductVariant">Order product variant instance</param>
        /// <returns>Download url</returns>
        public static string GetLicenseDownloadUrl(OrderProductVariant orderProductVariant)
        {
            if (orderProductVariant == null)
                throw new ArgumentNullException("orderProductVariant");

            string url = string.Empty;
            var productVariant = orderProductVariant.ProductVariant;
            if (productVariant != null && productVariant.IsDownload && orderProductVariant.LicenseDownloadId > 0)
            {
                url = string.Format("{0}GetLicense.ashx?OrderProductVariantGuid={1}", CommonHelper.GetStoreLocation(), orderProductVariant.OrderProductVariantGuid);
            }
            return url.ToLowerInvariant();
        }


        /// <summary>
        /// Gets a sample download url for a product variant
        /// </summary>
        /// <param name="productVariant">Product variant instance</param>
        /// <returns>Download url</returns>
        public static string GetSampleDownloadUrl(ProductVariant productVariant)
        {
            if (productVariant == null)
                throw new ArgumentNullException("productVariant");

            string url = string.Empty;
            if (productVariant.IsDownload && productVariant.HasSampleDownload)
            {
                url = CommonHelper.GetStoreLocation() + "GetDownload.ashx?SampleDownloadProductVariantID=" + productVariant.ProductVariantId.ToString();
            }
            return url.ToLowerInvariant();
        }

        /// <summary>
        /// Gets a download
        /// </summary>
        /// <param name="downloadId">Download identifier</param>
        /// <returns>Download</returns>
        public static Download GetDownloadById(int downloadId)
        {
            if (downloadId == 0)
                return null;

            var dbItem = DBProviderManager<DBDownloadProvider>.Provider.GetDownloadById(downloadId);
            var download = DBMapping(dbItem);
            return download;
        }

        /// <summary>
        /// Deletes a download
        /// </summary>
        /// <param name="downloadId">Download identifier</param>
        public static void DeleteDownload(int downloadId)
        {
            DBProviderManager<DBDownloadProvider>.Provider.DeleteDownload(downloadId);
        }

        /// <summary>
        /// Inserts a download
        /// </summary>
        /// <param name="useDownloadUrl">The value indicating whether DownloadURL property should be used</param>
        /// <param name="downloadUrl">The download URL</param>
        /// <param name="downloadBinary">The download binary</param>
        /// <param name="contentType">The content type</param>
        /// <param name="filename">The filename of the download</param>
        /// <param name="extension">The extension</param>
        /// <param name="isNew">A value indicating whether the download is new</param>
        /// <returns>Download</returns>
        public static Download InsertDownload(bool useDownloadUrl, string downloadUrl,
            byte[] downloadBinary, string contentType, string filename,
            string extension, bool isNew)
        {
            if (downloadUrl == null)
                downloadUrl = string.Empty;
            if (filename == null)
                filename = string.Empty;
            if (contentType == null)
                contentType = string.Empty;
            if (extension == null)
                extension = string.Empty;

            var dbItem = DBProviderManager<DBDownloadProvider>.Provider.InsertDownload(useDownloadUrl,
                downloadUrl, downloadBinary, contentType, filename, extension, isNew);
            var download = DBMapping(dbItem);
            return download;
        }

        /// <summary>
        /// Updates the download
        /// </summary>
        /// <param name="downloadId">The download identifier</param>
        /// <param name="useDownloadUrl">The value indicating whether DownloadURL property should be used</param>
        /// <param name="downloadUrl">The download URL</param>
        /// <param name="downloadBinary">The download binary</param>
        /// <param name="contentType">The content type</param>
        /// <param name="filename">The filename of the download</param>
        /// <param name="extension">The extension</param>
        /// <param name="isNew">A value indicating whether the download is new</param>
        /// <returns>Download</returns>
        public static Download UpdateDownload(int downloadId,
            bool useDownloadUrl, string downloadUrl,
            byte[] downloadBinary, string contentType, string filename,
            string extension, bool isNew)
        {
            if (downloadUrl == null)
                downloadUrl = string.Empty;
            if (filename == null)
                filename = string.Empty;
            if (contentType == null)
                contentType = string.Empty;
            if (extension == null)
                extension = string.Empty;

            var dbItem = DBProviderManager<DBDownloadProvider>.Provider.UpdateDownload(downloadId,
                useDownloadUrl, downloadUrl, downloadBinary, contentType, 
                filename, extension, isNew);
            var download = DBMapping(dbItem);
            return download;
        }

        /// <summary>
        /// Gets the download binary array
        /// </summary>
        /// <param name="fs">File stream</param>
        /// <param name="size">Download size</param>
        /// <returns>Download binary array</returns>
        public static byte[] GetDownloadBits(Stream fs, int size)
        {
            byte[] binary = new byte[size];
            fs.Read(binary, 0, size);
            return binary;
        }
        #endregion
    }
}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.