Ensures all IDisposables are in a using clause for all processes involved with the media uploader, there were also a few streams not being closed or disposed either!
This commit is contained in:
@@ -467,43 +467,46 @@ namespace Umbraco.Core.Models
|
||||
return fileSystem.GetUrl(fileNameThumb);
|
||||
}
|
||||
|
||||
private static Tuple<int, int> GetDimensions(MediaFileSystem fileSystem, string path)
|
||||
private static Tuple<int, int> GetDimensions(IFileSystem fileSystem, string path)
|
||||
{
|
||||
var fs = fileSystem.OpenFile(path);
|
||||
var image = Image.FromStream(fs);
|
||||
var fileWidth = image.Width;
|
||||
var fileHeight = image.Height;
|
||||
fs.Close();
|
||||
image.Dispose();
|
||||
|
||||
return new Tuple<int, int>(fileWidth, fileHeight);
|
||||
using (var fs = fileSystem.OpenFile(path))
|
||||
{
|
||||
using (var image = Image.FromStream(fs))
|
||||
{
|
||||
var fileWidth = image.Width;
|
||||
var fileHeight = image.Height;
|
||||
return new Tuple<int, int>(fileWidth, fileHeight);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static string DoResize(MediaFileSystem fileSystem, string path, string extension, int width, int height, int maxWidthHeight, string fileNameAddition)
|
||||
{
|
||||
var fs = fileSystem.OpenFile(path);
|
||||
var image = Image.FromStream(fs);
|
||||
fs.Close();
|
||||
using (var fs = fileSystem.OpenFile(path))
|
||||
{
|
||||
using (var image = Image.FromStream(fs))
|
||||
{
|
||||
fs.Close();
|
||||
|
||||
string fileNameThumb = String.IsNullOrEmpty(fileNameAddition) ?
|
||||
string.Format("{0}_UMBRACOSYSTHUMBNAIL.jpg", path.Substring(0, path.LastIndexOf("."))) :
|
||||
string.Format("{0}_{1}.jpg", path.Substring(0, path.LastIndexOf(".")), fileNameAddition);
|
||||
var fileNameThumb = String.IsNullOrEmpty(fileNameAddition)
|
||||
? string.Format("{0}_UMBRACOSYSTHUMBNAIL.jpg", path.Substring(0, path.LastIndexOf(".")))
|
||||
: string.Format("{0}_{1}.jpg", path.Substring(0, path.LastIndexOf(".")), fileNameAddition);
|
||||
|
||||
var thumb = GenerateThumbnail(fileSystem,
|
||||
image,
|
||||
maxWidthHeight,
|
||||
width,
|
||||
height,
|
||||
path,
|
||||
extension,
|
||||
fileNameThumb,
|
||||
maxWidthHeight == 0);
|
||||
var thumb = GenerateThumbnail(fileSystem,
|
||||
image,
|
||||
maxWidthHeight,
|
||||
width,
|
||||
height,
|
||||
path,
|
||||
extension,
|
||||
fileNameThumb,
|
||||
maxWidthHeight == 0);
|
||||
|
||||
fileNameThumb = thumb.Item3;
|
||||
fileNameThumb = thumb.Item3;
|
||||
|
||||
image.Dispose();
|
||||
|
||||
return fileNameThumb;
|
||||
return fileNameThumb;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Tuple<int, int, string> GenerateThumbnail(MediaFileSystem fileSystem, Image image, int maxWidthHeight, int fileWidth,
|
||||
@@ -530,44 +533,45 @@ namespace Umbraco.Core.Models
|
||||
heightTh = 1;
|
||||
|
||||
// Create new image with best quality settings
|
||||
var bp = new Bitmap(widthTh, heightTh);
|
||||
var g = Graphics.FromImage(bp);
|
||||
g.SmoothingMode = SmoothingMode.HighQuality;
|
||||
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
||||
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
||||
g.CompositingQuality = CompositingQuality.HighQuality;
|
||||
using (var bp = new Bitmap(widthTh, heightTh))
|
||||
{
|
||||
using (var g = Graphics.FromImage(bp))
|
||||
{
|
||||
g.SmoothingMode = SmoothingMode.HighQuality;
|
||||
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
||||
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
||||
g.CompositingQuality = CompositingQuality.HighQuality;
|
||||
|
||||
// Copy the old image to the new and resized
|
||||
var rect = new Rectangle(0, 0, widthTh, heightTh);
|
||||
g.DrawImage(image, rect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
|
||||
// Copy the old image to the new and resized
|
||||
var rect = new Rectangle(0, 0, widthTh, heightTh);
|
||||
g.DrawImage(image, rect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
|
||||
|
||||
// Copy metadata
|
||||
var imageEncoders = ImageCodecInfo.GetImageEncoders();
|
||||
ImageCodecInfo codec = null;
|
||||
if (extension.ToLower() == "png" || extension.ToLower() == "gif")
|
||||
codec = imageEncoders.Single(t => t.MimeType.Equals("image/png"));
|
||||
else
|
||||
codec = imageEncoders.Single(t => t.MimeType.Equals("image/jpeg"));
|
||||
// Copy metadata
|
||||
var imageEncoders = ImageCodecInfo.GetImageEncoders();
|
||||
ImageCodecInfo codec = null;
|
||||
if (extension.ToLower() == "png" || extension.ToLower() == "gif")
|
||||
codec = imageEncoders.Single(t => t.MimeType.Equals("image/png"));
|
||||
else
|
||||
codec = imageEncoders.Single(t => t.MimeType.Equals("image/jpeg"));
|
||||
|
||||
// Set compresion ratio to 90%
|
||||
var ep = new EncoderParameters();
|
||||
ep.Param[0] = new EncoderParameter(Encoder.Quality, 90L);
|
||||
|
||||
// Save the new image using the dimensions of the image
|
||||
var newFileName = thumbnailFileName.Replace("UMBRACOSYSTHUMBNAIL",
|
||||
string.Format("{0}x{1}", widthTh, heightTh));
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
bp.Save(ms, codec, ep);
|
||||
ms.Seek(0, 0);
|
||||
|
||||
// Set compresion ratio to 90%
|
||||
var ep = new EncoderParameters();
|
||||
ep.Param[0] = new EncoderParameter(Encoder.Quality, 90L);
|
||||
fileSystem.AddFile(newFileName, ms);
|
||||
}
|
||||
|
||||
// Save the new image using the dimensions of the image
|
||||
string newFileName = thumbnailFileName.Replace("UMBRACOSYSTHUMBNAIL",
|
||||
string.Format("{0}x{1}", widthTh, heightTh));
|
||||
var ms = new MemoryStream();
|
||||
bp.Save(ms, codec, ep);
|
||||
ms.Seek(0, 0);
|
||||
|
||||
fileSystem.AddFile(newFileName, ms);
|
||||
|
||||
ms.Close();
|
||||
bp.Dispose();
|
||||
g.Dispose();
|
||||
|
||||
return new Tuple<int, int, string>(widthTh, heightTh, newFileName);
|
||||
return new Tuple<int, int, string>(widthTh, heightTh, newFileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -153,32 +153,35 @@ namespace umbraco.presentation.umbraco.webservices
|
||||
// get the current file
|
||||
var uploadFile = context.Request.Files[j];
|
||||
|
||||
// if there was a file uploded
|
||||
if (uploadFile.ContentLength > 0)
|
||||
using (var inputStream = uploadFile.InputStream)
|
||||
{
|
||||
// Ensure we get the filename without the path in IE in intranet mode
|
||||
// http://stackoverflow.com/questions/382464/httppostedfile-filename-different-from-ie
|
||||
var fileName = uploadFile.FileName;
|
||||
if(fileName.LastIndexOf(@"\") > 0)
|
||||
fileName = fileName.Substring(fileName.LastIndexOf(@"\") + 1);
|
||||
|
||||
fileName = Umbraco.Core.IO.IOHelper.SafeFileName(fileName);
|
||||
|
||||
var postedMediaFile = new PostedMediaFile
|
||||
// if there was a file uploded
|
||||
if (uploadFile.ContentLength > 0)
|
||||
{
|
||||
FileName = fileName,
|
||||
DisplayName = context.Request["name"],
|
||||
ContentType = uploadFile.ContentType,
|
||||
ContentLength = uploadFile.ContentLength,
|
||||
InputStream = uploadFile.InputStream,
|
||||
ReplaceExisting = replaceExisting
|
||||
};
|
||||
// Ensure we get the filename without the path in IE in intranet mode
|
||||
// http://stackoverflow.com/questions/382464/httppostedfile-filename-different-from-ie
|
||||
var fileName = uploadFile.FileName;
|
||||
if (fileName.LastIndexOf(@"\") > 0)
|
||||
fileName = fileName.Substring(fileName.LastIndexOf(@"\") + 1);
|
||||
|
||||
// Get concrete MediaFactory
|
||||
var factory = MediaFactory.GetMediaFactory(parentNodeId, postedMediaFile, AuthenticatedUser);
|
||||
fileName = Umbraco.Core.IO.IOHelper.SafeFileName(fileName);
|
||||
|
||||
// Handle media Item
|
||||
var media = factory.HandleMedia(parentNodeId, postedMediaFile, AuthenticatedUser);
|
||||
var postedMediaFile = new PostedMediaFile
|
||||
{
|
||||
FileName = fileName,
|
||||
DisplayName = context.Request["name"],
|
||||
ContentType = uploadFile.ContentType,
|
||||
ContentLength = uploadFile.ContentLength,
|
||||
InputStream = inputStream,
|
||||
ReplaceExisting = replaceExisting
|
||||
};
|
||||
|
||||
// Get concrete MediaFactory
|
||||
var factory = MediaFactory.GetMediaFactory(parentNodeId, postedMediaFile, AuthenticatedUser);
|
||||
|
||||
// Handle media Item
|
||||
var media = factory.HandleMedia(parentNodeId, postedMediaFile, AuthenticatedUser);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -60,24 +60,27 @@ namespace umbraco.cms.businesslogic.media
|
||||
|
||||
public void SaveAs(string filename)
|
||||
{
|
||||
var s = new FileStream(filename, FileMode.Create);
|
||||
try
|
||||
using (var s = new FileStream(filename, FileMode.Create))
|
||||
{
|
||||
int readCount;
|
||||
var buffer = new byte[8192];
|
||||
try
|
||||
{
|
||||
int readCount;
|
||||
var buffer = new byte[8192];
|
||||
|
||||
if (InputStream.CanSeek)
|
||||
InputStream.Seek(0, SeekOrigin.Begin);
|
||||
if (InputStream.CanSeek)
|
||||
InputStream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
while ((readCount = InputStream.Read(buffer, 0, buffer.Length)) != 0)
|
||||
s.Write(buffer, 0, readCount);
|
||||
while ((readCount = InputStream.Read(buffer, 0, buffer.Length)) != 0)
|
||||
s.Write(buffer, 0, readCount);
|
||||
|
||||
s.Flush();
|
||||
}
|
||||
finally
|
||||
{
|
||||
s.Close();
|
||||
s.Flush();
|
||||
}
|
||||
finally
|
||||
{
|
||||
s.Close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user