public Media HandleMedia(int parentNodeId, PostedMediaFile postedFile, User user)
        {
            // Check to see if a file exists
            Media media;
            string mediaName = !string.IsNullOrEmpty(postedFile.DisplayName)
                ? postedFile.DisplayName
                : ExtractTitleFromFileName(postedFile.FileName);

            if (postedFile.ReplaceExisting && TryFindExistingMedia(parentNodeId, postedFile.FileName, out media))
            {
                // Do nothing as existing media is returned
            }
            else
            {
                media = Media.MakeNew(mediaName,
                    MediaType.GetByAlias(MediaTypeAlias),
                    user,
                    parentNodeId);
            }

            if (postedFile.ContentLength > 0)
                DoHandleMedia(media, postedFile, user);

            media.XmlGenerate(new XmlDocument());

            return media;
        }
        public override void DoHandleMedia(Media media, PostedMediaFile uploadedFile, User user)
        {
            // Get umbracoFile property
            var propertyId = media.getProperty(Constants.Conventions.Media.File).Id;

            // Get paths
            var destFilePath = FileSystem.GetRelativePath(propertyId, uploadedFile.FileName);
            var ext = Path.GetExtension(destFilePath).Substring(1);

            //var absoluteDestPath = HttpContext.Current.Server.MapPath(destPath);
            //var absoluteDestFilePath = HttpContext.Current.Server.MapPath(destFilePath);

            // Set media properties
            media.getProperty(Constants.Conventions.Media.File).Value = FileSystem.GetUrl(destFilePath);
            media.getProperty(Constants.Conventions.Media.Bytes).Value = uploadedFile.ContentLength;

            if (media.getProperty(Constants.Conventions.Media.Extension) != null)
                media.getProperty(Constants.Conventions.Media.Extension).Value = ext;

            // Legacy: The 'extensio' typo applied to MySQL (bug in install script, prior to v4.6.x)
            if (media.getProperty("umbracoExtensio") != null)
                media.getProperty("umbracoExtensio").Value = ext;

            FileSystem.AddFile(destFilePath, uploadedFile.InputStream, uploadedFile.ReplaceExisting);

            // Save media
            media.Save();
        }
예제 #3
0
        public override void DoHandleMedia(Media media, PostedMediaFile uploadedFile, User user)
        {
            // Get umbracoFile property
            var propertyId = media.getProperty("umbracoFile").Id;

            // Get paths
            var destFilePath = FileSystem.GetRelativePath(propertyId, uploadedFile.FileName);
            var ext = Path.GetExtension(destFilePath).Substring(1);

            //var absoluteDestPath = HttpContext.Current.Server.MapPath(destPath);
            //var absoluteDestFilePath = HttpContext.Current.Server.MapPath(destFilePath);

            // Set media properties
            media.getProperty("umbracoFile").Value = FileSystem.GetUrl(destFilePath);
            media.getProperty("umbracoBytes").Value = uploadedFile.ContentLength;

            if (media.getProperty("umbracoExtension") != null)
                media.getProperty("umbracoExtension").Value = ext;

            if (media.getProperty("umbracoExtensio") != null)
                media.getProperty("umbracoExtensio").Value = ext;

            FileSystem.AddFile(destFilePath, uploadedFile.InputStream, uploadedFile.ReplaceExisting);

            // Save media
            media.Save();
        }
        public override void DoHandleMedia(Media media, PostedMediaFile postedFile, BusinessLogic.User user)
        {
            // Get Image object, width and height
            var image = System.Drawing.Image.FromStream(postedFile.InputStream);
            var fileWidth = image.Width;
            var fileHeight = image.Height;

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

            // Get paths
            var destFileName = ConstructDestFileName(propertyId, postedFile.FileName);
            var destPath = ConstructDestPath(propertyId);
            var destFilePath = VirtualPathUtility.Combine(destPath, destFileName);
            var ext = VirtualPathUtility.GetExtension(destFileName).Substring(1);

            var absoluteDestPath = HttpContext.Current.Server.MapPath(destPath);
            var absoluteDestFilePath = HttpContext.Current.Server.MapPath(destFilePath);

            // Set media properties
            media.getProperty("umbracoFile").Value = destFilePath;
            media.getProperty("umbracoWidth").Value = fileWidth;
            media.getProperty("umbracoHeight").Value = fileHeight;
            media.getProperty("umbracoBytes").Value = postedFile.ContentLength;

            if (media.getProperty("umbracoExtension") != null)
                media.getProperty("umbracoExtension").Value = ext;

            if (media.getProperty("umbracoExtensio") != null)
                media.getProperty("umbracoExtensio").Value = ext;

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

            // Generate thumbnail
            var thumbDestFilePath = Path.Combine(absoluteDestPath, Path.GetFileNameWithoutExtension(destFileName) + "_thumb");
            GenerateThumbnail(image, 100, fileWidth, fileHeight, thumbDestFilePath + ".jpg");

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

            image.Dispose();

            // Save file
            postedFile.SaveAs(absoluteDestFilePath);

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

            // Save media
            media.Save();
        }
        public virtual bool CanHandleMedia(int parentNodeId, PostedMediaFile postedFile, User user)
        {
            try
            {
                var parentNode = new Media(parentNodeId);

                return parentNodeId <= -1 || user.Applications.Any(app => app.alias.ToLower() == Constants.Applications.Media) && (user.StartMediaId <= 0 || ("," + parentNode.Path + ",").Contains("," + user.StartMediaId + ",")) && parentNode.ContentType.AllowedChildContentTypeIDs.Contains(MediaType.GetByAlias(MediaTypeAlias).Id);
            }
            catch
            {
                return false;
            }
        }
        public override void DoHandleMedia(Media media, PostedMediaFile postedFile, User user)
        {
            // Set media property to upload the file as well as set all related properties
            media.MediaItem.SetValue("umbracoFile", postedFile.FileName, postedFile.InputStream);

            // Copy back the values from the internal IMedia to ensure that the values are persisted when saved
            foreach (var property in media.MediaItem.Properties)
            {
                media.getProperty(property.Alias).Value = property.Value;
            }

            // Save media (using legacy media object to ensure the usage of the legacy events).
            media.Save();
        }
예제 #7
0
        public override void DoHandleMedia(Media media, PostedMediaFile postedFile, User user)
        {
            // Set media property to upload the file as well as set all related properties
            media.MediaItem.SetValue("umbracoFile", postedFile.FileName, postedFile.InputStream);

            // Copy back the values from the internal IMedia to ensure that the values are persisted when saved
            foreach (var property in media.MediaItem.Properties)
            {
                media.getProperty(property.Alias).Value = property.Value;
            }

            // Save media (using legacy media object to ensure the usage of the legacy events).
            media.Save();
        }
예제 #8
0
        public virtual bool CanHandleMedia(int parentNodeId, PostedMediaFile postedFile, User user)
        {
            try
            {
                var parentNode = new Media(parentNodeId);

                return(parentNodeId > -1 ?
                       user.Applications.Any(app => app.alias.ToLower() == "media") && (user.StartMediaId <= 0 || ("," + parentNode.Path + ",").Contains("," + user.StartMediaId + ",")) && parentNode.ContentType.AllowedChildContentTypeIDs.Contains(MediaType.GetByAlias(MediaTypeAlias).Id) :
                       true);
            }
            catch
            {
                return(false);
            }
        }
예제 #9
0
        public static IMediaFactory GetMediaFactory(int parentId, PostedMediaFile postedFile, User user)
        {
            var ext = Path.GetExtension(postedFile.FileName);
            if (ext == null)
                return null;

            var factories = Factories.Where(mf => mf.Extensions.Contains(ext.ToLower().TrimStart('.'))).ToList();

            if (factories.Count == 0)
                factories = Factories.Where(mf => mf.Extensions.Contains("*")).ToList();

            if (factories.Count > 0)
                return factories.FirstOrDefault(factory => factory.CanHandleMedia(parentId, postedFile, user));

            return null;
        }
예제 #10
0
        public override void DoHandleMedia(Media media, PostedMediaFile postedFile, BusinessLogic.User user)
        {
            // Get Image object, width and height
            var image      = System.Drawing.Image.FromStream(postedFile.InputStream);
            var fileWidth  = image.Width;
            var fileHeight = image.Height;

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

            // Get paths
            var destFilePath = FileSystem.GetRelativePath(propertyId, postedFile.FileName);
            var ext          = Path.GetExtension(destFilePath).Substring(1);

            // 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("umbracoExtension") != null)
            {
                media.getProperty("umbracoExtension").Value = ext;
            }

            if (media.getProperty("umbracoExtensio") != null)
            {
                media.getProperty("umbracoExtensio").Value = ext;
            }

            // Generate thumbnail
            var thumbDestFilePath = Path.Combine(Path.GetDirectoryName(destFilePath), Path.GetFileNameWithoutExtension(destFilePath) + "_thumb");

            GenerateThumbnail(image, 100, fileWidth, fileHeight, ext, thumbDestFilePath + ".jpg");

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

            image.Dispose();

            FileSystem.AddFile(destFilePath, postedFile.InputStream, postedFile.ReplaceExisting);

            // Save media
            media.Save();
        }
예제 #11
0
        public override void DoHandleMedia(Media media, PostedMediaFile uploadedFile, User user)
        {
            // Get umbracoFile property
            var propertyId = media.getProperty("umbracoFile").Id;

            // Get paths
            var destFileName = ConstructDestFileName(propertyId, uploadedFile.FileName);
            var destPath     = ConstructDestPath(propertyId);
            var destFilePath = VirtualPathUtility.Combine(destPath, destFileName);
            var ext          = VirtualPathUtility.GetExtension(destFileName).Substring(1);

            var absoluteDestPath     = HttpContext.Current.Server.MapPath(destPath);
            var absoluteDestFilePath = HttpContext.Current.Server.MapPath(destFilePath);

            // Set media properties
            media.getProperty("umbracoFile").Value  = destFilePath;
            media.getProperty("umbracoBytes").Value = uploadedFile.ContentLength;

            if (media.getProperty("umbracoExtension") != null)
            {
                media.getProperty("umbracoExtension").Value = ext;
            }

            if (media.getProperty("umbracoExtensio") != null)
            {
                media.getProperty("umbracoExtensio").Value = ext;
            }

            // Create directory
            if (UmbracoSettings.UploadAllowDirectories)
            {
                Directory.CreateDirectory(absoluteDestPath);
            }

            // Save file
            uploadedFile.SaveAs(absoluteDestFilePath);

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

            // Save media
            media.Save();
        }
예제 #12
0
        public static IMediaFactory GetMediaFactory(int parentId, PostedMediaFile postedFile, User user)
        {
            var ext = Path.GetExtension(postedFile.FileName);

            if (ext == null)
            {
                return(null);
            }

            var factories = Factories.Where(mf => mf.Extensions.Contains(ext.ToLower().TrimStart('.'))).ToList();

            if (factories.Count == 0)
            {
                factories = Factories.Where(mf => mf.Extensions.Contains("*")).ToList();
            }

            if (factories.Count > 0)
            {
                return(factories.FirstOrDefault(factory => factory.CanHandleMedia(parentId, postedFile, user)));
            }

            return(null);
        }
예제 #13
0
			public Media HandleMedia(int parentNodeId, PostedMediaFile postedFile, User user)
			{
				throw new NotImplementedException();
			}
예제 #14
0
 public abstract void DoHandleMedia(Media media, PostedMediaFile uploadedFile, User user);
예제 #15
0
        public Media HandleMedia(int parentNodeId, PostedMediaFile postedFile, User user, bool replaceExisting)
        {
            postedFile.ReplaceExisting = replaceExisting;

            return HandleMedia(parentNodeId, postedFile, user);
        }
예제 #16
0
 public Media HandleMedia(int parentNodeId, PostedMediaFile postedFile, User user)
 {
     return(HandleMedia(parentNodeId, postedFile, user, false));
 }
예제 #17
0
        public Media HandleMedia(int parentNodeId, PostedMediaFile postedFile, User user, bool replaceExisting)
        {
            postedFile.ReplaceExisting = replaceExisting;

            return(HandleMedia(parentNodeId, postedFile, user));
        }
예제 #18
0
 public abstract void DoHandleMedia(Media media, PostedMediaFile uploadedFile, User user);
예제 #19
0
			public Media HandleMedia(int parentNodeId, PostedMediaFile postedFile, User user, bool replaceExisting)
			{
				throw new NotImplementedException();
			}
예제 #20
0
        public UploadResponse ProcessUploadRequest(HttpContext context)
        {
            int parentNodeId;
            if (int.TryParse(context.Request["parentNodeId"], out parentNodeId) && context.Request.Files.Count > 0)
            {
                try
                {
                    var parentNode = new Media(parentNodeId);
                    // Check FilePath
                    if (!string.IsNullOrEmpty(context.Request["path"]))
                    {
                        var pathParts = context.Request["path"].Trim('/').Split('/');
                        
                        foreach (var pathPart in pathParts.Where(part => string.IsNullOrWhiteSpace(part) == false))
                                parentNode = GetOrCreateFolder(parentNode, pathPart);

                        parentNodeId = parentNode.Id;
                    }

                    // Check whether to replace existing
                    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++)
                    {
                        // get the current file
                        var uploadFile = context.Request.Files[j];

                        using (var inputStream = uploadFile.InputStream)
                        {
                            // if there was a file uploded
                            if (uploadFile.ContentLength > 0)
                            {
                                // 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
                                {
                                    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);
                            }
                        }
                    }

                    var scripts = new ClientTools(new Page());
                    scripts.SyncTree(parentNode.Path, true);

                    // log succes
                    LogHelper.Info<MediaUploader>(string.Format("Success uploading to parent {0}", parentNodeId));
                }
                catch (Exception e)
                {
                    // log error
                    LogHelper.Error<MediaUploader>(string.Format("Error uploading to parent {0}", parentNodeId), e);
                }
            }
            else
            {
                // log error
                LogHelper.Warn<MediaUploader>(string.Format("Parent node id is in incorrect format: {0}", parentNodeId));
            }
            
            return new UploadResponse();
        }
예제 #21
0
        public void ProcessUploadRequest(HttpContext context, XmlTextWriter xmlTextWriter)
        {
            int parentNodeId;
            if (int.TryParse(context.Request["parentNodeId"], out parentNodeId) && context.Request.Files.Count > 0)
            {
                try
                {
                    // Check Path
                    if (!string.IsNullOrEmpty(context.Request["path"]))
                    {
                        var pathParts = context.Request["path"].Trim('/').Split('/');

                        var parentNode = new Media(parentNodeId);
                        foreach (var pathPart in pathParts)
                        {
                            if (!string.IsNullOrEmpty(pathPart))
                            {
                                parentNode = GetOrCreateFolder(parentNode, pathPart);
                            }
                        }
                        parentNodeId = parentNode.Id;
                    }

                    // Check whether to replace existing
                    bool replaceExisting = (context.Request["replaceExisting"] == "1");

                    // loop through uploaded files
                    for (var j = 0; j < context.Request.Files.Count; j++)
                    {
                        // get the current file
                        var uploadFile = context.Request.Files[j];

                        // if there was a file uploded
                        if (uploadFile.ContentLength > 0)
                        {
                            var postedMediaFile = new PostedMediaFile
                            {
                                FileName = uploadFile.FileName,
                                ContentType = uploadFile.ContentType,
                                ContentLength = uploadFile.ContentLength,
                                InputStream = uploadFile.InputStream
                            };

                            // Get concrete MediaFactory
                            var factory = MediaFactory.GetMediaFactory(parentNodeId, postedMediaFile, AuthenticatedUser);

                            // Handle media Item
                            var media = factory.HandleMedia(parentNodeId, postedMediaFile, AuthenticatedUser, replaceExisting);
                        }
                    }

                    // log succes
                    Log.Add(LogTypes.New, parentNodeId, "Succes");
                }
                catch (Exception e)
                {
                    // log error
                    Log.Add(LogTypes.Error, parentNodeId, e.ToString());
                }
            }
            else
            {
                // log error
                Log.Add(LogTypes.Error, -1, "Parent node id is in incorrect format");
            }
        }