예제 #1
0
        public ActionResult InsertSignatrue(string signatureBase64, string fileName)
        {
            var statusResultInsertSignature = "error";
            var descErrorInsertSignature    = "";
            var signedFileName = "";

            try
            {
                if (signatureBase64 == null || signatureBase64.Trim().Length == 0)
                {
                    descErrorInsertSignature = "Signature is empty";
                    return(Json(new
                    {
                        statusResultInsertSignature = statusResultInsertSignature,
                        descErrorInsertSignature = descErrorInsertSignature,
                        signedFileName = signedFileName
                    }, JsonRequestBehavior.AllowGet));
                }
                if (fileName == null || fileName.Trim().Length == 0 || fileName.IndexOf(".pdf") == -1)
                {
                    descErrorInsertSignature = "File Name is empty";
                    return(Json(new
                    {
                        statusResultInsertSignature = statusResultInsertSignature,
                        descErrorInsertSignature = descErrorInsertSignature,
                        signedFileName = signedFileName
                    }, JsonRequestBehavior.AllowGet));
                }

                byte[] signByteArray = Convert.FromBase64String(signatureBase64);
                if (signByteArray == null || signByteArray.Length == 0)
                {
                    descErrorInsertSignature = "Định dạng chữ ký không hợp lệ";
                    return(Json(new
                    {
                        statusResultInsertSignature = statusResultInsertSignature,
                        descErrorInsertSignature = descErrorInsertSignature,
                        signedFileName = signedFileName
                    }, JsonRequestBehavior.AllowGet));
                }

                var session = System.Web.HttpContext.Current.Session;

                if (session["pdfSig"] != null)
                {
                    try
                    {
                        pdfSig = session["pdfSig"] as SignPdfFile;
                    }
                    catch (Exception ex)
                    {
                        Logger.Error($"{DateTime.Now.ToString("HH:mm:ss dd/MM/yyyy")} InsertSignatrue {ex.Message} {JsonConvert.SerializeObject(ex)}");
                        descErrorInsertSignature = "Không tìm thấy phiên làm việc";
                        return(Json(new
                        {
                            statusResultInsertSignature = statusResultInsertSignature,
                            descErrorInsertSignature = descErrorInsertSignature,
                            signedFileName = signedFileName
                        }, JsonRequestBehavior.AllowGet));
                    }
                }
                if (pdfSig == null)
                {
                    descErrorInsertSignature = "Không tìm thấy phiên làm việc";
                    return(Json(new
                    {
                        statusResultInsertSignature = statusResultInsertSignature,
                        descErrorInsertSignature = descErrorInsertSignature,
                        signedFileName = signedFileName
                    }, JsonRequestBehavior.AllowGet));
                }

                string HCC_FILE_PDF = GetUrlFileDefaut();

                string[] stringSeparators = new string[] { ".pdf" };
                String[] chainBase64      = fileName.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
                String   name             = fileName.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries)[0] + "_signed";
                String   ext = "pdf";

                bool exists = System.IO.File.Exists(Path.Combine(HCC_FILE_PDF, name + "." + ext));
                if (exists)
                {
                    int    index  = 1;
                    String name_2 = name + "_" + index + "." + "_signed_1";
                    String path   = Path.Combine(HCC_FILE_PDF, name_2 + "." + ext);
                    while (System.IO.File.Exists(path))
                    {
                        index++;
                        name_2 = name + "_" + index + "." + "_signed_2";
                        path   = Path.Combine(HCC_FILE_PDF, name_2 + "." + ext);
                    }
                    name = name_2;
                }

                TimestampConfig timestampConfig = new TimestampConfig();
                timestampConfig.UseTimestamp = false;
                if (!pdfSig.insertSignature(signatureBase64, Path.Combine(HCC_FILE_PDF, name + "." + ext), timestampConfig))
                {
                    descErrorInsertSignature = "Insert signature into file fail.";
                    return(Json(new
                    {
                        statusResultInsertSignature = statusResultInsertSignature,
                        descErrorInsertSignature = descErrorInsertSignature,
                        signedFileName = signedFileName
                    }, JsonRequestBehavior.AllowGet));
                }
                else
                {
                    #region lưu đường dẫn file rác
                    var filecu = _fileKyRepos.FirstOrDefault(x => x.DuongDanTep == fileName);
                    if (filecu != null)
                    {
                        filecu.DaSuDung = false;
                        _fileKyRepos.Update(filecu);

                        #region lưu đường dẫn file đã ký
                        var fileKy = new LogFileKy
                        {
                            HoSoId         = filecu.HoSoId,
                            ThuTucId       = filecu.ThuTucId,
                            DuongDanTep    = name + "." + ext,
                            LoaiTepDinhKem = filecu.LoaiTepDinhKem,
                            DaSuDung       = true
                        };
                        _fileKyRepos.Insert(fileKy);
                        #endregion
                    }
                    #endregion

                    statusResultInsertSignature = "success";
                    signedFileName = name + "." + ext;
                }

                return(Json(new
                {
                    statusResultInsertSignature = statusResultInsertSignature,
                    descErrorInsertSignature = descErrorInsertSignature,
                    signedFileName = signedFileName
                }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                Logger.Error($"{DateTime.Now.ToString("HH:mm:ss dd/MM/yyyy")} InsertSignatrue {ex.Message} {JsonConvert.SerializeObject(ex)}");
                descErrorInsertSignature = "Lỗi trong quá trình xử lý";
                return(Json(new
                {
                    statusResultInsertSignature = statusResultInsertSignature,
                    descErrorInsertSignature = descErrorInsertSignature,
                    signedFileName = signedFileName
                }, JsonRequestBehavior.AllowGet));
            }
        }
예제 #2
0
        public ActionResult InsertPDFHoSo(long hoSoId)
        {
            try
            {
                var    hoSo         = _hoSoRepos.Get(hoSoId);
                string HCC_FILE_PDF = GetUrlFileDefaut();
                var    xls          = FileHoSo(hoSoId);
                using (FlexCel.Render.FlexCelPdfExport pdf = new FlexCel.Render.FlexCelPdfExport())
                {
                    pdf.Workbook = xls;

                    using (MemoryStream ms = new MemoryStream())
                    {
                        pdf.BeginExport(ms);
                        pdf.ExportAllVisibleSheets(false, "PDF");
                        pdf.EndExport();
                        ms.Position = 0;
                        var file      = File(ms.ToArray(), "application/pdf");
                        var outStream = new MemoryStream();
                        using (iTextSharp.text.Document document = new iTextSharp.text.Document())
                            using (PdfCopy copy = new PdfCopy(document, outStream))
                            {
                                document.Open();
                                copy.AddDocument(new PdfReader((file.FileContents)));
                            }

                        String ext = "pdf";

                        var doanhnghiep = _doanhNghiepRepos.GetAll().FirstOrDefault(t => t.Id == hoSo.DoanhNghiepId);

                        //lấy tỉnh để tạo thêm thư mục
                        var strTinh = "unknown";
                        if (doanhnghiep != null)
                        {
                            strTinh = !string.IsNullOrEmpty(doanhnghiep.Tinh) ? RemoveUnicode(doanhnghiep.Tinh).ToLower().Trim().Replace(" ", "-") : "unknown";
                        }

                        var maSoThue      = hoSo.MaSoThue;
                        var strThuMucHoSo = "HOSO_0";
                        if (!string.IsNullOrEmpty(hoSo.StrThuMucHoSo))
                        {
                            strThuMucHoSo = hoSo.StrThuMucHoSo;
                        }
                        var folder = $"{MA_THU_TUC.THU_TUC_98}\\{strTinh}\\{maSoThue}\\{hoSo.StrThuMucHoSo}\\HoSo\\";

                        if (!Directory.Exists(Path.Combine(HCC_FILE_PDF, folder)))
                        {
                            Directory.CreateDirectory(Path.Combine(HCC_FILE_PDF, folder));
                        }

                        var filename = Path.Combine(folder, @"HoSo_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + "_" + Guid.NewGuid() + "." + ext);

                        var name = (Path.Combine(HCC_FILE_PDF, filename));
                        System.IO.File.WriteAllBytes(name, outStream.ToArray());

                        #region lưu đường dẫn file rác
                        var fileKy = new LogFileKy
                        {
                            HoSoId         = hoSo.Id,
                            ThuTucId       = hoSo.ThuTucId,
                            DuongDanTep    = filename,
                            LoaiTepDinhKem = (int)CommonENum.LOAI_FILE_KY.DON_DANG_KY,
                            DaSuDung       = false
                        };
                        _fileKyRepos.Insert(fileKy);
                        #endregion

                        return(Json(new
                        {
                            fileName = filename
                        }, JsonRequestBehavior.AllowGet));
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Fatal(ex.Message);
                return(Json(new
                {
                    fileName = ""
                }, JsonRequestBehavior.AllowGet));
            }
        }