-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudiobookProcesser.cs
135 lines (119 loc) · 6.19 KB
/
AudiobookProcesser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using ProcessAudiobooks_UI.CustomControls;
using ProcessAudiobooks_UI.DataObjects;
using Renci.SshNet;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace ProcessAudiobooks_UI
{
public class AudiobookProcesser
{
private TextBox tbCommand;
private TextBox tbLocalPath;
private TextBox tbRemotePath;
private EnhancedListView eLvAudiobook;
private Processing processing = Processing.Stopped;
private Button btnStartCreateAudiobooks, btnStopCreateAudiobooks;
public AudiobookProcesser(TextBox tbCommand, TextBox tbLocalPath, TextBox tbRemotePath, EnhancedListView eLvAudiobook, Button btnStartCreateAudiobooks , Button btnStopCreateAudiobooks)
{
this.tbCommand = tbCommand;
this.tbLocalPath = tbLocalPath;
this.tbRemotePath = tbRemotePath;
this.eLvAudiobook = eLvAudiobook;
this.btnStopCreateAudiobooks = btnStopCreateAudiobooks;
this.btnStartCreateAudiobooks = btnStartCreateAudiobooks;
}
public async Task StartProcess(ssh sshClient)
{
if (this.processing == Processing.Stopped) //if not started start.
{
this.processing = Processing.Started;
this.btnStartCreateAudiobooks.IsEnabled = false;
this.btnStopCreateAudiobooks.IsEnabled = true;
foreach (DataObjects.Audiobook book in eLvAudiobook.Items)
{
if (book.Status == DataObjects.AudiobookProcessingStatus.Ready)
{
if (this.processing == Processing.Stopped)
break;
book.Status = DataObjects.AudiobookProcessingStatus.Processing; //set audiobook status
this.eLvAudiobook.Items.Refresh();
ConsoleWindow.WriteInfo("Starting Audiobook: " + book.Name);
ConsoleWindow.WriteInfo("Copying Audiobook to processing path");
string destFolder = tbLocalPath.Text + "\\" + book.outputName;
Directory.CreateDirectory(destFolder);
string[] fileList = book.FileList.ToArray();
foreach (String file in fileList)
{
// get the file attributes for file or directory
FileAttributes attr = File.GetAttributes(file);
string[] fileArray = file.Split("\\");
string fileName = fileArray[fileArray.Length - 1];
//detect whether its a directory or file
if ((attr & FileAttributes.Directory) == FileAttributes.Directory) // if its a directory copy the directory and files individually
{
string trueDestFolder = destFolder + "\\" + fileName;
this.CopyDirectory(file, trueDestFolder);
}
else //if its a file just do a simple file copy
{
System.IO.File.Copy(file, file.Replace(file, destFolder + "\\" + fileName), true);
}
}
//Create Command
String command = tbCommand.Text;
string cmdOutputPath = "output/" + book.outputName;
command = this.addVariables(command, cmdOutputPath, book);
string commandDestFolder = tbRemotePath.Text + "/" + book.outputName;
//Run Command
ConsoleWindow.WriteInfo("Copying book to its output directory");
await sshClient.RunCommand("cd \"" + commandDestFolder + "\" && " +command);
//copy files back
string finalOutputPath = this.addVariables(book.outputPath, cmdOutputPath, book);
ConsoleWindow.WriteInfo("Copying book to its output directory");
this.CopyDirectory(tbLocalPath.Text + "\\" + book.outputName + "\\output\\", finalOutputPath);
ConsoleWindow.WriteInfo("Cleaning up!");
Directory.Delete(tbLocalPath.Text + "\\" + finalOutputPath, true);
ConsoleWindow.WriteInfo("Finished Audiobook: " + book.Name);
book.Status = DataObjects.AudiobookProcessingStatus.Completed; //set audiobook status
this.eLvAudiobook.Items.Refresh();
}
}
}
this.processing = Processing.Stopped;
this.btnStartCreateAudiobooks.IsEnabled = true;
this.btnStopCreateAudiobooks.IsEnabled = false;
}
private void CopyDirectory(string file, string trueDestFolder)
{
Directory.CreateDirectory(trueDestFolder);
//Now Create all of the directories
foreach (string dirPath in Directory.GetDirectories(file, "*",
SearchOption.AllDirectories))
Directory.CreateDirectory(dirPath.Replace(file, trueDestFolder));
//Copy all the files & Replaces any files with the same name
foreach (string newPath in Directory.GetFiles(file, "*.*",
SearchOption.AllDirectories))
File.Copy(newPath, newPath.Replace(file, trueDestFolder), true);
}
public string addVariables(string data,string cmdOutputPath, Audiobook book)
{
return data.Replace("%Name%", book.Name).Replace("%outputName%", cmdOutputPath).Replace("%Artist%", book.Artist).Replace("%Album%", book.Album)
.Replace("%Genre%", book.Genre).Replace("%Year%", book.Year).Replace("%Writer%", book.Writer);
}
public void StopProcess()
{
this.processing = Processing.Stopped;
}
}
public enum Processing
{
Stopped = 0,
Started = 1
}
}