Exemplo n.º 1
0
 public DocumentServiceTrackerHelper(
     SecurityContext securityContext,
     UserManager userManager,
     TenantManager tenantManager,
     FilesLinkUtility filesLinkUtility,
     EmailValidationKeyProvider emailValidationKeyProvider,
     BaseCommonLinkUtility baseCommonLinkUtility,
     SocketManager socketManager,
     GlobalStore globalStore,
     DisplayUserSettingsHelper displayUserSettingsHelper,
     IDaoFactory daoFactory,
     IOptionsMonitor <ILog> options,
     DocumentServiceHelper documentServiceHelper,
     EntryManager entryManager,
     FileShareLink fileShareLink,
     FilesMessageService filesMessageService,
     DocumentServiceConnector documentServiceConnector,
     NotifyClient notifyClient,
     MailMergeTaskRunner mailMergeTaskRunner,
     FileTrackerHelper fileTracker)
 {
     SecurityContext            = securityContext;
     UserManager                = userManager;
     TenantManager              = tenantManager;
     FilesLinkUtility           = filesLinkUtility;
     EmailValidationKeyProvider = emailValidationKeyProvider;
     BaseCommonLinkUtility      = baseCommonLinkUtility;
     SocketManager              = socketManager;
     GlobalStore                = globalStore;
     DisplayUserSettingsHelper  = displayUserSettingsHelper;
     DaoFactory               = daoFactory;
     DocumentServiceHelper    = documentServiceHelper;
     EntryManager             = entryManager;
     FileShareLink            = fileShareLink;
     FilesMessageService      = filesMessageService;
     DocumentServiceConnector = documentServiceConnector;
     NotifyClient             = notifyClient;
     MailMergeTaskRunner      = mailMergeTaskRunner;
     FileTracker              = fileTracker;
     Logger = options.CurrentValue;
 }
Exemplo n.º 2
0
        private TrackResponse ProcessMailMerge <T>(T fileId, TrackerData fileData)
        {
            if (fileData.Users == null || fileData.Users.Count == 0 || !Guid.TryParse(fileData.Users[0], out var userId))
            {
                userId = FileTracker.GetEditingBy(fileId).FirstOrDefault();
            }

            string saveMessage;

            try
            {
                SecurityContext.AuthenticateMe(userId);

                var user    = UserManager.GetUsers(userId);
                var culture = string.IsNullOrEmpty(user.CultureName) ? TenantManager.GetCurrentTenant().GetCulture() : CultureInfo.GetCultureInfo(user.CultureName);
                Thread.CurrentThread.CurrentCulture   = culture;
                Thread.CurrentThread.CurrentUICulture = culture;

                if (string.IsNullOrEmpty(fileData.Url))
                {
                    throw new ArgumentException("emptry url");
                }

                if (fileData.MailMerge == null)
                {
                    throw new ArgumentException("MailMerge is null");
                }

                var    message = fileData.MailMerge.Message;
                Stream attach  = null;
                switch (fileData.MailMerge.Type)
                {
                case MailMergeType.AttachDocx:
                case MailMergeType.AttachPdf:
                    var downloadRequest = (HttpWebRequest)WebRequest.Create(DocumentServiceConnector.ReplaceDocumentAdress(fileData.Url));

                    // hack. http://ubuntuforums.org/showthread.php?t=1841740
                    if (WorkContext.IsMono)
                    {
                        ServicePointManager.ServerCertificateValidationCallback += (s, ce, ca, p) => true;
                    }

                    using (var downloadStream = new ResponseStream(downloadRequest.GetResponse()))
                    {
                        const int bufferSize = 2048;
                        var       buffer     = new byte[bufferSize];
                        int       readed;
                        attach = new MemoryStream();
                        while ((readed = downloadStream.Read(buffer, 0, bufferSize)) > 0)
                        {
                            attach.Write(buffer, 0, readed);
                        }
                        attach.Position = 0;
                    }

                    if (string.IsNullOrEmpty(fileData.MailMerge.Title))
                    {
                        fileData.MailMerge.Title = "Attach";
                    }

                    var attachExt = fileData.MailMerge.Type == MailMergeType.AttachDocx ? ".docx" : ".pdf";
                    var curExt    = FileUtility.GetFileExtension(fileData.MailMerge.Title);
                    if (curExt != attachExt)
                    {
                        fileData.MailMerge.Title += attachExt;
                    }

                    break;

                case MailMergeType.Html:
                    var httpWebRequest = (HttpWebRequest)WebRequest.Create(DocumentServiceConnector.ReplaceDocumentAdress(fileData.Url));

                    // hack. http://ubuntuforums.org/showthread.php?t=1841740
                    if (WorkContext.IsMono)
                    {
                        ServicePointManager.ServerCertificateValidationCallback += (s, ce, ca, p) => true;
                    }

                    using (var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
                        using (var stream = httpWebResponse.GetResponseStream())
                            if (stream != null)
                            {
                                using (var reader = new StreamReader(stream, Encoding.GetEncoding(Encoding.UTF8.WebName)))
                                {
                                    message = reader.ReadToEnd();
                                }
                            }
                    break;
                }

                using (var mailMergeTask =
                           new MailMergeTask
                {
                    From = fileData.MailMerge.From,
                    Subject = fileData.MailMerge.Subject,
                    To = fileData.MailMerge.To,
                    Message = message,
                    AttachTitle = fileData.MailMerge.Title,
                    Attach = attach
                })
                {
                    var response = MailMergeTaskRunner.Run(mailMergeTask);
                    Logger.InfoFormat("DocService mailMerge {0}/{1} send: {2}",
                                      fileData.MailMerge.RecordIndex + 1, fileData.MailMerge.RecordCount, response);
                }
                saveMessage = null;
            }
            catch (Exception ex)
            {
                Logger.Error(
                    string.Format("DocService mailMerge{0} error: userId - {1}, url - {2}",
                                  (fileData.MailMerge == null ? "" : " " + fileData.MailMerge.RecordIndex + "/" + fileData.MailMerge.RecordCount),
                                  userId, fileData.Url),
                    ex);
                saveMessage = ex.Message;
            }

            if (fileData.MailMerge != null &&
                fileData.MailMerge.RecordIndex == fileData.MailMerge.RecordCount - 1)
            {
                var errorCount = fileData.MailMerge.RecordErrorCount;
                if (!string.IsNullOrEmpty(saveMessage))
                {
                    errorCount++;
                }

                NotifyClient.SendMailMergeEnd(userId, fileData.MailMerge.RecordCount, errorCount);
            }

            return(new TrackResponse {
                Message = saveMessage
            });
        }