Skip to content

Commit

Permalink
Merge pull request #5 from thewhobox/master
Browse files Browse the repository at this point in the history
Update debug
  • Loading branch information
thewhobox authored Dec 9, 2023
2 parents 75a8cdc + 37c9031 commit bbb0971
Show file tree
Hide file tree
Showing 21 changed files with 126 additions and 59 deletions.
2 changes: 1 addition & 1 deletion ShareLoader.Share/ItemModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class ItemModel
public string Name { get; set; }
public string Id { get; set; }
public string Downloader { get; set; }
public int Size { get; set; }
public long Size { get; set; }
public bool IsOnline { get; set; }
public int GroupId { get; set; }
public string Url { get; set; }
Expand Down
20 changes: 15 additions & 5 deletions ShareLoader.Share/SettingsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,29 @@ public class SettingsHelper
{
public static string FilePath { get; set; } = "/shareloader/settings.txt";

public static Dictionary<string, string> Settings { get; set; }
public SettingsHelper()
{
if(!Directory.Exists("/shareloader/"))
{
Console.WriteLine("/shareloader/ doesnt exist");
FilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "settings.txt");
Console.WriteLine("Using: " + FilePath);
}
}

public static Dictionary<string, string> Settings { get; set; } = new Dictionary<string, string>();

public static T GetSetting<T>(string name)
public static T? GetSetting<T>(string name)
{
if(Settings == null) Load();
if(!Settings.ContainsKey(name)) return (T)Convert.ChangeType(null, typeof(T));
if(Settings == null || !Settings.ContainsKey(name)) return (T?)Convert.ChangeType(null, typeof(T?));
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(Settings[name]);
}

public static string GetSetting(string name)
{
if(Settings == null) Load();
if(!Settings.ContainsKey(name)) return "";
if(Settings == null || !Settings.ContainsKey(name)) return "";
return Settings[name];
}

Expand Down Expand Up @@ -51,7 +61,7 @@ public static void Load()
}

string content = System.IO.File.ReadAllText(FilePath);
Settings = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(content);
Settings = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(content) ?? new();
}

public static void Save()
Expand Down
34 changes: 22 additions & 12 deletions ShareLoader/Background/AccountChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,28 @@ private async void Check()
}
}

using (DownloadContext context = new DownloadContext())
try
{
foreach (AccountProfile profile in Profiles.Values)
using (DownloadContext context = new DownloadContext())
{
IDownloadManager downloader = DownloadHelper.GetDownloader(profile);
foreach (AccountProfile profile in Profiles.Values)
{
IDownloadManager? downloader = DownloadHelper.GetDownloader(profile);
if(downloader == null)
continue;

if (!profile.IsLoggedIn)
profile.IsLoggedIn = await downloader.DoLogin(profile);
if (!profile.IsLoggedIn)
profile.IsLoggedIn = await downloader.DoLogin(profile);

if (!profile.IsLoggedIn) continue;
await downloader.GetAccounInfo(profile);
if (!profile.IsLoggedIn) continue;
await downloader.GetAccounInfo(profile);

context.Accounts.Update(profile.Model);
}
context.Accounts.Update(profile.Model);
}

context.SaveChanges();
}
context.SaveChanges();
}
} catch {}

LastChecked = DateTime.Now;
await Task.Delay(TimeSpan.FromMinutes(1));
Expand All @@ -71,7 +76,12 @@ private async Task Login(AccountModel acc)
{
AccountProfile profile = new AccountProfile(acc);

IDownloadManager downloader = DownloadHelper.GetDownloader(profile);
IDownloadManager? downloader = DownloadHelper.GetDownloader(profile);
if(downloader == null)
{
Console.WriteLine($"Anmeldung fehlgeschlagen. Kein Manager für Profil {profile.Model.Name}");
return;
}
bool success = await downloader.DoLogin(profile);
if (success)
{
Expand Down
10 changes: 8 additions & 2 deletions ShareLoader/Background/DownloadChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public bool isDownloading {
get { return _currentItems.Count > 0; }
}
public bool nothingToDownload = false;
private DownloadGroup _currentGroup;
private List<DownloadModel> _currentItems = new List<DownloadModel>();
AccountChecker account;
Timer _timer;
Expand Down Expand Up @@ -105,6 +104,7 @@ private async void Check()
Manager = manager,
Profile = profile
};
await manager.DoLogin(profile);
_currentItems.Add(model);
checkCounter = 0;
DoDownload(model);
Expand All @@ -124,7 +124,13 @@ private async void DoDownload(DownloadModel model)
bool acceptRange = await model.Manager.CheckStreamRange(model.Item, model.Profile);


SettingsModel settings = SettingsHelper.GetSetting<SettingsModel>("settings");
SettingsModel? settings = SettingsHelper.GetSetting<SettingsModel>("settings");
if(settings == null)
{
ChangeItemState(model.Item, States.Waiting);
Console.WriteLine("Aborted Download due to no settings found");
return;
}

string groupDir = System.IO.Path.Combine(settings.DownloadFolder, model.Item.DownloadGroupID.ToString());
string filesDir = System.IO.Path.Combine(settings.DownloadFolder, model.Item.DownloadGroupID.ToString(), "files");
Expand Down
2 changes: 1 addition & 1 deletion ShareLoader/Background/ExtractChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private async void GetProgress(System.Diagnostics.Process p)

if (m.Success)
{
if(m.Groups[1].Value != "0")
if(m.Groups[1].Value != "0" && _currentItem != null)
await SocketHelper.Instance.SendIDExtract(_currentItem, int.Parse(m.Groups[1].Value));
}

Expand Down
16 changes: 10 additions & 6 deletions ShareLoader/Background/MoveChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class MoveChecker
public DateTime LastChecked { get; set; } = DateTime.Now;
private DownloadItem? _currentItem;
private DownloadType _currentType;
private string _currentSort;
private List<DownloadItem> _items;
private string _currentSort = "";
private List<DownloadItem> _items = new();
DownloadChecker download;
ExtractChecker extract;

Expand Down Expand Up @@ -76,9 +76,10 @@ private async void Check()

private async void DoMove()
{
if(_currentItem == null) return;
System.Console.WriteLine("Moving now: " + _currentItem.Name);
ChangeItemState(States.Moving);
_ = SocketHelper.Instance.SendIDMoving(_currentItem);
await SocketHelper.Instance.SendIDMoving(_currentItem);

switch(_currentType)
{
Expand All @@ -100,7 +101,8 @@ private async void DoMove()

private void MoveMovie()
{
SettingsModel settings = SettingsHelper.GetSetting<SettingsModel>("settings");
SettingsModel? settings = SettingsHelper.GetSetting<SettingsModel>("settings");
if(settings == null || _currentItem == null) return;
string pathFrom = System.IO.Path.Combine(settings.DownloadFolder, _currentItem.DownloadGroupID.ToString(), "extracted", _currentItem.GroupID.ToString());
string[] files = Directory.GetFiles(pathFrom, "*.mkv");
string fileToMove = "";
Expand Down Expand Up @@ -139,8 +141,9 @@ private void MoveSoap()
else
_currentSort = _currentSort.Replace('/', Path.DirectorySeparatorChar);

SettingsModel settings = SettingsHelper.GetSetting<SettingsModel>("settings");
SettingsModel? settings = SettingsHelper.GetSetting<SettingsModel>("settings");

if(settings == null || _currentItem == null) return;
string pathFrom = System.IO.Path.Combine(settings.DownloadFolder, _currentItem.DownloadGroupID.ToString(), "extracted", _currentItem.GroupID.ToString());
string[] files = Directory.GetFiles(pathFrom, "*.mkv");

Expand Down Expand Up @@ -181,7 +184,8 @@ private void MoveSoap()

public void MoveOther()
{
SettingsModel settings = SettingsHelper.GetSetting<SettingsModel>("settings");
SettingsModel? settings = SettingsHelper.GetSetting<SettingsModel>("settings");
if(settings == null || _currentItem == null) return;
string pathFrom = System.IO.Path.Combine(settings.DownloadFolder, _currentItem.DownloadGroupID.ToString(), "extracted", _currentItem.GroupID.ToString());
string[] files = Directory.GetFiles(pathFrom);
string pathTo = Path.Combine(settings.MoveFolder, "Other");
Expand Down
8 changes: 4 additions & 4 deletions ShareLoader/Classes/DownloadHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ public static IDownloadManager GetDownloader(string item)
return downloader;
}

public static IDownloadManager GetDownloader(AccountProfile profile)
public static IDownloadManager? GetDownloader(AccountProfile profile)
{
if (profile == null) return null;
IDownloadManager downloader = null;
IDownloadManager? downloader = null;

var q = from t in Assembly.GetExecutingAssembly().GetTypes()
where t.IsClass && t.IsNested == false && t.Namespace == "ShareLoader.Manager"
select t;

foreach (Type t in q.ToList())
{
IDownloadManager down = (IDownloadManager)Activator.CreateInstance(t);
if (profile.Model.Hoster == down.Identifier)
IDownloadManager? down = (IDownloadManager?)Activator.CreateInstance(t);
if (down != null && profile.Model.Hoster == down.Identifier)
{
downloader = down;
break;
Expand Down
2 changes: 1 addition & 1 deletion ShareLoader/Classes/SocketHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void Handle(WebSocket socket, string request)
switch (paras[0])
{
case "register":
Sockets.Add(new SocketItem() { Socket = socket, Id = int.Parse(paras[1]), Gid = int.Parse(paras[2]), SubscribeKeys = paras[3] });
Sockets.Add(new SocketItem(socket, int.Parse(paras[1]), int.Parse(paras[2]), paras[3]));
break;
}
}
Expand Down
8 changes: 8 additions & 0 deletions ShareLoader/Classes/SocketItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,13 @@ public class SocketItem
public int Id { get; set; }
public int Gid { get; set; }
public string SubscribeKeys { get; set; }

public SocketItem(WebSocket socket, int id, int gid, string keys)
{
Socket = socket;
Id = id;
gid = Gid;
SubscribeKeys = keys;
}
}
}
2 changes: 1 addition & 1 deletion ShareLoader/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public IActionResult Add(AccountModel acc)

public IActionResult Delete(int Id)
{
AccountModel acc = _context.Accounts.SingleOrDefault(g => g.Id == Id);
AccountModel? acc = _context.Accounts.SingleOrDefault(g => g.Id == Id);
if(acc == null) return NotFound();
_context.Accounts.Remove(acc);
_context.SaveChanges();
Expand Down
4 changes: 2 additions & 2 deletions ShareLoader/Controllers/DownloadsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public IActionResult DeleteItem(int Id)
if(item.State == States.Downloading) return NotFound("Datei kann nicht gelöscht werden, während sie heruntergeladen wird.");
if(item.State == States.Extracting) return NotFound("Datei kann nicht gelöscht werden, während sie entpackt wird.");

string downloadPath = SettingsHelper.GetSetting<SettingsModel>("settings").DownloadFolder;
string downloadPath = SettingsHelper.GetSetting<SettingsModel>("settings")?.DownloadFolder ?? "";
string filePath = System.IO.Path.Combine(downloadPath, item.DownloadGroupID.ToString(), "files", item.Name);
try{
if(System.IO.File.Exists(filePath))
Expand Down Expand Up @@ -272,7 +272,7 @@ public IActionResult UnPauseItem(int id)
DownloadItem? item = _context.Items.SingleOrDefault(i => i.Id == id);
if(item == null) return NotFound();

string downloadPath = SettingsHelper.GetSetting<SettingsModel>("settings").DownloadFolder;
string downloadPath = SettingsHelper.GetSetting<SettingsModel>("settings")?.DownloadFolder ?? "";
string filePath = System.IO.Path.Combine(downloadPath, item.DownloadGroupID.ToString(), "files", item.Name);
string extractPath = System.IO.Path.Combine(downloadPath, item.DownloadGroupID.ToString(), "extracted", item.GroupID.ToString());

Expand Down
2 changes: 1 addition & 1 deletion ShareLoader/Controllers/SettingsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public SettingsController(DownloadContext context, ILogger<SettingsController> l

public IActionResult Index()
{
SettingsModel model = SettingsHelper.GetSetting<SettingsModel>("settings");
SettingsModel? model = SettingsHelper.GetSetting<SettingsModel>("settings");
return View(model);
}

Expand Down
22 changes: 20 additions & 2 deletions ShareLoader/Data/DownloadContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,32 @@
using Microsoft.AspNetCore.Mvc;
using ShareLoader.Models;

#pragma warning disable CS8618

namespace ShareLoader.Data;

public class DownloadContext : DbContext
{
public static string databasePath = "/shareloader/database.db";

public DownloadContext() : base() { }
public DownloadContext(DbContextOptions<DownloadContext> options) : base(options) { }
public DownloadContext() : base()
{
if(!Directory.Exists("/shareloader/"))
{
Console.WriteLine("/shareloader/ doesnt exist");
databasePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "database.db");
Console.WriteLine("Using: " + databasePath);
}
}
public DownloadContext(DbContextOptions<DownloadContext> options) : base(options)
{
if(!Directory.Exists("/shareloader/"))
{
Console.WriteLine("/shareloader/ doesnt exist");
databasePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "database.db");
Console.WriteLine("Using: " + databasePath);
}
}

public DbSet<DownloadGroup> Groups { get; set; }
public DbSet<DownloadItem> Items { get; set; }
Expand Down
7 changes: 6 additions & 1 deletion ShareLoader/Manager/ddlDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task<ItemModel> GetItemInfo(string Id)
if(item.IsOnline)
{
item.Name = jresp["result"][0]["name"].ToString();
item.Size = int.Parse(jresp["result"][0]["size"].ToString());
item.Size = long.Parse(jresp["result"][0]["size"].ToString());
}

return item;
Expand All @@ -67,9 +67,14 @@ public async Task<Stream> GetDownloadStream(DownloadItem item, AccountProfile pr
{
Stream? s = null;
try {
System.Console.WriteLine($"URL: https://ddownload.com/{item.ItemId}");
HttpResponseMessage resp = await profile.Client.GetAsync("https://ddownload.com/" + item.ItemId);
System.Console.WriteLine($"Code: {resp.StatusCode}");
if(start == 0)
{
System.Console.WriteLine($"Redirect: {resp.Headers.Location}");
s = await profile.Client.GetStreamAsync(resp.Headers.Location);
}
else {
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get, resp.Headers.Location);
req.Headers.Range = new System.Net.Http.Headers.RangeHeaderValue(start, item.Size);
Expand Down
8 changes: 4 additions & 4 deletions ShareLoader/Models/AccountModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ public class AccountModel
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Name { get; set; } = "";
public string Username { get; set; } = "";
public string Password { get; set; } = "";
public float TrafficLeft { get; set; }
public float TrafficLeftWeek { get; set; }
public bool IsPremium { get; set; }
public DateTime ValidTill { get; set; }
public float Credit { get; set; }
public string Hoster { get; set; }
public string Hoster { get; set; } = "";
public bool AllowClientRedirect { get; set; } = true; //Todo remove

public bool IsValid()
Expand Down
10 changes: 5 additions & 5 deletions ShareLoader/Models/DownloadItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ namespace ShareLoader.Models;
public class DownloadItem
{
[Key]
public int Id { get; set; }
public int Id { get; set; } = 0;
public string ItemId { get; set; } = "";
public string Name { get; set; } = "";
public int DownloadGroupID { get; set; }
public int DownloadGroupID { get; set; } = 0;
public int GroupID { get; set; } = -1;
[MaxLength(150)]
public string Url { get; set; }
public long Size { get; set; }
public string Url { get; set; } = "";
public long Size { get; set; } = 0;
public States State { get; set; } = States.Waiting;
[MaxLength(32)]
public string MD5 { get; set; } = "";
public string Hoster { get; set; }
public string Hoster { get; set; } = "";


public DownloadItem() { }
Expand Down
2 changes: 1 addition & 1 deletion ShareLoader/Models/ErrorModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public class ErrorModel
public int Id { get; set; }
public int GroupId { get; set; }
public int ItemId { get; set; }
public string FileName { get; set; }
public string FileName { get; set; } = "";
public string Text { get; set; } = "";
}
Loading

0 comments on commit bbb0971

Please sign in to comment.