//========================================================================================= // //========================================================================================= private void OnUploadWorkerDoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; using (UploadHelper helper = (UploadHelper)e.Argument) { bool allskip = false; bool allfile = false; try { helper.beginTime = DateTime.Now; helper.numberOfFiles = this.filelistGridView.RowCount; for (int i = 0; i < helper.numberOfFiles; i++) { AppMediator.SINGLETON.Ping(); // index, vname, vsize, vtype, rname, fpath, status DataRow uprow = this.filelistGridView.GetDataRow(i); helper.fileName = Path.Combine(uprow["fpath"].ToString(), uprow["rname"].ToString()); FileInfo fileInfo = new FileInfo(helper.fileName); if (fileInfo.Length == 0) { continue; } DataSet fileSet = null; bool uploadOk = true; bool exists = false; string fileId = String.Empty; helper.fileNumber = i + 1; helper.fileLength = fileInfo.Length; // guid, companyid, fileid, ftype, vsize, vtype, rname, title, description, wdate helper.InfoRow["rname"] = fileInfo.Name; helper.InfoRow["vsize"] = helper.fileLength; helper.InfoRow["vtype"] = helper.GetMIMEType(fileInfo.Extension); if (AppMediator.SINGLETON.PrepareUploadFile(helper.InfoSet, out fileId, out helper.uploadDay, out helper.fileGuid, out helper.maxChunkSize) == false) { fileSet = AppMediator.SINGLETON.GetFileInfo(fileId); if (allskip == true || fileSet == null) { helper.totalFileLength -= helper.fileLength; helper.statusMessage = "skipped"; worker.ReportProgress(helper.percentProgrss, helper as object); continue; } exists = true; if (allfile == false && fileSet != null) { uploadOk = false; DataRow filerow = fileSet.Tables[0].Rows[0]; if (filerow["cmodify"].ToString() == "T" && filerow["cfile"].ToString() == "T") { TimeSpan span = DateTime.Now - helper.beginTime; ConfirmDialog dialog = new ConfirmDialog(helper.MainBox, fileSet, fileInfo); switch (dialog.ShowDialog()) { // '예' // 현재 파일 Upload case DialogResult.Yes: uploadOk = true; break; // 모두 '예' // 나머지 모든 파일 Upload case DialogResult.OK: uploadOk = true; allfile = true; break; // '아니오' case DialogResult.No: break; // 모두 '아니오' // 나머지 모든 파일을 Skip 한다. case DialogResult.Cancel: uploadOk = false; allskip = true; break; // '취소' case DialogResult.Ignore: e.Cancel = true; break; } helper.beginTime = DateTime.Now - span; if (e.Cancel == true) { break; } } } } if (uploadOk == false) { AppMediator.SINGLETON.FailureCloseUploadFile(helper.fileGuid); helper.totalFileLength -= helper.fileLength; helper.statusMessage = "canceled"; worker.ReportProgress(helper.percentProgrss, helper as object); continue; } string title = fileInfo.Name; string contents = String.Empty; if (helper.numberOfFiles == 1) { if (this.SingleFileUpload(fileInfo.Name, ref title, ref contents) == false) { e.Cancel = true; break; } } helper.InfoRow["title"] = title; helper.InfoRow["description"] = contents; using (FileStream stream = new FileStream(helper.fileName, FileMode.Open, FileAccess.Read)) { int iterations = 0; helper.fileLength = stream.Length; helper.fileName = stream.Name; helper.fileWriteSize = 0; int chunksize = 16 * 1024; byte[] buffer = new byte[chunksize]; int bytesRead = stream.Read(buffer, 0, chunksize); while (bytesRead > 0) { if (worker.CancellationPending == true) { break; } try { if (AppMediator.SINGLETON.UploadFile(helper.fileGuid, buffer, helper.fileWriteSize, bytesRead) == true) { if (iterations == UploadHelper.AVERAGE_COUNT + 1) { long timeForInitChunks = (long)DateTime.Now.Subtract(helper.beginTime).TotalMilliseconds; long averageChunkTime = Math.Max(1, timeForInitChunks / UploadHelper.AVERAGE_COUNT); chunksize = (int)Math.Min(helper.maxChunkSize, chunksize * UploadHelper.PREFERRED_TRANSFER_DURATION / averageChunkTime); Array.Resize <byte>(ref buffer, chunksize); } helper.fileWriteSize += bytesRead; helper.totalWriteSize += bytesRead; helper.statusMessage = "transferring"; worker.ReportProgress(helper.percentProgrss, helper as object); } else { throw new Exception("server side error!"); } } catch (Exception ex) { if (helper.NumRetries++ < helper.MaxRetries) { // rewind the filestream and keep trying stream.Position -= bytesRead; } else { stream.Close(); throw new Exception(String.Format("Error occurred during upload, too many retries. \n{0}", ex.ToString())); } } bytesRead = stream.Read(buffer, 0, chunksize); iterations++; } stream.Close(); if (worker.CancellationPending == true) { break; } if (helper.CheckFileHash() == true) { helper.InfoRow["guid"] = Path.GetFileNameWithoutExtension(helper.fileGuid); if (exists == true && fileSet != null) { DataRow filerow = fileSet.Tables[0].Rows[0]; string guid = filerow["guid"].ToString(); string fileid = filerow["fileid"].ToString(); DateTime wdate = Convert.ToDateTime(filerow["wdate"].ToString()); AppMediator.SINGLETON.DeleteFile(guid, fileid, wdate); } AppMediator.SINGLETON.SuccessCloseUploadFile(helper.uploadDay, helper.fileGuid, helper.InfoSet); helper.statusMessage = "completed"; worker.ReportProgress(helper.percentProgrss, helper as object); } } } if (worker.CancellationPending == false && e.Cancel == false) { e.Result = helper as object; e.Cancel = false; return; } } catch (Exception ex) { OFileLog.SNG.WriteLog(ex.ToString()); string error = AppMediator.SINGLETON.ResourceHelper.TranslateWord("작업 중 오류가 발생하였습니다.") + Environment.NewLine + ex.Message; MessageBox.Show(error); } if (String.IsNullOrEmpty(helper.fileGuid) == false) { AppMediator.SINGLETON.FailureCloseUploadFile(helper.fileGuid); } helper.statusMessage = "stopped"; worker.ReportProgress(helper.percentProgrss, helper as object); e.Cancel = true; return; } }