Adding functionality to queueing contact creation
This commit is contained in:
@@ -0,0 +1,52 @@
|
|||||||
|
namespace TextSender_API.Controllers;
|
||||||
|
|
||||||
|
public class ContactManagement
|
||||||
|
{
|
||||||
|
#region Fields
|
||||||
|
private IConfiguration _config;
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region Constructors
|
||||||
|
public ContactManagement(IConfiguration config)
|
||||||
|
{
|
||||||
|
this._config = config;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region Methods
|
||||||
|
// TODO: Need to implement
|
||||||
|
// Queues the contact file to be processed latter
|
||||||
|
public void QueueContact(IFormFile file)
|
||||||
|
{
|
||||||
|
var directory = this._config["Paths:ContactQueueDirectory"];
|
||||||
|
|
||||||
|
if (!System.IO.File.Exists(directory))
|
||||||
|
{
|
||||||
|
// Directory does not exist
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var length = 16;
|
||||||
|
var chars = "ABCDEF0123456789";
|
||||||
|
var random = new Random();
|
||||||
|
var filename = new string(Enumerable.Repeat(chars, length).Select(s =>
|
||||||
|
s[random.Next(s.Length)]).ToArray());
|
||||||
|
var extension = ".txt";
|
||||||
|
|
||||||
|
var contactPath = $"{filename}{extension}";
|
||||||
|
|
||||||
|
if (System.IO.File.Exists(contactPath))
|
||||||
|
{
|
||||||
|
// Already exists
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
using (var filestream = new FileStream(contactPath, FileMode.Create))
|
||||||
|
{
|
||||||
|
file.CopyTo(filestream);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
@@ -9,15 +9,17 @@ namespace TextSender_API.Controllers;
|
|||||||
public class ContactQueueCreationController : ControllerBase
|
public class ContactQueueCreationController : ControllerBase
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
|
private IConfiguration _config;
|
||||||
private readonly ILogger<ContactQueueCreationController> _logger;
|
private readonly ILogger<ContactQueueCreationController> _logger;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
|
|
||||||
public ContactQueueCreationController(ILogger<ContactQueueCreationController> logger)
|
public ContactQueueCreationController(ILogger<ContactQueueCreationController> logger, IConfiguration config)
|
||||||
{
|
{
|
||||||
this._logger = logger;
|
this._logger = logger;
|
||||||
|
this._config = config;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -28,6 +30,20 @@ public class ContactQueueCreationController : ControllerBase
|
|||||||
{
|
{
|
||||||
// TODO: Save the file on the filesystem to be processed by a separate
|
// TODO: Save the file on the filesystem to be processed by a separate
|
||||||
// system
|
// system
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var ctMgr = new ContactManagement(this._config);
|
||||||
|
|
||||||
|
foreach (var file in contactFile)
|
||||||
|
{
|
||||||
|
ctMgr.QueueContact(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
Reference in New Issue
Block a user