Show existing TAP programs from Nextcloud in uploader
This commit is contained in:
1 parent
d82d7798c5
commit
132d1968c6
3 files changed
+123
-5
No files matched your search
+121
-3
@@ -7,6 +7,7 @@ using System.Net;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Windows.Forms;
|
||||
using System.Xml;
|
||||
|
||||
namespace PlasmaTapUploader
|
||||
{
|
||||
@@ -64,6 +65,13 @@ namespace PlasmaTapUploader
|
||||
|
||||
internal static class WebDav
|
||||
{
|
||||
public sealed class RemoteProgram
|
||||
{
|
||||
public string Name;
|
||||
public long Size;
|
||||
public DateTime? Modified;
|
||||
}
|
||||
|
||||
public static HttpWebRequest Request(CloudConfig config, string url, string method)
|
||||
{
|
||||
var request = (HttpWebRequest)WebRequest.Create(url);
|
||||
@@ -88,6 +96,56 @@ namespace PlasmaTapUploader
|
||||
}
|
||||
}
|
||||
|
||||
public static List<RemoteProgram> ListPrograms(CloudConfig config)
|
||||
{
|
||||
var request = Request(config, config.BaseUrl, "PROPFIND");
|
||||
request.Headers["Depth"] = "1";
|
||||
request.ContentType = "application/xml; charset=utf-8";
|
||||
byte[] body = Encoding.UTF8.GetBytes("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:propfind xmlns:d=\"DAV:\"><d:prop><d:resourcetype/><d:getcontentlength/><d:getlastmodified/></d:prop></d:propfind>");
|
||||
request.ContentLength = body.Length;
|
||||
using (var stream = request.GetRequestStream()) stream.Write(body, 0, body.Length);
|
||||
using (var response = (HttpWebResponse)request.GetResponse())
|
||||
{
|
||||
if ((int)response.StatusCode != 207)
|
||||
throw new InvalidOperationException("Nextcloud antwortet auf die Ordnerliste mit HTTP " + (int)response.StatusCode + ".");
|
||||
var document = new XmlDocument { XmlResolver = null };
|
||||
using (var reader = XmlReader.Create(response.GetResponseStream(), new XmlReaderSettings {
|
||||
DtdProcessing = DtdProcessing.Prohibit, XmlResolver = null, MaxCharactersInDocument = 20000000
|
||||
})) document.Load(reader);
|
||||
var ns = new XmlNamespaceManager(document.NameTable);
|
||||
ns.AddNamespace("d", "DAV:");
|
||||
var result = new List<RemoteProgram>();
|
||||
Uri baseUri = new Uri(config.BaseUrl);
|
||||
string folderPath = Uri.UnescapeDataString(baseUri.AbsolutePath);
|
||||
foreach (XmlNode node in document.SelectNodes("/d:multistatus/d:response", ns))
|
||||
{
|
||||
XmlNode href = node.SelectSingleNode("d:href", ns);
|
||||
if (href == null) continue;
|
||||
Uri itemUri;
|
||||
if (!Uri.TryCreate(baseUri, href.InnerText, out itemUri) ||
|
||||
!String.Equals(itemUri.Host, baseUri.Host, StringComparison.OrdinalIgnoreCase) ||
|
||||
itemUri.Port != baseUri.Port) continue;
|
||||
string path = Uri.UnescapeDataString(itemUri.AbsolutePath);
|
||||
if (!path.StartsWith(folderPath, StringComparison.Ordinal)) continue;
|
||||
string name = path.Substring(folderPath.Length);
|
||||
if (name.Length == 0 || name.IndexOf('/') >= 0 ||
|
||||
!name.EndsWith(".tap", StringComparison.OrdinalIgnoreCase)) continue;
|
||||
XmlNode prop = node.SelectSingleNode("d:propstat[d:status[contains(., ' 200 ')]]/d:prop", ns);
|
||||
if (prop == null || prop.SelectSingleNode("d:resourcetype/d:collection", ns) != null) continue;
|
||||
long size = 0;
|
||||
DateTime modified;
|
||||
XmlNode sizeNode = prop.SelectSingleNode("d:getcontentlength", ns);
|
||||
XmlNode dateNode = prop.SelectSingleNode("d:getlastmodified", ns);
|
||||
if (sizeNode != null) long.TryParse(sizeNode.InnerText, out size);
|
||||
DateTime? lastModified = dateNode != null && DateTime.TryParse(dateNode.InnerText, out modified)
|
||||
? (DateTime?)modified.ToLocalTime() : null;
|
||||
result.Add(new RemoteProgram { Name = name, Size = size, Modified = lastModified });
|
||||
}
|
||||
result.Sort(delegate(RemoteProgram a, RemoteProgram b) { return StringComparer.CurrentCultureIgnoreCase.Compare(a.Name, b.Name); });
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public static void Upload(CloudConfig config, string path, Action<int> progress)
|
||||
{
|
||||
FileInfo file = new FileInfo(path);
|
||||
@@ -175,13 +233,17 @@ namespace PlasmaTapUploader
|
||||
private readonly ProgressBar progressBar = new ProgressBar();
|
||||
private readonly Label statusLabel = new Label();
|
||||
private readonly BackgroundWorker worker = new BackgroundWorker();
|
||||
private readonly ListView remoteList = new ListView();
|
||||
private readonly Button refreshButton = new Button();
|
||||
private readonly Label remoteStatus = new Label();
|
||||
private readonly BackgroundWorker listWorker = new BackgroundWorker();
|
||||
|
||||
public MainForm(CloudConfig config)
|
||||
{
|
||||
this.config = config;
|
||||
Text = "Plasma TAP-Uploader";
|
||||
Font = new Font("Segoe UI", 9);
|
||||
ClientSize = new Size(650, 430);
|
||||
ClientSize = new Size(650, 650);
|
||||
StartPosition = FormStartPosition.CenterScreen;
|
||||
FormBorderStyle = FormBorderStyle.FixedDialog;
|
||||
MaximizeBox = false;
|
||||
@@ -209,7 +271,25 @@ namespace PlasmaTapUploader
|
||||
statusLabel.Location = new Point(22, 390);
|
||||
statusLabel.Size = new Size(605, 28);
|
||||
statusLabel.AutoEllipsis = true;
|
||||
Controls.AddRange(new Control[] { heading, hint, fileList, chooseButton, clearButton, uploadButton, progressBar, statusLabel });
|
||||
var remoteHeading = new Label { Text = "Programme in Nextcloud", Font = new Font("Segoe UI", 12, FontStyle.Bold),
|
||||
AutoSize = true, Location = new Point(22, 432) };
|
||||
refreshButton.Text = "Aktualisieren";
|
||||
refreshButton.Location = new Point(522, 430);
|
||||
refreshButton.Size = new Size(105, 29);
|
||||
remoteList.Location = new Point(22, 467);
|
||||
remoteList.Size = new Size(605, 136);
|
||||
remoteList.View = View.Details;
|
||||
remoteList.FullRowSelect = true;
|
||||
remoteList.GridLines = true;
|
||||
remoteList.Columns.Add("Programm", 355);
|
||||
remoteList.Columns.Add("Groesse", 95);
|
||||
remoteList.Columns.Add("Geaendert", 130);
|
||||
remoteStatus.Text = "Lade Programmliste...";
|
||||
remoteStatus.Location = new Point(22, 612);
|
||||
remoteStatus.Size = new Size(605, 25);
|
||||
remoteStatus.AutoEllipsis = true;
|
||||
Controls.AddRange(new Control[] { heading, hint, fileList, chooseButton, clearButton, uploadButton,
|
||||
progressBar, statusLabel, remoteHeading, refreshButton, remoteList, remoteStatus });
|
||||
|
||||
DragEnter += OnDragEnter;
|
||||
DragDrop += OnDragDrop;
|
||||
@@ -218,7 +298,30 @@ namespace PlasmaTapUploader
|
||||
chooseButton.Click += delegate { ChooseFiles(); };
|
||||
clearButton.Click += delegate { files.Clear(); fileList.Items.Clear(); progressBar.Value = 0; statusLabel.Text = "Liste geleert."; };
|
||||
uploadButton.Click += delegate { StartUpload(); };
|
||||
FormClosing += delegate(object sender, FormClosingEventArgs e) { if (worker.IsBusy) e.Cancel = true; };
|
||||
refreshButton.Click += delegate { RefreshPrograms(); };
|
||||
Shown += delegate { RefreshPrograms(); };
|
||||
FormClosing += delegate(object sender, FormClosingEventArgs e) { if (worker.IsBusy || listWorker.IsBusy) e.Cancel = true; };
|
||||
|
||||
listWorker.DoWork += delegate(object sender, DoWorkEventArgs e) { e.Result = WebDav.ListPrograms(config); };
|
||||
listWorker.RunWorkerCompleted += delegate(object sender, RunWorkerCompletedEventArgs e) {
|
||||
refreshButton.Enabled = true;
|
||||
if (e.Error != null) {
|
||||
remoteStatus.Text = "Programmliste konnte nicht geladen werden: " + WebDav.FriendlyError(e.Error);
|
||||
return;
|
||||
}
|
||||
var programs = (List<WebDav.RemoteProgram>)e.Result;
|
||||
remoteList.BeginUpdate();
|
||||
try {
|
||||
remoteList.Items.Clear();
|
||||
foreach (var program in programs) {
|
||||
var item = new ListViewItem(program.Name);
|
||||
item.SubItems.Add(program.Size.ToString("N0") + " B");
|
||||
item.SubItems.Add(program.Modified.HasValue ? program.Modified.Value.ToString("dd.MM.yyyy HH:mm") : "-");
|
||||
remoteList.Items.Add(item);
|
||||
}
|
||||
} finally { remoteList.EndUpdate(); }
|
||||
remoteStatus.Text = programs.Count + " .tap-Programm(e) im Nextcloud-Ordner.";
|
||||
};
|
||||
|
||||
worker.WorkerReportsProgress = true;
|
||||
worker.DoWork += UploadWork;
|
||||
@@ -229,6 +332,14 @@ namespace PlasmaTapUploader
|
||||
worker.RunWorkerCompleted += UploadCompleted;
|
||||
}
|
||||
|
||||
private void RefreshPrograms()
|
||||
{
|
||||
if (listWorker.IsBusy) return;
|
||||
refreshButton.Enabled = false;
|
||||
remoteStatus.Text = "Lade Programmliste...";
|
||||
listWorker.RunWorkerAsync();
|
||||
}
|
||||
|
||||
private void OnDragEnter(object sender, DragEventArgs e)
|
||||
{
|
||||
e.Effect = !worker.IsBusy && e.Data.GetDataPresent(DataFormats.FileDrop)
|
||||
@@ -327,11 +438,13 @@ namespace PlasmaTapUploader
|
||||
int completed = failure == null ? 0 : failure.Completed;
|
||||
statusLabel.Text = completed + " von " + files.Count + " Datei(en) hochgeladen. Fehler: " + WebDav.FriendlyError(error);
|
||||
MessageBox.Show(this, statusLabel.Text, "Upload fehlgeschlagen", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
RefreshPrograms();
|
||||
return;
|
||||
}
|
||||
progressBar.Value = 100;
|
||||
statusLabel.Text = e.Result + " Datei(en) in Nextcloud gespeichert. Jetzt den ESP per Reset neu starten.";
|
||||
MessageBox.Show(this, statusLabel.Text, "Upload abgeschlossen", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
RefreshPrograms();
|
||||
}
|
||||
|
||||
private sealed class UploadFailure
|
||||
@@ -358,6 +471,11 @@ namespace PlasmaTapUploader
|
||||
Console.WriteLine("Nextcloud-Ordner erreichbar: " + config.Folder);
|
||||
return 0;
|
||||
}
|
||||
if (args.Length > 0 && args[0] == "--list")
|
||||
{
|
||||
Console.WriteLine("Programme im Nextcloud-Ordner: " + WebDav.ListPrograms(config).Count);
|
||||
return 0;
|
||||
}
|
||||
if (args.Length > 0 && args[0] == "--test-upload")
|
||||
{
|
||||
WebDav.WriteTest(config);
|
||||
|
||||
Reference in new issue
Block a user