Example #1
0
    } // End of the Add method

    /// <summary>
    /// Add customer files 
    /// </summary>
    /// <param name="order">A reference to the order</param>
    public static void AddCustomerFiles(Order order)
    {
        // Get the current domain
        Domain domain = Tools.GetCurrentDomain();

        // Get the order rows
        List<OrderRow> orderRows = OrderRow.GetByOrderId(order.id);

        // Get the customer
        Customer customer = Customer.GetOneById(order.customer_id);

        // Loop all of the order rows
        for (int i = 0; i < orderRows.Count; i++)
        {
            // Get the product
            Product product = Product.GetOneById(orderRows[i].product_id, domain.back_end_language);

            // Continue if the product is null
            if (product == null) { continue; }

            // Check if the product has a downloadable files
            if (product.downloadable_files == true)
            {
                // Create the customer file
                CustomerFile customerFile = new CustomerFile();
                customerFile.customer_id = customer.id;
                customerFile.product_id = product.id;
                customerFile.language_id = customer.language_id;
                customerFile.order_date = order.order_date;

                // Check if the file already exists
                CustomerFile savedFile = CustomerFile.GetOneById(customerFile.customer_id, customerFile.product_id, customerFile.language_id);

                if (savedFile == null)
                {
                    // Add the customer file
                    CustomerFile.Add(customerFile);
                }
            }
        }

    } // End of the AddCustomerFiles method
        public HttpResponseMessage get_file(Int32 id = 0, Int32 languageId = 0, string fileName = "")
        {
            // Get the authorization token
            string authToken    = Request.Headers.Authorization.Parameter;
            string decodedToken = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(authToken));

            // Get the email and password
            string email    = decodedToken.Substring(0, decodedToken.IndexOf(":"));
            string password = decodedToken.Substring(decodedToken.IndexOf(":") + 1);

            // Get the language
            if (Language.GetOneById(languageId) == null)
            {
                return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The language does not exist"));
            }

            // Get the customer
            Customer customer = Customer.GetOneByEmail(email);

            // Check if the customer exists and if the password is correct
            if (customer == null)
            {
                return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The customer does not exist"));
            }

            // Get the product
            Product product = Product.GetOneById(id, languageId);

            // Check if the product exist
            if (product == null)
            {
                return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The product does not exist"));
            }

            // Get the customer file
            CustomerFile customerFile = CustomerFile.GetOneById(customer.id, product.id, customer.language_id);

            // Check if the customer file exists
            if (customerFile == null)
            {
                return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The customer are not eligible to download the file"));
            }

            // Create the language id
            string strLanguageId = product.use_local_files == true?customer.language_id.ToString() : "0";

            // Create the file path
            string filePath = System.Web.HttpContext.Current.Server.MapPath("/Content/products/" + (id / 100).ToString() + "/" + id.ToString() + "/"
                                                                            + strLanguageId + "/dc_files/" + fileName);

            // Return the file if it exists
            if (File.Exists(filePath) == true)
            {
                HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
                FileStream          stream = new FileStream(filePath, FileMode.Open);
                result.Content = new StreamContent(stream);
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                return(result);
            }
            else
            {
                // The file does not exist
                return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The file does not exist"));
            }
        } // End of the get_file method