Exemplo n.º 1
0
        /// <summary>
        /// Tries the recognize provider.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="contentType">Type of the content.</param>
        /// <param name="fileSize">Size of the file.</param>
        /// <param name="providerName">Name of the provider.</param>
        /// <param name="profileName">Name of the profile.</param>
        /// <returns></returns>
        public static bool TryRecognizeStorageProvider(FolderElement element, string fileName,
                                                       String contentType, long fileSize,
                                                       out String providerName, out string profileName)
        {
            providerName = String.Empty;
            profileName  = String.Empty;
            FileLibraryLocationSection section = (FileLibraryLocationSection)
                                                 ConfigurationManager.GetSection(folderElementLocationsCfgName);

            if (section != null)
            {
                FileLibraryLocationCollection            locations = section.Locations;
                Dictionary <String, LocationRuleElement> matchList =
                    new Dictionary <string, LocationRuleElement>();

                string hierarhy = element.GetElementPath("\\");
                foreach (FileLibraryLocationElement location in locations)
                {
                    bool   wildcard     = location.Path.EndsWith(wildCardChar);
                    string locationPath = wildcard == true?
                                          location.Path.Substring(0, location.Path.IndexOf(wildCardChar))
                                              : location.Path;

                    if (hierarhy.Equals(locationPath,
                                        StringComparison.InvariantCultureIgnoreCase) ||
                        ((wildcard == true) && (hierarhy.StartsWith(locationPath, StringComparison.InvariantCultureIgnoreCase))))
                    {
                        LocationRuleCollections rules = location.LocationRules;

                        foreach (LocationRuleElement rule in rules)
                        {
                            bool found = true;

                            if (!String.IsNullOrEmpty(rule.Extension))
                            {
                                found = fileName.EndsWith(rule.Extension,
                                                          StringComparison.InvariantCultureIgnoreCase) ? found : false;
                            }

                            if (!String.IsNullOrEmpty(rule.MimeType))
                            {
                                found = contentType.Equals(rule.MimeType,
                                                           StringComparison.InvariantCultureIgnoreCase) ? found : false;
                            }

                            if (!String.IsNullOrEmpty(rule.MaxSize))
                            {
                                String regExpPat = @"(?<size>[0-9]+)\s*(?<unit>Kb|Mb|Gb)?";
                                Regex  regExp    = new Regex(regExpPat, RegexOptions.IgnoreCase);
                                Match  sizeMatch = regExp.Match(rule.MaxSize);
                                if (sizeMatch.Success)
                                {
                                    long   maxSize = Convert.ToInt64(sizeMatch.Groups["size"].Value);
                                    string unit    = sizeMatch.Groups["unit"].Value;
                                    if (!String.IsNullOrEmpty(unit))
                                    {
                                        if (unit.Equals("Kb", StringComparison.InvariantCultureIgnoreCase))
                                        {
                                            maxSize *= 1024;
                                        }
                                        else if (unit.Equals("Mb", StringComparison.InvariantCultureIgnoreCase))
                                        {
                                            maxSize *= 1048576;
                                        }
                                        else if (unit.Equals("Gb", StringComparison.InvariantCultureIgnoreCase))
                                        {
                                            maxSize *= 1073741824;
                                        }
                                    }

                                    found = fileSize >= maxSize ? found : false;
                                }
                            }

                            if ((found == true) || (String.IsNullOrEmpty(rule.MaxSize) &&
                                                    String.IsNullOrEmpty(rule.MimeType) &&
                                                    String.IsNullOrEmpty(rule.MaxSize)))
                            {
                                matchList.Add(location.Path, rule);
                                break;
                            }
                        }
                    }
                }

                if (matchList.Count != 0)
                {
                    //get location element with max depth
                    int    maxDepth = -1;
                    String maxKey   = String.Empty;
                    foreach (String key in matchList.Keys)
                    {
                        int depth = key.Split(new Char[] { '\\' }).Length;
                        if (depth > maxDepth)
                        {
                            maxDepth = depth;
                            maxKey   = key;
                        }
                    }
                    providerName = matchList[maxKey].StorageProvider;
                    profileName  = matchList[maxKey].DownloadProfile;
                    return(true);
                }
            }

            return(false);
        }