Fixes: #U4-2087

This commit is contained in:
Shannon Deminick
2013-04-12 02:11:01 +06:00
parent 3ff1b01e46
commit 26a7bf8187
8 changed files with 352 additions and 320 deletions

View File

@@ -1,12 +1,14 @@
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Umbraco.Core.IO;
using umbraco.interfaces;
using umbraco.IO;
using Umbraco.Core;
using Content = umbraco.cms.businesslogic.Content;
namespace umbraco.editorControls
@@ -14,12 +16,13 @@ namespace umbraco.editorControls
[ValidationProperty("IsValid")]
public class uploadField : HtmlInputFile, IDataEditor
{
private const String _thumbnailext = ".jpg";
private const string Thumbnailext = ".jpg";
private readonly cms.businesslogic.datatype.DefaultData _data;
private readonly String _thumbnails;
private String _text;
private readonly string _thumbnails;
private string _text;
private CustomValidator _customValidator;
private MediaFileSystem _fs;
private readonly MediaFileSystem _fs;
public uploadField(IData Data, string ThumbnailSizes)
{
@@ -28,29 +31,68 @@ namespace umbraco.editorControls
_thumbnails = ThumbnailSizes;
}
protected override void CreateChildControls()
{
base.CreateChildControls();
_customValidator = new CustomValidator
{
EnableClientScript = false,
Display = ValidatorDisplay.Dynamic,
ErrorMessage = ui.Text("errors", "dissallowedMediaType")
};
_customValidator.ErrorMessage += "<br/>";
//NOTE: it would be better to have this as a normal composite control but we don't want to
// break compatibility so we cannot make this inherit from a different class than it already
// is, so now we have to hack this together and add this directly to the page validators collection
// since we cannot add it as a control to this one.
Page.Validators.Add(_customValidator);
}
/// <summary>
/// Internal logic for validation controls to detect whether or not it's valid (has to be public though)
/// </summary>
/// <value>Am I valid?</value>
/// <value>Am I valid?</value>
/// <remarks>
/// This is used for the required and regex validation of a document type's property
/// </remarks>
public string IsValid
{
get
{
string tempText = Text;
bool isEmpty = PostedFile == null || String.IsNullOrEmpty(PostedFile.FileName);
var tempText = Text;
var isEmpty = PostedFile == null || string.IsNullOrEmpty(PostedFile.FileName);
// checkbox, if it's used the file will be deleted and we should throw a validation error
if (Page.Request[ClientID + "clear"] != null && Page.Request[ClientID + "clear"] != "")
return "";
else if (!isEmpty)
if (isEmpty == false)
return PostedFile.FileName;
else if (!String.IsNullOrEmpty(tempText))
return tempText;
else
return "";
return string.IsNullOrEmpty(tempText) == false
? tempText
: "";
}
}
public String Text
/// <summary>
/// Checks if the file is valid based on our dissallowed file types
/// </summary>
/// <param name="postedFile"></param>
/// <returns></returns>
internal bool IsValidFile(HttpPostedFile postedFile)
{
//return true if there is no file
if (postedFile == null) return true;
if (postedFile.FileName.IsNullOrWhiteSpace()) return true;
//now check the file type
var extension = Path.GetExtension(postedFile.FileName).TrimStart(".");
return UmbracoSettings.DissallowedUploadFiles.Any(x => x.InvariantEquals(extension)) == false;
}
public string Text
{
get { return _text; }
set { _text = value; }
@@ -79,14 +121,14 @@ namespace umbraco.editorControls
if (helper.Request(ClientID + "clear") == "1")
{
// delete file
deleteFile(_text);
DeleteFile(_text);
// set filename in db to nothing
_text = "";
_data.Value = _text;
foreach (string prop in "umbracoExtension,umbracoBytes,umbracoWidth,umbracoHeight".Split(','))
foreach (var prop in "umbracoExtension,umbracoBytes,umbracoWidth,umbracoHeight".Split(','))
{
try
{
@@ -104,61 +146,68 @@ namespace umbraco.editorControls
}
}
if (PostedFile != null && PostedFile.FileName != String.Empty)
if (PostedFile == null || PostedFile.FileName == string.Empty) return;
//don't save if the file is invalid
if (IsValidFile(PostedFile) == false)
{
_data.Value = PostedFile;
//set the validator to not valid
_customValidator.IsValid = false;
return;
}
// we update additional properties post image upload
if (_data.Value != DBNull.Value && !string.IsNullOrEmpty(_data.Value.ToString()))
_data.Value = PostedFile;
// we update additional properties post image upload
if (_data.Value != DBNull.Value && string.IsNullOrEmpty(_data.Value.ToString()) == false)
{
var content = Content.GetContentFromVersion(_data.Version);
// update extension in UI
try
{
Content content = Content.GetContentFromVersion(_data.Version);
// update extension in UI
try
{
var extensionControl = FindControlRecursive<noEdit>(Page, "prop_umbracoExtension");
if (extensionControl != null)
{
extensionControl.RefreshLabel(content.getProperty("umbracoExtension").Value.ToString());
}
}
catch
{
}
// update file size in UI
try
{
var bytesControl = FindControlRecursive<noEdit>(Page, "prop_umbracoBytes");
if (bytesControl != null)
{
bytesControl.RefreshLabel(content.getProperty("umbracoBytes").Value.ToString());
}
}
catch
{
}
try
{
var widthControl = FindControlRecursive<noEdit>(Page, "prop_umbracoWidth");
if (widthControl != null)
{
widthControl.RefreshLabel(content.getProperty("umbracoWidth").Value.ToString());
}
var heightControl = FindControlRecursive<noEdit>(Page, "prop_umbracoHeight");
if (heightControl != null)
{
heightControl.RefreshLabel(content.getProperty("umbracoHeight").Value.ToString());
}
}
catch
var extensionControl = FindControlRecursive<noEdit>(Page, "prop_umbracoExtension");
if (extensionControl != null)
{
extensionControl.RefreshLabel(content.getProperty("umbracoExtension").Value.ToString());
}
}
Text = _data.Value.ToString();
catch
{
}
// update file size in UI
try
{
var bytesControl = FindControlRecursive<noEdit>(Page, "prop_umbracoBytes");
if (bytesControl != null)
{
bytesControl.RefreshLabel(content.getProperty("umbracoBytes").Value.ToString());
}
}
catch
{
}
try
{
var widthControl = FindControlRecursive<noEdit>(Page, "prop_umbracoWidth");
if (widthControl != null)
{
widthControl.RefreshLabel(content.getProperty("umbracoWidth").Value.ToString());
}
var heightControl = FindControlRecursive<noEdit>(Page, "prop_umbracoHeight");
if (heightControl != null)
{
heightControl.RefreshLabel(content.getProperty("umbracoHeight").Value.ToString());
}
}
catch
{
}
}
Text = _data.Value.ToString();
}
#endregion
@@ -166,20 +215,20 @@ namespace umbraco.editorControls
[Obsolete("This method is now obsolete due to a change in the way that files are handled. If you need to check if a URL for an uploaded file is safe you should implement your own as this method will be removed in a future version", false)]
public string SafeUrl(string url)
{
if (!String.IsNullOrEmpty(url))
return Regex.Replace(url, @"[^a-zA-Z0-9\-\.\/\:]{1}", "_");
else
return String.Empty;
return string.IsNullOrEmpty(url) == false
? Regex.Replace(url, @"[^a-zA-Z0-9\-\.\/\:]{1}", "_")
: String.Empty;
}
protected override void OnInit(EventArgs e)
{
EnsureChildControls();
base.OnInit(e);
if (_data != null && _data.Value != null)
Text = _data.Value.ToString();
}
private void deleteFile(string fileUrl)
private void DeleteFile(string fileUrl)
{
if (fileUrl.Length > 0)
{
@@ -189,7 +238,7 @@ namespace umbraco.editorControls
if (_fs.FileExists(relativeFilePath))
_fs.DeleteFile(relativeFilePath);
string extension = (relativeFilePath.Substring(relativeFilePath.LastIndexOf(".") + 1, relativeFilePath.Length - relativeFilePath.LastIndexOf(".") - 1));
var extension = (relativeFilePath.Substring(relativeFilePath.LastIndexOf(".") + 1, relativeFilePath.Length - relativeFilePath.LastIndexOf(".") - 1));
extension = extension.ToLower();
//check for thumbnails
@@ -200,8 +249,8 @@ namespace umbraco.editorControls
try
{
if (_fs.FileExists(relativeThumbFilePath + _thumbnailext))
_fs.DeleteFile(relativeThumbFilePath + _thumbnailext);
if (_fs.FileExists(relativeThumbFilePath + Thumbnailext))
_fs.DeleteFile(relativeThumbFilePath + Thumbnailext);
}
catch
{
@@ -209,12 +258,12 @@ namespace umbraco.editorControls
if (_thumbnails != "")
{
string[] thumbnailSizes = _thumbnails.Split(";".ToCharArray());
foreach (string thumb in thumbnailSizes)
var thumbnailSizes = _thumbnails.Split(";".ToCharArray());
foreach (var thumb in thumbnailSizes)
{
if (thumb != "")
{
string relativeExtraThumbFilePath = relativeThumbFilePath + "_" + thumb + _thumbnailext;
string relativeExtraThumbFilePath = relativeThumbFilePath + "_" + thumb + Thumbnailext;
try
{
@@ -271,7 +320,16 @@ namespace umbraco.editorControls
/// <param name="output"> The HTML writer to write out to </param>
protected override void Render(HtmlTextWriter output)
{
if (!string.IsNullOrEmpty(Text))
//render the validator if it is not valid
//NOTE: it would be better to have this as a normal composite control but we don't want to
// break compatibility so we cannot make this inherit from a different class than it already
// is, so now we have to hack this together.
if (_customValidator.IsValid == false)
{
_customValidator.RenderControl(output);
}
if (string.IsNullOrEmpty(Text) == false)
{
var relativeFilePath = _fs.GetRelativePath(_text);
var ext = relativeFilePath.Substring(relativeFilePath.LastIndexOf(".") + 1, relativeFilePath.Length - relativeFilePath.LastIndexOf(".") - 1);
@@ -281,7 +339,7 @@ namespace umbraco.editorControls
{
hasThumb = _fs.FileExists(relativeThumbFilePath);
// 4.8.0 added support for png thumbnails (but for legacy it might have been jpg - hence the check before)
if (!hasThumb && (ext == "gif" || ext == "png"))
if (hasThumb == false && (ext == "gif" || ext == "png"))
{
relativeThumbFilePath = relativeFilePath.Replace("." + ext, "_thumb.png");
hasThumb = _fs.FileExists(relativeThumbFilePath);