• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

djsuperchief / Kyameru / 8401706343

23 Mar 2024 12:18PM UTC coverage: 89.113% (-7.0%) from 96.15%
8401706343

push

github

web-flow
Merge pull request #104 from djsuperchief/103-release-config

103 release config

308 of 387 branches covered (79.59%)

Branch coverage included in aggregate %.

1812 of 1992 relevant lines covered (90.96%)

21.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.41
/source/components/Kyameru.Component.Ftp/Implementation/To.cs
1
using Kyameru.Component.Ftp.Contracts;
2
using Kyameru.Component.Ftp.Settings;
3
using Kyameru.Core.Contracts;
4
using Kyameru.Core.Entities;
5
using Microsoft.Extensions.Logging;
6
using System;
7
using System.Collections.Generic;
8
using System.IO;
9
using System.Text;
10
using System.Threading;
11
using System.Threading.Tasks;
12

13
namespace Kyameru.Component.Ftp
14
{
15
    /// <summary>
16
    /// To component.
17
    /// </summary>
18
    public class To : IFtpTo
19
    {
20
        /// <summary>
21
        /// Ftp settings.
22
        /// </summary>
23
        private readonly FtpSettings ftpSettings;
24

25
        /// <summary>
26
        /// Ftp client.
27
        /// </summary>
28
        private readonly FtpClient ftpClient;
29

30
        /// <summary>
31
        /// Archive path.
32
        /// </summary>
33
        private readonly string archivePath;
34

35
        /// <summary>
36
        /// Source path.
37
        /// </summary>
38
        private readonly string source;
39

40
        /// <summary>
41
        /// Initializes a new instance of the <see cref="To"/> class.
42
        /// </summary>
43
        /// <param name="headers">Valid headers.</param>
44
        /// <param name="webRequestUtility">Web request utility.</param>
45
        public To(Dictionary<string, string> headers, IWebRequestUtility webRequestUtility)
7✔
46
        {
7✔
47
            Dictionary<string, string> config = headers.ToToConfig();
7✔
48
            this.archivePath = config.GetKeyValue("Archive");
7✔
49
            this.source = config.GetKeyValue("Source");
7✔
50
            this.ftpSettings = new FtpSettings(config);
7✔
51
            this.ftpClient = new FtpClient(this.ftpSettings, webRequestUtility);
7✔
52
            this.ftpClient.OnLog += FtpClient_OnLog;
7✔
53
            this.ftpClient.OnError += FtpClient_OnError;
7✔
54
        }
7✔
55

56

57

58
        /// <summary>
59
        /// Logging event.
60
        /// </summary>
61
        public event EventHandler<Log> OnLog;
62

63
        /// <summary>
64
        /// Process Message.
65
        /// </summary>
66
        /// <param name="item">Message to process.</param>
67
        public void Process(Routable item)
68
        {
6✔
69
            try
70
            {
6✔
71
                this.ftpClient.UploadFile(this.GetSource(item), item.Headers["SourceFile"]);
6✔
72
                this.ArchiveFile(item);
5✔
73
            }
5✔
74
            catch (Exception ex)
1✔
75
            {
1✔
76
                item.SetInError(this.GetError("Upload", string.Format(Resources.ERROR_UPLOADING, item.Headers["FileName"])));
1✔
77
                this.RaiseLog(string.Format(Resources.ERROR_UPLOADING, item.Headers["FileName"]), LogLevel.Error, ex);
1✔
78
            }
1✔
79
        }
6✔
80

81
        public async Task ProcessAsync(Routable routable, CancellationToken cancellationToken)
82
        {
1✔
83
            if (!cancellationToken.IsCancellationRequested)
1✔
84
            {
1✔
85
                Process(routable);
1✔
86
            }
1✔
87

88
            await Task.CompletedTask;
1✔
89
        }
1✔
90

91
        /// <summary>
92
        /// Archives the outgoing file.
93
        /// </summary>
94
        /// <param name="item">Message to process.</param>
95
        private void ArchiveFile(Routable item)
96
        {
5✔
97
            if ((this.source == "File" || string.IsNullOrWhiteSpace(this.source)) && !string.IsNullOrWhiteSpace(this.archivePath))
5✔
98
            {
1✔
99
                string fileName = item.Headers["SourceFile"];
1✔
100
                string currentDirectory = System.IO.Directory.GetParent(item.Headers["FullSource"]).FullName;
1✔
101
                string archiveDir = this.GetPath(currentDirectory);
1✔
102
                this.EnsureDirectoryExists(archiveDir);
1✔
103
                System.IO.File.Move(item.Headers["FullSource"], Path.Combine(archiveDir, fileName));
1✔
104
            }
1✔
105
        }
5✔
106

107
        /// <summary>
108
        /// Gets the fully qualified path for archiving.
109
        /// </summary>
110
        /// <param name="originalPath">Original path for outgoing file.</param>
111
        /// <returns>Returns the fully qualified path for archive.</returns>
112
        private string GetPath(string originalPath)
113
        {
1✔
114
            string response = string.Empty;
1✔
115
            if (this.archivePath.Contains(".."))
1!
116
            {
1✔
117
                response = new Uri(System.IO.Path.Combine(originalPath, this.archivePath)).LocalPath;
1✔
118
            }
1✔
119
            else
120
            {
×
121
                response = this.archivePath;
×
122
            }
×
123

124
            return response;
1✔
125
        }
1✔
126

127
        /// <summary>
128
        /// Ensures a directory exists.
129
        /// </summary>
130
        /// <param name="path">Path to check.</param>
131
        private void EnsureDirectoryExists(string path)
132
        {
1✔
133
            DirectoryInfo directoryInfo = new DirectoryInfo(System.IO.Directory.GetParent(path).FullName);
1✔
134
            if (!directoryInfo.Exists)
1✔
135
            {
1✔
136
                directoryInfo.Create();
1✔
137
            }
1✔
138
        }
1✔
139

140
        /// <summary>
141
        /// Gets the source to upload.
142
        /// </summary>
143
        /// <param name="item">Message to process.</param>
144
        /// <returns>Returns a byte array of the file.</returns>
145
        private byte[] GetSource(Routable item)
146
        {
6✔
147
            byte[] response = null;
6✔
148
            if (this.source == "File" || string.IsNullOrWhiteSpace(this.source))
6✔
149
            {
1✔
150
                response = System.IO.File.ReadAllBytes(item.Headers["FullSource"]);
1✔
151
            }
1✔
152
            else
153
            {
5✔
154
                if (item.Headers.ContainsKey("DataType") && item.Headers["DataType"] == "String")
5✔
155
                {
1✔
156
                    response = System.Text.Encoding.UTF8.GetBytes((string)item.Body);
1✔
157
                }
1✔
158
                else
159
                {
4✔
160
                    response = (byte[])item.Body;
4✔
161
                }
4✔
162
            }
5✔
163

164
            return response;
6✔
165
        }
6✔
166

167
        /// <summary>
168
        /// Gets a log entity for logging.
169
        /// </summary>
170
        /// <param name="message">Message to log.</param>
171
        /// <param name="logLevel">Level of logging.</param>
172
        /// <param name="ex">Exception to log.</param>
173
        /// <returns>Returns an instance of the <see cref="Log"/> class.</returns>
174
        private Log RaiseLog(string message, LogLevel logLevel, Exception ex = null)
175
        {
4✔
176
            return new Log(logLevel, message, ex);
4✔
177
        }
4✔
178

179
        /// <summary>
180
        /// Gets an error entity for tracing.
181
        /// </summary>
182
        /// <param name="action">Current action.</param>
183
        /// <param name="message">Error message.</param>
184
        /// <returns>Returns an instance of the <see cref="Error"/> class.</returns>
185
        private Error GetError(string action, string message)
186
        {
1✔
187
            return new Error("ToFtp", action, message);
1✔
188
        }
1✔
189

190
        private void FtpClient_OnError(object sender, Exception e)
191
        {
1✔
192
            this.OnLog?.Invoke(this, this.RaiseLog(Resources.ERROR_FTPPROCESSING, LogLevel.Error, e));
1!
193
        }
1✔
194

195

196
        private void FtpClient_OnLog(object sender, string e)
197
        {
6✔
198
            this.OnLog?.Invoke(this, this.RaiseLog(e, LogLevel.Information));
6✔
199
        }
6✔
200
    }
201
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc