Skip to content

Commit 8d74bb9

Browse files
committed
Extension packaging: initial commit
1 parent 74824d8 commit 8d74bb9

42 files changed

Lines changed: 2135 additions & 314 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/Libraries/SmartStore.Core/IO/IStorageProvider.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/Libraries/SmartStore.Core/IO/FileSystemStorageProvider.cs renamed to src/Libraries/SmartStore.Core/IO/Media/FileSystemStorageProvider.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
using System.IO;
44
using System.Linq;
55
using System.Web.Hosting;
6+
using SmartStore.Utilities;
67

7-
namespace SmartStore.Core.IO
8+
namespace SmartStore.Core.IO.Media
89
{
9-
public class FileSystemStorageProvider : IStorageProvider {
10+
public class FileSystemStorageProvider : IStorageProvider
11+
{
1012
private readonly string _storagePath;
1113
private readonly string _publicPath;
1214

13-
public FileSystemStorageProvider(FileSystemSettings settings, IWebHelper webHelper)
15+
public FileSystemStorageProvider(FileSystemSettings settings)
1416
{
15-
var mediaPath = webHelper.MapPath("~/Media/");
17+
var mediaPath = CommonHelper.MapPath("~/Media/");
1618
_storagePath = Path.Combine(mediaPath, settings.DirectoryName);
1719

1820
var appPath = "";

src/Libraries/SmartStore.Core/IO/IStorageFile.cs renamed to src/Libraries/SmartStore.Core/IO/Media/IStorageFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.IO;
33

4-
namespace SmartStore.Core.IO
4+
namespace SmartStore.Core.IO.Media
55
{
66
public interface IStorageFile
77
{

src/Libraries/SmartStore.Core/IO/IStorageFolder.cs renamed to src/Libraries/SmartStore.Core/IO/Media/IStorageFolder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
using System;
33

4-
namespace SmartStore.Core.IO
4+
namespace SmartStore.Core.IO.Media
55
{
66
public interface IStorageFolder
77
{
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+

2+
using System.Collections.Generic;
3+
4+
namespace SmartStore.Core.IO.Media
5+
{
6+
public interface IStorageProvider
7+
{
8+
/// <summary>
9+
/// Retrieves the public URL for a given file within the storage provider.
10+
/// </summary>
11+
/// <param name="path">The relative path within the storage provider.</param>
12+
/// <returns>The public URL.</returns>
13+
string GetPublicUrl(string path);
14+
15+
/// <summary>
16+
/// Retrieves a file within the storage provider.
17+
/// </summary>
18+
/// <param name="path">The relative path to the file within the storage provider.</param>
19+
/// <returns>The file.</returns>
20+
/// <exception cref="ArgumentException">If the file is not found.</exception>
21+
IStorageFile GetFile(string path);
22+
23+
/// <summary>
24+
/// Lists the files within a storage provider's path.
25+
/// </summary>
26+
/// <param name="path">The relative path to the folder which files to list.</param>
27+
/// <returns>The list of files in the folder.</returns>
28+
IEnumerable<IStorageFile> ListFiles(string path);
29+
30+
/// <summary>
31+
/// Lists the folders within a storage provider's path.
32+
/// </summary>
33+
/// <param name="path">The relative path to the folder which folders to list.</param>
34+
/// <returns>The list of folders in the folder.</returns>
35+
IEnumerable<IStorageFolder> ListFolders(string path);
36+
37+
/// <summary>
38+
/// Creates a folder in the storage provider.
39+
/// </summary>
40+
/// <param name="path">The relative path to the folder to be created.</param>
41+
/// <exception cref="ArgumentException">If the folder already exists.</exception>
42+
void CreateFolder(string path);
43+
44+
/// <summary>
45+
/// Deletes a folder in the storage provider.
46+
/// </summary>
47+
/// <param name="path">The relative path to the folder to be deleted.</param>
48+
/// <exception cref="ArgumentException">If the folder doesn't exist.</exception>
49+
void DeleteFolder(string path);
50+
51+
/// <summary>
52+
/// Renames a folder in the storage provider.
53+
/// </summary>
54+
/// <param name="oldPath">The relative path to the folder to be renamed.</param>
55+
/// <param name="newPath">The relative path to the new folder.</param>
56+
void RenameFolder(string path, string newPath);
57+
58+
/// <summary>
59+
/// Deletes a file in the storage provider.
60+
/// </summary>
61+
/// <param name="path">The relative path to the file to be deleted.</param>
62+
/// <exception cref="ArgumentException">If the file doesn't exist.</exception>
63+
void DeleteFile(string path);
64+
65+
/// <summary>
66+
/// Renames a file in the storage provider.
67+
/// </summary>
68+
/// <param name="oldPath">The relative path to the file to be renamed.</param>
69+
/// <param name="newPath">The relative path to the new file.</param>
70+
void RenameFile(string path, string newPath);
71+
72+
/// <summary>
73+
/// Creates a file in the storage provider.
74+
/// </summary>
75+
/// <param name="path">The relative path to the file to be created.</param>
76+
/// <exception cref="ArgumentException">If the file already exists.</exception>
77+
/// <returns>The created file.</returns>
78+
IStorageFile CreateFile(string path);
79+
}
80+
}
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Web;
6+
using System.Web.Hosting;
7+
using SmartStore.Core.Logging;
8+
9+
namespace SmartStore.Core.IO.VirtualPath
10+
{
11+
12+
public class DefaultVirtualPathProvider : IVirtualPathProvider
13+
{
14+
private readonly ILogger _logger;
15+
16+
public DefaultVirtualPathProvider(ILogger logger)
17+
{
18+
this._logger = logger;
19+
}
20+
21+
public virtual string GetDirectoryName(string virtualPath)
22+
{
23+
return Path.GetDirectoryName(virtualPath).Replace(Path.DirectorySeparatorChar, '/');
24+
}
25+
26+
public virtual IEnumerable<string> ListFiles(string path)
27+
{
28+
return HostingEnvironment
29+
.VirtualPathProvider
30+
.GetDirectory(path)
31+
.Files
32+
.OfType<VirtualFile>()
33+
.Select(f => VirtualPathUtility.ToAppRelative(f.VirtualPath));
34+
}
35+
36+
public virtual IEnumerable<string> ListDirectories(string path)
37+
{
38+
return HostingEnvironment
39+
.VirtualPathProvider
40+
.GetDirectory(path)
41+
.Directories
42+
.OfType<VirtualDirectory>()
43+
.Select(d => VirtualPathUtility.ToAppRelative(d.VirtualPath));
44+
}
45+
46+
public virtual string Combine(params string[] paths)
47+
{
48+
return Path.Combine(paths).Replace(Path.DirectorySeparatorChar, '/');
49+
}
50+
51+
public virtual string ToAppRelative(string virtualPath)
52+
{
53+
if (IsMalformedVirtualPath(virtualPath))
54+
return null;
55+
56+
try
57+
{
58+
string result = VirtualPathUtility.ToAppRelative(virtualPath);
59+
60+
// In some cases, ToAppRelative doesn't normalize the path. In those cases,
61+
// the path is invalid.
62+
// Example:
63+
// ApplicationPath: /Foo
64+
// VirtualPath : ~/Bar/../Blah/Blah2
65+
// Result : /Blah/Blah2 <= that is not an app relative path!
66+
if (!result.StartsWith("~/"))
67+
{
68+
_logger.Information("Path '{0}' cannot be made app relative: Path returned ('{1}') is not app relative.".FormatCurrent(virtualPath, result));
69+
return null;
70+
}
71+
return result;
72+
}
73+
catch (Exception e)
74+
{
75+
// The initial path might have been invalid (e.g. path indicates a path outside the application root)
76+
_logger.Information("Path '{0}' cannot be made app relative".FormatCurrent(virtualPath), e);
77+
return null;
78+
}
79+
}
80+
81+
/// <summary>
82+
/// We want to reject path that contains ".." going outside of the application root.
83+
/// ToAppRelative does that already, but we want to do the same while avoiding exceptions.
84+
///
85+
/// Note: This method doesn't detect all cases of malformed paths, it merely checks
86+
/// for *some* cases of malformed paths, so this is not a replacement for full virtual path
87+
/// verification through VirtualPathUtilty methods.
88+
/// In other words, !IsMalformed does *not* imply "IsWellformed".
89+
/// </summary>
90+
public bool IsMalformedVirtualPath(string virtualPath)
91+
{
92+
if (string.IsNullOrEmpty(virtualPath))
93+
return true;
94+
95+
if (virtualPath.IndexOf("..") >= 0)
96+
{
97+
virtualPath = virtualPath.Replace(Path.DirectorySeparatorChar, '/');
98+
string rootPrefix = virtualPath.StartsWith("~/") ? "~/" : virtualPath.StartsWith("/") ? "/" : "";
99+
if (!string.IsNullOrEmpty(rootPrefix))
100+
{
101+
string[] terms = virtualPath.Substring(rootPrefix.Length).Split('/');
102+
int depth = 0;
103+
foreach (var term in terms)
104+
{
105+
if (term == "..")
106+
{
107+
if (depth == 0)
108+
{
109+
_logger.Information("Path '{0}' cannot be made app relative: Too many '..'".FormatCurrent(virtualPath));
110+
return true;
111+
}
112+
depth--;
113+
}
114+
else
115+
{
116+
depth++;
117+
}
118+
}
119+
}
120+
}
121+
122+
return false;
123+
}
124+
125+
public virtual Stream OpenFile(string virtualPath)
126+
{
127+
return HostingEnvironment.VirtualPathProvider.GetFile(virtualPath).Open();
128+
}
129+
130+
public virtual StreamWriter CreateText(string virtualPath)
131+
{
132+
return File.CreateText(MapPath(virtualPath));
133+
}
134+
135+
public virtual Stream CreateFile(string virtualPath)
136+
{
137+
return File.Create(MapPath(virtualPath));
138+
}
139+
140+
public virtual DateTime GetFileLastWriteTimeUtc(string virtualPath)
141+
{
142+
return File.GetLastWriteTimeUtc(MapPath(virtualPath));
143+
}
144+
145+
public string GetFileHash(string virtualPath)
146+
{
147+
return GetFileHash(virtualPath, new[] { virtualPath });
148+
}
149+
150+
public string GetFileHash(string virtualPath, IEnumerable<string> dependencies)
151+
{
152+
return HostingEnvironment.VirtualPathProvider.GetFileHash(virtualPath, dependencies);
153+
}
154+
155+
public virtual void DeleteFile(string virtualPath)
156+
{
157+
File.Delete(MapPath(virtualPath));
158+
}
159+
160+
public virtual string MapPath(string virtualPath)
161+
{
162+
return HostingEnvironment.MapPath(virtualPath);
163+
}
164+
165+
public string Normalize(string virtualPath)
166+
{
167+
return HostingEnvironment.VirtualPathProvider.GetFile(virtualPath).VirtualPath;
168+
}
169+
170+
public virtual bool FileExists(string virtualPath)
171+
{
172+
return HostingEnvironment.VirtualPathProvider.FileExists(virtualPath);
173+
}
174+
175+
public virtual bool TryFileExists(string virtualPath)
176+
{
177+
if (IsMalformedVirtualPath(virtualPath))
178+
return false;
179+
180+
try
181+
{
182+
return FileExists(virtualPath);
183+
}
184+
catch (Exception e)
185+
{
186+
_logger.Information("File '{0}' can not be checked for existence. Assuming doesn't exist.".FormatCurrent(virtualPath), e);
187+
return false;
188+
}
189+
}
190+
191+
public virtual bool DirectoryExists(string virtualPath)
192+
{
193+
return HostingEnvironment.VirtualPathProvider.DirectoryExists(virtualPath);
194+
}
195+
196+
public virtual void CreateDirectory(string virtualPath)
197+
{
198+
Directory.CreateDirectory(MapPath(virtualPath));
199+
}
200+
201+
public virtual void DeleteDirectory(string virtualPath)
202+
{
203+
Directory.Delete(MapPath(virtualPath));
204+
}
205+
206+
}
207+
208+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.IO;
5+
6+
namespace SmartStore.Core.IO.VirtualPath
7+
{
8+
9+
public interface IVirtualPathProvider
10+
{
11+
string Combine(params string[] paths);
12+
string ToAppRelative(string virtualPath);
13+
string MapPath(string virtualPath);
14+
string Normalize(string virtualPath);
15+
16+
bool FileExists(string virtualPath);
17+
bool TryFileExists(string virtualPath);
18+
Stream OpenFile(string virtualPath);
19+
StreamWriter CreateText(string virtualPath);
20+
Stream CreateFile(string virtualPath);
21+
DateTime GetFileLastWriteTimeUtc(string virtualPath);
22+
string GetFileHash(string virtualPath);
23+
string GetFileHash(string virtualPath, IEnumerable<string> dependencies);
24+
void DeleteFile(string virtualPath);
25+
26+
bool DirectoryExists(string virtualPath);
27+
void CreateDirectory(string virtualPath);
28+
string GetDirectoryName(string virtualPath);
29+
void DeleteDirectory(string virtualPath);
30+
31+
IEnumerable<string> ListFiles(string path);
32+
IEnumerable<string> ListDirectories(string path);
33+
}
34+
35+
}

0 commit comments

Comments
 (0)