private async Task <NodeEntry> SaveAttachment(MimeEntity attachment, string parentId) { using (var memoryAttachment = new MemoryStream()) { if (attachment is MimePart) { ((MimePart)attachment).Content.DecodeTo(memoryAttachment); } else { ((MessagePart)attachment).Message.WriteTo(memoryAttachment); } var bytes = memoryAttachment.ToArray(); var Title = $"{IdGenerator.GenerateId()}{ Path.GetExtension(attachment.ContentDisposition?.FileName)}"; FormDataParam fileParamsAttachment = new FormDataParam(bytes, Title); string pid = await GenerateComponentPID(parentId); return(await _alfrescoHttp.CreateNode(EmailFolder.Entry.Id, fileParamsAttachment, ImmutableList <Parameter> .Empty .Add(new Parameter(AlfrescoNames.Headers.NodeType, "ssl:component", ParameterType.GetOrPost)) .Add(new Parameter(AlfrescoNames.ContentModel.Title, Title, ParameterType.GetOrPost)) .Add(new Parameter(PidProperty, pid, ParameterType.GetOrPost)) .Add(new Parameter("ssl:fileName", attachment.ContentDisposition?.FileName, ParameterType.GetOrPost)) .Add(new Parameter(HeaderNames.ContentType, "multipart/form-data", ParameterType.HttpHeader)) )); } }
public async Task <NodeEntry> ConvertToOutputFormat(string documentId, string componentId, string reason, string organization) { var nodeEntry = await _alfrescoHttpClient.GetNodeInfo(componentId, ImmutableList <Parameter> .Empty .Add(new Parameter(AlfrescoNames.Headers.Include, $"{AlfrescoNames.Includes.Properties}, {AlfrescoNames.Includes.Path}", ParameterType.QueryString))); var extension = Path.GetExtension(nodeEntry?.Entry?.Name); if (!_fileExtensions.Any(x => extension.Contains(x))) { return(await _alfrescoHttpClient.UpdateNode(componentId, new NodeBodyUpdate() .AddProperty(SpisumNames.Properties.FileIsInOutputFormat, "impossible"))); } var properties = nodeEntry?.Entry?.Properties?.As <JObject>().ToDictionary(); string pid = properties.GetNestedValueOrDefault(SpisumNames.Properties.Pid)?.ToString(); var componentPid = pid.Split('/'); FormDataParam pdf = null; if (nodeEntry?.Entry?.Content.MimeType != MediaTypeNames.Application.Pdf) { pdf = await _alfrescoHttpClient.GetThumbnailPdf(componentId, ImmutableList <Parameter> .Empty .Add(new Parameter("c", "force", ParameterType.QueryString))); } else { pdf = await _alfrescoHttpClient.NodeContent(componentId); } var data = await _pdfService.ConvertToPdfA2B(new MemoryStream(pdf.File)); if (_signerConfiguration.Base != null || _signerConfiguration.Url != null) { SealResponse signer = await _signerClient.Seal(data); await _signerService.CheckAndUpdateComponent(componentId, signer.Output); data = signer.Output; } await _componentService.UploadNewVersionComponent(documentId, componentId, data, Path.ChangeExtension(properties.GetNestedValueOrDefault(SpisumNames.Properties.FileName)?.ToString(), ".pdf"), MediaTypeNames.Application.Pdf); return(await _alfrescoHttpClient.UpdateNode(componentId, new NodeBodyUpdate() .AddProperty(SpisumNames.Properties.FileIsInOutputFormat, "yes") .AddProperty(SpisumNames.Properties.FinalVersion, true) .AddProperty(SpisumNames.Properties.SettleReason, reason) .AddProperty(SpisumNames.Properties.KeepForm, SpisumNames.KeepForm.Original_InOutputFormat) .AddProperty(SpisumNames.Properties.LinkRendering, int.Parse(componentPid[1]) + 1) .AddProperty(SpisumNames.Properties.ListOriginalComponent, int.Parse(componentPid[1])) .AddProperty(SpisumNames.Properties.CompanyImplementingDataFormat, organization) .AddProperty(SpisumNames.Properties.AuthorChangeOfDataFormat, $"{_identityUser.FirstName} {_identityUser.LastName}") .AddProperty(SpisumNames.Properties.OriginalDataFormat, nodeEntry?.Entry?.Content?.MimeType) .AddProperty(SpisumNames.Properties.ImprintFile, Hashes.Sha256CheckSum(new MemoryStream(data))) .AddProperty(SpisumNames.Properties.DataCompleteVerificationItem, DateTime.Now) .AddProperty(SpisumNames.Properties.UsedAlgorithm, "SHA-256"))); }
private async Task UploadPidGeneratorFile(byte[] sctipFile, string fileName, string fileNameWithoutExtension, List <Rules> rules) { var fileParams = new FormDataParam(sctipFile, fileName); var createdScriptPidGenerator = await _alfrescoHttpClient.CreateNode(scriptsNodeId, fileParams, ImmutableList <Parameter> .Empty .Add(new Parameter(HeaderNames.ContentType, "multipart/form-data", ParameterType.HttpHeader)) .Add(new Parameter(AlfrescoNames.Headers.OverWrite, true, ParameterType.GetOrPost))); await CreateRules(fileNameWithoutExtension, createdScriptPidGenerator.Entry.Id, rules); }
private async Task <NodeEntry> SaveEMLFile(MimeMessage message, FormDataParam fileParams) { try { return(await _alfrescoHttp.CreateNode(EmailFolder.Entry.Id, fileParams, ImmutableList <Parameter> .Empty .Add(new Parameter(HeaderNames.ContentType, "multipart/form-data", ParameterType.HttpHeader)) .Add(new Parameter(AlfrescoNames.Headers.NodeType, "ssl:email", ParameterType.GetOrPost)) .Add(new Parameter(AlfrescoNames.ContentModel.Title, message.MessageId, ParameterType.GetOrPost)) .Add(new Parameter("ssl:fileName", $"{message.Subject}.eml", ParameterType.GetOrPost)) .Add(new Parameter("ssl:digitalDeliveryDeliveryDate", message.Date.ToUniversalTime().ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'"), ParameterType.GetOrPost)) .Add(new Parameter("ssl:emailMessageId", message.MessageId, ParameterType.GetOrPost)) .Add(new Parameter("ssl:emailRecipient", message.To.Mailboxes.First().Address, ParameterType.GetOrPost)) .Add(new Parameter("ssl:emailRecipientName", message.To.Mailboxes.First().Name, ParameterType.GetOrPost)) .Add(new Parameter("ssl:emailSender", message.From.Mailboxes.First().Address, ParameterType.GetOrPost)) .Add(new Parameter("ssl:emailSenderName", message.From.Mailboxes.First().Name, ParameterType.GetOrPost)) .Add(new Parameter("ssl:digitalDeliverySubject", message.Subject, ParameterType.GetOrPost)) )); } catch { return(null); } }
public async Task Init() { string XSDFilePath = Path.Combine(_alfrescoConfig.ConfigurationFiles.FolderName, _alfrescoConfig.ConfigurationFiles.ContentModels.FolderName, _alfrescoConfig.ConfigurationFiles.ContentModels.XSDValidationFile); List <ConfigurationContent> files = (from string file in _alfrescoConfig.ConfigurationFiles.ContentModels.Files select new ConfigurationContent { FileName = file, FilePath = Path.Combine(_alfrescoConfig.ConfigurationFiles.FolderName, _alfrescoConfig.ConfigurationFiles.ContentModels.FolderName, file) }).ToList(); foreach (var file in files) { try { var validationResult = XMLValidator.ValidateXML("http://www.alfresco.org/model/dictionary/1.0", file.FilePath, XSDFilePath); if (validationResult.IsOK) { // Company Home var repositoryRootFolder = await _alfrescoHttpClient.GetNodeInfo(AlfrescoNames.Aliases.Root); var modelsNode = await _alfrescoHttpClient.GetNodeInfo(AlfrescoNames.Aliases.Root, ImmutableList <Parameter> .Empty .Add(new Parameter(AlfrescoNames.Headers.RelativePath, $"{DataDictionaryConfiguration.DataDictionary}/{DataDictionaryConfiguration.Models}", ParameterType.QueryString))); FormDataParam fileParams; using (var memstream = new MemoryStream()) { File.OpenRead(file.FilePath).CopyTo(memstream); fileParams = new FormDataParam(memstream.ToArray(), file.FileName); }; var createdChild = await _alfrescoHttpClient.CreateNode(modelsNode.Entry.Id, fileParams, ImmutableList <Parameter> .Empty .Add(new Parameter(HeaderNames.ContentType, "multipart/form-data", ParameterType.HttpHeader)) .Add(new Parameter(AlfrescoNames.Headers.OverWrite, true, ParameterType.GetOrPost))); var properties = new NodeBodyUpdate { Properties = new Dictionary <string, object> { { AlfrescoNames.ContentModel.ModelActive, true } } }; await _alfrescoHttpClient.UpdateNode(createdChild.Entry.Id, properties, ImmutableList <Parameter> .Empty .Add(new Parameter(HeaderNames.ContentType, MediaTypeNames.Application.Json, ParameterType.HttpHeader))); } else { Log.Error(validationResult.ErrorMessage); } } catch (Exception e) { Log.Error(e, "XML or XML file not found"); } } }
private async Task <List <MimeMessage> > RecieveEmails(IEmailConfiguration configuration) { List <MimeMessage> emails = new List <MimeMessage>(); try { EmailFolder = await _alfrescoHttp.GetNodeInfo(AlfrescoNames.Aliases.Root, ImmutableList <Parameter> .Empty .Add(new Parameter(AlfrescoNames.Headers.Include, AlfrescoNames.Includes.Properties, ParameterType.QueryString)) .Add(new Parameter(AlfrescoNames.Headers.RelativePath, "Sites/Mailroom/documentLibrary/MailBox/Unprocessed", ParameterType.QueryString))); NewTimestampText = GetTimestampProperty(EmailFolder); using (var emailClient = new Pop3Client()) { emailClient.Connect(configuration.Pop3.Host, configuration.Pop3.Port, configuration.Pop3.UseSSL); // Remove OAUTH2 because we dont use it right now. emailClient.AuthenticationMechanisms.Remove("XOAUTH2"); emailClient.Authenticate(configuration.Username, configuration.Password); for (int i = 0; i < emailClient.Count; i++) { using (var memory = new MemoryStream()) { var message = emailClient.GetMessage(i); // Check if message was downloaded if (IsMessageAlreadyDownloaded(configuration.Username, message)) { continue; } message.WriteTo(memory); // If third parameter is set - It will end up with badrequest var file = new FormDataParam(memory.ToArray(), $"{IdGenerator.GenerateId()}.eml", null, "message/rfc822"); var emlFile = await SaveEMLFile(message, file); if (emlFile == null) { return(emails); } var attachments = await SaveAllAttachments(message.Attachments, emlFile?.Entry?.Id); await _alfrescoHttp.UpdateNode(emlFile?.Entry?.Id, new NodeBodyUpdate() .AddProperty("ssl:digitalDeliveryAttachmentsCount", attachments?.Count)); if (attachments == null) { emailClient.Disconnect(false); return(emails); } if (emlFile == null) { // Delete all attachments if eml fails to upload await DeleteNodes(attachments); emailClient.Disconnect(false); return(emails); } try { // Update attachment count await _alfrescoHttp.UpdateNode(emlFile?.Entry?.Id, new NodeBodyUpdate() .AddProperty(ComponentCounterProperty, attachments.Count)); await CreateAllSecondaryChildren(emlFile, attachments); } catch { // Delete .eml file and it's attachments if ucreation of secondary childrens fails await DeleteNode(emlFile); await DeleteNodes(attachments); emailClient.Disconnect(false); return(emails); } EmailProvider provider = new EmailProvider(configuration); try { var body = GetAutomaticResponseBodyText( Path.Combine(_emailServerConfiguration.AutomaticResponse.BodyTextFile.Folder, _emailServerConfiguration.AutomaticResponse.BodyTextFile.FileName), new List <ReplaceTexts> { new ReplaceTexts("[predmet_doruceneho_emailu]", message.Subject), new ReplaceTexts("[nazev_organizace]", _emailServerConfiguration.AutomaticResponse.OrganizationName) }); await SendEmail( message?.From?.Mailboxes?.FirstOrDefault()?.Address, configuration.Username, _emailServerConfiguration?.AutomaticResponse?.EmailSubject, body); } catch (Exception e) { } // if succesfully is uploaded emails.Add(emailClient.GetMessage(i)); CurrentDownloadedMessagesCount++; await SaveTimeStamp(configuration.Username, message); } } // update download time anyway await SaveTimeStamp(configuration.Username); // Disconect marks emails as "downloaded" and they will not appear next time emailClient.Disconnect(true); return(emails); } } catch (Exception e) { Log.Error(e.Message); return(emails); } }