예제 #1
0
        public void FileMover(int itemID, string imgRootPath, string sourceFileCol, string pathCollection, string isActive, string imageType, string description, string displayOrder, string imgPreFix,int itemLargeThumbNailSize,int itemMediumThumbNailSize, int itemSmallThumbNailSize)
        {
            string pathList = string.Empty;
            string[] sourceFileList = sourceFileCol.Split('%');
            string destpath = HostingEnvironment.MapPath("~/" + imgRootPath);
            if (!Directory.Exists(destpath))
            {
                Directory.CreateDirectory(destpath);
            }

            Random randVar = new Random();

            try
            {
                string[] sourceList = pathCollection.Split('%');
                //  DirectoryInfo destDir = new DirectoryInfo(destpath);

                #region FileDelete
                // Delete images from dstination folder of same itemID
                /*  
                                   if (destDir.Exists)
                                   {
                                       FileInfo[] files = GetFilesList(int.Parse(itemID), destDir);

                                       string listFiles = string.Empty;
                                       int j = 0;
                                       if (files.Length != 0)
                                       {
                                           while (j < files.Length)
                                           {

                                               if (files[j].FullName.Contains(".db") || files[j].FullName.Contains(".DB"))
                                               {
                                                   System.IO.File.Delete(files[j].FullName);
                                               }
                                               j++;
                                           }
                                           foreach (FileInfo file in files)
                                           {
                                               foreach (string fileStr in sourceList)
                                               {
                                                   string sourceCol = HostingEnvironment.MapPath("~/" + fileStr);
                                                   string destCol = file.DirectoryName + "\\" + file;
                                                   if (destCol == sourceCol)
                                                   {
                                                       break;
                                                   }
                                                   else
                                                   {
                                                       listFiles += sourceCol + ",";
                                                   }

                                               }
                                           }
                                       }





                                       if (listFiles.Contains(","))
                                       {
                                           listFiles = listFiles.Remove(listFiles.LastIndexOf(","));


                                           string[] delFileCol = listFiles.Split(',');

                                           int count = delFileCol.Length;
                                           int i = 0;
                                           while (i < count)
                                           {
                                               if (File.Exists(delFileCol[i]))
                                               {
                                                   if (delFileCol[i].Contains(".db") || delFileCol[i].Contains(".DB"))
                                                   {
                                                       System.IO.File.Delete(delFileCol[i]);
                                                   }

                                                   else
                                                   {
                                                       string[] path_Splitter = delFileCol[i].ToString().Split('\\');
                                                       int words_length = path_Splitter.Length;
                                                       string[] words_Splitter = path_Splitter[words_length - 1].Split('_');
                                                       int length = words_Splitter.Length;
                                                       if (words_Splitter[length - 2].ToString() == itemID)
                                                       {
                                                           System.IO.File.Delete(delFileCol[i]);
                                                       }
                                                   }
                                               }
                                               i++;
                                           }
                                       }
                                   }*/
                #endregion

                //Move files from source to destination folder
                for (int i = 0; i < sourceFileList.Length; i++)
                {
                    string sourceCol = HostingEnvironment.MapPath("~/" + sourceList[i]);
                    string fileExt = sourceFileList[i].Substring(sourceFileList[i].LastIndexOf("."));
                    string fileName = imgPreFix + itemID + '_' + randVar.Next(111111, 999999).ToString() + fileExt;
                    pathList += imgRootPath + fileName + "%";
                    //TODO:: make only image Name instead Path 
                    sourceFileList[i] = fileName;
                    string destination = Path.Combine(destpath, sourceFileList[i]);
                    if (sourceCol != destination)
                    {
                        if (File.Exists(sourceCol) && !File.Exists(destination))
                        {
                            File.Copy(sourceCol, destination);
                            //image Thumbnails generates here
                            string VertualUrl0 = destpath + "Large\\";
                            string VertualUrl1 = destpath + "Medium\\";
                            string VertualUrl2 = destpath + "Small\\";

                            if (!Directory.Exists(VertualUrl0))
                                Directory.CreateDirectory(VertualUrl0);
                            if (!Directory.Exists(VertualUrl1))
                                Directory.CreateDirectory(VertualUrl1);
                            if (!Directory.Exists(VertualUrl2))
                                Directory.CreateDirectory(VertualUrl2);

                            VertualUrl0 = VertualUrl0 + fileName;
                            VertualUrl1 = VertualUrl1 + fileName;
                            VertualUrl2 = VertualUrl2 + fileName;

                            string[] imageTypeFiles = new string[] { "jpg", "jpeg", "jpe", "gif", "bmp", "png", "ico" };
                            bool IsValidImage = false;
                            foreach (string x in imageTypeFiles)
                            {
                                if (fileExt.Contains(x))
                                {
                                    IsValidImage = true;
                                    break;
                                }
                            }

                            if (IsValidImage)
                            {
                                PictureManager.CreateThmnail(destination, itemLargeThumbNailSize, VertualUrl0);
                                PictureManager.CreateThmnail(destination, itemMediumThumbNailSize, VertualUrl1);
                                PictureManager.CreateThmnail(destination, itemSmallThumbNailSize, VertualUrl2);
                            }
                            else
                            {
                                System.IO.File.Copy(destination, VertualUrl0);
                                System.IO.File.Copy(destination, VertualUrl1);
                                System.IO.File.Copy(destination, VertualUrl2);
                            }
                        }
                    }
                }
                if (pathList.Contains("%"))
                {
                    pathList = pathList.Remove(pathList.LastIndexOf("%"));
                }

                //Save to database
                ImageUploaderSqlhandler imageSql = new ImageUploaderSqlhandler();
                imageSql.SaveImageSettings(itemID, pathList, isActive, imageType, description, displayOrder);

                //// delete files created earlier
                //DirectoryInfo temDir = new DirectoryInfo(sourcepath);
                //if (temDir.Exists)
                //{
                //    FileInfo[] files = temDir.GetFiles();
                //    foreach (FileInfo file in files)
                //    {
                //        if (file.CreationTime.ToShortDateString() != DateTime.Now.ToShortDateString())
                //        {
                //            System.IO.File.Delete(file.FullName);
                //        }
                //    }
                //}
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #2
0
    public string SaveImageContents(Int32 ItemID, string imageRootPath, string sourceFileCol, string dataCollection, int itemLargeThumbNailSize, int itemMediumThumbNailSize, int itemSmallThumbNailSize, string imgPreFix)
    {

        if (dataCollection.Contains("#"))
        {
            dataCollection = dataCollection.Remove(dataCollection.LastIndexOf("#"));
        }
        SQLHandler sageSql = new SQLHandler();
        string[] individualRow = dataCollection.Split('#');
        string[] words;

        StringBuilder sbPathList = new StringBuilder();
        StringBuilder sbIsActiveList = new StringBuilder();
        StringBuilder sbImageType = new StringBuilder();
        StringBuilder sbDescription = new StringBuilder();
        StringBuilder sbDisplayOrder = new StringBuilder();
        StringBuilder sbSourcePathList = new StringBuilder();

        foreach (string str in individualRow)
        {
            words = str.Split('%');
            sbPathList.Append(words[0] + "%");
            sbIsActiveList.Append(words[1] + "%");
            sbImageType.Append(words[2] + "%");
            sbDescription.Append(words[3] + "%");
            sbDisplayOrder.Append(words[4] + "%");
        }
        string pathList = string.Empty;
        string isActive = string.Empty;
        string imageType = string.Empty;
        string description = string.Empty;
        string displayOrder = string.Empty;

        pathList = sbPathList.ToString();
        isActive = sbIsActiveList.ToString();
        imageType = sbImageType.ToString();
        description = sbDescription.ToString();
        displayOrder = sbDisplayOrder.ToString();

        if (pathList.Contains("%"))
        {
            pathList = pathList.Remove(pathList.LastIndexOf("%"));
        }
        if (isActive.Contains("%"))
        {
            isActive = isActive.Remove(isActive.LastIndexOf("%"));
        }
        if (imageType.Contains("%"))
        {
            imageType = imageType.Remove(imageType.LastIndexOf("%"));
        }

        if (sourceFileCol.Contains("%"))
        {
            sourceFileCol = sourceFileCol.Remove(sourceFileCol.LastIndexOf("%"));
        }

        ImageUploaderSqlhandler imageManager = new ImageUploaderSqlhandler();

        try
        {
            FileHelperController fhc = new FileHelperController();
            //TODO:: delete all previous files infos lists
            fhc.FileMover(ItemID, imageRootPath, sourceFileCol, pathList, isActive, imageType, description, displayOrder, imgPreFix, itemLargeThumbNailSize, itemMediumThumbNailSize, itemSmallThumbNailSize);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return "Success";

    }