Skip to content

Commit

Permalink
TelegramChatsStorage logging
Browse files Browse the repository at this point in the history
  • Loading branch information
UAVXP committed Nov 4, 2019
1 parent e6c3b95 commit 7ea86b3
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion MedocUpdates/TelegramChatsStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public static bool Save()

// Checking again
if (!File.Exists(m_storageFilePath))
{
Log.Write(LogLevel.NORMAL, "TelegramChatsStorage: Save(): Cannot create chatID storage file");
return false;
}

string chatIDsFileStr = "";
foreach(long chatID in m_TelegramChatIDs)
Expand All @@ -39,28 +42,43 @@ public static bool Save()
}

File.WriteAllText(m_storageFilePath, chatIDsFileStr);
if (!File.Exists(m_storageFilePath))
if (!File.Exists(m_storageFilePath)) // TODO: Check for file size instead of availability
{
Log.Write(LogLevel.NORMAL, "TelegramChatsStorage: Save(): Storage file is gone in the middle of writing");
return false; // And here we're going to fail
}

return true;
}

public static bool Restore()
{
if (!File.Exists(m_storageFilePath))
{
Log.Write(LogLevel.NORMAL, "TelegramChatsStorage: Restore(): Cannot restore - storage file doesn't exist");
return false;
}

string chatIDsStr = File.ReadAllText(m_storageFilePath);
if(chatIDsStr.Trim().Length <= 0)
{
Log.Write(LogLevel.NORMAL, "TelegramChatsStorage: Restore(): Cannot restore - storage file is empty. Using the empty list instead");
return false;
}

string[] userIDsStr = chatIDsStr.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
if(userIDsStr.Length <= 0)
{
Log.Write(LogLevel.NORMAL, "TelegramChatsStorage: Restore(): Cannot restore - parsing error");
return false;
}

long[] userIDs = Array.ConvertAll(userIDsStr, long.Parse);
if(userIDs.Length <= 0)
{
Log.Write(LogLevel.NORMAL, "TelegramChatsStorage: Restore(): Cannot restore - convertation error");
return false;
}

m_TelegramChatIDs.AddRange(userIDs);
return true;
Expand Down

0 comments on commit 7ea86b3

Please sign in to comment.