Comments (9)
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:
- 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.
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.
(the difference was that I put the Replace
before the TrimEnd
.Replace('\\', '/').TrimEnd('/', '\\')
from n2cms.
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.
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.
here is a nuget build with this change if anyone feels like testing it-
https://www.dropbox.com/sh/ld58mzb1xenknrp/cp1Ln7uPEt
from n2cms.
btw, this issue is also probably related #135
from n2cms.
That looks more robust now, great work.
from n2cms.
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)
- Roles list page redirection url issue HOT 2
- LazyInitializationException HOT 3
- Setup and use N2CMS on the latest mono v5 under Debian Jessie HOT 12
- EditableTags auto-complete issue, not working HOT 3
- Collaboration does not work?
- Persister.Save or Persister.Repository.SaveOrUpdate doesn't save collection (IEnumerable<T>) HOT 2
- How to upgrade the N2CMS v2.5 to latest version? HOT 1
- CSS files have Cache-Control header set to private
- DirectURLInjector parser_PageNotFound crashes when sub page does not exist
- Two form in same page MVC HOT 1
- Email Form and TLS 1.2 HOT 2
- ContentRelation not serializable so ContentItem is not serializable
- Error when saving
- Cannot run the sources or template projects HOT 5
- Who uses N2? HOT 2
- How to install?
- Error "Could not find the type or name of the "Slideshow"" HOT 1
- create sitemap HOT 2
- Can i use it with postgres ? HOT 1
- Very broken, most likely a dead project
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from n2cms.