示例#1
0
        public static string GetRelativePath(string absolutePath)
        {
            if (!Uri.IsWellFormedUriString(absolutePath, UriKind.Absolute))
            {
                throw new ArgumentException(string.Format("bad path format {0} is not absolute", absolutePath));
            }

            var appender = Appenders.FirstOrDefault(x => absolutePath.Contains(x.Append) || absolutePath.Contains(x.AppendSecure));

            if (appender == null)
            {
                return(absolutePath);
            }

            if (SecureHelper.IsSecure() && !string.IsNullOrEmpty(appender.AppendSecure))
            {
                return(absolutePath.Remove(0, appender.AppendSecure.Length));
            }
            else
            {
                return(absolutePath.Remove(0, appender.Append.Length));
            }
        }
示例#2
0
        public string GetPath(HttpContext httpContext, IOptionsMonitor <ILog> options, string relativePath)
        {
            if (!string.IsNullOrEmpty(relativePath) && relativePath.IndexOf('~') == 0)
            {
                throw new ArgumentException(string.Format("bad path format {0} remove '~'", relativePath), "relativePath");
            }

            var result = relativePath;
            var ext    = Path.GetExtension(relativePath).ToLowerInvariant();

            if (Appenders.Any())
            {
                var avaliableAppenders      = Appenders.Where(x => x.Extensions != null && x.Extensions.Split('|').Contains(ext) || string.IsNullOrEmpty(ext)).ToList();
                var avaliableAppendersCount = avaliableAppenders.LongCount();

                Appender appender;
                if (avaliableAppendersCount > 1)
                {
                    appender = avaliableAppenders[(int)(relativePath.Length % avaliableAppendersCount)];
                }
                else if (avaliableAppendersCount == 1)
                {
                    appender = avaliableAppenders.First();
                }
                else
                {
                    appender = Appenders.First();
                }

                if (appender.Append.IndexOf('~') == 0)
                {
                    var query = string.Empty;
                    //Rel path
                    if (relativePath.IndexOfAny(new[] { '?', '=', '&' }) != -1)
                    {
                        //Cut it
                        query        = relativePath.Substring(relativePath.IndexOf('?'));
                        relativePath = relativePath.Substring(0, relativePath.IndexOf('?'));
                    }
                    //if (HostingEnvironment.IsHosted)
                    //{
                    //    result = VirtualPathUtility.ToAbsolute(string.Format("{0}/{1}{2}", appender.Append.TrimEnd('/'), relativePath.TrimStart('/'), query));
                    //}
                    //else
                    //{
                    result = string.Format("{0}/{1}{2}", appender.Append.TrimEnd('/').TrimStart('~'), relativePath.TrimStart('/'), query);
                    //}
                }
                else
                {
                    //TODO HostingEnvironment.IsHosted
                    if (SecureHelper.IsSecure(httpContext, options) && !string.IsNullOrEmpty(appender.AppendSecure))
                    {
                        result = string.Format("{0}/{1}", appender.AppendSecure.TrimEnd('/'), relativePath.TrimStart('/'));
                    }
                    else
                    {
                        //Append directly
                        result = string.Format("{0}/{1}", appender.Append.TrimEnd('/'), relativePath.TrimStart('/'));
                    }
                }
            }
            //To LOWER! cause Amazon is CASE SENSITIVE!
            return(result.ToLowerInvariant());
        }
示例#3
0
        public static string GetPath(string relativePath)
        {
            if (relativePath.StartsWith("~"))
            {
                throw new ArgumentException(string.Format("bad path format {0} remove '~'", relativePath), "relativePath");
            }

            var result = relativePath;
            var ext    = Path.GetExtension(relativePath).ToLowerInvariant();

            if (CoreContext.Configuration.Standalone && StaticUploader.CanUpload())
            {
                try
                {
                    result = CdnStorageSettings.Load().DataStore.GetInternalUri("", relativePath, TimeSpan.Zero, null).AbsoluteUri.ToLower();
                    if (!string.IsNullOrEmpty(result))
                    {
                        return(result);
                    }
                }
                catch (Exception)
                {
                }
            }

            if (Appenders.Any())
            {
                var avaliableAppenders      = Appenders.Where(x => x.Extensions.Split('|').Contains(ext) || String.IsNullOrEmpty(ext)).ToList();
                var avaliableAppendersCount = avaliableAppenders.LongCount();

                AppenderConfigurationElement appender;
                if (avaliableAppendersCount > 1)
                {
                    appender = avaliableAppenders[(int)(relativePath.Length % avaliableAppendersCount)];
                }
                else if (avaliableAppendersCount == 1)
                {
                    appender = avaliableAppenders.First();
                }
                else
                {
                    appender = Appenders.First();
                }

                if (appender.Append.StartsWith("~"))
                {
                    var query = string.Empty;
                    //Rel path
                    if (relativePath.IndexOfAny(new[] { '?', '=', '&' }) != -1)
                    {
                        //Cut it
                        query        = relativePath.Substring(relativePath.IndexOf('?'));
                        relativePath = relativePath.Substring(0, relativePath.IndexOf('?'));
                    }
                    if (HostingEnvironment.IsHosted)
                    {
                        result = VirtualPathUtility.ToAbsolute(string.Format("{0}/{1}{2}", appender.Append.TrimEnd('/'), relativePath.TrimStart('/'), query));
                    }
                    else
                    {
                        result = string.Format("{0}/{1}{2}", appender.Append.TrimEnd('/').TrimStart('~'), relativePath.TrimStart('/'), query);
                    }
                }
                else
                {
                    if (HostingEnvironment.IsHosted && SecureHelper.IsSecure() && !string.IsNullOrEmpty(appender.AppendSecure))
                    {
                        result = string.Format("{0}/{1}", appender.AppendSecure.TrimEnd('/'), relativePath.TrimStart('/'));
                    }
                    else
                    {
                        //Append directly
                        result = string.Format("{0}/{1}", appender.Append.TrimEnd('/'), relativePath.TrimStart('/'));
                    }
                }
            }
            //To LOWER! cause Amazon is CASE SENSITIVE!
            return(result.ToLowerInvariant());
        }
示例#4
0
        public static string GetPath(string relativePath)
        {
            if (relativePath.StartsWith("~"))
            {
                throw new ArgumentException(string.Format("bad path format {0} remove '~'", relativePath),
                                            "relativePath");
            }

            string result = relativePath;

            var ext = Path.GetExtension(relativePath).ToLowerInvariant();

            try
            {
                FixerConfigurationElement fixer = FixersByExt.ContainsKey(ext) ? FixersByExt[ext] : null;
                if (fixer != null && !string.IsNullOrEmpty(fixer.AppendBeforeExt))
                {
                    relativePath = relativePath.Substring(0, relativePath.LastIndexOf(ext))
                                   + fixer.AppendBeforeExt + ext;
                }
            }
            catch
            {
            }

            if (Appenders.Any())
            {
                var avaliableAppenders      = Appenders.Where(x => x.Extensions.Split('|').Contains(ext) || String.IsNullOrEmpty(ext));
                var avaliableAppendersCount = avaliableAppenders.LongCount();

                AppenderConfigurationElement appender;


                if (avaliableAppendersCount > 1)
                {
                    appender = avaliableAppenders.ToList()[(int)(relativePath.Length % avaliableAppendersCount)];
                }
                else if (avaliableAppendersCount == 1)
                {
                    appender = avaliableAppenders.First();
                }
                else
                {
                    appender = Appenders.First();
                }


                if (appender.Append.StartsWith("~"))
                {
                    string query = string.Empty;
                    //Rel path
                    if (relativePath.IndexOfAny(new[] { '?', '=', '&' }) != -1)
                    {
                        //Cut it
                        query        = relativePath.Substring(relativePath.IndexOf('?'));
                        relativePath = relativePath.Substring(0, relativePath.IndexOf('?'));
                    }
                    result = VirtualPathUtility.ToAbsolute(
                        string.Format("{0}/{1}{2}", appender.Append.TrimEnd('/'),
                                      relativePath.TrimStart('/'), query)
                        );
                }
                else
                {
                    if (SecureHelper.IsSecure() && !string.IsNullOrEmpty(appender.AppendSecure))
                    {
                        result = string.Format("{0}/{1}", appender.AppendSecure.TrimEnd('/'),
                                               relativePath.TrimStart('/'));
                    }
                    else
                    {
                        //Append directly
                        result = string.Format("{0}/{1}", appender.Append.TrimEnd('/'), relativePath.TrimStart('/'));
                    }
                }
            }
            //To LOWER! cause Amazon is CASE SENSITIVE!
            return(result.ToLowerInvariant());
        }