public PackageForUpload GetPackageForUpload()
        {
            lock (synchRoot)
            {
                RecycleTimeoutUploads();
                if (uploadingPackages.Count > 2)
                {
                    return null;
                }
                string fullFilePath = tools.PickPackageForUpload();
                if (fullFilePath == null)
                {
                    return null;
                }

                byte[] packageContent = FileUtils.GetFileContent(fullFilePath);
                byte[] shortVersionContent = null;

                try
                {
                    //if the size of the package is bigger then let's say 100.000 bytes, create a short-version of the package too

                    if (packageContent.Length > 100000)
                    {
                        FastZip zipFile = new FastZip();

                        //get a temporary directory name
                        string tmpDirName = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

                        //unzip the archive
                        zipFile.ExtractZip(fullFilePath, tmpDirName, "");

                        //add -short to the name of the package
                        string shortFileName = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(fullFilePath) + "-short.zip");


                        try
                        {
                            DirectoryInfo tmpLogsDir = new DirectoryInfo(Path.Combine(tmpDirName, "Logs"));
                            if (tmpLogsDir.Exists)
                            {
                                FileInfo[] files = tmpLogsDir.GetFiles();
                                foreach (FileInfo logFile in files)
                                {
                                    logFile.CopyTo(Path.Combine(tmpDirName, logFile.Name));
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            ProcessException.Handle(e);
                            Debug.Fail(e.Message);
                        }

                        //create a new package, but without Data folder
                        zipFile.CreateZip(shortFileName, tmpDirName, false, string.Empty);

                        shortVersionContent = FileUtils.GetFileContent(shortFileName);

                        try
                        {
                            //clean up
                            Directory.Delete(tmpDirName, true);
                            File.Delete(shortFileName);
                        }
                        catch (Exception ex)
                        {
                            ProcessException.Handle(ex);
                            Debug.Fail(ex.Message);
                        }
                    }
                }
                catch (Exception ex)
                {
                    ProcessException.Handle(ex);
                    Debug.Fail(ex.Message);
                }

                uploadingPackages.Add(fullFilePath, DateTime.Now);
                PackageForUpload packageForUpload = new PackageForUpload(Path.GetFileName(fullFilePath), packageContent, shortVersionContent);

                return packageForUpload;
            }
        }
		public void StartProcessing()
		{
			ProcessException.Handle("Start processing " + this.reportHashCode);

			if(!ShouldStartJob())
			{
				return;
			}
			else
			{
				tools.AddProcessingTicket(ticket);	
			}

			try
			{
				bool shouldAddSnapshot = tools.AddSnapshotToReport & ShouldAddSnapshot();
				ticket.HasSnapshot = shouldAddSnapshot;
        
				//create the name of the package
				string packageName = string.Format("err-ID_{4}-{0}-v.{1}-{2}{3}{5}", report.InstallationName, report.InstallationVersion, DateTime.Now.ToString("yyyy.MM.dd_HH_mm_ss"), (shouldAddSnapshot ? "-s" : string.Empty), reportHashCode, (report.ClientErrorReport.IsCrush ? "-c" : string.Empty));
#if DEBUG
				packageName = string.Format("err_TEST-ID_{4}-{0}-v.{1}-{2}{3}{5}", report.InstallationName, report.InstallationVersion, DateTime.Now.ToString("yyyy.MM.dd_HH_mm_ss"), (shouldAddSnapshot ? "-s" : string.Empty), reportHashCode, (report.ClientErrorReport.IsCrush ? "-c" : string.Empty));
#endif

				//if it is issues, use other name format
				if(report.ClientErrorReport.IsIssue)
				{
					packageName = "issue" + "-" + report.InstallationName + "-" + "v." + report.InstallationVersion + "-" + DateTime.Now.ToString("yyyy.MM.dd_HH_mm_ss") + "-" + report.ClientErrorReport.GetAdditionalItemValue("ApplicationName").Replace(" ", "_")+"-"+reportHashCode;
					shouldAddSnapshot = true;
					ticket.HasSnapshot = shouldAddSnapshot;
#if DEBUG
          packageName = "issue" + "-" + "TEST" + "-" + report.InstallationName + "-" + "v." + report.InstallationVersion + "-" + DateTime.Now.ToString("yyyy.MM.dd_HH_mm_ss") + "-" + report.ClientErrorReport.GetAdditionalItemValue("ApplicationName").Replace(" ", "_") + "-" + reportHashCode;
#endif
				}

        DirectoryInfo packageDir = new DirectoryInfo(Path.Combine(tools.ProcessingFolderPath, packageName));
				if(packageDir.Exists)
				{
					return;
				}

				packageDir.Create();
        
        //save the screenshot image into the package folder
				if (report.ClientErrorReport.ScreenCaptureImageContent != null)
				{
					try
					{
						FileUtils.SaveToFile(Path.Combine(packageDir.FullName, report.ClientErrorReport.ScreenCaptureImageName),
						                     report.ClientErrorReport.ScreenCaptureImageContent);
					}
					catch (Exception e)
					{
						ProcessException.Handle(e);
						Debug.WriteLine(e);
					}
				}

				//save the xml containg the error report details
				report.ToXml(Path.Combine(packageDir.FullName, "ReportDetails.xml"));
				//save the xslt transform file in the package to have a nice quick view of the xml error report
				ErrorReportXLST.SaveToFile(Path.Combine(packageDir.FullName, "ReportDetails.xslt"));

				//add the snapshot into the package if necessary
				if (shouldAddSnapshot)
				{
					DirectoryInfo dataDir = packageDir.CreateSubdirectory("Data");
					if (File.Exists(report.LastSnapshot))
					{
						File.Copy(report.LastSnapshot,
						          Path.Combine(dataDir.FullName, Path.GetFileName(report.LastSnapshot)));
					}

					foreach (string commandLog in report.CommandLogs)
					{
						if (File.Exists(commandLog))
						{
							File.Copy(commandLog,
							          Path.Combine(dataDir.FullName, Path.GetFileName(commandLog)));
						}
					}

          //add config into the package
          DirectoryInfo configDir = packageDir.CreateSubdirectory("Config");
          foreach (DirectoryInfo cfg in report.ConfigFolders)
          {
            if(!cfg.Exists)
            {
              continue; 
            }
            DirectoryInfo destDir = configDir.CreateSubdirectory(cfg.Name);

            foreach (FileInfo fileInfo in cfg.GetFiles())
            {
              fileInfo.CopyTo(Path.Combine(destDir.FullName, fileInfo.Name));
            }
          }
				}

        

        DirectoryInfo logsDir = packageDir.CreateSubdirectory("Logs");

        if (Directory.Exists(report.LogsDirectory))
        {
          string[] logFiles = Directory.GetFiles(report.LogsDirectory);
          foreach (string logFile in logFiles)
          {
            File.Copy(logFile,
                    Path.Combine(logsDir.FullName, Path.GetFileName(logFile)));
          }
        }

				//create the zip package in processing folder
				FastZip zip = new FastZip();
				zip.CreateEmptyDirectories = true;

				string zipFileFullName = Path.Combine(tools.ProcessingFolderPath, packageName  + ".zip");

				zip.CreateZip(zipFileFullName, packageDir.FullName, true, "");
				
				//detele the package folder from processing folder
				try
				{
					packageDir.Delete(true);
				}
				catch { }

				tools.MovePackageToBeUploaded(zipFileFullName);
			}
			catch (Exception ex)
			{
				ProcessException.Handle(ex);
				Debug.WriteLine(ex);
			}
			finally
			{
				tools.RemoveProcessingTicket(ticket);
			}
		}
        public PackageForUpload GetPackageForUpload(int maxContentLength, string fileExt, out string metaFileName, out string errorType, out SummaryField[] summaryItems, out string[] splittedFiles)
        {
            lock (synchRoot)
            {
                #region Validate info
                RecycleTimeoutUploads();

                metaFileName = null;
                summaryItems = new SummaryField[0];
                errorType = string.Empty;
                splittedFiles = new string[0];
                byte[] shortVersionContent = null;

                PackageForUpload packageForUpload = null;

                string fullFilePath = tools.PickPackageForUpload();
                if (fullFilePath == null)
                {
                    return null;
                }

                if (System.IO.Path.GetExtension(fullFilePath).ToLower().Equals(fileExt.ToLower()))
                {
                    uploadingPackages.Add(fullFilePath, DateTime.Now);
                    shortVersionContent = FileUtils.GetFileContent(fullFilePath);
                    packageForUpload = new PackageForUpload(Path.GetFileName(fullFilePath), null, shortVersionContent);
                    splittedFiles = new string[1];
                    splittedFiles[0] = fullFilePath;
                    return packageForUpload;

                }
                string storeFolder = Path.GetDirectoryName(fullFilePath);
                #endregion

                byte[] packageContent = FileUtils.GetFileContent(fullFilePath);


                try
                {
                    FastZip zipFile = new FastZip();

                    //get a temporary directory name
                    string tmpDirName = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

                    //unzip the archive
                    zipFile.ExtractZip(fullFilePath, tmpDirName, "");

                    string xmlFilename = System.IO.Path.Combine(tmpDirName, "ReportDetails.xml");

                    //add -short to the name of the package
                    metaFileName = Path.GetFileNameWithoutExtension(fullFilePath) + "-meta.zip";
                    string shortFilePath = Path.Combine(Path.GetTempPath(), metaFileName);

                    #region Get Summary information from ReportDetails.xml
                    string xmlDefineFields = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SummaryFields.xml");

                    XmlMetaProcessing metaProcessing = new XmlMetaProcessing(xmlDefineFields, xmlFilename);
                    summaryItems = metaProcessing.SummaryFields;

                    string snapShotFolderName = System.IO.Path.Combine(tmpDirName, "Data");

                    string hasSnapShot = "False";
                    if (System.IO.Directory.Exists(snapShotFolderName))
                        hasSnapShot = "True";


                    if (summaryItems.Length > 0)
                    {
                        foreach (SummaryField item in summaryItems)
                        {
                            if (item.Name.ToLower().Equals("errorlocation"))
                            {
                                errorType = (item.Value != null ? item.Value.ToString() : "");
                            }
                            else if (item.Name.ToLower().Equals("hassnapshot"))
                            {
                                item.Value = hasSnapShot;
                            }
                        }
                    }

                    #endregion

#if SHORT_INFO_INCLUDE_LOG
						try
						{
							DirectoryInfo tmpLogsDir = new DirectoryInfo(Path.Combine(tmpDirName, "Logs"));
							if (tmpLogsDir.Exists)
							{
								FileInfo[] files = tmpLogsDir.GetFiles();
								foreach (FileInfo logFile in files)
								{
									logFile.CopyTo(Path.Combine(tmpDirName, logFile.Name));
								}
							}
						}
						catch (Exception e)
						{
							ProcessException.Handle(e);
							Debug.Fail(e.Message);
						}
#endif
                    //create a new package, but without Data folder - this is Meta package
                    zipFile.CreateZip(shortFilePath, tmpDirName, false, string.Empty);

                    shortVersionContent = FileUtils.GetFileContent(shortFilePath);
                    if (packageContent.Length > maxContentLength)
                    {
                        splittedFiles = SplitFile(maxContentLength, fullFilePath, fileExt);
                    }
                    else
                    {
                        splittedFiles = new string[1];
                        splittedFiles[0] = fullFilePath;
                    }

                    try
                    {
                        //clean up
                        Directory.Delete(tmpDirName, true);
                        File.Delete(shortFilePath);
                        //Delete the main file after splited
                        File.Delete(fullFilePath);
                    }
                    catch (Exception ex)
                    {
                        ProcessException.Handle(ex);
                        Debug.Fail(ex.Message);
                    }
                }
                catch (Exception ex)
                {
                    ProcessException.Handle(ex);
                    Debug.Fail(ex.Message);
                }

                uploadingPackages.Add(fullFilePath, DateTime.Now);
                packageForUpload = new PackageForUpload(Path.GetFileName(fullFilePath), null, shortVersionContent);

                return packageForUpload;
            }
        }