Exemplo n.º 1
0
        /// <summary>
        /// Downloads the image of an advert.
        /// </summary>
        /// <param name="advert">Advert to download the image.</param>
        private static void DownloadAdvertImage(Advert advert)
        {
            // Compose advert image name
            string advertImageName =
                string.Format(Resources.AdvertImageFileName, advert.AdvertID.ToString());

            bool imageWasDownloaded = false;
            bool imageExistsInFileSystem = File.Exists(AdvertHelper.AdvertsLocalFolder + advertImageName);

            if (imageExistsInFileSystem == false)
            {
                try
                {
                    // Download image
                    (new WebClient()).DownloadFile(Resources.AdvertsBaseURL + advertImageName,
                        AdvertHelper.AdvertsLocalFolder + advertImageName);

                    imageWasDownloaded = true;
                }
                catch (Exception ex)
                {
                    // Log error
                    LogManager.LogError(ex);

                    // Image could not be downloaded
                    advert.Image = null;
                }
            }

            if (imageExistsInFileSystem || imageWasDownloaded)
            {
                // Get image from file system
                using (FileStream imgStream = File.OpenRead(AdvertHelper.AdvertsLocalFolder + advertImageName))
                {
                    advert.Image = new Bitmap(imgStream);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes advert logic.
        /// </summary>
        public static void Init()
        {
            if (AdvertHelper.IsInitialized == false)
            {
                AdvertHelper.IsInitialized = true;
                AdvertHelper.AvailablesAdverts = new List<Advert>();

                try
                {
                    // Create advert local folder if not exists
                    if (Directory.Exists(AdvertHelper.AdvertsLocalFolder) == false)
                    {
                        Directory.CreateDirectory(AdvertHelper.AdvertsLocalFolder);
                    }

                    // Get advert-xml from repository
                    XDocument xdoc = XDocument.Load(Resources.AdvertsBaseURL + Resources.AdvertsConfigFileName);

                    // Update latest advert-xml downloaded with the new one
                    xdoc.Save(AdvertHelper.AdvertsConfigFilePath);
                }
                catch (Exception ex)
                {
                    AdvertHelper.IsInitialized = false;

                    // Log error
                    LogManager.LogError(ex);
                }

                if (File.Exists(AdvertHelper.AdvertsConfigFilePath) == true)
                {
                    try
                    {
                        // Load adverts config file
                        XDocument xdoc = XDocument.Load(AdvertHelper.AdvertsConfigFilePath);

                        // Get enabled adverts
                        IEnumerable<XElement> advertsFromConfig = xdoc.Descendants(AdvertsConfigNodes.Advert)
                            .Where(n => n.Attribute(AdvertsConfigNodes.AdvertAttrEnabled)
                                .Value.ToLower() == (true).ToString().ToLower());

                        // Loop through adverts from config
                        foreach (XElement advertNode in advertsFromConfig)
                        {
                            bool downloadAdvertImage = false;

                            var advert = new Advert();

                            advert.AdvertID = Convert.ToInt32(advertNode.Attribute(AdvertsConfigNodes.AdvertAttrId).Value);

                            advert.LinkURL = advertNode.Descendants(AdvertsConfigNodes.Link).First()
                                .Attribute(AdvertsConfigNodes.LinkAttrUrl).Value;

                            // Get compiled attribute value
                            string compiledValue = advertNode.Attribute(AdvertsConfigNodes.AdvertAttrCompiled).Value;

                            if (compiledValue.ToLower() == (true).ToString().ToLower())
                            {
                                // Advert is compiled, get image from CompiledAdverts
                                Advert advertCompiled = AdvertHelper.CompiledAdverts.FirstOrDefault(a => a.AdvertID == advert.AdvertID);

                                if (advertCompiled != null)
                                {
                                    advert.Image = advertCompiled.Image;
                                }
                                else
                                {
                                    // Advert is marked as compiled but was not found in resources
                                    downloadAdvertImage = true;
                                }
                            }
                            else
                            {
                                downloadAdvertImage = true;
                            }

                            if (downloadAdvertImage == true)
                            {
                                AdvertHelper.DownloadAdvertImage(advert);

                                if (advert.Image == null)
                                {
                                    // Image could not be downloaded, continue with next advert
                                    continue;
                                }
                            }

                            // Add to available adverts
                            AdvertHelper.AvailablesAdverts.Add(advert);

                        } // END foreach
                    }
                    catch (Exception ex)
                    {
                        AdvertHelper.IsInitialized = false;

                        // Log error
                        LogManager.LogError(ex);
                    }
                } // END if

                if (AdvertHelper.AvailablesAdverts.Count == 0)
                {
                    // Indicate all compiled adverts as available adverts
                    AdvertHelper.AvailablesAdverts = new List<Advert>();
                    foreach (Advert compiledAd in AdvertHelper.CompiledAdverts)
                    {
                        AdvertHelper.AvailablesAdverts.Add(compiledAd);
                    }

                    if (AdvertHelper.AvailablesAdverts.Count == 0)
                    {
                        // No available adverts, show default advert
                        AdvertHelper.AvailablesAdverts.Add(AdvertHelper.DefaultAdvert);
                    }
                }

            } // END if (AdvertHelper.IsInitialized == false)
        }