Slight MediaFactory refactors, mostly to fix issue U4-1344 where files wouldn't

upload if the "magic" umbraco props don't exist (witdth, height, bytes)
This commit is contained in:
Sebastiaan Janssen
2012-12-23 11:46:04 -01:00
parent 6589d5d5d3
commit dc111768e3
2 changed files with 41 additions and 43 deletions

View File

@@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Configuration;
using System.Web.Script.Serialization;
@@ -11,6 +9,7 @@ using System.Web.Security;
using System.Web.UI;
using System.Xml;
using System.Xml.Serialization;
using Umbraco.Core.Logging;
using umbraco.BasePages;
using umbraco.BusinessLogic;
using umbraco.businesslogic.Exceptions;
@@ -137,19 +136,15 @@ namespace umbraco.presentation.umbraco.webservices
{
var pathParts = context.Request["path"].Trim('/').Split('/');
foreach (var pathPart in pathParts)
{
if (!string.IsNullOrEmpty(pathPart))
{
foreach (var pathPart in pathParts.Where(part => string.IsNullOrWhiteSpace(part) == false))
parentNode = GetOrCreateFolder(parentNode, pathPart);
}
}
parentNodeId = parentNode.Id;
}
// Check whether to replace existing
var parsed = false;
bool replaceExisting = (context.Request["replaceExisting"] == "1" || (bool.TryParse(context.Request["replaceExisting"], out parsed) && parsed));
bool parsed;
var replaceExisting = (context.Request["replaceExisting"] == "1" || (bool.TryParse(context.Request["replaceExisting"], out parsed) && parsed));
// loop through uploaded files
for (var j = 0; j < context.Request.Files.Count; j++)
@@ -190,22 +185,20 @@ namespace umbraco.presentation.umbraco.webservices
scripts.SyncTree(parentNode.Path, true);
// log succes
Log.Add(LogTypes.New, parentNodeId, "Succes");
LogHelper.Info<MediaUploader>(string.Format("Successful upload to parent ID: {0}", parentNodeId));
}
catch (Exception e)
{
// log error
Log.Add(LogTypes.Error, parentNodeId, e.ToString());
LogHelper.Error<MediaUploader>(string.Format("Error uploading to parent ID {0}", parentNodeId), e);
}
}
else
{
// log error
Log.Add(LogTypes.Error, -1, "Parent node id is in incorrect format");
LogHelper.Warn<MediaUploader>(string.Format("Parent node id is in incorrect format: {0}", parentNodeId));
}
return new UploadResponse();
}
@@ -217,16 +210,16 @@ namespace umbraco.presentation.umbraco.webservices
if (GlobalSettings.UseSSL && !context.Request.IsSecureConnection)
throw new UserAuthorizationException("This installation requires a secure connection (via SSL). Please update the URL to include https://");
string username = context.Request["username"];
string password = context.Request["password"];
string ticket = context.Request["ticket"];
var username = context.Request["username"];
var password = context.Request["password"];
var ticket = context.Request["ticket"];
bool isValid = false;
var isValid = false;
if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
{
var mp = Membership.Providers[UmbracoSettings.DefaultBackofficeProvider];
if (mp.ValidateUser(username, password))
if (mp != null && mp.ValidateUser(username, password))
{
var user = new User(username);
isValid = user.Applications.Any(app => app.alias == "media");
@@ -239,6 +232,8 @@ namespace umbraco.presentation.umbraco.webservices
{
var t = FormsAuthentication.Decrypt(ticket);
var user = new User(username);
if (t != null)
isValid = user.LoginName.ToLower() == t.Name.ToLower() && user.Applications.Any(app => app.alias == "media");
if (isValid)
@@ -278,14 +273,13 @@ namespace umbraco.presentation.umbraco.webservices
if (appSetting > 0)
return appSetting;
var configXml = new XmlDocument();
configXml.PreserveWhitespace = true;
var configXml = new XmlDocument { PreserveWhitespace = true };
configXml.Load(HttpContext.Current.Server.MapPath("/web.config"));
var requestLimitsNode = configXml.SelectSingleNode("//configuration/system.webServer/security/requestFiltering/requestLimits");
if (requestLimitsNode != null)
{
if (requestLimitsNode.Attributes["maxAllowedContentLength"] != null)
if (requestLimitsNode.Attributes != null && requestLimitsNode.Attributes["maxAllowedContentLength"] != null)
{
var maxAllowedContentLength = Convert.ToInt32(requestLimitsNode.Attributes["maxAllowedContentLength"].Value);
if (maxAllowedContentLength > 0)
@@ -294,10 +288,8 @@ namespace umbraco.presentation.umbraco.webservices
}
var httpRuntime = ConfigurationManager.GetSection("system.web/httpRuntime") as HttpRuntimeSection;
if (httpRuntime != null)
return httpRuntime.MaxRequestLength;
return 4096;
return httpRuntime == null ? 4096 : httpRuntime.MaxRequestLength;
}
private Media GetOrCreateFolder(Media parent, string name)

View File

@@ -5,12 +5,10 @@ using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using Umbraco.Core.Logging;
using umbraco.BasePages;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.datatype;
using Encoder = System.Text.Encoder;
using Umbraco.Core.IO;
namespace umbraco.cms.businesslogic.media
{
@@ -26,10 +24,10 @@ namespace umbraco.cms.businesslogic.media
get { return new List<string> { "jpeg", "jpg", "gif", "bmp", "png", "tiff", "tif" }; }
}
public override void DoHandleMedia(Media media, PostedMediaFile postedFile, BusinessLogic.User user)
public override void DoHandleMedia(Media media, PostedMediaFile postedFile, User user)
{
// Get Image object, width and height
var image = System.Drawing.Image.FromStream(postedFile.InputStream);
var image = Image.FromStream(postedFile.InputStream);
var fileWidth = image.Width;
var fileHeight = image.Height;
@@ -42,9 +40,15 @@ namespace umbraco.cms.businesslogic.media
// Set media properties
media.getProperty("umbracoFile").Value = FileSystem.GetUrl(destFilePath);
media.getProperty("umbracoWidth").Value = fileWidth;
media.getProperty("umbracoHeight").Value = fileHeight;
media.getProperty("umbracoBytes").Value = postedFile.ContentLength;
if (media.getProperty("umbracoWidth") != null)
media.getProperty("umbracoWidth").Value = fileWidth;
if (media.getProperty("umbracoHeight") != null)
media.getProperty("umbracoHeight").Value = fileHeight;
if (media.getProperty("umbracoBytes") != null)
media.getProperty("umbracoBytes").Value = postedFile.ContentLength;
if (media.getProperty("umbracoExtension") != null)
media.getProperty("umbracoExtension").Value = ext;
@@ -78,7 +82,10 @@ namespace umbraco.cms.businesslogic.media
// Get DataTypeDefinition of upload field
dataTypeDef = DataTypeDefinition.GetByDataTypeId(uploadFieldDataTypeId);
}
catch { }
catch (Exception e)
{
LogHelper.Error<UmbracoImageMediaFactory>("Could get Upload Field datatype definition", e);
}
if (dataTypeDef != null)
{
@@ -94,8 +101,7 @@ namespace umbraco.cms.businesslogic.media
var thumbnailSizes = thumbnails.Split(";".ToCharArray());
foreach (var thumb in thumbnailSizes.Where(thumb => thumb != ""))
{
GenerateThumbnail(image, int.Parse(thumb), fileWidth, fileHeight, ext,
destFilePath + "_" + thumb + ".jpg");
GenerateThumbnail(image, int.Parse(thumb), fileWidth, fileHeight, ext, destFilePath + "_" + thumb + ".jpg");
}
}
}
@@ -115,6 +121,7 @@ namespace umbraco.cms.businesslogic.media
// fixes for empty width or height
if (widthTh == 0)
widthTh = 1;
if (heightTh == 0)
heightTh = 1;
@@ -131,15 +138,14 @@ namespace umbraco.cms.businesslogic.media
// Copy metadata
var codecs = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo codec = null;
if (ext.ToLower() == "png" || ext.ToLower() == "gif")
codec = codecs.Single(t => t.MimeType.Equals("image/png"));
else
codec = codecs.Single(t => t.MimeType.Equals("image/jpeg"));
var codec = ext.ToLower() == "png" || ext.ToLower() == "gif"
? codecs.Single(t => t.MimeType.Equals("image/png"))
: codecs.Single(t => t.MimeType.Equals("image/jpeg"));
// Set compresion ratio to 90%
var ep = new EncoderParameters();
ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 90L);
ep.Param[0] = new EncoderParameter(Encoder.Quality, 90L);
// Save the new image
if (codec != null)