Changeset 1397


Ignore:
Timestamp:
12/22/2009 2:27:23 AM (5 years ago)
Author:
lowjoel
Message:

Factorise out the upload code to a new class for clarity

Location:
branches/eraser6/BlackBox/Eraser
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/eraser6/BlackBox/Eraser/BlackBoxUploadForm.cs

    r1395 r1397  
    5454        { 
    5555            IList<BlackBoxReport> reports = (IList<BlackBoxReport>)e.Argument; 
    56             string uploadDir = Path.Combine(Path.GetTempPath(), "Eraser Crash Reports"); 
    57             if (!Directory.Exists(uploadDir)) 
    58                 Directory.CreateDirectory(uploadDir); 
    59  
    6056            for (int i = 0; i < reports.Count; ++i) 
    6157            { 
    62                 //Generate the base name of the report. 
    63                 string reportBaseName = Path.Combine(uploadDir, reports[i].Name); 
    64  
    65                 //Calculate the base progress percentage for this job. Add values up to 10 
    66                 //to indicate report progress 
    67                 int baseProgress = i * 100 / reports.Count; 
     58                //Calculate the base progress percentage for this job. 
    6859                int progressPerReport = 100 / reports.Count; 
     60                int baseProgress = i * progressPerReport; 
    6961                int stepsPerReport = 2; 
    7062 
    71                 using (FileStream archiveStream = new FileStream(reportBaseName + ".tar", 
    72                     FileMode.Create, FileAccess.Write)) 
    73                 { 
    74                     UploadWorker.ReportProgress(baseProgress, 
    75                         S._("Compressing Report {0}", reports[i].Name)); 
    76  
    77                     //Add the report into a tar file 
    78                     TarArchive archive = TarArchive.CreateOutputTarArchive(archiveStream); 
    79                     foreach (FileInfo file in reports[i].Files) 
    80                         archive.WriteEntry(TarEntry.CreateEntryFromFile(file.FullName), false); 
    81                     archive.Close(); 
    82                 } 
    83  
    84                 int lastRead = 0; 
    85                 byte[] buffer = new byte[524288]; 
    86                 using (FileStream bzipFile = new FileStream(reportBaseName + ".tbz", 
    87                     FileMode.Create)) 
    88                 using (FileStream tarStream = new FileStream(reportBaseName + ".tar", 
    89                     FileMode.Open, FileAccess.Read, FileShare.Read, 262144, FileOptions.DeleteOnClose)) 
    90                 using (BZip2OutputStream bzipStream = new BZip2OutputStream(bzipFile, 262144)) 
    91                 { 
    92                     //Compress the tar file 
    93                     while ((lastRead = tarStream.Read(buffer, 0, buffer.Length)) != 0) 
     63                BlackBoxReportUploader uploader = new BlackBoxReportUploader(reports[i]); 
     64 
     65                //Check that a similar report has not yet been uploaded. 
     66                UploadWorker.ReportProgress(baseProgress, 
     67                    S._("Checking for status of report {0}...", reports[i].Name)); 
     68                if (!uploader.ReportIsNew()) 
     69                    continue; 
     70                 
     71                //No similar reports have been uploaded. Compress the report. 
     72                UploadWorker.ReportProgress(baseProgress, 
     73                    S._("Compressing Report {0}: {1}%", reports[i].Name, 0)); 
     74                uploader.Compress(delegate(object from, ProgressChangedEventArgs progress) 
    9475                    { 
    95                         bzipStream.Write(buffer, 0, lastRead); 
    9676                        UploadWorker.ReportProgress(baseProgress + 
    97                             (int)(tarStream.Position * progressPerReport / tarStream.Length / 
    98                                 stepsPerReport)); 
    99                     } 
    100                 } 
    101  
    102                 using (FileStream bzipFile = new FileStream(reportBaseName + ".tbz", 
    103                     FileMode.Open, FileAccess.Read, FileShare.Read, 131072, FileOptions.DeleteOnClose)) 
    104                 using (Stream logFile = reports[i].DebugLog) 
    105                 { 
    106                     //Upload the file 
    107                     UploadWorker.ReportProgress(baseProgress + progressPerReport / 2, 
    108                         S._("Uploading Report {0}", reports[i].Name)); 
    109                     MultipartFormDataBuilder builder = new MultipartFormDataBuilder(); 
    110                     builder.AddPart(new FormFileField("CrashReport", "Report.tbz", bzipFile)); 
    111                     builder.AddPart(new FormFileField("DebugLog", "Debug.log", logFile)); 
    112  
    113                     Uri blackBoxServer = new Uri("http://eraser.heidi.ie/BlackBox/upload.php"); 
    114                     WebRequest reportRequest = HttpWebRequest.Create(blackBoxServer); 
    115                     reportRequest.ContentType = "multipart/form-data; boundary=" + builder.Boundary; 
    116                     reportRequest.Method = "POST"; 
    117                     using (Stream formStream = builder.Stream) 
     77                            progress.ProgressPercentage * progressPerReport / 100 / stepsPerReport, 
     78                            S._("Compressing Report {0}: {1}%", 
     79                                reports[i].Name, progress.ProgressPercentage)); 
     80                    }); 
     81 
     82                //Upload the report. 
     83                UploadWorker.ReportProgress(baseProgress + progressPerReport / 2, 
     84                    S._("Uploading Report {0}: {1}%", reports[i].Name)); 
     85                uploader.Upload(delegate(object from, ProgressChangedEventArgs progress) 
    11886                    { 
    119                         reportRequest.ContentLength = formStream.Length; 
    120                         using (Stream requestStream = reportRequest.GetRequestStream()) 
    121                         { 
    122                             while ((lastRead = formStream.Read(buffer, 0, buffer.Length)) != 0) 
    123                             { 
    124                                 requestStream.Write(buffer, 0, lastRead); 
    125                                 UploadWorker.ReportProgress(baseProgress + progressPerReport / stepsPerReport + 
    126                                     (int)(formStream.Position * progressPerReport / formStream.Length / 2)); 
    127                             } 
    128                         } 
    129  
    130                         HttpWebResponse response = reportRequest.GetResponse() as HttpWebResponse; 
    131                         if (response.StatusCode != HttpStatusCode.OK) 
    132                         { 
    133                             using (Stream responseStream = response.GetResponseStream()) 
    134                             using (TextReader reader = new StreamReader(responseStream)) 
    135                                 throw new ArgumentException(reader.ReadToEnd()); 
    136                         } 
    137                     } 
    138                 } 
     87                        UploadWorker.ReportProgress(baseProgress + progressPerReport / stepsPerReport + 
     88                            progress.ProgressPercentage * progressPerReport / 100 / stepsPerReport, 
     89                            S._("Uploading Report {0}: {1}%", 
     90                                reports[i].Name, progress.ProgressPercentage)); 
     91                    }); 
    13992            } 
    14093        } 
     
    163116 
    164117        private IList<BlackBoxReport> Reports; 
     118    } 
     119 
     120    class BlackBoxReportUploader 
     121    { 
     122        /// <summary> 
     123        /// Constructor. 
     124        /// </summary> 
     125        /// <param name="report">The report to upload.</param> 
     126        public BlackBoxReportUploader(BlackBoxReport report) 
     127        { 
     128            Report = report; 
     129            if (!Directory.Exists(UploadTempDir)) 
     130                Directory.CreateDirectory(UploadTempDir); 
     131 
     132            ReportBaseName =  Path.Combine(UploadTempDir, Report.Name); 
     133        } 
     134 
     135        /// <summary> 
     136        /// Verifies the stack trace against the server to see if the report is new. 
     137        /// </summary> 
     138        /// <returns>True if the report is new; false otherwise</returns> 
     139        public bool ReportIsNew() 
     140        { 
     141            throw new NotImplementedException(); 
     142        } 
     143 
     144        public void Compress(ProgressChangedEventHandler progressChanged) 
     145        { 
     146            using (FileStream archiveStream = new FileStream(ReportBaseName + ".tar", 
     147                    FileMode.Create, FileAccess.Write)) 
     148            { 
     149                //Add the report into a tar file 
     150                TarArchive archive = TarArchive.CreateOutputTarArchive(archiveStream); 
     151                foreach (FileInfo file in Report.Files) 
     152                    archive.WriteEntry(TarEntry.CreateEntryFromFile(file.FullName), false); 
     153                archive.Close(); 
     154            } 
     155 
     156            using (FileStream bzipFile = new FileStream(ReportBaseName + ".tbz", 
     157                FileMode.Create)) 
     158            using (FileStream tarStream = new FileStream(ReportBaseName + ".tar", 
     159                FileMode.Open, FileAccess.Read, FileShare.Read, 262144, FileOptions.DeleteOnClose)) 
     160            using (BZip2OutputStream bzipStream = new BZip2OutputStream(bzipFile, 262144)) 
     161            { 
     162                //Compress the tar file 
     163                int lastRead = 0; 
     164                byte[] buffer = new byte[524288]; 
     165                while ((lastRead = tarStream.Read(buffer, 0, buffer.Length)) != 0) 
     166                { 
     167                    bzipStream.Write(buffer, 0, lastRead); 
     168                    progressChanged(this, new ProgressChangedEventArgs( 
     169                        (int)(tarStream.Position * 100 / tarStream.Length), null)); 
     170                } 
     171            } 
     172        } 
     173 
     174        public void Upload(ProgressChangedEventHandler progressChanged) 
     175        { 
     176            using (FileStream bzipFile = new FileStream(ReportBaseName + ".tbz", 
     177                FileMode.Open, FileAccess.Read, FileShare.Read, 131072, FileOptions.DeleteOnClose)) 
     178            using (Stream logFile = Report.DebugLog) 
     179            { 
     180                //Build the POST request 
     181                MultipartFormDataBuilder builder = new MultipartFormDataBuilder(); 
     182                builder.AddPart(new FormFileField("CrashReport", "Report.tbz", bzipFile)); 
     183                builder.AddPart(new FormFileField("DebugLog", "Debug.log", logFile)); 
     184 
     185                //Upload the POST request 
     186                Uri blackBoxServer = new Uri("http://eraser.heidi.ie/BlackBox/upload.php"); 
     187                WebRequest reportRequest = HttpWebRequest.Create(blackBoxServer); 
     188                reportRequest.ContentType = "multipart/form-data; boundary=" + builder.Boundary; 
     189                reportRequest.Method = "POST"; 
     190                using (Stream formStream = builder.Stream) 
     191                { 
     192                    reportRequest.ContentLength = formStream.Length; 
     193                    using (Stream requestStream = reportRequest.GetRequestStream()) 
     194                    { 
     195                        int lastRead = 0; 
     196                        byte[] buffer = new byte[524288]; 
     197                        while ((lastRead = formStream.Read(buffer, 0, buffer.Length)) != 0) 
     198                        { 
     199                            requestStream.Write(buffer, 0, lastRead); 
     200                            progressChanged(this, new ProgressChangedEventArgs( 
     201                                (int)(formStream.Position * 100 / formStream.Length), null)); 
     202                        } 
     203                    } 
     204                } 
     205 
     206                HttpWebResponse response = reportRequest.GetResponse() as HttpWebResponse; 
     207                if (response.StatusCode != HttpStatusCode.OK) 
     208                { 
     209                    using (Stream responseStream = response.GetResponseStream()) 
     210                    using (TextReader reader = new StreamReader(responseStream)) 
     211                        throw new ArgumentException(reader.ReadToEnd()); 
     212                } 
     213            } 
     214        } 
     215 
     216        /// <summary> 
     217        /// The path to where the temporary files are stored before uploading. 
     218        /// </summary> 
     219        private static readonly string UploadTempDir = 
     220            Path.Combine(Path.GetTempPath(), "Eraser Crash Reports"); 
     221 
     222        /// <summary> 
     223        /// The report being uploaded. 
     224        /// </summary> 
     225        private BlackBoxReport Report; 
     226 
     227        /// <summary> 
     228        /// The base name of the report. 
     229        /// </summary> 
     230        private readonly string ReportBaseName; 
    165231    } 
    166232 
  • branches/eraser6/BlackBox/Eraser/Strings.NL.resx

    r1394 r1397  
    139139    <value>(Untranslated)</value> 
    140140  </data> 
    141   <data name="Compressing Report {0}" xml:space="preserve"> 
    142     <value>(Untranslated)</value> 
    143   </data> 
    144   <data name="Uploading Report {0}" xml:space="preserve"> 
     141  <data name="Checking for status of report {0}..." xml:space="preserve"> 
     142    <value>(Untranslated)</value> 
     143  </data> 
     144  <data name="Compressing Report {0}: {1}%" xml:space="preserve"> 
     145    <value>(Untranslated)</value> 
     146  </data> 
     147  <data name="Uploading Report {0}: {1}%" xml:space="preserve"> 
    145148    <value>(Untranslated)</value> 
    146149  </data> 
  • branches/eraser6/BlackBox/Eraser/Strings.en.resx

    r1394 r1397  
    139139    <value>Please help us to continue developing Eraser - donate some coffee!</value> 
    140140  </data> 
    141   <data name="Compressing Report {0}" xml:space="preserve"> 
    142     <value>Compressing Report {0}</value> 
    143   </data> 
    144   <data name="Uploading Report {0}" xml:space="preserve"> 
    145     <value>Uploading Report {0}</value> 
     141  <data name="Checking for status of report {0}..." xml:space="preserve"> 
     142    <value>Checking for status of report {0}...</value> 
     143  </data> 
     144  <data name="Compressing Report {0}: {1}%" xml:space="preserve"> 
     145    <value>Compressing Report {0}: {1}%</value> 
     146  </data> 
     147  <data name="Uploading Report {0}: {1}%" xml:space="preserve"> 
     148    <value>Uploading Report {0}: {1}%</value> 
    146149  </data> 
    147150  <data name="Close" xml:space="preserve"> 
  • branches/eraser6/BlackBox/Eraser/Strings.it.resx

    r1394 r1397  
    139139    <value>Aiutateci a continuare lo sviluppo di Eraser - offriteci un caffè!</value> 
    140140  </data> 
    141   <data name="Compressing Report {0}" xml:space="preserve"> 
     141  <data name="Checking for status of report {0}..." xml:space="preserve"> 
    142142    <value>(Untranslated)</value> 
    143143  </data> 
    144   <data name="Uploading Report {0}" xml:space="preserve"> 
     144  <data name="Compressing Report {0}: {1}%" xml:space="preserve"> 
     145    <value>(Untranslated)</value> 
     146  </data> 
     147  <data name="Uploading Report {0}: {1}%" xml:space="preserve"> 
    145148    <value>(Untranslated)</value> 
    146149  </data> 
  • branches/eraser6/BlackBox/Eraser/Strings.resx

    r1394 r1397  
    139139    <value>Please help us to continue developing Eraser - donate some coffee!</value> 
    140140  </data> 
    141   <data name="Compressing Report {0}" xml:space="preserve"> 
    142     <value>Compressing Report {0}</value> 
    143   </data> 
    144   <data name="Uploading Report {0}" xml:space="preserve"> 
    145     <value>Uploading Report {0}</value> 
     141  <data name="Checking for status of report {0}..." xml:space="preserve"> 
     142    <value>Checking for status of report {0}...</value> 
     143  </data> 
     144  <data name="Compressing Report {0}: {1}%" xml:space="preserve"> 
     145    <value>Compressing Report {0}: {1}%</value> 
     146  </data> 
     147  <data name="Uploading Report {0}: {1}%" xml:space="preserve"> 
     148    <value>Uploading Report {0}: {1}%</value> 
    146149  </data> 
    147150  <data name="Close" xml:space="preserve"> 
Note: See TracChangeset for help on using the changeset viewer.