You are on page 1of 14

<%@ Page Language="C#" %>

<%@ Import Namespace="System" %>


<%@ Import Namespace="System.Diagnostics" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Threading" %>
<html>
<head>
<title>Download Web Application</title>
</head>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
if (Request.Form["__EVENTTARGET"] == "Read_Log")
{
Read_Log();
}
Welcome.Text = "Salut, <b>" + Context.User.Identity.Name + "</b>!<p> Ce
vreti sa downloadam?";
Output_Logfolder1.Attributes.Add("onchange", "validateLog();");
Output_Logfolder2.Attributes.Add("onchange", "validateLog2();");
Extensions_Wanted2.Attributes.Add("onblur", "validate();");
Extensions_Unwanted2.Attributes.Add("onblur", "validate();");
Recursive2.InputAttributes.Add("onchange", "recLevel();");
if (Output_Logfolder1.Text == "")
{
Output_Logfile1.Enabled = false;
}
else
{
Output_Logfile1.Enabled = true;
}
if (Output_Logfolder2.Text == "")
{
Output_Logfile2.Enabled = false;
}
else
{
Output_Logfile2.Enabled = true;
}
if (Recursive2.Checked == false)
{
RecursiveLevel2.Enabled = false;
}
else
{
RecursiveLevel2.Enabled = true;
}
}
void Signout_Click(object sender, EventArgs e)
{
FormsAuthentication.SignOut();
Response.Redirect("Logon.aspx");
}
void ConsoleTextChange(object sender, EventArgs e)
{
Read_Log();
}
protected void Action_Select(object sender, EventArgs e)
{
switch (ActionList.SelectedItem.Text)
{
case "Fisier":
{
SitePnl.Visible = false;
FilePnl.Visible = true;
ConsoleOutput.Visible = false;
DownloadStatus.Visible = false;
return;
}
case "Site":
{
FilePnl.Visible = false;
SitePnl.Visible = true;
ConsoleOutput.Visible = false;
DownloadStatus.Visible = false;
return;
}
default:
FilePnl.Visible = false;
SitePnl.Visible = false;
ConsoleOutput.Visible = false;
DownloadStatus.Visible = false;
return;
}
}
void Download_File(object sender, EventArgs e)
{
Validate();
if (Page.IsValid)
{
ConsoleOutput.Text = "";
ValidationText.Text = "";
DownloadStatus.Visible = true;
ConsoleOutput.Visible = true;
DownloadFile();
return;
}
ValidationText.Text = "Campurile de mai sus nu au fost completate corect
!";
return;
}
void Download_Site(object sender, EventArgs e)
{
Validate();
if (Page.IsValid)
{
ConsoleOutput.Text = "";
ValidationText.Text = "";
DownloadStatus.Visible = true;
ConsoleOutput.Visible = true;
DownloadSite();
return;
}
ValidationText.Text = "Campurile de mai sus nu au fost completate corect
!";
return;
}
void Read_Log()
{
System.Threading.Thread.Sleep(1000);
if (File.Exists(Output_Logfolder2.Text + @"\" + Output_Logfile2.Text))
{
long sf = 0;
FileInfo fi = new FileInfo(Output_Logfolder2.Text + @"\" + Output_Lo
gfile2.Text);
long si = fi.Length;
while (si != sf)
{
FileInfo file = new FileInfo(Output_Logfolder2.Text + @"\" + Out
put_Logfile2.Text);
FileStream s = null;
try
{
s = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare
.None);
return;
}
catch (IOException)
{
return;
}
finally
{
if (s != null)
{
s.Close();
Stream stream = File.Open(Output_Logfolder2.Text + @"\"
+ Output_Logfile2.Text, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader streamReader = new StreamReader(stream);
string str = streamReader.ReadToEnd();
ConsoleOutput.Text = str;
Page.ClientScript.RegisterStartupScript(this.GetType(),
"CallMyFunctionScroll", "scrollBottom();", true);
streamReader.Close();
stream.Close();
}
else
{
sf = fi.Length;
Stream stream = File.Open(Output_Logfolder2.Text + @"\"
+ Output_Logfile2.Text, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader streamReader = new StreamReader(stream);
string str = streamReader.ReadToEnd();
ConsoleOutput.Text = str;
Page.ClientScript.RegisterStartupScript(this.GetType(),
"CallMyFunctionScroll", "scrollBottom();", true);
Page.ClientScript.RegisterStartupScript(this.GetType(),
"CallMyFunctionRead", "readLog();", true);
streamReader.Close();
stream.Close();
si = fi.Length;
}
}
}
}
}
void Downloader(String dwnld)
{
ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe");
Process p = new Process();
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
p = Process.Start(startInfo);
p.StandardInput.WriteLine(@dwnld);
p.StandardInput.WriteLine(@"exit");
p.Close();
Read_Log();
}
void DownloadFile()
{
StringBuilder wgetQuery = new StringBuilder();
wgetQuery.Append("wget --no-check-certificate --progress=bar:force ");
if (UrlTxt1.Text.Length > 0)
{
if (UrlTxt1.Text.StartsWith("http://") || UrlTxt1.Text.StartsWith("h
ttps://"))
{
wgetQuery.Append(UrlTxt1.Text + " ");
}
else
{
wgetQuery.Append("http://" + UrlTxt1.Text + " ");
}
}
if (Output_Folder1.Text.Length > 0)
{
String out_folder = Output_Folder1.Text;
wgetQuery.Append("--output-document=" + out_folder + @"\");
}
if (Output_File1.Text.Length > 0)
{
wgetQuery.Append(Output_File1.Text);
}
if (Output_Extension1.Text.Length > 0)
{
wgetQuery.Append(Output_Extension1.Text + " ");
}
if (Output_Logfolder1.Text.Length > 0)
{
String out_logfolder = Output_Logfolder1.Text;
wgetQuery.Append(" --append-output=" + out_logfolder + @"\");
if (Output_Logfile1.Text.Length > 0)
{
wgetQuery.Append(Output_Logfile1.Text + " ");
}
else
{
Output_Logfile1.Text = "logfile";
wgetQuery.Append(Output_Logfile1.Text + " ");
}
}
if (ServerResponse1.Checked == true)
{
wgetQuery.Append(" -S ");
}
if (Try1.Text.Length > 0)
{
wgetQuery.Append(" --tries=" + Try1.Text + " ");
}
if (Limit1.Text.Length > 0)
{
wgetQuery.Append(" --limit-rate=" + Limit1.Text + "k ");
}
ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe");
Process p = new Process();
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
p = Process.Start(startInfo);
p.StandardInput.WriteLine(@wgetQuery);
p.StandardInput.WriteLine(@"exit");
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
p.WaitForExit();
p.Close();
if (File.Exists(Output_Logfolder1.Text + @"\" + Output_Logfile1.Text))
{
long sf = 0;
FileInfo fi = new FileInfo(Output_Logfolder1.Text + @"\" + Output_Lo
gfile1.Text);
long si = fi.Length;
while (si != sf)
{

sf = fi.Length;
Stream stream = File.Open(Output_Logfolder1.Text + @"\" + Output
_Logfile1.Text, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader streamReader = new StreamReader(stream);
string str = streamReader.ReadToEnd();
ConsoleOutput.Text = str;
streamReader.Close();
stream.Close();
}
}
Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction"
, "scrollBottom()", true);
}
void DownloadSite()
{
StringBuilder wgetQuery = new StringBuilder();
wgetQuery.Append("wget --no-check-certificate --progress=bar:force ");
if (UrlTxt2.Text.Length > 0)
{
if (UrlTxt2.Text.StartsWith("http://"))
{
wgetQuery.Append(UrlTxt2.Text + " ");
}
else
{
wgetQuery.Append("http://" + UrlTxt2.Text + " ");
}
}
if (Output_Folder2.Text.Length > 0)
{
String out_folder = Output_Folder2.Text;
wgetQuery.Append(" -P " + out_folder + @"\");
}
if (CreateFolder2.Checked == false)
{
wgetQuery.Append(" -nd ");
}
if (Extensions_Wanted2.Text.Length > 0)
{
wgetQuery.Append(" -A " + Extensions_Wanted2.Text);
}
if (Extensions_Unwanted2.Text.Length > 0)
{
wgetQuery.Append(" -R " + Extensions_Unwanted2.Text);
}
if (ParentDirectories2.Checked == false)
{
wgetQuery.Append(" -np ");
}
if (Recursive2.Checked == true)
{
wgetQuery.Append(" -r ");
if (RecursiveLevel2.Text.Length > 0)
{
wgetQuery.Append(" -l " + Extensions_Unwanted2.Text);
}
else
{
wgetQuery.Append(" --level=5 ");
}
}
if (Output_Logfolder2.Text.Length > 0)
{
String out_logfolder = Output_Logfolder2.Text;
wgetQuery.Append(" --append-output=" + out_logfolder + @"\");
if (Output_Logfile2.Text.Length > 0)
{
wgetQuery.Append(Output_Logfile2.Text + " ");
}
else
{
Output_Logfile2.Text = "logfile";
wgetQuery.Append(Output_Logfile2.Text + " ");
}
}
if (ServerResponse2.Checked == true)
{
wgetQuery.Append(" -S ");
}
if (Try2.Text.Length > 0)
{
wgetQuery.Append(" --tries=" + Try2.Text + " ");
}
if (Limit2.Text.Length > 0)
{
wgetQuery.Append(" --limit-rate=" + Limit2.Text + "k ");
}
Downloader(wgetQuery.ToString());
}

</script>
<script type="text/javascript">
function readLog() {
console.log("in readLog() javascript");
__doPostBack("Read_Log", null);
}
function validate(_this) {
var wanted = document.getElementById("Extensions_Wanted2");
var unWanted = document.getElementById("Extensions_Unwanted2");
wanted.onblur = function () {
if (wanted.value != "") {
unWanted.value = "";
};
}
unWanted.onblur = function () {
if (unWanted.value != "") {
wanted.value = "";
}
};
}
function validateLog(_this) {
var file = document.getElementById("Output_Logfile1");
var fileR = document.getElementById("Output_Logfile1R");
var folder = document.getElementById("Output_Logfolder1");
folder.onblur = function () {
if (folder.value != "") {
file.disabled = false;
fileR.style.display = 'inherit';
} else {
file.disabled = true;
fileR.style.display = 'none';
file.value = "";
}
}
}
function validateLog2(_this) {
var file = document.getElementById("Output_Logfile2");
var fileR = document.getElementById("Output_Logfile2R");
var folder = document.getElementById("Output_Logfolder2");
folder.onblur = function () {
if (folder.value != "") {
file.disabled = false;
fileR.style.display = 'inherit';
} else {
file.disabled = true;
fileR.style.display = 'none';
file.value = "";
}
}
}
function scrollBottom() {
var ConsoleOutput = document.getElementById("ConsoleOutput");
ConsoleOutput.scrollTop = ConsoleOutput.scrollHeight;
ConsoleOutput.blur();
}
function recLevel(_this) {
var recursive = document.getElementById("Recursive2");
var recursiveLevel = document.getElementById("RecursiveLevel2");
var recursiveLevelR = document.getElementById("RecursiveLevel2R");
if (recursive.checked) {
recursiveLevel.disabled = false;
recursiveLevelR.style.display = 'inherit';
} else {
recursiveLevel.disabled = true;
recursiveLevelR.style.display = 'none';
recursiveLevel.value = "";
}
}
</script>
<body>
<div id="wrapper" style="width: 1080px; margin-right: auto; margin-left: aut
o; margin-top: 75px;">
<h3>Download Web Application</h3>
<asp:Label ID="Welcome" runat="server" />
<form id="Form" runat="server">
<asp:DropDownList ID="ActionList" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="Action_Select">
<asp:ListItem></asp:ListItem>
<asp:ListItem>Fisier</asp:ListItem>
<asp:ListItem>Site</asp:ListItem>
</asp:DropDownList>
<p>
<%--File--%>
<asp:Panel ID="FilePnl" runat="server" Visible="false">
<asp:Label Text="Parametrii Download:" runat="server" />
<table>
<tr>
<td>
<asp:Label ID="UrlL1" Text="URL:" runat="server"
/>
</td>
<td>
<asp:TextBox ID="UrlTxt1" Width="500" runat="ser
ver" ValidationGroup="FileValidation" />
<asp:RequiredFieldValidator
ControlToValidate="UrlTxt1"
ErrorMessage="Camp necesar de completat!"
runat="server" />
</td>
<td></td>
</tr>
<tr>
<td>
<asp:Label ID="Output_FolderL1" Text="Cale Folde
r Download (local):" runat="server" />
</td>
<td>
<asp:TextBox ID="Output_Folder1" Width="500" run
at="server" ValidationGroup="FileValidation" />
<asp:RequiredFieldValidator
ControlToValidate="Output_Folder1"
ErrorMessage="Camp necesar de completat!"
runat="server" />
</td>
<td></td>
</tr>
<tr>
<td>
<asp:Label ID="Output_FileL1" Text="Nume Fisier
Download (local):" runat="server" />
</td>
<td>
<asp:TextBox ID="Output_File1" Width="500" runat
="server" ValidationGroup="FileValidation" />
<asp:RequiredFieldValidator
ControlToValidate="Output_File1"
ErrorMessage="Camp necesar de completat!"
runat="server" />
</td>
<td></td>
</tr>
<tr>
<td>
<asp:Label ID="Output_ExtensionL1" Text="Extensi
e Fisier Download (local):" runat="server" />
</td>
<td>
<asp:TextBox ID="Output_Extension1" Width="500"
runat="server" ValidationGroup="FileValidation" />
<asp:RequiredFieldValidator
ControlToValidate="Output_Extension1"
ErrorMessage="Camp necesar de completat!"
runat="server" />
</td>
<td></td>
</tr>
<tr>
<td>
<asp:Label ID="Output_LogfolderL1" Text="Folder
Log (local):" runat="server" />
</td>
<td>
<asp:TextBox ID="Output_Logfolder1" Width="500"
runat="server" />
</td>
<td></td>
</tr>
<tr>
<td>
<asp:Label ID="Output_LogfileL1" Text="Fisier Lo
g (local):" runat="server" />
</td>
<td>
<asp:TextBox ID="Output_Logfile1" Width="500" ru
nat="server" Enabled="false" />
<asp:Label ID="Output_Logfile1R" Text="logfile (
nume implicit)" runat="server" Style="display: none; float: right;" />
</td>
<td></td>
</tr>
<tr>
<td>
<asp:Label ID="ServerResponse1L" Text="Logheaza
Raspunsul Serverului:" runat="server" />
</td>
<td>
<asp:CheckBox ID="ServerResponse1" Width="500" r
unat="server" />
</td>
<td></td>
</tr>
<tr>
<td>
<asp:Label ID="Try1L" Text="Nr. Incercari conect
are:" runat="server" />
</td>
<td>
<asp:TextBox ID="Try1" Width="500" runat="server
" ValidationGroup="FileValidation" />
<asp:RangeValidator
ControlToValidate="Try1"
MinimumValue="1"
MaximumValue="99999"
Type="Integer"
ErrorMessage="Valoarea trebuie sa reprezinte
un numar!"
runat="server" />
</td>
<td></td>
</tr>
<tr>
<td>
<asp:Label ID="LimitL1" Text="Limita Download:"
runat="server" />
</td>
<td>
<asp:TextBox ID="Limit1" Width="500" runat="serv
er" ValidationGroup="FileValidation" /><asp:Label Text=" (k)" runat="server" />
<asp:RangeValidator
ControlToValidate="Limit1"
MinimumValue="1"
MaximumValue="99999"
Type="Integer"
ErrorMessage="Valoarea trebuie sa reprezinte
un numar!"
runat="server" />
</td>
<td></td>
</tr>
</table>
<p>
<asp:Button ID="DownloadFileBtn" OnClick="Download_File"
Text="Download" runat="server" ValidationGroup="FileValidation" Style="float: l
eft;" />
</asp:Panel>
<%--Site--%>
<asp:Panel ID="SitePnl" runat="server" Visible="false">
<asp:Label Text="Parametrii Download:" runat="server" />
<table>
<tr>
<td>
<asp:Label ID="UrlL2" Text="URL:" runat="server"
/>
</td>
<td>
<asp:TextBox ID="UrlTxt2" Width="500" runat="ser
ver" ValidationGroup="SiteValidation" />
<asp:RequiredFieldValidator
ControlToValidate="UrlTxt2"
ErrorMessage="Camp necesar de completat!"
runat="server" />
</td>
<td></td>
</tr>
<tr>
<td>
<asp:Label ID="Output_FolderL2" Text="Cale Folde
r Download (local):" runat="server" />
</td>
<td>
<asp:TextBox ID="Output_Folder2" Width="500" run
at="server" ValidationGroup="SiteValidation" />
<asp:RequiredFieldValidator
ControlToValidate="UrlTxt2"
ErrorMessage="Camp necesar de completat!"
runat="server" />
</td>
<td></td>
</tr>
<tr>
<td>
<asp:Label ID="CreateFolder2L" Text="Creeaza Str
uctura Foldere:" runat="server" />
</td>
<td>
<asp:CheckBox ID="CreateFolder2" Width="500" run
at="server" />
</td>
<td></td>
</tr>
<tr>
<td>
<asp:Label ID="Output_LogfolderL2" Text="Folder
Log (local):" runat="server" />
</td>
<td>
<asp:TextBox ID="Output_Logfolder2" Width="500"
runat="server" />
</td>
<td></td>
</tr>
<tr>
<td>
<asp:Label ID="Output_LogfileL2" Text="Fisier Lo
g (local):" runat="server" />
</td>
<td>
<asp:TextBox ID="Output_Logfile2" Width="500" ru
nat="server" />
<asp:Label ID="Output_Logfile2R" Text="logfile (
nume implicit)" runat="server" Style="display: none; float: right;" />
</td>
<td></td>
</tr>
<tr>
<td>
<asp:Label ID="Extensions_WantedL2" Text="Extens
ii Dorite:" runat="server" />
</td>
<td>
<asp:TextBox ID="Extensions_Wanted2" Width="500"
runat="server" /><asp:Label Text=" (separate prin virgula)" runat="server" />
</td>
<td></td>
</tr>
<tr>
<td>
<asp:Label ID="Extensions_UnwantedL2" Text="Exte
nsii Nedorite:" runat="server" />
</td>
<td>
<asp:TextBox ID="Extensions_Unwanted2" Width="50
0" runat="server" /><asp:Label Text=" (separate prin virgula)" runat="server" />
</td>
<td></td>
</tr>
<tr>
<td>
<asp:Label ID="RecursiveL2" Text="Download Recur
siv:" runat="server" />
</td>
<td>
<asp:CheckBox ID="Recursive2" runat="server" />
</td>
<td></td>
</tr>
<tr>
<td>
<asp:Label ID="RecursiveLevelL2" Text="Nivel Rec
ursivitate:" runat="server" />
</td>
<td>
<asp:TextBox ID="RecursiveLevel2" Width="500" ru
nat="server" Enabled="false" ValidationGroup="SiteValidation" />
<asp:Label ID="RecursiveLevel2R" Text="5 (numar
implicit)" runat="server" Style="display: none; float: right;" />
<asp:RangeValidator
ControlToValidate="RecursiveLevel2"
MinimumValue="1"
MaximumValue="99999"
Type="Integer"
ErrorMessage="Valoarea trebuie sa reprezinte
un numar!"
runat="server" />
</td>
<td></td>
</tr>
<tr>
<td>
<asp:Label ID="ParentDirectories2L" Text="Downlo
ad Directoare Parinte:" runat="server" />
</td>
<td>
<asp:CheckBox ID="ParentDirectories2" Width="500
" runat="server" />
</td>
<td></td>
</tr>
<tr>
<td>
<asp:Label ID="Try2L" Text="Nr. Incercari conect
are:" runat="server" />
</td>
<td>
<asp:TextBox ID="Try2" Width="500" runat="server
" ValidationGroup="SiteValidation" />
<asp:RangeValidator
ControlToValidate="Try2"
MinimumValue="1"
MaximumValue="99999"
Type="Integer"
ErrorMessage="Valoarea trebuie sa reprezinte
un numar!"
runat="server" />
</td>
<td></td>
</tr>
<tr>
<td>
<asp:Label ID="ServerResponse2L" Text="Logheaza
Raspunsul Serverului:" runat="server" />
</td>
<td>
<asp:CheckBox ID="ServerResponse2" Width="500" r
unat="server" />
</td>
<td></td>
</tr>
<tr>
<td>
<asp:Label ID="LimitL2" Text="Limita Download:"
runat="server" />
</td>
<td>
<asp:TextBox ID="Limit2" Width="500" runat="serv
er" ValidationGroup="SiteValidation" /><asp:Label Text=" (k)" runat="server" />
<asp:RangeValidator
ControlToValidate="Limit2"
MinimumValue="1"
MaximumValue="99999"
Type="Integer"
ErrorMessage="Valoarea trebuie sa reprezinte
un numar!"
runat="server" />
</td>
<td></td>
</tr>
</table>
<p>
<asp:Button ID="DownloadSiteBtn" OnClick="Download_Site"
Text="Download" runat="server" ValidationGroup="SiteValidation" Style="float: l
eft;" />
</asp:Panel>
<asp:Button ID="SignOutBtn" OnClick="Signout_Click" Text="Sign O
ut" runat="server" Style="float: left;" /><br>
<p>
<asp:Label ID="ValidationText" Text="" runat="server" />
<p>
<asp:Label ID="DownloadStatus" Text="Status Download:" runat="se
rver" Visible="false" />
<asp:TextBox TextMode="Multiline" Wrap="true" ID="ConsoleOutput"
OnTextChanged="ConsoleTextChange" runat="server" Visible="false" Style="max-wid
th: 1000px; width: 1000px; height: 250px; margin-right: auto; margin-left: auto;
margin-top: 20px;" />
</form>
</div>
</body>
</html>

You might also like