Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug/name duplication #34

Merged
merged 8 commits into from
Jul 20, 2022
73 changes: 46 additions & 27 deletions DropboxSync.BLL/Services/DropboxService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public DropboxService(ILogger<DropboxService> logger)

_dropboxClient = new DropboxClient(_dropboxConfig.AccessToken);

if (!CheckDropboxClient()) throw new Exception($"An error occured during Dropbox Client checkout. Please read the precedent " +
if (!CheckDropboxClient()) throw new Exception($"An error occurred during Dropbox Client checkout. Please read the precedent " +
$"logs to understand the error");

if (!AsyncHelper.RunSync(VerifyRootFolder)) throw new DropboxRootFolderMissingException(nameof(ROOT_FOLDER));
Expand Down Expand Up @@ -157,9 +157,9 @@ public async Task<DropboxSavedFile> SaveUnprocessedFileAsync(string fileName, Da

string dropboxId = dropboxUploadResult.Id.Substring(dropboxUploadResult.Id.IndexOf(':') + 1);

DropboxSavedFile finalOuput = new DropboxSavedFile(dropboxId, dropboxUploadResult.PathDisplay);
DropboxSavedFile finalOutput = new DropboxSavedFile(dropboxId, dropboxUploadResult.PathDisplay);

return finalOuput;
return finalOutput;
}

/// <summary>
Expand All @@ -175,19 +175,35 @@ public async Task<bool> CreateDossierAsync(string dossierName, DateTime createdA

string completeFolder = GenerateDossierFolderPath(createdAt.Year, dossierName);

CreateFolderResult createFolderResult = await _dropboxClient.Files.CreateFolderV2Async(completeFolder);
try
{
CreateFolderResult createFolderResult = await _dropboxClient.Files.CreateFolderV2Async(completeFolder);

if (!createFolderResult.Metadata.IsFolder)
if (!createFolderResult.Metadata.IsFolder)
{
_logger.LogError("{date} | Something was created but not a folder at path {dropboxPath}",
DateTime.Now, createFolderResult.Metadata.PathDisplay);
return false;
}

_logger.LogInformation("{date} | Successfully created folder for dossier \"{dossierName}\" at path {dropboxFolderPath}",
DateTime.Now, dossierName, createFolderResult.Metadata.PathDisplay);

return true;
}
catch (ApiException<CreateFolderError> e)
{
_logger.LogError("{date} | Something was created but not a folder at path {dropboxPath}",
DateTime.Now, createFolderResult.Metadata.PathDisplay);
_logger.LogError("{date} | The Dossier folder creation failed, please use another name : {e}",
DateTime.Now, e.Message);
return false;
}
catch (Exception e)
{
_logger.LogError("{date} | An exception was throw during Dossier creation! : {e}",
DateTime.Now, e.Message);

_logger.LogInformation("{date} | Successfully created folder for dossier \"{dossierName}\" at path {dropboxFolderPath}",
DateTime.Now, dossierName, createFolderResult.Metadata.PathDisplay);

return true;
return false;
}
}

/// <summary>
Expand Down Expand Up @@ -609,7 +625,9 @@ private string GenerateDossierFolderPath(int year, string dossierName)
/// <param name="year">File creation year</param>
/// <param name="dossierName">Dossier name</param>
/// <param name="fileType">File type</param>
/// <returns>Dropbox's absolute path to the <paramref name="fileType"/> folder in dossier named <paramref name="dossierName"/></returns>
/// <returns>
/// Dropbox's absolute path to the <paramref name="fileType"/> folder in dossier named <paramref name="dossierName"/>
/// </returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="InvalidEnumValueException"></exception>
Expand All @@ -630,29 +648,29 @@ private string GenerateDossierFolderPath(int year, string dossierName, FileTypes
}

/// <summary>
/// Generate a name for the file to save in Dropbox. The name is composed of the the date and the filename seperated by
/// <paramref name="seperator"/>. If at date <c>2022-10-22</c> at <c>18:42</c> a file with name <c>MyFilesName.pdf</c>
/// Generate a name for the file to save in Dropbox. The name is composed of the the date and the filename separated by
/// <paramref name="separator"/>. If at date <c>2022-10-22</c> at <c>18:42</c> a file with name <c>MyFilesName.pdf</c>
/// is created, then the generated name would look like this.
/// <code>
/// 2022.10.22 1842-MyFilesName.pdf
/// </code>
/// </summary>
/// <param name="fileName">File's complete name. The filename must respect the Regex</param>
/// <param name="createdAt"></param>
/// <param name="seperator"></param>
/// <param name="separator"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentOutOfRangeException"></exception>
/// <exception cref="InvalidFileNameException"></exception>
private string GenerateDropboxFileName(string fileName, DateTime createdAt, char seperator = '-')
private string GenerateDropboxFileName(string fileName, DateTime createdAt, char separator = '-')
{
fileName = fileName.Trim();

if (string.IsNullOrEmpty(fileName)) throw new ArgumentNullException(nameof(fileName));
if (fileName.StringMatchFileRegEx()) throw new InvalidFileNameException(nameof(fileName));
if (createdAt > DateTime.Now) throw new ArgumentOutOfRangeException(nameof(DateTime));

return string.Join(seperator, createdAt.ToString("yyyy.MM.dd HHmm"), fileName);
return string.Join(separator, createdAt.ToString("yyyy.MM.dd HHmm"), fileName);
}

/// <summary>
Expand Down Expand Up @@ -736,11 +754,11 @@ private async Task<bool> VerifyRootFolder()
return false;
}

bool firstOccurence = true;
bool firstOccurrence = true;

do
{
if (!firstOccurence) listFolderResult = await _dropboxClient.Files.ListFolderContinueAsync(listFolderResult.Cursor);
if (!firstOccurrence) listFolderResult = await _dropboxClient.Files.ListFolderContinueAsync(listFolderResult.Cursor);

foreach (Metadata file in listFolderResult.Entries)
{
Expand Down Expand Up @@ -774,7 +792,8 @@ private async Task<bool> VerifyRootFolder()
}

/// <summary>
/// Verify if the folder at full dropbox path <paramref name="folderFullPath"/> exist in Dropbox. If <paramref name="createIfDontExist"/>
/// Verify if the folder at full dropbox path <paramref name="folderFullPath"/> exist in Dropbox.
/// If <paramref name="createIfDontExist"/>
/// is <c>true</c> then the folder is created.
/// </summary>
/// <param name="folderFullPath">
Expand All @@ -785,8 +804,8 @@ private async Task<bool> VerifyRootFolder()
/// </param>
/// <param name="createIfDontExist">If <c>true</c>, creates the folder at the researched destination</param>
/// <returns>
/// <c>null</c> if listing at path <see cref="ROOT_FOLDER"/> failed or if folder isn't found and <paramref name="createIfDontExist"/> is false.
/// Otherwise returns the Dropbox's required folder's path.
/// <c>null</c> if listing at path <see cref="ROOT_FOLDER"/> failed or if folder isn't found and
/// <paramref name="createIfDontExist"/> is false. Otherwise returns the Dropbox's required folder's path.
/// </returns>
/// <exception cref="ArgumentNullException"></exception>
private async Task<string?> VerifyFolderExist(string folderFullPath, bool createIfDontExist = false)
Expand All @@ -800,18 +819,18 @@ private async Task<bool> VerifyRootFolder()
return null;
}

bool firstOccurence = true;
bool firstOccurrence = true;

do
{
if (!firstOccurence) listFolderResult = await _dropboxClient.Files.ListFolderContinueAsync(listFolderResult.Cursor);
if (!firstOccurrence) listFolderResult = await _dropboxClient.Files.ListFolderContinueAsync(listFolderResult.Cursor);

foreach (Metadata metadata in listFolderResult.Entries)
{
if (metadata.IsFolder && metadata.PathDisplay.Equals(folderFullPath)) return metadata.AsFolder.PathDisplay;
}

firstOccurence = false;
firstOccurrence = false;
}
while (listFolderResult.HasMore);

Expand Down Expand Up @@ -870,11 +889,11 @@ private bool CheckDropboxClient()
}
catch (Exception e)
{
_logger.LogError(e, "{date} | An exception occured during Dropbox SDK checkout.", DateTime.Now);
_logger.LogError(e, "{date} | An exception occurred during Dropbox SDK checkout.", DateTime.Now);
return false;
}

_logger.LogWarning("{date} | Something wrong happened during echo check. More informations are needed", DateTime.Now);
_logger.LogWarning("{date} | Something wrong happened during echo check. More information are needed", DateTime.Now);
return false;
}

Expand Down
11 changes: 0 additions & 11 deletions DropboxSync.UIL/BrokerEventListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,6 @@ private void ReceiveMessage(ReceiverLink receiver, CancellationToken cancellatio
SendToFailedQueue(textMessage, brokerEvent);
}

// if (EventRedirection(brokerEvent, textMessage))
// {
// // When a message is successfully treated, a ACK is sent to notify the broker
// _logger.LogInformation("{date} | Event {eventName} treated with success!", DateTime.Now, eventModel.EventName);
// }
// else
// {
// SendToFailedQueue(textMessage, brokerEvent);
// }

receiver.Accept(message);
}
}
Expand Down Expand Up @@ -341,7 +331,6 @@ public void CheckFailedQueue()

BrokerEvent eventType = (BrokerEvent)brokerEventParseResult;

// bool redirectionResult = Task.Run<bool>(() => EventRedirection(eventType, failedEvent.MessageJson)).Result;
bool redirectionResult = Task.Run<bool>(() =>
_eventManagerLocator.RedirectToManager(failedEvent.MessageJson)).Result;

Expand Down