Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
Fix duplicate files in zips, add priority support, remove download st…
Browse files Browse the repository at this point in the history
…ats (for now)
  • Loading branch information
noahc3 committed Jul 3, 2019
1 parent 524a5e2 commit 7d74f6e
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 240 deletions.
32 changes: 17 additions & 15 deletions SDSetupBackend/Controllers/v1.cs
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
Expand Down Expand Up @@ -57,7 +59,8 @@ public class v1 : ControllerBase {
Program.uuidLocks.Add(uuid);

string[] requestedPackages = packages.Split(';');
List<KeyValuePair<string, string>> files = new List<KeyValuePair<string, string>>();
//List<KeyValuePair<string, string>> files = new List<KeyValuePair<string, string>>();
OrderedDictionary files = new OrderedDictionary();
foreach (string k in requestedPackages) {
//sanitize input
if (k.Contains("/") || k.Contains("\\") || k.Contains("..") || k.Contains("~") || k.Contains("%")) {
Expand All @@ -69,29 +72,27 @@ public class v1 : ControllerBase {
foreach (string f in EnumerateAllFiles((Program.Files + "/" + packageset + "/" + k + "/" + channel).AsPath())) {
if (client == "hbswitch") {
if (f.StartsWith((Program.Files + "/" + packageset + "/" + k + "/" + channel + "/sd").AsPath())) {
files.Add(new KeyValuePair<string, string>(f.Replace((Program.Files + "/" + packageset + "/" + k + "/" + channel + "/sd").AsPath(), ""), f));
files[f.Replace((Program.Files + "/" + packageset + "/" + k + "/" + channel + "/sd").AsPath(), "")] = f;
}
} else {
files.Add(new KeyValuePair<string, string>(f.Replace((Program.Files + "/" + packageset + "/" + k + "/" + channel).AsPath(), ""), f));
files[f.Replace((Program.Files + "/" + packageset + "/" + k + "/" + channel).AsPath(), "")] = f;
}
}


}

Program.dlStats.IncrementPackageDownloadCount(k);
}

Program.dlStats.AllTimeBundles++;
DeletingFileStream stream = (DeletingFileStream) ZipFromFilestreams(files.ToArray(), uuid);
DeletingFileStream stream = (DeletingFileStream) ZipFromFilestreams(files, uuid);

Program.generatedZips[uuid] = stream;
stream.Timeout(30000);

Program.uuidLocks.Remove(uuid);
return new ObjectResult("READY");
} catch (Exception) {
} catch (Exception e) {
Program.uuidLocks.Remove(uuid);
Console.WriteLine(e.Message);
return new ObjectResult("Internal server error occurred");
}
}
Expand Down Expand Up @@ -136,13 +137,15 @@ public class v1 : ControllerBase {
[HttpGet("fetch/dlstats")]
public ActionResult GetDownloadStats() {
//TODO: dont do this
return new ObjectResult(Program.dlStats.ToDataBinary(U.GetPackageListInLatestPackageset()));
//return new ObjectResult(Program.dlStats.ToDataBinary(U.GetPackageListInLatestPackageset()));
return null;
}

[HttpGet("fetch/dlstatsdebug")]
public ActionResult GetDownloadStatsDebug() {
//TODO: dont do this
return new ObjectResult(JsonConvert.SerializeObject(DownloadStats.FromDataBinary(Program.dlStats.ToDataBinary(U.GetPackageListInLatestPackageset())), Formatting.Indented));
//return new ObjectResult(JsonConvert.SerializeObject(DownloadStats.FromDataBinary(Program.dlStats.ToDataBinary(U.GetPackageListInLatestPackageset())), Formatting.Indented));
return null;
}

[HttpGet("get/latestpackageset")]
Expand Down Expand Up @@ -196,18 +199,17 @@ public class v1 : ControllerBase {
}


public static Stream ZipFromFilestreams(KeyValuePair<string, string>[] files, string uuid) {
public static Stream ZipFromFilestreams(OrderedDictionary files, string uuid) {

DeletingFileStream outputMemStream = new DeletingFileStream((Program.Temp + "/" + Guid.NewGuid().ToString().Replace("-", "").ToLower()).AsPath(), FileMode.Create, uuid);
ZipOutputStream zipStream = new ZipOutputStream(outputMemStream);

zipStream.SetLevel(3); //0-9, 9 being the highest level of compression

foreach(KeyValuePair<string, string> f in files) {
ZipEntry newEntry = new ZipEntry(f.Key);
foreach(DictionaryEntry f in files) {
ZipEntry newEntry = new ZipEntry((string) f.Key);
newEntry.DateTime = DateTime.Now;
zipStream.PutNextEntry(newEntry);
FileStream fs = new FileStream(f.Value, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
FileStream fs = new FileStream((string) f.Value, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
fs.CopyTo(zipStream, 4096);
//StreamUtils.Copy(fs, zipStream, new byte[4096]);
fs.Close();
Expand Down
60 changes: 30 additions & 30 deletions SDSetupBackend/Program.cs
Expand Up @@ -34,9 +34,9 @@ public class Program {
public static string latestPackageset = "default";
public static string latestAppVersion = "NO VERSION";

public static DownloadStats dlStats;
private static bool dlStatsInitialized = false;
private static Timer dlStatsSaveTimer;
//public static DownloadStats dlStats;
//private static bool dlStatsInitialized = false;
//private static Timer dlStatsSaveTimer;

private static string _privelegedUUID;
private static string privelegedUUID {
Expand Down Expand Up @@ -96,10 +96,10 @@ public class Program {
try {
#endif
//if dlstats was initialized, write them to disk before reloading.
if (dlStatsInitialized) {
dlStats.VerifyStatisticsIntegrity(U.GetPackageListInLatestPackageset());
File.WriteAllText((Config + "/dlstats.bin").AsPath(), dlStats.ToDataBinary(U.GetPackageList(latestPackageset)));
}
//if (dlStatsInitialized) {
// dlStats.VerifyStatisticsIntegrity(U.GetPackageListInLatestPackageset());
// File.WriteAllText((Config + "/dlstats.bin").AsPath(), dlStats.ToDataBinary(U.GetPackageList(latestPackageset)));
//}

//use temporary variables so if anything goes wrong, values wont be out of sync.
Dictionary<string, string> _Manifests = new Dictionary<string, string>();
Expand Down Expand Up @@ -139,32 +139,32 @@ public class Program {
//this must be set before GetPackageListInLatestPackageset() is called
Files = _Files;

DownloadStats _dlStats;
//DownloadStats _dlStats;

if (File.Exists((_Config + "/dlstats.bin").AsPath())) {
_dlStats = DownloadStats.FromDataBinary(File.ReadAllText((_Config + "/dlstats.bin").AsPath()));
} else {
_dlStats = new DownloadStats();
}
//if (File.Exists((_Config + "/dlstats.bin").AsPath())) {
// _dlStats = DownloadStats.FromDataBinary(File.ReadAllText((_Config + "/dlstats.bin").AsPath()));
//} else {
// _dlStats = new DownloadStats();
//}

Manifest latestManifest = JsonConvert.DeserializeObject<Manifest>(_Manifests[_latestPackageset]);
_dlStats.VerifyStatisticsIntegrity(U.GetPackageList(_latestPackageset), latestManifest);
//_dlStats.VerifyStatisticsIntegrity(U.GetPackageList(_latestPackageset), latestManifest);
_Manifests[_latestPackageset] = JsonConvert.SerializeObject(latestManifest);

if (dlStatsSaveTimer != null) dlStatsSaveTimer.Stop();
dlStatsSaveTimer = new Timer();
#if (DEBUG)
dlStatsSaveTimer.Interval = 10000; //10 seconds
#else
dlStatsSaveTimer.Interval = 600000; //10 minutes
#endif
dlStatsSaveTimer.AutoReset = true;
dlStatsSaveTimer.Elapsed += (sender, e) => {
dlStats.VerifyStatisticsIntegrity(U.GetPackageListInLatestPackageset());
File.WriteAllText((Config + "/dlstats.bin").AsPath(), dlStats.ToDataBinary(U.GetPackageList(latestPackageset)));
Console.WriteLine("[ SAVE ] Wrote download stats to file (" + DateTime.Now.ToShortDateString() + " | " + DateTime.Now.ToShortTimeString() + ").");
};
dlStatsSaveTimer.Start();
//if (dlStatsSaveTimer != null) dlStatsSaveTimer.Stop();
//dlStatsSaveTimer = new Timer();
#if (DEBUG) //
//dlStatsSaveTimer.Interval = 10000; //10 seconds
#else //
//dlStatsSaveTimer.Interval = 600000; //10 minutes
#endif //
//dlStatsSaveTimer.AutoReset = true;
//dlStatsSaveTimer.Elapsed += (sender, e) => {
// dlStats.VerifyStatisticsIntegrity(U.GetPackageListInLatestPackageset());
// File.WriteAllText((Config + "/dlstats.bin").AsPath(), dlStats.ToDataBinary(U.GetPackageList(latestPackageset)));
// Console.WriteLine("[ SAVE ] Wrote download stats to file (" + DateTime.Now.ToShortDateString() + " | " + DateTime.Now.ToShortTimeString() + ").");
//};
//dlStatsSaveTimer.Start();

//update the real variables
Temp = _Temp;
Expand All @@ -173,9 +173,9 @@ public class Program {
latestAppVersion = _latestAppVersion;
validChannels = _validChannels;
Manifests = _Manifests;
dlStats = _dlStats;
//dlStats = _dlStats;

dlStatsInitialized = true;
//dlStatsInitialized = true;
#if (!DEBUG)
} catch (Exception e) {
return "[ERROR] Something went wrong while reloading: \n\n\nMessage:\n " + e.Message + "\n\nStack Trace:\n" + e.StackTrace + "\n\n\nThe server will continue running and no changes will be saved";
Expand Down
1 change: 0 additions & 1 deletion SDSetupBackend/Properties/launchSettings.json
Expand Up @@ -18,7 +18,6 @@
},
"sdsetup_backend": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/v1/fetch",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
Expand Down
4 changes: 2 additions & 2 deletions SDSetupBlazor/G.cs
Expand Up @@ -15,8 +15,8 @@ namespace SDSetupBlazor
{
public static class G {
#if (DEBUG)
//public static string hostname = "http://localhost:58109";
public static string hostname = "https://files.sdsetup.com";
public static string hostname = "http://localhost:5000";
//public static string hostname = "https://files.sdsetup.com";
#else
public static string hostname = "https://files.sdsetup.com";
#endif
Expand Down

0 comments on commit 7d74f6e

Please sign in to comment.