Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified lib/businesslogic.dll
Binary file not shown.
Binary file modified lib/cms.dll
Binary file not shown.
Binary file modified lib/interfaces.dll
Binary file not shown.
Binary file modified lib/umbraco.DataLayer.dll
Binary file not shown.
Binary file modified lib/umbraco.dll
Binary file not shown.
Binary file modified lib/umbraco.editorControls.dll
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,25 @@ public class DefaultFileMediaFactory : MediaFactory

#region IMediaFactory Members

public override Media CreateMedia(IconI parent, HttpPostedFile uploadFile)
{
string filename = uploadFile.FileName;
public override Media CreateMedia(IconI parent, HttpPostedFile uploadFile)
{
//Get the safe file name
string filename = SafeFileName(uploadFile.FileName);

// Create new media object
Media media = Media.MakeNew(filename, MediaType.GetByAlias("File"),
new User(0), parent.Id);
// Create new media object
Media media = Media.MakeNew(filename, MediaType.GetByAlias("File"),
new User(0), parent.Id);

// Get umbracoFile property
int propertyId = media.getProperty("umbracoFile").Id;
// Get umbracoFile property
int propertyId = media.getProperty("umbracoFile").Id;

// Set media properties
media.getProperty("umbracoFile").Value = VirtualPathUtility.Combine(ConstructRelativeDestPath(propertyId), filename);
media.getProperty("umbracoBytes").Value = uploadFile.ContentLength;
media.getProperty("umbracoExtension").Value = VirtualPathUtility.GetExtension(filename).Substring(1);
// Set media properties
media.getProperty("umbracoFile").Value = VirtualPathUtility.Combine(ConstructRelativeDestPath(propertyId), filename);
media.getProperty("umbracoBytes").Value = uploadFile.ContentLength;
media.getProperty("umbracoExtension").Value = VirtualPathUtility.GetExtension(filename).Substring(1);

return media;
}
return media;
}

#endregion
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,46 +28,47 @@ public class DefaultImageMediaFactory : MediaFactory

#region IMediaFactory Members

public override Media CreateMedia(IconI parent, HttpPostedFile uploadFile)
{
string filename = uploadFile.FileName;
public override Media CreateMedia(IconI parent, HttpPostedFile uploadFile)
{
//Get the safe file name
string filename = SafeFileName(uploadFile.FileName);

// Create new media object
Media media = Media.MakeNew(filename, MediaType.GetByAlias("Image"),
new User(0), parent.Id);
// Create new media object
Media media = Media.MakeNew(filename, MediaType.GetByAlias("Image"),
new User(0), parent.Id);

// Get Image object, width and height
Image image = Image.FromStream(uploadFile.InputStream);
int fileWidth = image.Width;
int fileHeight = image.Height;
// Get Image object, width and height
Image image = Image.FromStream(uploadFile.InputStream);
int fileWidth = image.Width;
int fileHeight = image.Height;

// Get umbracoFile property
int propertyId = media.getProperty("umbracoFile").Id;
// Get umbracoFile property
int propertyId = media.getProperty("umbracoFile").Id;

// Get paths
string relativeDestPath = ConstructRelativeDestPath(propertyId);
string relativeDestFilePath = VirtualPathUtility.Combine(relativeDestPath, filename);
string ext = VirtualPathUtility.GetExtension(filename).Substring(1);
// Get paths
string relativeDestPath = ConstructRelativeDestPath(propertyId);
string relativeDestFilePath = VirtualPathUtility.Combine(relativeDestPath, filename);
string ext = VirtualPathUtility.GetExtension(filename).Substring(1);

// Set media properties
SetImageMediaProperties(media, relativeDestFilePath, fileWidth, fileHeight, uploadFile.ContentLength, ext);
// Set media properties
SetImageMediaProperties(media, relativeDestFilePath, fileWidth, fileHeight, uploadFile.ContentLength, ext);

// Create directory
if (UmbracoSettings.UploadAllowDirectories)
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(relativeDestPath));
// Create directory
if (UmbracoSettings.UploadAllowDirectories)
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(relativeDestPath));

// Generate thumbnail
string absoluteDestPath = HttpContext.Current.Server.MapPath(relativeDestPath);
string absoluteDestFilePath = Path.Combine(absoluteDestPath, Path.GetFileNameWithoutExtension(filename) + "_thumb");
GenerateThumbnail(image, 100, fileWidth, fileHeight, absoluteDestFilePath + ".jpg");
// Generate thumbnail
string absoluteDestPath = HttpContext.Current.Server.MapPath(relativeDestPath);
string absoluteDestFilePath = Path.Combine(absoluteDestPath, Path.GetFileNameWithoutExtension(filename) + "_thumb");
GenerateThumbnail(image, 100, fileWidth, fileHeight, absoluteDestFilePath + ".jpg");

// Generate additional thumbnails based on PreValues set in DataTypeDefinition uploadField
GenerateAdditionalThumbnails(image, fileWidth, fileHeight, absoluteDestFilePath);
// Generate additional thumbnails based on PreValues set in DataTypeDefinition uploadField
GenerateAdditionalThumbnails(image, fileWidth, fileHeight, absoluteDestFilePath);

image.Dispose();
image.Dispose();

return media;
}
return media;
}

// -------------------------------------------------------------------------
// Private members
Expand Down
7 changes: 7 additions & 0 deletions src/noerd.Umb.DataTypes.multipleFileUpload/IMediaFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,12 @@ public interface IMediaFactory
/// <param name="propertyId">The umbracoFile property id</param>
/// <returns>The path to the folder where the uploaded file will be placed.</returns>
string ConstructRelativeDestPath(int propertyId);

/// <summary>
/// Function for handling illegal characters in the file name.
/// </summary>
/// <param name="url">The uploaded file name.</param>
/// <returns>The safe file name that the uploaded file will be saved with.</returns>
string SafeFileName(string fileName);
}
}
10 changes: 10 additions & 0 deletions src/noerd.Umb.DataTypes.multipleFileUpload/MediaFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using umbraco.cms.businesslogic.media;
using umbraco.BusinessLogic.console;
using System.Web;
using System;
using System.Text.RegularExpressions;

namespace noerd.Umb.DataTypes.multipleFileUpload
{
Expand Down Expand Up @@ -37,5 +39,13 @@ public string ConstructRelativeDestPath(int propertyId)

return VirtualPathUtility.ToAbsolute(MEDIA_PATH);
}

public string SafeFileName(string fileName)
{
if (!String.IsNullOrEmpty(fileName))
return Regex.Replace(fileName, @"[^a-zA-Z0-9\-\.\/\:]{1}", "_");
else
return String.Empty;
}
}
}
90 changes: 47 additions & 43 deletions src/noerd.Umb.DataTypes.multipleFileUpload/MultipleFileUpload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,49 +149,53 @@ private static void ConfigValidationEventHandler(object sender, ValidationEventA
// Public members
// -------------------------------------------------------------------------

public static void HandleUpload(HttpContext context, int nodeId)
{
context.Response.Write(context.Request.Files.Count);

// loop through uploaded files
for (int j = 0; j < context.Request.Files.Count; j++)
{
// get parent node
Media parentNode = new Media(nodeId);

// get the current file
HttpPostedFile uploadFile = context.Request.Files[j];

// if there was a file uploded
if (uploadFile.ContentLength > 0)
{
// Get concrete MediaFactory
IMediaFactory factory = GetMediaFactory(uploadFile);
// Create media Item
Media media = factory.CreateMedia(parentNode, uploadFile);

// Get path
int propertyId = media.getProperty("umbracoFile").Id;
string path = HttpContext.Current.Server.MapPath(factory.ConstructRelativeDestPath(propertyId));

// Create directory
if (UmbracoSettings.UploadAllowDirectories)
Directory.CreateDirectory(path);

// Save file
string filePath = Path.Combine(path, uploadFile.FileName);
uploadFile.SaveAs(filePath);

// Close stream
uploadFile.InputStream.Close();

// Save media
media.Save();
// Genereate xml cache
media.XmlGenerate(new XmlDocument());
}
}
}
public static void HandleUpload(HttpContext context, int nodeId)
{
context.Response.Write(context.Request.Files.Count);

// loop through uploaded files
for (int j = 0; j < context.Request.Files.Count; j++)
{
// get parent node
Media parentNode = new Media(nodeId);

// get the current file
HttpPostedFile uploadFile = context.Request.Files[j];

// if there was a file uploded
if (uploadFile.ContentLength > 0)
{
// Get concrete MediaFactory
IMediaFactory factory = GetMediaFactory(uploadFile);

//Get the safe file name
string fileName = factory.SafeFileName(uploadFile.FileName);

// Create media Item
Media media = factory.CreateMedia(parentNode, uploadFile);

// Get path
int propertyId = media.getProperty("umbracoFile").Id;
string path = HttpContext.Current.Server.MapPath(factory.ConstructRelativeDestPath(propertyId));

// Create directory
if (UmbracoSettings.UploadAllowDirectories)
Directory.CreateDirectory(path);

// Save file
string filePath = Path.Combine(path, fileName);
uploadFile.SaveAs(filePath);

// Close stream
uploadFile.InputStream.Close();

// Save media
media.Save();
// Genereate xml cache
media.XmlGenerate(new XmlDocument());
}
}
}

// IDataEditor Members
// -------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>noerd.Umb.DataTypes.multipleFileUpload</RootNamespace>
<AssemblyName>noerd.Umb.DataTypes.multipleFileUpload</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileUpgradeFlags>
</FileUpgradeFlags>
<OldToolsVersion>2.0</OldToolsVersion>
Expand Down