Skip to content

Commit

Permalink
Fixed possible null reference.
Browse files Browse the repository at this point in the history
WriteAnimeHTTPToFile() now has permissions check and will log error if needed.
Formatting improvements.
  • Loading branch information
RickDB authored and RickDB committed Jan 7, 2017
1 parent 515c193 commit ff7b117
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
36 changes: 27 additions & 9 deletions JMMServer/AniDB_API/Commands/AniDBHTTPCommand_GetFullAnime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
using System.Xml;
using JMMServer;
using JMMServer.Providers.Azure;
using NLog;

namespace AniDBAPI.Commands
{
public class AniDBHTTPCommand_GetFullAnime : AniDBHTTPCommand, IAniDBHTTPCommand
{
private static Logger logger = LogManager.GetCurrentClassLogger();

private int animeID;

public int AnimeID
Expand Down Expand Up @@ -146,19 +149,34 @@ private XmlDocument LoadAnimeHTTPFromFile(int animeID)

private void WriteAnimeHTTPToFile(int animeID, string xml)
{
string filePath = ServerSettings.AnimeXmlDirectory;
try
{
string filePath = ServerSettings.AnimeXmlDirectory;

if (!Directory.Exists(filePath))
Directory.CreateDirectory(filePath);

if (!Directory.Exists(filePath))
Directory.CreateDirectory(filePath);
string fileName = $"AnimeDoc_{animeID}.xml";
string fileNameWithPath = Path.Combine(filePath, fileName);

string fileName = string.Format("AnimeDoc_{0}.xml", animeID);
string fileNameWithPath = Path.Combine(filePath, fileName);
// First check to make sure we not rights issue
if (!Utils.IsDirectoryWritable(filePath))
Utils.GrantAccess(filePath);

StreamWriter sw;
sw = File.CreateText(fileNameWithPath);
sw.Write(xml);
sw.Close();
// Check again and only if write-able we create it
if (Utils.IsDirectoryWritable(filePath))
{
StreamWriter sw;
sw = File.CreateText(fileNameWithPath);
sw.Write(xml);
sw.Close();
}

}
catch (Exception ex)
{
logger.Error($"Error occurred during WriteAnimeHTTPToFile(): {ex}");
}
}

public virtual enHelperActivityType Process()
Expand Down
26 changes: 12 additions & 14 deletions JMMServer/ServerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2202,32 +2202,30 @@ public static void DebugSettingsToLog()
System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
try
{
if (a != null)
{
logger.Info(string.Format("JMM Server Version: v{0}", Utils.GetApplicationVersion(a)));
}
if(Utils.GetApplicationVersion(a) != null)
logger.Info($"JMM Server Version: v{Utils.GetApplicationVersion(a)}");
}
catch (Exception ex)
{
// oopps, can't create file
logger.Warn("Error in log: {0}", ex.ToString());
logger.Warn("Error in log (server version lookup): {0}", ex.ToString());
}

try
{
logger.Info(string.Format("Database Version: {0}", DatabaseFactory.Instance.GetDatabaseVersion()));
if(DatabaseFactory.Instance != null)
logger.Info($"Database Version: {DatabaseFactory.Instance.GetDatabaseVersion()}");
}
catch (Exception ex)
{
// oopps, can't create file
logger.Warn("Error in log: {0}", ex.Message);
logger.Warn("Error in log (database version lookup: {0}", ex.Message);
}

logger.Info(string.Format("Operating System: {0}", Utils.GetOSInfo()));
logger.Info($"Operating System: {Utils.GetOSInfo()}");

string screenSize = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width.ToString() + "x" +
System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height.ToString();
logger.Info(string.Format("Screen Size: {0}", screenSize));
logger.Info($"Screen Size: {screenSize}");


try
Expand All @@ -2242,9 +2240,8 @@ public static void DebugSettingsToLog()
if (File.Exists(mediaInfoPath))
{
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(mediaInfoPath);
mediaInfoVersion = string.Format("MediaInfo DLL {0}.{1}.{2}.{3} ({4})", fvi.FileMajorPart,
fvi.FileMinorPart,
fvi.FileBuildPart, fvi.FilePrivatePart, mediaInfoPath);
mediaInfoVersion =
$"MediaInfo DLL {fvi.FileMajorPart}.{fvi.FileMinorPart}.{fvi.FileBuildPart}.{fvi.FilePrivatePart} ({mediaInfoPath})";
}
logger.Info(mediaInfoVersion);

Expand All @@ -2259,8 +2256,9 @@ public static void DebugSettingsToLog()
hasherInfoVersion = string.Format("Hasher DLL found at {0}", fullHasherexepath);
logger.Info(hasherInfoVersion);
}
catch
catch (Exception ex)
{
logger.Error("Error in log (hasher / info): {0}", ex.Message);
}

logger.Info("-------------------------------------------------------");
Expand Down

0 comments on commit ff7b117

Please sign in to comment.