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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Webzip
{
class ProcedureDownloads
{
#region Déclarations
#region Gadgets
public static Syncfusion.Windows.Forms.Grid.GridControl GridDownloads;
public static Syncfusion.Windows.Forms.Grid.GridControl GridTelecharger;
public static Syncfusion.Windows.Forms.Grid.GridControl GridErreurs;
static Syncfusion.Windows.Forms.Grid.GridRangeStyle GridDownloadsC1;//Image
static Syncfusion.Windows.Forms.Grid.GridRangeStyle GridDownloadsC4;//Réception
static Syncfusion.Windows.Forms.Grid.GridRangeStyle GridDownloadsC5;//Barre progression
static Syncfusion.Windows.Forms.Grid.GridRangeStyle GridDownloadsC6;//Ecoulé
static Syncfusion.Windows.Forms.Grid.GridRangeStyle GridDownloadsC7;//Restant
static Syncfusion.Windows.Forms.Grid.GridRangeStyle GridDownloadsC8;//Débit
static Syncfusion.Windows.Forms.Grid.GridRangeStyle GridDownloadsC11;//Infos
static Syncfusion.Windows.Forms.Grid.GridRangeStyle GridTelechargerC1;//Image
static Syncfusion.Windows.Forms.Grid.GridRangeStyle GridTelechargerC2;//URL
static Syncfusion.Windows.Forms.Grid.GridRangeStyle GridErreursC1;//Image
static Syncfusion.Windows.Forms.Grid.GridRangeStyle GridErreursC2;//URL
public static DevComponents.DotNetBar.RibbonBar RibbonBarDownloads;
public static DevComponents.DotNetBar.LabelItem LabelNombreATelecharger;
public static DevComponents.DotNetBar.LabelItem LabelNombreTelecharger;
public static DevComponents.DotNetBar.LabelItem LabelNombreErreur;
public static DevComponents.DotNetBar.LabelItem LabelUrl;
#endregion Gadgets
#region Delegate-Thread
delegate void DelageFunction();
delegate void DownloadCallback(string Url, object value);
delegate void UpdateProgessCallback(Int64 BytesRead, Int64 TotalBytes);
delegate void UpdateFinCallback(Boolean value,string Url);
delegate void Delete(int value);
delegate void SetTexte(string value);
delegate void BooleanErreur(Boolean value);
#endregion Delegate-Thread
#region Download
static HttpWebRequest DownloadRequest;
static HttpWebResponse DownloadReponse;
private Stream strResponse;
private Stream strLocal;
Boolean goPause = false;
public static string Identifiant;
public static string Password;
public static string RepertoireDownload;
static string FichierDownload;
static long TailleTelecharge = 0;
Int64 Octets;
Int64 TotalOctets;
static int NombreTelecharge=0;
static int NombreATelecharge = 0;
static int NombreErreur = 0;
Stopwatch TimerDownload = new Stopwatch();
Stopwatch TimerDebit = new Stopwatch();
Stopwatch TimerImage = new Stopwatch();
#endregion Download
#region Gif
Image gif1;
int frameCount1;
FrameDimension frameDimension1;
int currentFrame1 = -1;
#endregion Gif
#endregion Déclarations
public void Telechargement()
{
gif1 = global::Webzip.Properties.Resources.Download;
frameDimension1 = new FrameDimension(gif1.FrameDimensionsList[0]);
frameCount1 = gif1.GetFrameCount(frameDimension1);
NombreATelecharge = GridDownloads.RowCount;
while (GridDownloads.RowCount>0)
{
GridDownloadsC1 = new Syncfusion.Windows.Forms.Grid.GridRangeStyle();//Image
GridDownloadsC4 = new Syncfusion.Windows.Forms.Grid.GridRangeStyle();//Réception
GridDownloadsC5 = new Syncfusion.Windows.Forms.Grid.GridRangeStyle();//Progressbar
GridDownloadsC6 = new Syncfusion.Windows.Forms.Grid.GridRangeStyle();//Ecoulé
GridDownloadsC7 = new Syncfusion.Windows.Forms.Grid.GridRangeStyle();//Restant
GridDownloadsC8 = new Syncfusion.Windows.Forms.Grid.GridRangeStyle();//Débit
GridDownloadsC11 = new Syncfusion.Windows.Forms.Grid.GridRangeStyle();//Infos
GridDownloadsC1.Range = Syncfusion.Windows.Forms.Grid.GridRangeInfo.Cell(1, 1);//Image
GridDownloadsC4.Range = Syncfusion.Windows.Forms.Grid.GridRangeInfo.Cell(1, 4);//Réception
GridDownloadsC5.Range = Syncfusion.Windows.Forms.Grid.GridRangeInfo.Cell(1, 5);//Progressbar
GridDownloadsC6.Range = Syncfusion.Windows.Forms.Grid.GridRangeInfo.Cell(1, 6);//Ecoulé
GridDownloadsC7.Range = Syncfusion.Windows.Forms.Grid.GridRangeInfo.Cell(1, 7);//Restant
GridDownloadsC8.Range = Syncfusion.Windows.Forms.Grid.GridRangeInfo.Cell(1, 8);//Débit
GridDownloadsC11.Range = Syncfusion.Windows.Forms.Grid.GridRangeInfo.Cell(1, 11);//Infos
FichierDownload = Path.Combine(GridDownloads[1, 10].Text, GridDownloads[1,2].Text);
NewProgressBar();
GridDownloads.Invoke(new DelageFunction(GridDownloadsAddRange));
string UrlTelechargement = GridDownloads[1, 9].Text;
string TexteLabelUrl = UrlTelechargement.Replace("%20", " ");
LabelUrl.Invoke(new SetTexte(SetLabelUrlTexte), new object[] { TexteLabelUrl });
if (File.Exists(FichierDownload))
{
TailleTelecharge = new FileInfo(FichierDownload).Length;
Download(UrlTelechargement, TailleTelecharge);
}
else { Download(UrlTelechargement, 0); }
}
LabelUrl.Invoke(new SetTexte(SetLabelUrlTexte), new object[] { "" });
}
void Download(string UrlDownload, object startPoint)
{
try
{
TimerDebit.Start();
TimerImage.Start();
TimerDownload.Restart();
int startPointInt = Convert.ToInt32(startPoint);
DownloadRequest = (HttpWebRequest)WebRequest.Create(UrlDownload);
DownloadRequest.Timeout = 5000;
DownloadRequest.AddRange(startPointInt);
if (!string.IsNullOrEmpty(Identifiant)) { DownloadRequest.Credentials = new NetworkCredential(Identifiant, Password); }
else { DownloadRequest.Credentials = CredentialCache.DefaultCredentials; }
GridDownloadsC11.StyleInfo.Text = "Demande informations de téléchargement...";
DownloadReponse = (HttpWebResponse)DownloadRequest.GetResponse();
Int64 FileSize = DownloadReponse.ContentLength;
string content = DownloadReponse.ContentType; ;
strResponse = DownloadReponse.GetResponseStream();
if (startPointInt == 0) { strLocal = new FileStream(FichierDownload, FileMode.Create, FileAccess.Write, FileShare.None); }
else { strLocal = new FileStream(FichierDownload, FileMode.Append, FileAccess.Write, FileShare.None); }
int bytesSize = 0;
byte[] downBuffer = new byte[2048];
try
{
while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
StreamWriter f = new StreamWriter(Path.Combine(Application.StartupPath, "bytes.txt"));
f.WriteLine(Convert.ToString(bytesSize));
f.Close();
strLocal.Write(downBuffer, 0, bytesSize);
UpdateProgress(strLocal.Length, FileSize + startPointInt);
if (goPause == true) { break; }
}
}
catch (Exception e)
{ MessageBox.Show("bytesSize = strResponse.Read\r"+e.Message); }
strResponse.Close();
strLocal.Close();
TimerDownload.Stop();
TimerDebit.Stop();
TimerImage.Stop();
if (goPause == false)
{
long result = new FileInfo(FichierDownload).Length;
if (result == FileSize + startPointInt) {FinProgress(false, UrlDownload);}//Erreur=false
else { FinProgress(true, UrlDownload); }//Erreur=true
GridDownloads.Invoke(new DelageFunction(GridDownloadRemoveRow));
StreamWriter FichierUrlDownload = new StreamWriter(Path.Combine(Application.StartupPath, "Vidéos.txt"));
for (int Compte = 1; Compte <= GridDownloads.RowCount; Compte++)
{ FichierUrlDownload.WriteLine(GridDownloads[Compte, 9].Text); }
FichierUrlDownload.Close();
}
}
catch (Exception e) { MessageBox.Show("void Download\r"+e.Message); }
}
Image GetFrame(int index, string Gif)
{
switch (Gif)
{
case "Gif1":
gif1.SelectActiveFrame(frameDimension1, index);
return gif1.Clone() as Image;
}
return null;
}
Image GetNextFrame(string Gif)
{
switch (Gif)
{
case "Gif1":
currentFrame1++;
if (currentFrame1 >= frameCount1 || currentFrame1 < 1) currentFrame1 = 0;
return GetFrame(currentFrame1, Gif);
}
return null;
}
public static string GetFilename(string Url)
{
string Filename = "";
try
{
DownloadRequest = (HttpWebRequest)WebRequest.Create(Url);
if (Identifiant != null & Password != null) DownloadRequest.Credentials = new NetworkCredential(Identifiant, Password);
DownloadRequest.Method = WebRequestMethods.Http.Head;
using (DownloadReponse = (HttpWebResponse)DownloadRequest.GetResponse())
{
string disposition = DownloadReponse.Headers["Content-Disposition"];// S'il y a un en-tête Content-Disposition, on l'utilise
if (!string.IsNullOrEmpty(disposition))
{
string fileNamePart = disposition.Split(';').Select(s => s.Trim()).FirstOrDefault(s => s.StartsWith("filename="));
if (!string.IsNullOrEmpty(fileNamePart))
{
string NomFichier = fileNamePart.Substring("filename=".Length);
NomFichier = NomFichier.Replace("\"", "");
return NomFichier;
}
}
string responseUri = DownloadReponse.ResponseUri.AbsoluteUri;// Sinon on utilise l'URI de la réponse (éventuellement redirigée)
Filename=Uri.UnescapeDataString(Path.GetFileName(responseUri));
DownloadReponse.Close();
}
}
catch (Exception e)
{ MessageBox.Show("GetFilename()\r" + e.Message); }
return Filename;
}
public static Int64 GetFileSize(string Url)
{
Int64 FileSize = 0;
try
{
DownloadRequest = (HttpWebRequest)WebRequest.Create(Url);
if (!string.IsNullOrEmpty(Identifiant)) { DownloadRequest.Credentials = new NetworkCredential(Identifiant, Password); }
else { DownloadRequest.Credentials = CredentialCache.DefaultCredentials; }
//DownloadRequest.Timeout = 5000;
DownloadReponse = (HttpWebResponse)DownloadRequest.GetResponse();
FileSize=DownloadReponse.ContentLength;
DownloadReponse.Close();
return FileSize;
}
catch (Exception e)
{ MessageBox.Show("GetFileSize()\r" + e.Message); }
return FileSize;
}
static void GridDownloadsAddRange()
{
GridDownloads.RangeStyles.AddRange(new Syncfusion.Windows.Forms.Grid.GridRangeStyle[] { GridDownloadsC1, GridDownloadsC4, GridDownloadsC5, GridDownloadsC6, GridDownloadsC7, GridDownloadsC8, GridDownloadsC11 });
}
static void GridDownloadRemoveRow()
{ GridDownloads.Rows.RemoveRange(1, 1); }
void UpdateProgress(Int64 BytesRead, Int64 TotalBytes)
{
try
{
if(TimerDebit.Elapsed.Seconds==2)
{
try
{
int Secondes = Convert.ToInt32(TimerDownload.ElapsedMilliseconds / 1000);
if (Secondes > 0 & Octets > 0)
{
Int64 Debit = (Octets / Secondes);
if (Debit > 1048576) { GridDownloadsC8.StyleInfo.Text = string.Format("{0} Mo/s", Math.Round((double)(float)Debit / (float)1048576, 2)); }
else { GridDownloadsC8.StyleInfo.Text = string.Format("{0} Ko/s", Math.Round((double)(float)Debit / (float)1024, 2)); }
Int64 OctetsRestant = TotalOctets - Octets;
Int64 SecondesRestantes = OctetsRestant / Debit;
GridDownloadsC7.StyleInfo.Text = string.Format("{0:00}:{1:00}:{2:00}", SecondesRestantes / 3600, (SecondesRestantes / 60) % 60, SecondesRestantes % 60);
}
TimerDebit.Restart();
}
catch (Exception e)
{ MessageBox.Show("UpdateProgress()if(TimerDebit.Elapsed.Seconds==2)\r" + e.Message); }
}
if (TimerImage.ElapsedMilliseconds>300)
{
try
{
Image img = GetNextFrame("Gif1");
GridDownloadsC1.StyleInfo.BackgroundImage = img;
TimerImage.Restart();
}
catch (Exception e)
{ MessageBox.Show("UpdateProgress()if (TimerImage.ElapsedMilliseconds>300)\r" + e.Message); }
}
Octets = BytesRead;
TotalOctets = TotalBytes;
GridDownloadsC4.StyleInfo.Text =string.Format("{0:#,#0}", Convert.ToDouble(BytesRead));
GridDownloadsC5.StyleInfo.ProgressBar.ProgressValue =Convert.ToInt32((BytesRead * 100) / TotalBytes);
GridDownloadsC6.StyleInfo.Text =TimerDownload.Elapsed.ToString(@"hh\:mm\:ss");
if (goPause == false) { GridDownloadsC11.StyleInfo.Text = "En cours de téléchargement"; }
}
catch (Exception e)
{ MessageBox.Show("UpdateProgress()\r" + e.Message); }
}
void FinProgress(Boolean Erreur,string UrlDownload)
{
switch(Erreur)
{
case false:
try
{
int IndexLigne = GridTelecharger.RowCount + 1;
GridTelechargerC1 = new Syncfusion.Windows.Forms.Grid.GridRangeStyle();
GridTelechargerC2 = new Syncfusion.Windows.Forms.Grid.GridRangeStyle();
GridTelechargerC1.Range = Syncfusion.Windows.Forms.Grid.GridRangeInfo.Cell(IndexLigne, 1);
GridTelechargerC1.StyleInfo.BackgroundImage = Webzip.Properties.Resources.Téléchargé;
GridTelechargerC2.Range = Syncfusion.Windows.Forms.Grid.GridRangeInfo.Cell(IndexLigne, 2);
GridTelechargerC2.StyleInfo.Text = UrlDownload;
GridTelecharger.RangeStyles.AddRange(new Syncfusion.Windows.Forms.Grid.GridRangeStyle[] { GridTelechargerC1, GridTelechargerC2 });
//RibbonBarDownloads.Invoke(new BooleanErreur(UpdateTelechargement), new object[] { false });
UpdateTelechargement(false);
StreamWriter FichierUrlTelecharge = new StreamWriter(Path.Combine(Application.StartupPath, "Fichiers téléchargés.txt"), true);
FichierUrlTelecharge.WriteLine(UrlDownload);
FichierUrlTelecharge.Close();
}
catch (Exception e)
{ MessageBox.Show("FinProgress()case false\r" + e.Message); }
break;
case true:
try
{
int IndexLigne1 = GridErreurs.RowCount + 1;
GridErreursC1 = new Syncfusion.Windows.Forms.Grid.GridRangeStyle();
GridErreursC2 = new Syncfusion.Windows.Forms.Grid.GridRangeStyle();
GridErreursC1.Range = Syncfusion.Windows.Forms.Grid.GridRangeInfo.Cell(IndexLigne1, 1);
GridErreursC1.StyleInfo.BackgroundImage = Webzip.Properties.Resources.Erreur;
GridErreursC2.Range = Syncfusion.Windows.Forms.Grid.GridRangeInfo.Cell(IndexLigne1, 2);
GridErreursC2.StyleInfo.Text = UrlDownload;
GridErreurs.RangeStyles.AddRange(new Syncfusion.Windows.Forms.Grid.GridRangeStyle[] { GridErreursC1, GridErreursC2 });
//RibbonBarDownloads.Invoke(new BooleanErreur(UpdateTelechargement), new object[] { true });
UpdateTelechargement(true);
StreamWriter FichierUrlErreur = new StreamWriter(Path.Combine(Application.StartupPath, "Erreurs téléchargement.txt"), true);
FichierUrlErreur.WriteLine(UrlDownload);
FichierUrlErreur.Close();
}
catch (Exception e)
{ MessageBox.Show("FinProgress()case true\r" + e.Message); }
break;
}
}
static void SetLabelUrlTexte(string Texte)
{
LabelUrl.Text = Texte;
LabelUrl.Refresh();
}
static void UpdateTelechargement(Boolean Erreur)
{
NombreATelecharge--;
LabelNombreATelecharger.Text = String.Format("{0:#,#}", NombreATelecharge);
switch(Erreur)
{
case false:
NombreTelecharge++;
LabelNombreTelecharger.Text = String.Format("{0:#,#}", NombreTelecharge);
break;
case true:
NombreErreur++;
LabelNombreErreur.Text = String.Format("{0:#,#}", NombreErreur);
break;
}
}
static void NewProgressBar()
{
GridDownloadsC5.StyleInfo.ProgressBar.BackGradientEndColor = System.Drawing.SystemColors.ControlLightLight;
GridDownloadsC5.StyleInfo.ProgressBar.BackGradientStartColor = System.Drawing.SystemColors.ControlDark;
GridDownloadsC5.StyleInfo.ProgressBar.BackMultipleColors = new System.Drawing.Color[] { System.Drawing.SystemColors.ControlDark, System.Drawing.SystemColors.ControlLightLight, System.Drawing.SystemColors.Control };
GridDownloadsC5.StyleInfo.ProgressBar.FontColor = System.Drawing.SystemColors.HighlightText;
GridDownloadsC5.StyleInfo.ProgressBar.ForeColor = System.Drawing.Color.MediumBlue;
GridDownloadsC5.StyleInfo.ProgressBar.ForegroundImage = null;
GridDownloadsC5.StyleInfo.ProgressBar.GradientEndColor = System.Drawing.Color.Lime;
GridDownloadsC5.StyleInfo.ProgressBar.GradientStartColor = System.Drawing.Color.Red;
GridDownloadsC5.StyleInfo.ProgressBar.MultipleColors = new System.Drawing.Color[] { System.Drawing.Color.DarkRed, System.Drawing.Color.Red, System.Drawing.Color.Black };
GridDownloadsC5.StyleInfo.ProgressBar.ProgressStyle = Syncfusion.Windows.Forms.Tools.ProgressBarStyles.Gradient;
GridDownloadsC5.StyleInfo.ProgressBar.StretchImage = false;
GridDownloadsC5.StyleInfo.ProgressBar.StretchMultGrad = false;
GridDownloadsC5.StyleInfo.ProgressBar.TextShadow = false;
GridDownloadsC5.StyleInfo.ProgressBar.TubeEndColor = System.Drawing.SystemColors.Control;
GridDownloadsC5.StyleInfo.ProgressBar.TubeStartColor = System.Drawing.SystemColors.ControlDark;
GridDownloadsC5.StyleInfo.ProgressBar.ProgressValue = 0;
GridDownloadsC5.StyleInfo.ProgressBar.ForeSegments = false;
GridDownloadsC5.StyleInfo.ProgressBar.FontColor = Color.Black;
}
}
} |
Partager