/// <summary> /// Checks for a file in storefront, client, or storefront folders. Returns path to first file found or Null if file does not exist in any location /// Example: for a path of /Images/File1.png this function will check for [storefrontfolder]/Images/File1.png, if the file is found it will return the physical path /// if the file is not found, it will check [client folder]/Images/File1.png. if file is found in client folder, it will return that physical path /// If the file is not found in the client folder, it will check the server folder [/Content/Server]/Images/File1.png. If file is found that path will be returned. /// If file is not found in any of these locations, null is returned /// </summary> /// <param name="storeFront"></param> /// <param name="path">Path to file, can start with / or not, / is implied</param> /// <param name="applicationPath"></param> /// <param name="client">Client to search if file is not found in the storefront, or storefront is null</param> /// <param name="server">HTTP Server</param> /// <returns></returns> public static string ChooseFilePath(this StoreFront storeFront, Client client, string path, string applicationPath, HttpServerUtilityBase server) { string fullVirtualPath = null; string fullPath = null; if (storeFront != null && storeFront.IsActiveBubble()) { fullVirtualPath = storeFront.StoreFrontVirtualDirectoryToMap(applicationPath) + "/" + path.TrimStart('/'); fullPath = server.MapPath(fullVirtualPath); if (System.IO.File.Exists(fullPath)) { return fullPath; } } if (client != null && client.IsActiveDirect()) { fullVirtualPath = client.ClientVirtualDirectoryToMap(applicationPath) + "/" + path.TrimStart('/'); fullPath = server.MapPath(fullVirtualPath); if (System.IO.File.Exists(fullPath)) { return fullPath; } } fullVirtualPath = "~/Content/Server/" + path.TrimStart('/'); fullPath = server.MapPath(fullVirtualPath); if (!System.IO.File.Exists(fullPath)) { return null; } return fullPath; }
/// <summary> /// Checks for a file in storefront, client, or storefront folders. Returns path to first file found or Null if file does not exist in any location /// Example: for a virtua;l folder '/Images' and fileNameStart of 'File1' this function will check for [storefrontfolder]/Images/File1.*, if the file is found it will return the physical path /// if the file is not found, it will check [client folder]/Images/File1.*. if file is found in client folder, it will return that physical path /// If the file is not found in the client folder, it will check the server folder [/Content/Server]/Images/File1.*. If file is found that path will be returned. /// If file is not found in any of these locations, null is returned /// </summary> /// <param name="storeFront"></param> /// <param name="path">Path to file, can start with / or not, / is implied</param> /// <param name="applicationPath"></param> /// <param name="client">Client to search if file is not found in the storefront, or storefront is null</param> /// <param name="server">HTTP Server</param> /// <returns></returns> public static string ChooseFileNameWildcard(this StoreFront storeFront, Client client, string virtualFolder, string fileNameStart, string applicationPath, HttpServerUtilityBase server) { string virtualPath = null; string folderPath = null; if (storeFront != null && storeFront.IsActiveBubble()) { virtualPath = storeFront.StoreFrontVirtualDirectoryToMap(applicationPath) + "/" + virtualFolder.Trim('/'); folderPath = server.MapPath(virtualPath); if (System.IO.Directory.Exists(folderPath)) { IOrderedEnumerable<string> files = System.IO.Directory.GetFiles(folderPath, fileNameStart + ".*") .OrderByDescending(s => s.EndsWith(".png")) .ThenByDescending(s => s.EndsWith(".jpg")) .ThenByDescending(s => s.EndsWith(".jpeg")) .ThenByDescending(s => s.EndsWith(".gif")) .ThenByDescending(s => s.EndsWith(".pdf")) .ThenByDescending(s => s.EndsWith(".doc")) .ThenByDescending(s => s.EndsWith(".xls")) .ThenByDescending(s => s.EndsWith(".mp3")) .ThenByDescending(s => s.EndsWith(".txt")); if (files.Count() != 0) { return new System.IO.FileInfo(files.First()).Name; } } } if (client != null && client.IsActiveDirect()) { virtualPath = client.ClientVirtualDirectoryToMap(applicationPath) + "/" + virtualFolder.Trim('/'); folderPath = server.MapPath(virtualPath); if (System.IO.Directory.Exists(folderPath)) { IOrderedEnumerable<string> files = System.IO.Directory.GetFiles(folderPath, fileNameStart + ".*") .OrderByDescending(s => s.EndsWith(".png")) .ThenByDescending(s => s.EndsWith(".jpg")) .ThenByDescending(s => s.EndsWith(".jpeg")) .ThenByDescending(s => s.EndsWith(".gif")) .ThenByDescending(s => s.EndsWith(".pdf")) .ThenByDescending(s => s.EndsWith(".doc")) .ThenByDescending(s => s.EndsWith(".xls")) .ThenByDescending(s => s.EndsWith(".mp3")) .ThenByDescending(s => s.EndsWith(".txt")); if (files.Count() != 0) { return new System.IO.FileInfo(files.First()).Name; } } } virtualPath = "~/Content/Server/" + virtualFolder.Trim('/'); folderPath = server.MapPath(virtualPath); if (System.IO.Directory.Exists(folderPath)) { IOrderedEnumerable<string> serverFiles = System.IO.Directory.GetFiles(folderPath, fileNameStart + ".*") .OrderByDescending(s => s.EndsWith(".png")) .ThenByDescending(s => s.EndsWith(".jpg")) .ThenByDescending(s => s.EndsWith(".jpeg")) .ThenByDescending(s => s.EndsWith(".gif")) .ThenByDescending(s => s.EndsWith(".pdf")) .ThenByDescending(s => s.EndsWith(".doc")) .ThenByDescending(s => s.EndsWith(".xls")) .ThenByDescending(s => s.EndsWith(".mp3")) .ThenByDescending(s => s.EndsWith(".txt")); if (serverFiles.Count() != 0) { return new System.IO.FileInfo(serverFiles.First()).Name; } } return null; }