예제 #1
0
        /// <summary>
        /// Gets the filesystem-relative path of a full path or of an URL.
        /// </summary>
        /// <param name="fullPathOrUrl">The full path or URL.</param>
        /// <returns>The path, relative to this filesystem's root.</returns>
        /// <remarks>
        /// <para>The relative path is relative to this filesystem's root, not starting with any
        /// directory separator. All separators are forward-slashes.</para>
        /// </remarks>
        public string GetRelativePath(string fullPathOrUrl)
        {
            // test URL
            var path = fullPathOrUrl.Replace('\\', '/'); // ensure URL separator char

            // if it starts with the root path, strip it and trim the starting slash to make it relative
            // eg "c:/websites/test/root/Media/1234/img.jpg" => "1234/img.jpg"
            // or on unix systems "/var/wwwroot/test/Meia/1234/img.jpg"
            if (_ioHelper.PathStartsWith(path, _rootPathFwd, '/'))
            {
                return(path.Substring(_rootPathFwd.Length).TrimStart(Constants.CharArrays.ForwardSlash));
            }

            // if it starts with the root URL, strip it and trim the starting slash to make it relative
            // eg "/Media/1234/img.jpg" => "1234/img.jpg"
            if (_ioHelper.PathStartsWith(path, _rootUrl, '/'))
            {
                return(path.Substring(_rootUrl.Length).TrimStart(Constants.CharArrays.ForwardSlash));
            }

            // unchanged - what else?
            return(path.TrimStart(Constants.CharArrays.ForwardSlash));
        }