Files
schedtxt_api/Controllers/ContactManagement.cs
T

68 lines
1.6 KiB
C#

using TextSender_API.Models;
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 ContactCreationQueue QueueContact(IFormFile file)
{
var queue = new ContactCreationQueue();
queue.Processed = false;
var directory = this._config["Paths:ContactQueueDirectory"];
if (directory!.ElementAt(directory!.Length - 1) != '/')
{
directory.Append('/');
}
if (!System.IO.Directory.Exists(directory))
{
queue.Status = "Missing directory";
// Directory does not exist
return queue;
}
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 = $"{directory}/{filename}{extension}";
queue.Filepath = contactPath;
if (System.IO.File.Exists(contactPath))
{
// Already exists
queue.Status = "File exists";
return queue;
}
queue.Status = "Created";
using (var filestream = new FileStream(contactPath, FileMode.Create))
{
file.CopyTo(filestream);
}
return queue;
}
#endregion
}