Giter Site home page Giter Site logo

Comments (9)

ahmadh21 avatar ahmadh21 commented on July 30, 2024

Hey Ben, thanks for being so prompt. Here is a sample fix I'm using temporarily. As you realized this is a problem only if using a virtual directory. Here is my suggested fix below

    public static IEnumerable<SlideshowImage> EnumerateImagesInDirectories(string relativeBasePath, string filter)
    {
        foreach (var f in N2.Utility.RecursiveListFiles(System.Web.Hosting.HostingEnvironment.MapPath(relativeBasePath.Insert(0, "~")), filter))
        {
            if (!(f.EndsWith("jpg") || f.EndsWith("gif") || f.EndsWith("png") || f.EndsWith("jpeg")))
                continue;

            yield return new SlideshowImage() {
                ImageHref = SlideshowDirectoryInclude.RemapVirtualPath(f),
                Description = null,
                Title = System.IO.Path.GetFileName(f)
            };
        }
    }

    private static string RemapVirtualPath(string physicalPath)
    {
        var appPhysicalPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath.TrimEnd('/', '\\');
        var appVirtualPath = System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath;
        var finalPath = physicalPath.Substring(appPhysicalPath.Length).Replace('\\', '/');

        if (appVirtualPath.Length > 1)
        {
            finalPath = appVirtualPath + finalPath;
        }

        return finalPath;
    }

I've added the tilde to relativeBasePath in MapPath to tell it to map from the executing directory, and a helper method to do the string tinkering.

Here is an image below of the path madness:

funstuff

  • Does not allow you to specify the "full" virtual path in your slideshow (if your virtual directory in IIS is localhost/abc and your uploads are localhost/abc/uploads you can't specify the slideshow to look under /abc/uploads as this would break). While this is OK, it is not the way the rest of N2 works. If you wanted to "standardize" this you'll have to do more string manipulation.

from n2cms.

bherila avatar bherila commented on July 30, 2024

Thanks for the response! One little thing, I changed your method to this and put it in Utility.cs instead of Slideshow.cs

        /// <summary>Maps a physical path back to a virtual path, thanks to ahmadh21.</summary>
        /// <param name="physicalPath"></param>
        /// <returns></returns>
        private static string RemapVirtualPath(string physicalPath)
        {
            var appPhysicalPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath.Replace('\\', '/').TrimEnd('/', '\\');
            var appVirtualPath = System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath.Replace('\\', '/').TrimEnd('/', '\\');
            var finalPath = physicalPath.Substring(appPhysicalPath.Length);

            if (appVirtualPath.Length > 1)
            {
                finalPath = appVirtualPath + finalPath;
            }

            return finalPath;
        }

from n2cms.

bherila avatar bherila commented on July 30, 2024

(the difference was that I put the Replace before the TrimEnd

.Replace('\\', '/').TrimEnd('/', '\\')

from n2cms.

bherila avatar bherila commented on July 30, 2024

Realize, this is still not going to work if you have a virtual directory under the N2 site web root (e.g. to a remote images folder). I'm also adding an additional check to hopefully defend against that.

        /// <summary>Maps a physical path back to a virtual path, thanks to ahmadh21.</summary>
        /// <param name="physicalPath"></param>
        /// <returns></returns>
        public static string RemapVirtualPath(string physicalPath)
        {
            physicalPath = physicalPath.Replace('\\', '/').TrimEnd('/', '\\');
            var appPhysicalPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath.Replace('\\', '/').TrimEnd('/', '\\');
            var appVirtualPath = System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath.Replace('\\', '/').TrimEnd('/', '\\');

            if (!physicalPath.StartsWith(appPhysicalPath, StringComparison.OrdinalIgnoreCase))
                throw new ArgumentException("Physical path given must map below application physical path");

            var finalPath = physicalPath.Substring(appPhysicalPath.Length);

            if (appVirtualPath.Length > 1)
            {
                finalPath = appVirtualPath + finalPath;
            }

            return finalPath;
        }

from n2cms.

bherila avatar bherila commented on July 30, 2024

And then I've also slightly modified the EnumerateImagesInDirectories

        public static IEnumerable<SlideshowImage> EnumerateImagesInDirectories(string relativeBasePath, string filter)
        {
            // sanitize input just in case
            relativeBasePath = relativeBasePath.Replace('\\', '/'); 
            filter = filter ?? "*";
            if (relativeBasePath[0] == '/')
                relativeBasePath = '~' + relativeBasePath;

            foreach (var f in N2.Utility.RecursiveListFiles(System.Web.Hosting.HostingEnvironment.MapPath(relativeBasePath), filter))
            {
                if (!(f.EndsWith("jpg") || f.EndsWith("gif") || f.EndsWith("png") || f.EndsWith("jpeg")))
                    continue;

                yield return new SlideshowImage() {
                    ImageHref = N2.Utility.RemapVirtualPath(f),
                    Description = null,
                    Title = System.IO.Path.GetFileName(f)
                };
            }
        }

from n2cms.

bherila avatar bherila commented on July 30, 2024

here is a nuget build with this change if anyone feels like testing it-
https://www.dropbox.com/sh/ld58mzb1xenknrp/cp1Ln7uPEt

from n2cms.

bherila avatar bherila commented on July 30, 2024

btw, this issue is also probably related #135

from n2cms.

ahmadh21 avatar ahmadh21 commented on July 30, 2024

That looks more robust now, great work.

from n2cms.

bherila avatar bherila commented on July 30, 2024

Thanks, I also looked at stackoverflow, btw, and I could not find a better solution at the moment. I am still contemplating a Github code search to see if I can turn up anything better.

from n2cms.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.