/// <summary>
        /// Processes media for a specified media folder
        /// </summary>
        /// <param name="uploadFolder">The upload folder to process media for</param>
        /// <param name="allMediaFolders">A list of all media folders</param>
        private void DoMediaProcessing(UploadFolder uploadFolder, IEnumerable<MediaFolder> allMediaFolders)
        {
            DirectoryInfo uploadDirectory;

            Uri uploadFolderUri = new Uri(FolderLocation.UploadFolder, uploadFolder.RelativePath);
            uploadDirectory = new DirectoryInfo(uploadFolderUri.LocalPath);

            FileInfo[] mediaInUploadFolder = uploadDirectory.GetFiles("*.*", SearchOption.TopDirectoryOnly);

            //get media folders corespodning to the upload folder
            IEnumerable<MediaFolder> mediaFoldersCorespondingToUploadFolders = allMediaFolders.Where(x => x.UploadFolderID == uploadFolder.UploadFolderID);

            //place new images into the upload folder remove processed images from the upload folder
            foreach (var mediaFolder in mediaFoldersCorespondingToUploadFolders)
            {
                Uri mediaFolderUri = new Uri(FolderLocation.UploadFolder, mediaFolder.RelativePath);
                ResizeSettings resizeSettings = null;

                if (mediaFolder.Property != null)
                {
                    resizeSettings = new ResizeSettings();

                    if (mediaFolder.Property.ImageWidth != null)
                    {
                        resizeSettings.MaxWidth = (int)mediaFolder.Property.ImageWidth;
                    }
                    if (mediaFolder.Property.ImageHeight != null)
                    {
                        resizeSettings.MaxHeight = (int)mediaFolder.Property.ImageHeight;
                    }
                    resizeSettings.Mode = FitMode.Max;
                    foreach (var mediaToProcess in mediaInUploadFolder)
                    {
                        ImageBuilder.Current.Build(mediaToProcess.FullName, mediaFolderUri.LocalPath + "\\" + mediaToProcess.Name, resizeSettings);
                    }
                }
                else
                {
                    foreach (var mediaToProcess in mediaInUploadFolder)
                    {
                        mediaToProcess.CopyTo(mediaFolderUri.LocalPath + "\\" + mediaToProcess.Name);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a folder
        /// </summary>
        /// <param name="newMediaFolder">New folder to create</param>
        /// <param name="uploadFolder">The upload folder to upload to this folder (optional)</param>
        public void CreateFolder(MediaFolder newMediaFolder, UploadFolder uploadFolder = null)
        {
            //Uri mediaFolderPath = new Uri(HttpContext.Current.Server.MapPath("/ContentFolders"));
            //Uri uploadFolderPath = new Uri(HttpContext.Current.Server.MapPath("/UploadFolders"));
            DirectoryInfo newMediaFolderDirectoryInfo;
            DirectoryInfo newUploadFolderDirectoryInfo;

            if (newMediaFolder == null)
            {
                //todo log error and return
                return;
            }

            //Create the main directories if they do not currently exists
            try
            {
                if (!Directory.Exists(FolderLocation.ContentFolder.LocalPath))
                    Directory.CreateDirectory(FolderLocation.ContentFolder.LocalPath);

                if (!Directory.Exists(FolderLocation.UploadFolder.LocalPath))
                    Directory.CreateDirectory(FolderLocation.UploadFolder.LocalPath);
            }
            catch (Exception)
            {
                //todo log error
                throw;
            }

            try
            {
                //Create the new directory
                DirectoryInfo newMediaFolderDirectory = new DirectoryInfo(FolderLocation.ContentFolder.LocalPath);
                newMediaFolderDirectoryInfo = newMediaFolderDirectory.CreateSubdirectory(newMediaFolder.Name);
                newMediaFolder.RelativePath = FolderLocation.ContentFolder.MakeRelativeUri(new Uri(newMediaFolderDirectoryInfo.FullName)).ToString();
            }
            catch (Exception)
            {
                //todo log error
                throw;
            }

            if (newMediaFolder.UploadFolderID == 0)
            {
                if (uploadFolder != null)
                {
                    try
                    {
                        DirectoryInfo newUploadFolderDirectory = new DirectoryInfo(FolderLocation.UploadFolder.LocalPath);
                        newUploadFolderDirectoryInfo = newUploadFolderDirectory.CreateSubdirectory(uploadFolder.Name);
                        uploadFolder.RelativePath = FolderLocation.UploadFolder.MakeRelativeUri(new Uri(newUploadFolderDirectoryInfo.FullName)).ToString();
                        try
                        {
                            _mediaManagerContext.Entry(uploadFolder).State = EntityState.Added;
                            _mediaManagerContext.SaveChanges();

                            try
                            {
                                newMediaFolder.UploadFolderID = uploadFolder.UploadFolderID;
                            }
                            catch (Exception)
                            {
                                //todo log error
                                throw;
                            }
                        }
                        catch (Exception)
                        {
                            //todo log error
                            throw;
                        }
                    }
                    catch (Exception)
                    {
                        //todo log error
                        throw;
                    }
                }
            }

            try
            {
                _mediaManagerContext.Entry(newMediaFolder).State = EntityState.Added;
                _mediaManagerContext.SaveChanges();
            }
            catch (Exception)
            {
                //todo log error
                throw;
            }

            #region commented out
            //if (Directory.Exists(mainContentFolderPath) && Directory.Exists(mainUploadFolderPath))
            //{
            //	string newMediaFolderPath = HttpContext.Current.Server.MapPath("/ContentFolders/" + newMediaFolder.Name);
            //	DirectoryInfo mediaFolderDirectory = new DirectoryInfo(newMediaFolderPath);
            //	try
            //	{

            //	}
            //	catch (Exception)
            //	{

            //		throw;
            //	}
            //	if (newMediaFolder.UploadFolderID != 0)
            //	{
            //		string newUploadFolderPath = HttpContext.Current.Server.MapPath("/UploadFolders/" + uploadFolder.Name);
            //		DirectoryInfo uploadFolderDirectory = new DirectoryInfo(newUploadFolderPath);
            //	}
            //}
            //else
            //{
            //	try
            //	{
            //		if (!Directory.Exists(mainContentFolderPath))
            //			Directory.CreateDirectory(mainContentFolderPath);

            //		if (!Directory.Exists(mainUploadFolderPath))
            //			Directory.CreateDirectory(mainUploadFolderPath);
            //	}
            //	catch (Exception)
            //	{
            //		throw;
            //	}
            //}
            #endregion
        }
Ejemplo n.º 3
0
        public MediaFolder AddMediaFolder(MediaFolder mediaFolder)
        {
            DirectoryInfo newMediaFolderDirectoryInfo;
            DirectoryInfo newUploadFolderDirectoryInfo;
            MediaFolder existingMediaFolder;

            //Create the main directories if they do not currently exists
            try
            {
                if (!Directory.Exists(contentFolderURI.LocalPath))
                    Directory.CreateDirectory(contentFolderURI.LocalPath);

                if (!Directory.Exists(uploadFolderURI.LocalPath))
                    Directory.CreateDirectory(uploadFolderURI.LocalPath);
            }
            catch (Exception)
            {
                //todo log error
                throw;
            }

            DirectoryInfo newMediaFolderDirectory = new DirectoryInfo(contentFolderURI.LocalPath);
            newMediaFolderDirectoryInfo = newMediaFolderDirectory.CreateSubdirectory(mediaFolder.Name);

            //Check to see if a media folder with that name already exists
            existingMediaFolder = unitOfWork.MediaFolderRepository.GetFirst(x => x.Name.Equals(mediaFolder.Name, StringComparison.InvariantCulture));
            if (existingMediaFolder != null)
            {
                return existingMediaFolder;
            }

            if (mediaFolder.UploadFolderID == 0)
            {
                //create new upload folder
                UploadFolder uploadFolder = new UploadFolder();
                uploadFolder.Name = mediaFolder.UploadFolder.Name;
                DirectoryInfo newUploadFolderDirectory = new DirectoryInfo(uploadFolderURI.LocalPath);
                newUploadFolderDirectoryInfo = newUploadFolderDirectory.CreateSubdirectory(mediaFolder.UploadFolder.Name);
                uploadFolder.RelativePath = uploadFolderURI.MakeRelativeUri(new Uri(newUploadFolderDirectoryInfo.FullName)).ToString();
                //unitOfWork.UploadFolderRepository.Insert(uploadFolder);
                //unitOfWork.Save();
                //mediaFolder.UploadFolderID = uploadFolder.UploadFolderID;
                mediaFolder.UploadFolder = uploadFolder;
            }
            else
            {
                mediaFolder.UploadFolder = null;
            }

            //Create the new media directory
            mediaFolder.RelativePath = contentFolderURI.MakeRelativeUri(new Uri(newMediaFolderDirectoryInfo.FullName)).ToString();

            unitOfWork.MediaFolderRepository.Insert(mediaFolder);
            unitOfWork.Save();

            return mediaFolder;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Updates a folder
        /// </summary>
        /// <param name="mediaFolder">Media Folder to Update</param>
        /// <param name="uploadFolder">Upload Folder to Update (optional)</param>
        public void UpdateFolder(MediaFolder mediaFolder, UploadFolder uploadFolder = null)
        {
            DirectoryInfo newUploadFolderDirectoryInfo;

            if (mediaFolder == null || (mediaFolder.UploadFolderID == 0 && uploadFolder == null))
            {
                //todo log error and return
                return;
            }

            if (mediaFolder.UploadFolderID == 0)
            {
                try
                {
                    DirectoryInfo newUploadFolderDirectory = new DirectoryInfo(FolderLocation.UploadFolder.LocalPath);
                    newUploadFolderDirectoryInfo = newUploadFolderDirectory.CreateSubdirectory(uploadFolder.Name);
                    uploadFolder.RelativePath = FolderLocation.UploadFolder.MakeRelativeUri(new Uri(newUploadFolderDirectoryInfo.FullName)).ToString();
                    try
                    {
                        _mediaManagerContext.Entry(uploadFolder).State = EntityState.Added;
                        _mediaManagerContext.SaveChanges();

                        try
                        {
                            mediaFolder.UploadFolderID = uploadFolder.UploadFolderID;
                        }
                        catch (Exception)
                        {
                            //todo log error
                            throw;
                        }
                    }
                    catch (Exception)
                    {
                        //todo log error
                        throw;
                    }
                }
                catch (Exception)
                {
                    //todo log error
                    throw;
                }
            }

            try
            {
                _mediaManagerContext.Entry(mediaFolder).State = EntityState.Modified;
                _mediaManagerContext.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                        eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }
        }
Ejemplo n.º 5
0
        public MediaFolder UpdateMediaFolder(MediaFolder mediaFolder)
        {
            DirectoryInfo newUploadFolderDirectoryInfo;

            if (mediaFolder.UploadFolderID == 0)
            {
                //create new media folder
                UploadFolder uploadFolder = new UploadFolder();
                uploadFolder.Name = mediaFolder.UploadFolder.Name;
                DirectoryInfo newUploadFolderDirectory = new DirectoryInfo(uploadFolderURI.LocalPath);
                newUploadFolderDirectoryInfo = newUploadFolderDirectory.CreateSubdirectory(mediaFolder.UploadFolder.Name);
                uploadFolder.RelativePath = uploadFolderURI.MakeRelativeUri(new Uri(newUploadFolderDirectoryInfo.FullName)).ToString();
                //mediaFolder.UploadFolder = uploadFolder;
                unitOfWork.UploadFolderRepository.Insert(uploadFolder);
                mediaFolder.UploadFolderID = uploadFolder.UploadFolderID;
            }

            mediaFolder.UploadFolder = null;

            unitOfWork.MediaFolderRepository.Update(mediaFolder);
            unitOfWork.Save();

            return mediaFolder;
        }