Esempio n. 1
0
        public HttpResponseMessage Download()
        {
            var zippedData       = Request.Content.ReadAsByteArrayAsync().Result;
            var decompressString = Encoding.UTF8.GetString(Decompress(zippedData));
            var obj            = JObject.Parse(decompressString);
            var p              = Convert.ToString(obj["p"]);
            var uid            = Convert.ToString(obj["uid"]);
            var file           = Convert.ToString(obj["file"]);
            var gridJsWorkbook = new GridJsWorkbook();
            var folderName     = Guid.NewGuid().ToString();
            var outPath        = AppSettings.OutputDirectory + folderName;
            var fullPath       = outPath + "/" + file;

            var gridInterruptMonitor = new GridInterruptMonitor();

            gridJsWorkbook.SetInterruptMonitorForSave(gridInterruptMonitor);
            var thread = new Thread(GridInterruptMonitor);

            try
            {
                thread.Start(new object[] { gridInterruptMonitor, Api.Configuration.MillisecondsTimeout, file });
                gridJsWorkbook.MergeExcelFileFromJson(uid, p);
                Directory.CreateDirectory(outPath);
                gridJsWorkbook.SaveToExcelFile(fullPath);

                var response = new Response
                {
                    StatusCode = 200,
                    Status     = "OK",
                    FileName   = file,
                    FolderName = folderName
                };
                return(Request.CreateResponse(HttpStatusCode.OK, response));
            }
            catch (Exception e)
            {
                var exception = e.InnerException ?? e;
                var message   = $"{e.Message} | File = {file} | UId = {uid} | P = {p}";
                NLogger.LogError(App, "Download", message, folderName);

                var status = exception.Message;
                if (e is GridCellException gridCellException && gridCellException.Code == GridExceptionType.Interrupted)
                {
                    NLogger.LogError($"Editor Download=>{folderName}=>{AppSettings.ProcessingTimedout}");
                    status = AppSettings.ProcessingTimedout;
                }

                var response = new Response
                {
                    StatusCode = 500,
                    Status     = status,
                    Text       = "Editor Download"
                };
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, response));
            }
            finally
            {
                thread.Interrupt();
            }
        }
Esempio n. 2
0
        public HttpResponseMessage DetailJson(string file1, string file2, string folderName)
        {
            file1      = Uri.UnescapeDataString(file1);
            file2      = Uri.UnescapeDataString(file2);
            folderName = Uri.UnescapeDataString(folderName);

            var gridJsWorkbook1 = new GridJsWorkbook();
            var gridJsWorkbook2 = new GridJsWorkbook();

            var gridInterruptMonitor1 = new GridInterruptMonitor();

            gridJsWorkbook1.SetInterruptMonitorForLoad(gridInterruptMonitor1, Api.Configuration.MillisecondsTimeout);
            var thread1 = new Thread(GridInterruptMonitor);

            var gridInterruptMonitor2 = new GridInterruptMonitor();

            gridJsWorkbook2.SetInterruptMonitorForLoad(gridInterruptMonitor2, Api.Configuration.MillisecondsTimeout);
            var thread2 = new Thread(GridInterruptMonitor);

            try
            {
                thread1.Start(new object[] { gridInterruptMonitor1, Api.Configuration.MillisecondsTimeout, file1 });
                thread2.Start(new object[] { gridInterruptMonitor2, Api.Configuration.MillisecondsTimeout, file2 });
                var filename1 = File.Exists(AppSettings.WorkingDirectory + folderName + "/" + file1) ? AppSettings.WorkingDirectory + folderName + "/" + file1 : AppSettings.OutputDirectory + folderName + "/" + file1;
                var filename2 = File.Exists(AppSettings.WorkingDirectory + folderName + "/" + file2) ? AppSettings.WorkingDirectory + folderName + "/" + file2 : AppSettings.OutputDirectory + folderName + "/" + file2;

                var workbook1 = new Workbook(filename1);
                var workbook2 = new Workbook(filename2);

                var worksheets1 = workbook1.Worksheets;
                var worksheets2 = workbook2.Worksheets;

                var sheetCount = worksheets1.Count > worksheets2.Count ? worksheets2.Count : worksheets1.Count;

                for (var i = 0; i < sheetCount; i++)
                {
                    var cells1 = worksheets1[i].Cells;
                    var cells2 = worksheets2[i].Cells;

                    var maxRow    = cells1.MaxRow > cells2.MaxRow ? cells1.MaxRow : cells2.MaxRow;
                    var maxColumn = cells1.MaxColumn > cells2.MaxColumn ? cells1.MaxColumn : cells2.MaxColumn;
                    for (var row = 0; row <= maxRow; row++)
                    {
                        for (var column = 0; column <= maxColumn; column++)
                        {
                            var cell1 = cells1[row, column];
                            var cell2 = cells2[row, column];

                            if (cell1.DisplayStringValue.Equals(cell2.DisplayStringValue))
                            {
                                continue;
                            }

                            var style2 = cell2.GetStyle();
                            style2.Pattern         = BackgroundType.Solid;
                            style2.ForegroundColor = Color.FromArgb(255, 255, 0);
                            style2.BackgroundColor = Color.FromArgb(255, 255, 0);
                            cell2.SetStyle(style2);
                        }
                    }
                }

                workbook1.Save(filename1);
                workbook2.Save(filename2);

                if (!Directory.Exists(AppSettings.OutputDirectory + folderName))
                {
                    Directory.CreateDirectory(AppSettings.OutputDirectory + folderName);
                }

                var compareJson = new CompareJson();

                var isCompleted = Task.Run(() =>
                {
                    gridJsWorkbook1.ImportExcelFile(filename1);
                    compareJson.File1Json = gridJsWorkbook1.ExportToJson();

                    gridJsWorkbook2.ImportExcelFile(filename2);
                    compareJson.File2Json = gridJsWorkbook2.ExportToJson();
                }).Wait(Api.Configuration.MillisecondsTimeout);

                if (!isCompleted)
                {
                    NLogger.LogError($"Compare DetailJson {folderName}=>{AppSettings.ProcessingTimedout}");
                    throw new TimeoutException(AppSettings.ProcessingTimedout);
                }

                var result = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(compareJson.ToJson())
                };
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
                return(result);
            }
            catch (Exception e)
            {
                var exception = e.InnerException ?? e;
                var message   = $"{exception.Message} | File1 = {file1} | File2 = {file2}";
                NLogger.LogError(App, "DetailJson", message, folderName);

                var status = exception.Message;
                if (e is GridCellException gridCellException && gridCellException.Code == GridExceptionType.Interrupted)
                {
                    status = AppSettings.ProcessingTimedout;
                }

                var response = new Response
                {
                    StatusCode = 500,
                    Status     = status,
                    FolderName = folderName,
                    Text       = "Compare Detail"
                };
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, response));
            }
            finally
            {
                thread1.Interrupt();
                thread2.Interrupt();
            }
        }
Esempio n. 3
0
        public HttpResponseMessage DetailJson(string file, string folderName)
        {
            file       = Uri.UnescapeDataString(file);
            folderName = Uri.UnescapeDataString(folderName);
            var    filePath       = File.Exists(AppSettings.WorkingDirectory + folderName + "/" + file) ? AppSettings.WorkingDirectory + folderName + "/" + file : AppSettings.OutputDirectory + folderName + "/" + file;
            var    gridJsWorkbook = new GridJsWorkbook();
            string stringContent  = null;

            var gridInterruptMonitor = new GridInterruptMonitor();

            gridJsWorkbook.SetInterruptMonitorForLoad(gridInterruptMonitor, Api.Configuration.MillisecondsTimeout);
            var thread = new Thread(GridInterruptMonitor);

            try
            {
                thread.Start(new object[] { gridInterruptMonitor, Api.Configuration.MillisecondsTimeout, filePath });
                Directory.CreateDirectory(AppSettings.OutputDirectory + folderName);
                var isCompleted = Task.Run(() =>
                {
                    gridJsWorkbook.ImportExcelFile(filePath);
                    stringContent = gridJsWorkbook.ExportToJson();
                }).Wait(Api.Configuration.MillisecondsTimeout);

                if (!isCompleted || string.IsNullOrEmpty(stringContent))
                {
                    NLogger.LogError($"Editor DetailJson {folderName}=>{AppSettings.ProcessingTimedout}");
                    throw new TimeoutException(AppSettings.ProcessingTimedout);
                }

                var result = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(stringContent)
                };
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
                return(result);
            }
            catch (Exception e)
            {
                var exception = e.InnerException ?? e;
                var message   = $"{exception.Message} | File = {file}";
                NLogger.LogError(App, "DetailJson", message, folderName);

                var status = exception.Message;
                if (e is GridCellException gridCellException && gridCellException.Code == GridExceptionType.Interrupted)
                {
                    status = AppSettings.ProcessingTimedout;
                }

                var response = new Response
                {
                    StatusCode = 500,
                    Status     = status,
                    FolderName = folderName,
                    Text       = "Editor Detail"
                };
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, response));
            }
            finally
            {
                thread.Interrupt();
            }
        }