ImageResizer  3.4.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
ImageResizer.Plugins.DiskCache.WebConfigWriter Class Reference

Handles writing a Web.Config to disk that uses Url Authorization to prevent visitors from accessing the files directly. Alternative Web.config content can be specified, this is a general-purpose implementation. Uses UTF-8 encoding. Also provides methods for efficient verification that the file still exists. Thread-safe. More...

Collaboration diagram for ImageResizer.Plugins.DiskCache.WebConfigWriter:
Collaboration graph
[legend]

Public Member Functions

 WebConfigWriter (ILoggerProvider lp, string physicalDirPath)
 
 WebConfigWriter (ILoggerProvider lp, string physicalDirPath, string alternateWebConfigContents)
 
void CheckWebConfigEvery5 ()
 Verifies a Web.config file is present in the directory every 5 minutes that the function is called, including the first time. More...
 
void CheckWebConfigOnce ()
 If CheckWebConfig has never executed, it is executed immediately, but only once. Verifies a Web.config file is present in the directory, and creates it if needed. More...
 
void CheckWebConfig ()
 Verifies a Web.config file is present in the directory, and creates it if needed. More...
 

Protected Member Functions

virtual string getNewWebConfigContents ()
 
void _checkWebConfig ()
 Should only be called inside a lock. Creates the cache dir and the web.config file if they are missing. Updates _lastCheckedWebConfig and _checkedWebConfigOnce More...
 

Protected Attributes

ILoggerProvider lp = null
 
string physicalDirPath = null
 
string webConfigContents
 This string contains the contents of a web.conig file that sets URL authorization to "deny all" inside the current directory. More...
 

Detailed Description

Handles writing a Web.Config to disk that uses Url Authorization to prevent visitors from accessing the files directly. Alternative Web.config content can be specified, this is a general-purpose implementation. Uses UTF-8 encoding. Also provides methods for efficient verification that the file still exists. Thread-safe.

Definition at line 14 of file WebConfigWriter.cs.

Member Function Documentation

void ImageResizer.Plugins.DiskCache.WebConfigWriter._checkWebConfig ( )
inlineprotected

Should only be called inside a lock. Creates the cache dir and the web.config file if they are missing. Updates _lastCheckedWebConfig and _checkedWebConfigOnce

Parameters
physicalPath

Definition at line 85 of file WebConfigWriter.cs.

85  {
86  try {
87  string webConfigPath = physicalDirPath.TrimEnd('/', '\\') + System.IO.Path.DirectorySeparatorChar + "Web.config";
88  if (lp.Logger != null) lp.Logger.Debug("Verifying Web.config exists in cache directory.");
89  if (System.IO.File.Exists(webConfigPath)) return; //Already exists, quit
90 
91 
92  //Web.config doesn't exist? make sure the directory exists!
93  if (!System.IO.Directory.Exists(physicalDirPath))
94  System.IO.Directory.CreateDirectory(physicalDirPath);
95 
96  if (lp.Logger != null) lp.Logger.Info("Creating missing Web.config in cache directory.");
97  //Create the Web.config file
98  System.IO.File.WriteAllText(webConfigPath, getNewWebConfigContents(),UTF8Encoding.UTF8);
99 
100  } finally {
101  _lastCheckedWebConfig = DateTime.UtcNow;
102  _checkedWebConfigOnce = true;
103  }
104  }
void ImageResizer.Plugins.DiskCache.WebConfigWriter.CheckWebConfig ( )
inline

Verifies a Web.config file is present in the directory, and creates it if needed.

Definition at line 75 of file WebConfigWriter.cs.

75  {
76  lock (_webConfigSyncObj) {
78  }
79  }
void _checkWebConfig()
Should only be called inside a lock. Creates the cache dir and the web.config file if they are missin...
void ImageResizer.Plugins.DiskCache.WebConfigWriter.CheckWebConfigEvery5 ( )
inline

Verifies a Web.config file is present in the directory every 5 minutes that the function is called, including the first time.

Returns

Definition at line 50 of file WebConfigWriter.cs.

50  {
51  if (_lastCheckedWebConfig < DateTime.UtcNow.Subtract(new TimeSpan(0, 5, 0))) {
52  lock (_webConfigSyncObj) {
53  if (_lastCheckedWebConfig < DateTime.UtcNow.Subtract(new TimeSpan(0, 5, 0)))
55  }
56  }
57  }
void _checkWebConfig()
Should only be called inside a lock. Creates the cache dir and the web.config file if they are missin...
void ImageResizer.Plugins.DiskCache.WebConfigWriter.CheckWebConfigOnce ( )
inline

If CheckWebConfig has never executed, it is executed immediately, but only once. Verifies a Web.config file is present in the directory, and creates it if needed.

Parameters
physicalPath

Definition at line 65 of file WebConfigWriter.cs.

65  {
66  if (_checkedWebConfigOnce) return;
67  lock (_webConfigSyncObj) {
68  if (_checkedWebConfigOnce) return;
70  }
71  }
void _checkWebConfig()
Should only be called inside a lock. Creates the cache dir and the web.config file if they are missin...

Member Data Documentation

string ImageResizer.Plugins.DiskCache.WebConfigWriter.webConfigContents
protected
Initial value:
=
"<?xml version=\"1.0\"?>" +
"<configuration xmlns=\"http://schemas.microsoft.com/.NetConfiguration/v2.0\">" +
"<system.web><authorization>" +
"<deny users=\"*\" />" +
"</authorization></system.web></configuration>"

This string contains the contents of a web.conig file that sets URL authorization to "deny all" inside the current directory.

Definition at line 30 of file WebConfigWriter.cs.


The documentation for this class was generated from the following file: