Skip to content

Commit

Permalink
Merge Development to master (#98)
Browse files Browse the repository at this point in the history
- minor security fixes
- code cleanup
  • Loading branch information
Cloud9Developer authored May 17, 2024
1 parent 356f77c commit 4b1dc7c
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 141 deletions.
24 changes: 0 additions & 24 deletions Jellyfin.Plugin.Newsletters/Configuration/PluginConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ public PluginConfiguration()

// default newsletter paths
NewsletterFileName = string.Empty;
MyDataDir = string.Empty;
CurrRunListFileName = "Currlist.txt";
ArchiveFileName = "Archive.txt";
NewsletterDataFileName = "NewsletterList.txt";
NewsletterDir = string.Empty;

// default libraries
Expand Down Expand Up @@ -232,31 +228,11 @@ public PluginConfiguration()
/// </summary>
public string NewsletterFileName { get; set; }

/// <summary>
/// Gets or sets a string setting.
/// </summary>
public string MyDataDir { get; set; }

/// <summary>
/// Gets a string setting.
/// </summary>
public string CurrRunListFileName { get; }

/// <summary>
/// Gets a string setting.
/// </summary>
public string ArchiveFileName { get; }

/// <summary>
/// Gets or sets a string setting.
/// </summary>
public string NewsletterDir { get; set; }

/// <summary>
/// Gets or sets a string setting.
/// </summary>
public string NewsletterDataFileName { get; set; }

/// <summary>
/// Gets or sets a value indicating whether Series should be scanned.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Jellyfin.Plugin.Newsletters/Configuration/configPage.html
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@
}

function saveConfig() {
console.log("test");
//console.log("test");
ApiClient.getPluginConfiguration(NewsletterConfig.pluginUniqueId).then(function (config) {
config.DebugMode = document.querySelector('#DebugMode').checked;
//serverDetails
Expand Down
8 changes: 1 addition & 7 deletions Jellyfin.Plugin.Newsletters/Emails/HTMLBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class HtmlBuilder
private readonly PluginConfiguration config;
private readonly string newslettersDir;
private readonly string newsletterHTMLFile;
private readonly string newsletterDataFile;
// private readonly string newsletterDataFile;

private string emailBody;
private Logger logger;
Expand All @@ -51,7 +51,6 @@ public HtmlBuilder()
jsonHelper = new JsonFileObj();
db = new SQLiteDatabase();
config = Plugin.Instance!.Configuration;
newsletterDataFile = config.MyDataDir + config.NewsletterDataFileName;
emailBody = config.Body;

newslettersDir = config.NewsletterDir; // newsletterdir
Expand All @@ -74,7 +73,6 @@ public HtmlBuilder()

public string GetDefaultHTMLBody()
{
// emailBody = "<html> <div> <table style='margin-left: auto; margin-right: auto;'> <tr> <td width='100%' height='100%' style='vertical-align: top; background-color: #000000;'> <table id='InsertHere' name='MainTable' style='margin-left: auto; margin-right: auto; border-spacing: 0 5px; padding-left: 2%; padding-right: 2%; padding-bottom: 1%;'> <tr style='text-align: center;'> <td colspan='2'> <span><h1 id='Title' style='color:#FFFFFF;'>Jellyfin Newsletter</h1><h3 id='Date' style='color:#FFFFFF;'>" + DateTime.Today.ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture) + "</h3></span> </td> </tr> <!-- Fill this in from code --> REPLACEME <!-- Fill that in from code --> </table> </td> </tr> </table> </div> </html>";
emailBody = config.Body;
return emailBody;
}
Expand Down Expand Up @@ -392,14 +390,10 @@ public void CleanUp(string htmlBody)

// append newsletter cycle data to Archive.txt
CopyNewsletterDataToArchive();

// remove newsletter cycle data
File.Delete(newsletterDataFile);
}

private void CopyNewsletterDataToArchive()
{
string archiveFile = config.MyDataDir + config.ArchiveFileName;
logger.Info("Appending NewsletterData for Current Newsletter Cycle to Archive Database..");

// copy tables
Expand Down
17 changes: 5 additions & 12 deletions Jellyfin.Plugin.Newsletters/Emails/smtp.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
#pragma warning disable 1591
using System;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Plugin.Newsletters.Configuration;
using Jellyfin.Plugin.Newsletters.Emails.HTMLBuilder;
using Jellyfin.Plugin.Newsletters.LOGGER;
using Jellyfin.Plugin.Newsletters.Shared.DATA;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.Tasks;
using Microsoft.AspNetCore.Http;
using MediaBrowser.Common.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

// using System.Net.NetworkCredential;
Expand All @@ -26,12 +19,13 @@ namespace Jellyfin.Plugin.Newsletters.Emails.EMAIL;
/// Interaction logic for SendMail.xaml.
/// </summary>
// [Route("newsletters/[controller]")]
[Authorize(Policy = Policies.RequiresElevation)]
[ApiController]
[Route("Smtp")]
public class Smtp : ControllerBase
{
private readonly PluginConfiguration config;
private readonly string newsletterDataFile;
// private readonly string newsletterDataFile;
private SQLiteDatabase db;
private Logger logger;

Expand All @@ -40,7 +34,6 @@ public Smtp()
db = new SQLiteDatabase();
logger = new Logger();
config = Plugin.Instance!.Configuration;
newsletterDataFile = config.MyDataDir + config.NewsletterDataFileName;
}

[HttpPost("SendTestMail")]
Expand Down Expand Up @@ -142,7 +135,7 @@ public void SendEmail()
}
}

public bool NewsletterDbIsPopulated()
private bool NewsletterDbIsPopulated()
{
foreach (var row in db.Query("SELECT COUNT(*) FROM CurrNewsletterData;"))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Jellyfin.Plugin.Newsletters</RootNamespace>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<Nullable>enable</Nullable>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<CodeAnalysisRuleSet>../jellyfin.ruleset</CodeAnalysisRuleSet>
Expand Down
3 changes: 1 addition & 2 deletions Jellyfin.Plugin.Newsletters/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ void SetConfigPaths(IApplicationPaths dataPaths)
config.LogDirectoryPath = dataPaths.LogDirectoryPath;

// Custom Paths
config.MyDataDir = $"{config.TempDirectory}/Newsletters/";
config.NewsletterDir = $"{config.MyDataDir}myNewsletters/";
config.NewsletterDir = $"{config.TempDirectory}/Newsletters/";
}

SetConfigPaths(applicationPaths);
Expand Down
65 changes: 0 additions & 65 deletions Jellyfin.Plugin.Newsletters/Scanner/NewsletterDataGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ public class NewsletterDataGenerator
// Global Vars
// Readonly
private readonly PluginConfiguration config;
private readonly string newslettersDir;
private readonly string newsletterDataFile;

private readonly string currRunList;
private readonly string archiveFile;
private readonly string myDataDir;
private Logger logger;
private SQLiteDatabase db;

Expand All @@ -50,56 +44,8 @@ public NewsletterDataGenerator()
logger = new Logger();
db = new SQLiteDatabase();
config = Plugin.Instance!.Configuration;
myDataDir = config.TempDirectory + "/Newsletters";

archiveFile = config.MyDataDir + config.ArchiveFileName; // curlist/archive
currRunList = config.MyDataDir + config.CurrRunListFileName;
newsletterDataFile = config.MyDataDir + config.NewsletterDataFileName;

archiveSeriesList = new List<JsonFileObj>();
newslettersDir = config.NewsletterDir; // newsletterdir
Directory.CreateDirectory(newslettersDir);
}

public Task GenerateDataForNextNewsletter()
{
try
{
db.CreateConnection();
archiveSeriesList = PopulateFromArchive(db); // Files that shouldn't be processed again
CopyCurrRunDataToNewsletterData();
}
catch (Exception e)
{
logger.Error("An error has occured: " + e);
}
finally
{
db.CloseConnection();
}

return Task.CompletedTask;
}

public List<JsonFileObj> PopulateFromArchive(SQLiteDatabase database)
{
List<JsonFileObj> myObj = new List<JsonFileObj>();
JsonFileObj helper, currArcObj;

foreach (var row in database.Query("SELECT * FROM ArchiveData;"))
{
logger.Debug("Inside of foreach..");
if (row is not null)
{
helper = new JsonFileObj();
currArcObj = helper.ConvertToObj(row);
myObj.Add(currArcObj);
}
}

logger.Debug("Returning ArchObj");

return myObj;
}

public string FetchImagePoster(JsonFileObj item)
Expand Down Expand Up @@ -161,15 +107,4 @@ private string UploadToImgur(string posterFilePath)
}
}
}

private void CopyCurrRunDataToNewsletterData()
{
if (File.Exists(currRunList)) // archiveFile
{
Stream input = File.OpenRead(currRunList);
Stream output = new FileStream(newsletterDataFile, FileMode.Append, FileAccess.Write, FileShare.None);
input.CopyTo(output);
File.Delete(currRunList);
}
}
}
37 changes: 13 additions & 24 deletions Jellyfin.Plugin.Newsletters/Scanner/Scraper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public class Scraper
// Global Vars
// Readonly
private readonly PluginConfiguration config;
private readonly string currRunScanList;
private readonly string archiveFile;
private readonly string currNewsletterDataFile;
// private readonly string currRunScanList;
// private readonly string archiveFile;
// private readonly string currNewsletterDataFile;
private readonly ILibraryManager libManager;

// Non-readonly
Expand All @@ -58,25 +58,19 @@ public Scraper(ILibraryManager libraryManager, IProgress<double> passedProgress,
libManager = libraryManager;

totalLibCount = currCount = 0;
// currRunScanListDir = config.TempDirectory + "/Newsletters/";
archiveFile = config.MyDataDir + config.ArchiveFileName;
currRunScanList = config.MyDataDir + config.CurrRunListFileName;
currNewsletterDataFile = config.MyDataDir + config.NewsletterDataFileName;
Directory.CreateDirectory(config.MyDataDir);

ng = new NewsletterDataGenerator();
db = new SQLiteDatabase();

logger.Debug("Setting Config Paths: ");
logger.Debug("\n DataPath: " + config.DataPath +
"\n TempDirectory: " + config.TempDirectory +
"\n PluginsPath: " + config.PluginsPath +
"\n MyDataDir: " + config.MyDataDir +
"\n NewsletterDir: " + config.NewsletterDir +
"\n ProgramDataPath: " + config.ProgramDataPath +
"\n ProgramSystemPath: " + config.ProgramSystemPath +
"\n SystemConfigurationFilePath: " + config.SystemConfigurationFilePath +
"\n LogDirectoryPath: " + config.LogDirectoryPath );
"\n TempDirectory: " + config.TempDirectory +
"\n PluginsPath: " + config.PluginsPath +
"\n NewsletterDir: " + config.NewsletterDir +
"\n ProgramDataPath: " + config.ProgramDataPath +
"\n ProgramSystemPath: " + config.ProgramSystemPath +
"\n SystemConfigurationFilePath: " + config.SystemConfigurationFilePath +
"\n LogDirectoryPath: " + config.LogDirectoryPath );
}

// This is the main function
Expand All @@ -86,7 +80,6 @@ public Task GetSeriesData()
try
{
db.CreateConnection();
// archiveObj = ng.PopulateFromArchive(db);
BuildJsonObjsToCurrScanfile();
CopyCurrRunDataToNewsletterData();
}
Expand Down Expand Up @@ -134,8 +127,6 @@ public void BuildObjs(List<BaseItem> items, string type)
foreach (BaseItem item in items)
{
currCount++;
// logger.Info("PROGRESS: " + currCount + " " + totalLibCount);
// double percentage = (double)currCount / (double)totalLibCount * 100;
progress.Report((double)currCount / (double)totalLibCount * 100);
if (item is not null)
{
Expand Down Expand Up @@ -184,7 +175,9 @@ public void BuildObjs(List<BaseItem> items, string type)
currFileObj.Filename = episode.PhysicalLocations[0];
currFileObj.Title = series.Name;
currFileObj.Type = type;
if (!InDatabase("CurrRunData", currFileObj.Filename.Replace("'", string.Empty, StringComparison.Ordinal)) && !InDatabase("CurrNewsletterData", currFileObj.Filename.Replace("'", string.Empty, StringComparison.Ordinal)) && !InDatabase("ArchiveData", currFileObj.Filename.Replace("'", string.Empty, StringComparison.Ordinal)))
if (!InDatabase("CurrRunData", currFileObj.Filename.Replace("'", string.Empty, StringComparison.Ordinal)) &&
!InDatabase("CurrNewsletterData", currFileObj.Filename.Replace("'", string.Empty, StringComparison.Ordinal)) &&
!InDatabase("ArchiveData", currFileObj.Filename.Replace("'", string.Empty, StringComparison.Ordinal)))
{
try
{
Expand Down Expand Up @@ -251,7 +244,6 @@ public void BuildObjs(List<BaseItem> items, string type)
finally
{
// save to "database" : Table currRunScanList
// WriteFile(append, currRunScanList, JsonConvert.SerializeObject(currFileObj) + ";;;");
logger.Debug("Adding to CurrRunData DB...");
currFileObj = NoNull(currFileObj);
db.ExecuteSQL("INSERT INTO CurrRunData (Filename, Title, Season, Episode, SeriesOverview, ImageURL, ItemID, PosterPath, Type) " +
Expand All @@ -274,8 +266,6 @@ public void BuildObjs(List<BaseItem> items, string type)
logger.Debug("\"" + currFileObj.Filename + "\" has already been processed either by Previous or Current Newsletter!");
}
}

// currCount++;
}
}

Expand Down Expand Up @@ -378,7 +368,6 @@ private string SetImageURL(JsonFileObj currObj)
}
}

// string url = ng.FetchImagePoster(obj.Title);
logger.Debug("Uploading poster...");
logger.Debug(currObj.ItemID);
logger.Debug(currObj.PosterPath);
Expand Down
7 changes: 2 additions & 5 deletions Jellyfin.Plugin.Newsletters/ScheduledTasks/ScanLibraryTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,9 @@ public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellat
{
cancellationToken.ThrowIfCancellationRequested();
progress.Report(0);
// _scanner
// ILibraryManager libManager = _scanner.GetLibrary();
// Scraper myScraper = new Scraper();

Scraper myScraper = new Scraper(_libraryManager, progress, cancellationToken);
return myScraper.GetSeriesData(); // .ConfigureAwait(false);
// return Task.CompletedTask;
return myScraper.GetSeriesData();
}
}
}
4 changes: 4 additions & 0 deletions jellyfin.ruleset
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<Rule Id="SA1602" Action="None" />
<!-- disable warning SA1633: The file header is missing or not located at the top of the file -->
<Rule Id="SA1633" Action="None" />
<!-- disable warning SA1028: Code should not contain trailing whitespace -->
<Rule Id="SA1028" Action="None" />
<!-- disable warning SA1005: Single line comment should begin with a space -->
<Rule Id="SA1005" Action="None" />
</Rules>

<Rules AnalyzerId="Microsoft.CodeAnalysis.NetAnalyzers" RuleNamespace="Microsoft.Design">
Expand Down

0 comments on commit 4b1dc7c

Please sign in to comment.