Exemple #1
0
        public bool SendStudentCertificate(StudentCertificateDTO token, string certificateBody, out string error)
        {
            try
            {
                var strKey = string.Format("{0}{3}{1}{3}{2}", token.StudentCertificateId, token.StudentInfo.UserId, token.CertificateId, KEY_SEPARATOR);
                var key    = _encryptionServices.EncryptText(strKey);
                token.Key = Uri.EscapeDataString(key);

                token.OnlineCertificateUrl = $"{"https:"}//{new Uri(Utils.GetKeyValue("baseUrl")).Authority}/Widget/{"User"}/{"Certificate"}?key={Uri.EscapeDataString(key)}";


                long emailId;
                _emailServices.SaveStudentCertificateMessage(token, certificateBody, out emailId, out error);

                if (emailId < 0)
                {
                    return(false);
                }

                var pdfConverter = new StandardPdfRenderer();

                // var fileName = $"Certificate_{token.StudentInfo.UserId}_{ShortGuid.NewGuid()}.pdf";
                //				var virtualUrl = "~/Certificates/" + fileName;
                //				var path       = HttpContext.Current.Server.MapPath(virtualUrl);
                //
                //				//var bytes = pdfConverter.Html2Pdf(certificateBody);
                //				//File.WriteAllBytes(path, bytes);
                //
                //                if (File.Exists(path)) TryDeleteFile(path);
                //
                //                var pdfDoc = pdfConverter.Html2PdfDoc(certificateBody, PdfPageSize.A4);
                //
                //				pdfDoc.WriteToFile(path);
                //
                //                pdfDoc.Close();
                //
                //                pdfDoc.ReleaseSourceDoc();

                //                var sended = _amazonEmailWrapper.SendEmailWithAttachment(emailId, path, out error);

                var outstream = pdfConverter.Html2PdfStream(certificateBody);
//
//                var filePath = $"{FileEnums.eFileOwners.Student}/Certificates/{token.StudentInfo.UserId}/{fileName}";
//
//                _s3Wrapper.Upload(filePath, "application/octet-stream", outstream, out error);
//
//                // var fullCertificatePath = $"{S3_ROOT_URL}{S3_BUCKET_NAME}/{filePath}";
//
//                outstream = pdfConverter.Html2PdfStream(certificateBody);

                var sended = _amazonEmailWrapper.SendEmailWithAttachment(emailId, outstream, out error);

                if (sended)
                {
                    var entity = StudentCertificatesRepository.GetById(token.StudentCertificateId);

                    entity.UpdateCertificateSendDate();

                    StudentCertificatesRepository.UnitOfWork.CommitAndRefreshChanges();
                }
                outstream.Close();
                outstream.Dispose();

                return(sended);
            }
            catch (Exception ex)
            {
                Logger.Error("Send Student Certificate for course " + token.CourseId, CurrentUserId, ex, CommonEnums.LoggerObjectTypes.Certificate);
                error = FormatError(ex);
                return(false);
            }
        }