예제 #1
0
        public static byte[] add_custom_cover(Guid applicationId, Guid currentUserId, byte[] pdfContent, Guid coverId, Guid ownerNodeId)
        {
            DocFileInfo pdfCover = null;

            pdfCover = DocumentsController.get_file(applicationId, coverId);
            if (pdfCover == null || string.IsNullOrEmpty(pdfCover.Extension) || pdfCover.Extension.ToLower() != "pdf")
            {
                pdfCover = null;
            }

            byte[] coverContent = pdfContent == null ? null : pdfCover.toByteArray(applicationId);

            if (coverContent == null || coverContent.Length == 0)
            {
                return(pdfContent);
            }

            Dictionary <string, string> dic = CNAPI.get_replacement_dictionary(applicationId, currentUserId, ownerNodeId, true);

            List <FormElement> tempElems = dic.Keys.ToList().Select(key => new FormElement()
            {
                Name      = key,
                Type      = FormElementTypes.Text,
                TextValue = dic[key]
            }).ToList();

            byte[] cover = PDFTemplates.fill_template(coverContent, tempElems);

            return(PDFUtil.merge_documents(new List <object>()
            {
                cover, pdfContent
            }));
        }
예제 #2
0
        protected void send_file(DocFileInfo file, bool logNeeded, bool addPDFCover = false, bool addPDFFooter = false,
                                 Guid?coverId = null, string pdfPassword = null, string contentType = null, bool isAttachment = true)
        {
            byte[] fileContent = file.toByteArray(paramsContainer.ApplicationID);

            if (fileContent.Length == 0)
            {
                send_empty_response();
                return;
            }

            //Save Log
            if (logNeeded && paramsContainer.CurrentUserID.HasValue)
            {
                LogController.save_log(paramsContainer.Tenant.Id, new Log()
                {
                    UserID           = paramsContainer.CurrentUserID,
                    Date             = DateTime.Now,
                    HostAddress      = PublicMethods.get_client_ip(HttpContext.Current),
                    HostName         = PublicMethods.get_client_host_name(HttpContext.Current),
                    Action           = Modules.Log.Action.Download,
                    SubjectID        = file.FileID,
                    Info             = file.toJson(paramsContainer.Tenant.Id),
                    ModuleIdentifier = ModuleIdentifier.DCT
                });
            }
            //end of Save Log

            if (file.Extension.ToLower() == "pdf")
            {
                addPDFCover = addPDFCover && file.OwnerNodeID.HasValue && coverId.HasValue &&
                              paramsContainer.ApplicationID.HasValue && paramsContainer.CurrentUserID.HasValue;

                if (addPDFFooter || addPDFCover)
                {
                    bool invalidPassword = false;

                    fileContent = PDFUtil.get_pdf_content(paramsContainer.Tenant.Id, fileContent, pdfPassword, ref invalidPassword);

                    if (invalidPassword)
                    {
                        string responseText = "{\"InvalidPassword\":" + true.ToString().ToLower() + "}";
                        paramsContainer.return_response(ref responseText);
                        return;
                    }
                }

                if (addPDFFooter)
                {
                    User currentUser = !paramsContainer.CurrentUserID.HasValue ? null :
                                       UsersController.get_user(paramsContainer.Tenant.Id, paramsContainer.CurrentUserID.Value);

                    if (currentUser == null)
                    {
                        currentUser = new User()
                        {
                            UserID    = Guid.NewGuid(),
                            UserName  = "******",
                            FirstName = "[anonymous]",
                            LastName  = "[anonymous]"
                        };
                    }

                    DownloadedFileMeta meta = new DownloadedFileMeta(PublicMethods.get_client_ip(HttpContext.Current),
                                                                     currentUser.UserName, currentUser.FirstName, currentUser.LastName, null);

                    fileContent = PDFTemplates.append_footer(fileContent, meta.toString());
                }

                if (addPDFCover)
                {
                    fileContent = Wiki2PDF.add_custom_cover(paramsContainer.Tenant.Id,
                                                            paramsContainer.CurrentUserID.Value, fileContent, coverId.Value, file.OwnerNodeID.Value);
                }

                if (!string.IsNullOrEmpty(pdfPassword))
                {
                    fileContent = PDFUtil.set_password(fileContent, pdfPassword);
                }
            }

            string retFileName = file.FileName + (string.IsNullOrEmpty(file.Extension) ? string.Empty : "." + file.Extension);

            paramsContainer.file_response(fileContent, retFileName, contentType: contentType, isAttachment: isAttachment);
        }