Cloud

Pain in the AssetUploader.aspx

Well actually it was the alternate access mappings that were messing this up. We are using AAMs for our public internet site. Normally you think that this would not cause a problem, but it did with the assetuploader.aspx page. In a nut shell the images were uploaded via https and are viewed via http in the uploader. The uploader was expecting the images via https and not http. This would cause the uploader not to show the thumbnail of the image. Needless to say, our end users did not like that, so I came up with the following solution:

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public partial class AssetUploader : AssetThumbnailerPage
{
#region OnLoad
protected override void OnLoad(EventArgs e)
{
int thumbSmallSize = 70;
int normalSize = 140;
string imageSize = string.Empty;

if (Request["ImageUrl"] == null || Request["ImageUrl"].StartsWith("http"))
{
base.OnLoad(e);
return;
}

if (Request["Size"] == null || Request["Size"] == string.Empty)
imageSize = "Small";
else
imageSize = Request["Size"];

if (Request["ImageUrl"] != null && Request["ImageUrl"].StartsWith("/"))
{
SPSite site = null;
SPWeb web = null;

try
{
Uri uri = new Uri(SPContext.Current.Web.Url);
site = new SPSite(SPContext.Current.Web.Url);
web = site.OpenWeb();
string url = uri.Scheme + "://" + uri.Host + Request["ImageUrl"];
SPFile file = MyAssetThumbnailer.RetrieveFile(url, out web);
byte[] fileByes = file.OpenBinary();
if (imageSize.ToLower().Equals("small"))
Response.BinaryWrite(JLLAssetThumbnailer.CreateThumbnail(fileByes, thumbSmallSize));
else
Response.BinaryWrite(JLLAssetThumbnailer.CreateThumbnail(fileByes, normalSize));
}

catch(Exception ex)
{
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(ex, "General");
base.OnLoad(e);
}

finally
{
web.Close();
web.Dispose();
site.Close();
site.Dispose();
}
}
}

internal class MyAssetThumbnailer
{
#region Constructor
private MyAssetThumbnailer()
{

}
#endregion

#region CreateThumbnail
public static byte[] CreateThumbnail(byte[] inputBuffer, int thumbMaxDimension)
{
MemoryStream stream = new MemoryStream(inputBuffer);
Bitmap bitmap = new Bitmap(stream);
Size imageSize = bitmap.Size;
Size size2 = getThumbnailDimensions(imageSize, thumbMaxDimension);
if (size2 == imageSize)
{
object[] objArray = new object[] { thumbMaxDimension.ToString(CultureInfo.InvariantCulture), imageSize.Width.ToString(CultureInfo.InvariantCulture), imageSize.Height.ToString(CultureInfo.InvariantCulture), inputBuffer.Length.ToString(CultureInfo.InvariantCulture) };
//ULS.SendTraceTag(ULSTagID.tag_6osl, ULSCat.msoulscat_CMS_Publishing, ULSTraceLevel.Medium, "No thumbnail made – thumbnail size [%s] is greater than image dimensions[%sx%s] bytes: %s", objArray);
return inputBuffer;
}
object[] data = new object[] { size2.Width.ToString(CultureInfo.InvariantCulture), size2.Height.ToString(CultureInfo.InvariantCulture), imageSize.Width.ToString(CultureInfo.InvariantCulture), imageSize.Height.ToString(CultureInfo.InvariantCulture), inputBuffer.Length.ToString(CultureInfo.InvariantCulture) };
//ULS.SendTraceTag(ULSTagID.tag_6osm, ULSCat.msoulscat_CMS_Publishing, ULSTraceLevel.Medium, "Generating thumbnail with dimensions[%sx%s] for image with dimensions[%sx%s] bytes: %s", data);
return imageObjectToBytes(bitmap.GetThumbnailImage(size2.Width, size2.Height, new System.Drawing.Image.GetThumbnailImageAbort(JLLAssetThumbnailer.thumbnailCallback), IntPtr.Zero));
}
#endregion

#region getThumbnailDimensions
private static Size getThumbnailDimensions(Size imageSize, int thumbMaxDimension)
{

if ((imageSize.Height > thumbMaxDimension) || (imageSize.Width > thumbMaxDimension))
{
SizeF ef = new SizeF((float)thumbMaxDimension, (float)thumbMaxDimension);
if (imageSize.Height <= imageSize.Width)
{
ef.Height = (imageSize.Height * thumbMaxDimension) / imageSize.Width;
}
else
{
ef.Width = (imageSize.Width * thumbMaxDimension) / imageSize.Height;
}
if (ef.Width < 1f)
{
ef.Width = 1f;
}
if (ef.Height < 1f)
{
ef.Height = 1f;
}
if (ef.Width > thumbMaxDimension)
{
ef.Width = thumbMaxDimension;
}
if (ef.Height > thumbMaxDimension)
{
ef.Height = thumbMaxDimension;
}
return Size.Ceiling(ef);
}
return imageSize;
}
#endregion

#region imageObjectToBytes
private static byte[] imageObjectToBytes(System.Drawing.Image imageObject)
{
MemoryStream stream = new MemoryStream();
imageObject.Save(stream, ImageFormat.Jpeg);
return stream.ToArray();
}
#endregion

#region RetrieveFile
public static SPFile RetrieveFile(string imageSPFileUrl, out SPWeb web)
{
SPFile fileOrFolderObject = null;
SPSite site = null;
web = null;
try
{
Uri uri2 = new Uri(HttpContext.Current.Request.Url, imageSPFileUrl);
site = SPContext.Current.Site;
SPSecurity.CatchAccessDeniedException = false;
web = site.OpenWeb();
fileOrFolderObject = web.GetFileOrFolderObject(uri2.AbsoluteUri) as SPFile;
}
catch (UnauthorizedAccessException exception)
{
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(exception, "General");
// ULS.SendTraceTag(ULSTagID.tag_7fl7, ULSCat.msoulscat_CMS_Publishing, ULSTraceLevel.High, "Access was denied to the object when trying to thumbnail the following URL: [%s] [%s]", new object[] { imageSPFileUrl, exception.ToString() });
}
catch (SPException exception2)
{
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(exception2, "General");
// ULS.SendTraceTag(ULSTagID.tag_7fl8, ULSCat.msoulscat_CMS_Publishing, ULSTraceLevel.High, "Could not retrieve file when trying to generate a thumbnail for the following URL : [%s] [%s]", new object[] { imageSPFileUrl, exception2.ToString() });
}
return fileOrFolderObject;
}
#endregion

#region thumbnailCallback
private static bool thumbnailCallback()
{
return false;
}
#endregion
}

The class MyAssetThumbnailer came from disassembling the code that the original asset uploader uses. I was unable to use the actual assembly because the class is marked internal. Microsoft did not make it easy on me, but .net reflector gave me what I needed to get this done.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to the Weekly Blog Digest:

Sign Up
Follow Us
TwitterLinkedinFacebookYoutubeInstagram