Example #1
0
        /// <summary>
        /// Get the absolute path to the language resources file
        /// </summary>
        /// <returns>Path</returns>
        protected virtual string GetLanguageFile()
        {
            string languageCode = GetSetting("LANG");
            string languageFile = $"{LANGUAGE_DIRECTORY}/{languageCode}.json";

            if (!_fileProvider.FileExists(GetFullPath(languageFile)))
            {
                languageFile = $"{LANGUAGE_DIRECTORY}/tr.json";
            }

            return(GetFullPath(languageFile));
        }
Example #2
0
        public Task SendEmailAsync(EmailAccount emailAccount, string subject, string body,
                                   string fromAddress, string fromName, string toAddress, string toName,
                                   string replyTo                       = null, string replyToName = null, string attachmentFilePath = null, string attachmentFileName = null,
                                   IEnumerable <string> bcc             = null, IEnumerable <string> cc = null,
                                   IDictionary <string, string> headers = null)
        {
            MailMessage message = new MailMessage
            {
                //from, to, reply to
                From = new MailAddress(fromAddress, fromName)
            };

            message.To.Add(new MailAddress(toAddress, toName));
            if (!string.IsNullOrEmpty(replyTo))
            {
                message.ReplyToList.Add(new MailAddress(replyTo, replyToName));
            }

            //BCC
            if (bcc != null)
            {
                foreach (string address in bcc.Where(bccValue => !string.IsNullOrWhiteSpace(bccValue)))
                {
                    message.Bcc.Add(address.Trim());
                }
            }

            //CC
            if (cc != null)
            {
                foreach (string address in cc.Where(ccValue => !string.IsNullOrWhiteSpace(ccValue)))
                {
                    message.CC.Add(address.Trim());
                }
            }

            //content
            message.Subject    = subject;
            message.Body       = body;
            message.IsBodyHtml = true;

            //headers
            if (headers != null)
            {
                foreach (KeyValuePair <string, string> header in headers)
                {
                    message.Headers.Add(header.Key, header.Value);
                }
            }

            //create the file attachment for this e-mail message
            if (!string.IsNullOrEmpty(attachmentFilePath) &&
                _fileProvider.FileExists(attachmentFilePath))
            {
                Attachment attachment = new Attachment(attachmentFilePath);
                attachment.ContentDisposition.CreationDate     = _fileProvider.GetCreationTime(attachmentFilePath);
                attachment.ContentDisposition.ModificationDate = _fileProvider.GetLastWriteTime(attachmentFilePath);
                attachment.ContentDisposition.ReadDate         = _fileProvider.GetLastAccessTime(attachmentFilePath);
                if (!string.IsNullOrEmpty(attachmentFileName))
                {
                    attachment.Name = attachmentFileName;
                }

                message.Attachments.Add(attachment);
            }


            //send email
            using (SmtpClient smtpClient = new SmtpClient())
            {
                smtpClient.UseDefaultCredentials = emailAccount.UseDefaultCredentials;
                smtpClient.Host        = emailAccount.Host;
                smtpClient.Port        = emailAccount.Port;
                smtpClient.EnableSsl   = emailAccount.EnableSsl;
                smtpClient.Credentials = emailAccount.UseDefaultCredentials ?
                                         CredentialCache.DefaultNetworkCredentials :
                                         new NetworkCredential(emailAccount.Username, emailAccount.Password);
                smtpClient.Send(message);
            }

            return(Task.CompletedTask);
        }
Example #3
0
 /// <summary>
 /// Get a value indicating whether some file (thumb) already exists
 /// </summary>
 /// <param name="thumbFilePath">Thumb file path</param>
 /// <param name="thumbFileName">Thumb file name</param>
 /// <returns>Result</returns>
 protected virtual bool GeneratedThumbExists(string thumbFilePath, string thumbFileName)
 {
     return(_fileProvider.FileExists(thumbFilePath));
 }
Example #4
0
        private void Initialize(string userAgentStringsPath, string crawlerOnlyUserAgentStringsPath)
        {
            List <XElement> crawlerItems        = null;
            bool            needSaveCrawlerOnly = false;

            if (!string.IsNullOrEmpty(crawlerOnlyUserAgentStringsPath) && _fileProvider.FileExists(crawlerOnlyUserAgentStringsPath))
            {
                //try to load crawler list from crawlers only file
                using (StreamReader sr = new StreamReader(crawlerOnlyUserAgentStringsPath))
                {
                    crawlerItems = XDocument.Load(sr).Root?.Elements("browscapitem").ToList();
                }
            }

            if (crawlerItems == null || !crawlerItems.Any())
            {
                //try to load crawler list from full user agents file
                using (StreamReader sr = new StreamReader(userAgentStringsPath))
                {
                    crawlerItems = XDocument.Load(sr).Root?.Element("browsercapitems")?.Elements("browscapitem")
                                   //only crawlers
                                   .Where(IsBrowscapItemIsCrawler).ToList();
                    needSaveCrawlerOnly = true;
                }
            }

            if (crawlerItems == null || !crawlerItems.Any())
            {
                throw new Exception("Incorrect file format");
            }

            _crawlerUserAgentsRegexp.AddRange(crawlerItems
                                              //get only user agent names
                                              .Select(e => e.Attribute("name"))
                                              .Where(e => !string.IsNullOrEmpty(e?.Value))
                                              .Select(e => e.Value)
                                              .Select(ToRegexp));

            if ((string.IsNullOrEmpty(crawlerOnlyUserAgentStringsPath) || _fileProvider.FileExists(crawlerOnlyUserAgentStringsPath)) && !needSaveCrawlerOnly)
            {
                return;
            }

            //try to write crawlers file
            using (StreamWriter sw = new StreamWriter(crawlerOnlyUserAgentStringsPath))
            {
                XElement root = new XElement("browsercapitems");

                foreach (XElement crawler in crawlerItems)
                {
                    foreach (XElement element in crawler.Elements().ToList())
                    {
                        if ((element.Attribute("name")?.Value.ToLower() ?? string.Empty) == "crawler")
                        {
                            continue;
                        }
                        element.Remove();
                    }

                    root.Add(crawler);
                }

                root.Save(sw);
            }
        }