Skip to content

Commit

Permalink
Merge pull request #2 from strohitv/duration
Browse files Browse the repository at this point in the history
Uploader now shows durations of upload.
  • Loading branch information
strohitv committed Jan 20, 2019
2 parents 401f688 + 26bc212 commit 77f52cd
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 14 deletions.
32 changes: 22 additions & 10 deletions STFU.Executable.AutoUploader/Forms/UploadForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using STFU.Lib.Youtube.Interfaces.Model.Enums;
using System;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;

namespace STFU.Executable.AutoUploader.Forms
Expand All @@ -12,9 +13,10 @@ public partial class UploadForm : Form
IAutomationUploader autoUploader = null;

string fileText = string.Empty;
string statusText = "Warte auf Dateien für den Upload...";
int progress = 0;

string[] statusTextLines = new [] { "Warte auf Dateien für den Upload...", string.Empty };

bool aborted = false;
bool ended = false;
bool allowChosingProcs = false;
Expand Down Expand Up @@ -55,11 +57,11 @@ private void CurrentUploadPropertyChanged(object sender, PropertyChangedEventArg
{
if (job.State == UploadState.ThumbnailUploading)
{
statusText = $"Lade Thumbnail hoch: {job.Progress:0.00} %";
statusTextLines[0] = $"Lade Thumbnail hoch: {job.Progress:0.00} %";
}
else
{
statusText = $"Lade Video hoch: {job.Progress:0.00} %";
statusTextLines[0] = $"Lade Video hoch: {job.Progress:0.00} %";
}
progress = (int)(job.Progress * 100);
}
Expand All @@ -70,19 +72,24 @@ private void CurrentUploadPropertyChanged(object sender, PropertyChangedEventArg
case UploadState.NotStarted:
case UploadState.Running:
fileText = job.Video.Title;
statusText = $"Video-Upload wird gestartet...";
statusTextLines[0] = $"Video-Upload wird gestartet...";
statusTextLines[1] = string.Empty;
break;
case UploadState.ThumbnailUploading:
statusText = $"Thumbnail-Upload wird gestartet...";
statusTextLines[0] = $"Thumbnail-Upload wird gestartet...";
statusTextLines[1] = string.Empty;
break;
case UploadState.CancelPending:
statusText = $"Upload wird abgebrochen...";
statusTextLines[0] = $"Upload wird abgebrochen...";
statusTextLines[1] = string.Empty;
break;
case UploadState.Error:
statusText = $"Es gab einen Fehler beim Upload.";
statusTextLines[0] = $"Es gab einen Fehler beim Upload.";
statusTextLines[1] = string.Empty;
break;
case UploadState.Canceled:
statusText = $"Upload wurde abgebrochen.";
statusTextLines[0] = $"Upload wurde abgebrochen.";
statusTextLines[1] = string.Empty;
break;
case UploadState.Successful:
fileText = oldtitle;
Expand All @@ -91,13 +98,18 @@ private void CurrentUploadPropertyChanged(object sender, PropertyChangedEventArg
throw new ArgumentException("Dieser Status wird nicht unterstützt.");
}
}
else if (e.PropertyName == nameof(job.RemainingDuration) || e.PropertyName == nameof(job.UploadedDuration))
{
statusTextLines[1] = $"Bisher benötigt: {job.UploadedDuration.ToString("hh\\:mm\\:ss")}, verbleibende Zeit: {job.RemainingDuration.ToString("hh\\:mm\\:ss")}";
}
}

private void UploaderPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(autoUploader.Uploader.State) && autoUploader.Uploader.State == UploaderState.Waiting)
{
statusText = $"Upload abgeschlossen.{Environment.NewLine}Warte auf Dateien für den Upload...";
statusTextLines[0] = "Upload abgeschlossen.";
statusTextLines[1] = "Warte auf Dateien für den Upload...";
}
}

Expand All @@ -118,7 +130,7 @@ private void AutoUploaderPropertyChanged(object sender, PropertyChangedEventArgs
private void refreshTimerTick(object sender, EventArgs e)
{
fileLabel.Text = fileText;
statusLabel.Text = statusText;
statusLabel.Text = statusTextLines.Aggregate((i, j) => i + Environment.NewLine + j);
prgbarProgress.Value = progress;

if (ended)
Expand Down
4 changes: 1 addition & 3 deletions STFU.Lib.Youtube.Automation/Programming/ScriptType.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace STFU.Lib.Youtube.Automation.Programming
namespace STFU.Lib.Youtube.Automation.Programming
{
internal enum ScriptType
{
Expand Down
10 changes: 10 additions & 0 deletions STFU.Lib.Youtube.Interfaces/Model/IYoutubeJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,15 @@ public interface IYoutubeJob : INotifyPropertyChanged
/// Contains information about the error if something fails
/// </summary>
IYoutubeError Error { get; }

/// <summary>
/// Time the Job has being uploaded
/// </summary>
TimeSpan UploadedDuration { get; }

/// <summary>
/// Remaining Time the Job will probably need to finish uploading
/// </summary>
TimeSpan RemainingDuration { get; }
}
}
35 changes: 35 additions & 0 deletions STFU.Lib.Youtube/Internal/InternalYoutubeJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,41 @@ public UploadState State
}
}

private TimeSpan uploadedDuration = new TimeSpan(0, 0, 0);
private TimeSpan remainingDuration = new TimeSpan(0, 0, 0);

public TimeSpan UploadedDuration
{
get
{
return uploadedDuration;
}
set
{
if (uploadedDuration != value)
{
uploadedDuration = value;
OnPropertyChanged();
}
}
}

public TimeSpan RemainingDuration
{
get
{
return remainingDuration;
}
set
{
if (remainingDuration != value)
{
remainingDuration = value;
OnPropertyChanged();
}
}
}

public IYoutubeVideo Video { get; }

public Uri Uri { get; set; }
Expand Down
43 changes: 42 additions & 1 deletion STFU.Lib.Youtube/Internal/Upload/FileUploader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel;
using System;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Runtime.CompilerServices;
Expand All @@ -25,6 +26,42 @@ private set
}
}

private DateTime started;
private TimeSpan uploadedDuration = new TimeSpan(0, 0, 0);
private TimeSpan remainingDuration = new TimeSpan(0, 0, 0);

public TimeSpan UploadedDuration
{
get
{
return uploadedDuration;
}
private set
{
if (uploadedDuration != value)
{
uploadedDuration = value;
OnPropertyChanged();
}
}
}

public TimeSpan RemainingDuration
{
get
{
return remainingDuration;
}
private set
{
if (remainingDuration != value)
{
remainingDuration = value;
OnPropertyChanged();
}
}
}

private RunningState state = RunningState.NotRunning;
public RunningState State
{
Expand Down Expand Up @@ -81,6 +118,7 @@ internal bool UploadFile(string path, HttpWebRequest request, long maxFileSize,
fileStream.Position = startPosition;
}

started = DateTime.Now;
State = RunningState.Running;

// Upload initiieren
Expand All @@ -95,6 +133,9 @@ internal bool UploadFile(string path, HttpWebRequest request, long maxFileSize,
{
requestStream.Write(buffer, 0, bytesRead);
Progress = fileStream.Position / (double)fileStream.Length * 100;

UploadedDuration = DateTime.Now - started;
RemainingDuration = new TimeSpan(0 , 0, (int)(UploadedDuration.TotalSeconds / Progress * 100));
}
}
catch (WebException)
Expand Down
8 changes: 8 additions & 0 deletions STFU.Lib.Youtube/Internal/Upload/YoutubeThumbnailUploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ private void FileUploaderPropertyChanged(object sender, PropertyChangedEventArgs
{
Job.Progress = fileUploader.Progress;
}
else if (e.PropertyName == nameof(fileUploader.RemainingDuration))
{
Job.RemainingDuration = fileUploader.RemainingDuration;
}
else if (e.PropertyName == nameof(fileUploader.UploadedDuration))
{
Job.UploadedDuration = fileUploader.UploadedDuration;
}
else
{
Job.Error = FailReasonConverter.GetError(fileUploader.FailureReason);
Expand Down
8 changes: 8 additions & 0 deletions STFU.Lib.Youtube/Internal/Upload/YoutubeVideoUploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ private void OnUploadProgressChanged(object sender, PropertyChangedEventArgs e)
{
Job.Progress = fileUploader.Progress;
}
else if (e.PropertyName == nameof(fileUploader.RemainingDuration))
{
Job.RemainingDuration = fileUploader.RemainingDuration;
}
else if (e.PropertyName == nameof(fileUploader.UploadedDuration))
{
Job.UploadedDuration = fileUploader.UploadedDuration;
}
else
{
Job.Error = FailReasonConverter.GetError(fileUploader.FailureReason);
Expand Down

0 comments on commit 77f52cd

Please sign in to comment.