Skip to content

Commit c2c1dac

Browse files
author
Luc Gosso
committed
Version 1.6 - Added log4net
1 parent cd636aa commit c2c1dac

6 files changed

+79
-43
lines changed

Project/DownloadIfMissingFileBlob.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
<HintPath>..\packages\EPiServer.Framework.9.0.0\lib\net45\EPiServer.Framework.dll</HintPath>
5050
<Private>True</Private>
5151
</Reference>
52+
<Reference Include="log4net, Version=1.2.11.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
53+
<HintPath>..\packages\log4net.2.0.0\lib\net40-full\log4net.dll</HintPath>
54+
</Reference>
5255
<Reference Include="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
5356
<HintPath>..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
5457
<Private>True</Private>

Project/DownloadIfMissingFileProvider.cs

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using EPiServer.Framework.Blobs;
22
using EPiServer.Web;
3+
using log4net;
34
using System;
45
using System.Collections.Specialized;
56
using System.IO;
@@ -13,10 +14,12 @@ namespace Gosso.EPiServerAddOn.DownloadIfMissingFileBlob
1314

1415
public class Provider : FileBlobProvider
1516
{
17+
private static readonly ILog Logger =
18+
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
1619

1720
public const string DefaultUrl = "modules/Gosso.EPiServerAddOn.DownloadIfMissingFileBlob/{UrlResolver}";
1821
//important to override with an "action" (that can be anything), not to cause it to be a default route so MVC will use it default when used with @Ajax.ActionLink and sometimes xforms action url
19-
22+
2023
public Provider()
2124
: this("[appDataPath]\\blobs")
2225
{
@@ -30,54 +33,77 @@ public Provider(String path) :
3033
base(path)
3134
{
3235
}
36+
3337
public override Blob GetBlob(Uri id)
3438
{
3539
FileBlob b = base.GetBlob(id) as FileBlob;
36-
if (HttpContext.Current != null && Activated)
37-
{ //then not interested
40+
if (HttpContext.Current != null && Activated && b != null)
41+
{
42+
//then not interested
3843

3944
if (!CheckIfProdServer()) // check so it is NOT the PRODUCTION server, lack if multidomain.
4045
{
4146
if (!File.Exists(b.FilePath)) // check if exist on disc
4247
{
4348
FileInfo fi = new FileInfo(b.FilePath);
44-
if (this.RestrictedFileExt.ToLower().IndexOf(fi.Extension.ToLower(), StringComparison.Ordinal) == -1) // check if download this fileextention
49+
if (this.RestrictedFileExt.ToLower()
50+
.IndexOf(fi.Extension.ToLower(), StringComparison.Ordinal) ==
51+
-1) // check if download this fileextention
4552
{
4653

4754
string guid = id.Segments[1].Replace("/", "");
4855
try
4956
{
5057
string url = GetUrlAsync(guid).Result; // get friendly url to file
5158

52-
if (!String.IsNullOrEmpty(url) && url.IndexOf("error", StringComparison.OrdinalIgnoreCase) == -1)
59+
if (!String.IsNullOrEmpty(url) &&
60+
url.IndexOf("error", StringComparison.OrdinalIgnoreCase) == -1)
5361
{
54-
int intIsSmallImage = b.FilePath.IndexOf("_", StringComparison.Ordinal); //check if it thumbnail
62+
string filename = fi.Name;
63+
int intIsSmallImage =
64+
filename.LastIndexOf("_",
65+
StringComparison.Ordinal); //check if a image thumbnail or variation
5566
if (intIsSmallImage > 0)
5667
{
57-
url += "/" + b.FilePath.ToLower().Substring(intIsSmallImage + 1, b.FilePath.Length - intIsSmallImage - 1)
68+
url += "/" + filename.ToLower()
69+
.Substring(intIsSmallImage + 1,
70+
filename.Length - intIsSmallImage - 1)
5871
.Replace(fi.Extension.ToLower(), "");
5972
}
6073
Task.Run(() => DownloadAndSave(b, url));
6174
}
6275
}
63-
catch (WebException)
76+
catch (WebException ee)
6477
{
6578
//nada
79+
Logger.Error("BlobFile could not be downloaded: " + id, ee);
6680
}
6781
}
82+
else
83+
{
84+
Logger.Debug(
85+
$"BlobFile type restriction to \"{this.RestrictedFileExt}\", file not downloaded: " +
86+
id);
87+
}
6888
}
6989
}
90+
else
91+
{
92+
Logger.Error(
93+
$"Check configuration web.config, seems like the blob module {this.Name} is activated in production. Activated should be false!");
94+
}
7095
}
7196
return b;
7297
}
7398

7499
/// <summary>
75-
/// todo: lack of check if multisite
100+
/// todo: lack of check if multis
76101
/// </summary>
77102
/// <returns></returns>
78103
private bool CheckIfProdServer()
79104
{
80-
return HttpContext.Current.Request.Url.ToString().ToLower().Replace("http://", "https://").StartsWith(ProdUrl.ToLower().Replace("http://", "https://"));
105+
return HttpContext.Current.Request.Url.ToString().ToLower().Replace("http://", "https://")
106+
.StartsWith(ProdUrl.ToLower().Replace("http://", "https://"));
81107
}
82108

83109
private async Task<string> GetUrlAsync(string guid)
@@ -91,10 +117,16 @@ private async void DownloadAndSave(FileBlob blob, string rawurl)
91117
{
92118
var client = new HttpClient();
93119
var response = await client.GetAsync(ProdUrl + rawurl);
94-
if (response.StatusCode == HttpStatusCode.OK) //yeah, sometimes not published or deleted, or wrong url //todo: display default image? no, it may be a temporary error or internet is offline
120+
if (response.StatusCode == HttpStatusCode.OK
121+
) //yeah, sometimes not published or deleted, or wrong url //todo: display default image? no, it may be a temporary error or internet is offline
95122
{
96123
Stream dataStream = await response.Content.ReadAsStreamAsync();
97124
blob.Write(dataStream); //thats it
125+
Logger.Debug("BlobFile downloaded: " + ProdUrl + rawurl);
126+
}
127+
else
128+
{
129+
Logger.Error("BlobFile could not be downloaded: " + ProdUrl + rawurl);
98130
}
99131
}
100132

@@ -124,7 +156,7 @@ public override void Initialize(string name, NameValueCollection config)
124156
UrlResolverUrl = config.Get("UrlResolverUrl");
125157
}
126158
else
127-
UrlResolverUrl = DefaultUrl.Replace("{UrlResolver}","urlresolver.ashx");
159+
UrlResolverUrl = DefaultUrl;
128160

129161
if (Activated)
130162
{
@@ -148,48 +180,28 @@ public override void Initialize(string name, NameValueCollection config)
148180
/// <summary>
149181
/// Path to blob repository, default is "[appDataPath]\\blobs"
150182
/// </summary>
151-
public new string Path
152-
{
153-
get;
154-
internal set;
155-
}
183+
public new string Path { get; internal set; }
156184

157185
/// <summary>
158186
/// Url to Production Server where blob should be downloaded
159187
/// </summary>
160-
public string ProdUrl
161-
{
162-
get;
163-
internal set;
164-
}
188+
public string ProdUrl { get; internal set; }
165189

166190
/// <summary>
167191
/// Absolute Url to UrlResolver.ashx on Production Server, default is Provider.DefaultUrl = "modules/Gosso.EPiServerAddOn.DownloadIfMissingFileBlob/UrlResolver.ashx";
168192
/// </summary>
169-
public string UrlResolverUrl
170-
{
171-
get;
172-
internal set;
173-
}
193+
public string UrlResolverUrl { get; internal set; }
174194

175195
/// <summary>
176196
/// If the AddOn is activated or not.
177197
/// </summary>
178-
public bool Activated
179-
{
180-
get;
181-
set;
182-
}
198+
public bool Activated { get; set; }
183199

184200

185201
/// <summary>
186202
/// Restiction to fileextentions NOT do be downloaded. eg ".doc.docx.html.exe"
187203
/// </summary>
188-
public string RestrictedFileExt
189-
{
190-
get;
191-
internal set;
192-
}
204+
public string RestrictedFileExt { get; internal set; }
193205

194206
}
195207
}

Project/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
3434
[assembly: AssemblyVersion("1.5.*")]
35-
[assembly: AssemblyFileVersion("1.5.0.0")]
35+
[assembly: AssemblyFileVersion("1.6.0")]

Project/README.MD

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
# FileBlob Provider AddOn for EPiServer Developers
2-
**Namespace: Gosso.EPiServerAddOn.DownloadIfMissingFileBlob** Version 1.4 (2017-02-07)
2+
**Namespace: Gosso.EPiServerAddOn.DownloadIfMissingFileBlob** version 1.6 (2017-09-05)
33

44
[![Platform](https://img.shields.io/badge/Episerver-%209.0+-orange.svg?style=flat)](http://world.episerver.com/cms/) [![Platform](https://img.shields.io/badge/Episerver-%2010.0-green.svg?style=flat)](http://world.episerver.com/cms/)
55

6-
**Applicable to CMS >9.0 (MVC or Webforms) - tested with CMS9.6.1 and CMS 10.0.1.0, compiled with CMS 9**
6+
**Applicable to CMS >9.0 (MVC or Webforms) - tested with CMS9.6.1 and CMS 10.0.1.0, compiled with CMS 9**
77

8-
**NOT TESTED with AZURE File Storage or with ImageVault**
8+
**NOT TESTED with AZURE File Storage**
9+
10+
**Work side by side with Imagevault but is not copying vault images**
11+
12+
**Compatible with ImageResizer.Plugins.EPiServerBlobReader**
913

1014
## Why?
1115
Ever restored the Production database to your developer environment and got a website without images? This lightweight AddOn keeps your local environment blob directory up to date.
@@ -80,4 +84,11 @@ Also put the file urlresolver.ashx at that place
8084
</location>
8185
```
8286

87+
## Debug
8388

89+
use log4net:
90+
```
91+
<logger name="Gosso" additivity="true">
92+
<level value="Debug" />
93+
</logger>
94+
```

Project/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<packages>
33
<package id="EPiServer.CMS.Core" version="9.0.0" targetFramework="net452" />
44
<package id="EPiServer.Framework" version="9.0.0" targetFramework="net452" />
5+
<package id="log4net" version="2.0.0" targetFramework="net452" />
56
<package id="Microsoft.AspNet.Mvc" version="4.0.20710.0" targetFramework="net452" />
67
<package id="Microsoft.AspNet.Razor" version="2.0.20710.0" targetFramework="net452" />
78
<package id="Microsoft.AspNet.WebPages" version="2.0.20710.0" targetFramework="net452" />

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
# FileBlob Provider AddOn for EPiServer Developers
2-
**Namespace: Gosso.EPiServerAddOn.DownloadIfMissingFileBlob** Version 1.4 (2017-02-07)
2+
**Namespace: Gosso.EPiServerAddOn.DownloadIfMissingFileBlob** version 1.6 (2017-09-05)
33

44
[![Platform](https://img.shields.io/badge/Episerver-%209.0+-orange.svg?style=flat)](http://world.episerver.com/cms/) [![Platform](https://img.shields.io/badge/Episerver-%2010.0-green.svg?style=flat)](http://world.episerver.com/cms/)
55

6-
**Applicable to CMS >9.0 (MVC or Webforms) - tested with CMS9.6.1 and CMS 10.0.1.0, compiled with CMS 9**
6+
**Applicable to CMS >9.0 (MVC or Webforms) - tested with CMS9.6.1 and CMS 10.0.1.0, compiled with CMS 9**
77

88
**NOT TESTED with AZURE File Storage**
99

1010
**Work side by side with Imagevault but is not copying vault images**
1111

12+
**Compatible with ImageResizer.Plugins.EPiServerBlobReader**
13+
1214
## Why?
1315
Ever restored the Production database to your developer environment and got a website without images? This lightweight AddOn keeps your local environment blob directory up to date.
1416
## What is it?
@@ -82,4 +84,11 @@ Also put the file urlresolver.ashx at that place
8284
</location>
8385
```
8486

87+
## Debug
8588

89+
use log4net:
90+
```
91+
<logger name="Gosso" additivity="true">
92+
<level value="Debug" />
93+
</logger>
94+
```

0 commit comments

Comments
 (0)