private bool LoadUpdateXML_server(Stream stream, Product product) { bool result = false; FileStruct fileStruct; try { XmlTextReader reader = new XmlTextReader(stream); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "File") { fileStruct = new FileStruct(serverAddress + Main.constURLParams_download_file, applicationPath); bool autoStart = reader.GetAttribute("autoStart") == "1"; int fileID = int.Parse(reader.GetAttribute("id")); int versionServer = int.Parse(reader.GetAttribute("version")); fileStruct.AddFileInfo(reader.GetAttribute("name"), fileID, reader.GetAttribute("savePath"), reader.GetAttribute("url"), versionServer, reader.GetAttribute("md5"), autoStart); fileStruct.md5Local = MD5Hash(fileStruct.SavePath); product.AddFile(fileStruct); } } reader.Close(); result = true; } catch (Exception ex) { ShowInfo("ERROR: " + ex.Message, Main.LogType.Normal); Log.Write(ex, this, "LoadUpdateXML_server", Log.LogType.ERROR); } return result; }
public bool DownloadFile(string url, FileStruct fileStruct) { bool result = false; long downloadProgres = 0; HttpWebRequest request; HttpWebResponse response; FileStream fs; BinaryReader r; BinaryWriter w = null; // URLEncode url = System.Web.HttpUtility.UrlPathEncode(url); Log.Write(new string[] { "URL: " + url, "SavePath:" + fileStruct.SavePath }, this, "DownloadFile", Log.LogType.DEBUG, true); //save this in a file with the same name try { request = (HttpWebRequest)WebRequest.Create(url); request.CookieContainer = cookieContainer; response = (HttpWebResponse)request.GetResponse(); if (response.ResponseUri.AbsoluteUri == url) { // try open file for writing fs = TryCreateFileStream(fileStruct.SavePath); if (fs == null) { // Orginal location is in use, save it in _tmp folder fileStruct.SaveSuccessfully = false; Common.MakeAllSubFolders(Common.BuildPath(fileStruct.BaseFolder, "_tmp")); fs = TryCreateFileStream(Common.BuildPath(fileStruct.BaseFolder, "_tmp") + fileStruct.md5Server); if (fs == null) { throw new Exception("Error creating file stream in tmp folder"); } } else { // File will be saved on orginal locatin fileStruct.SaveSuccessfully = true; } result = true; r = new BinaryReader(response.GetResponseStream()); w = new BinaryWriter(fs); while (true) { if ((downloadProgres % 1000) == 0) { if (DownloadProgress != null) { DownloadProgress(downloadProgres, response.ContentLength); } } w.Write(r.ReadByte()); downloadProgres++; } } } catch (System.IO.EndOfStreamException ex) { //Log.Write(ex, this, "DownloadFile", Log.LogType.ERROR); } catch (Exception ex) { result = false; Log.Write(ex, this, "DownloadFile", Log.LogType.ERROR, true); } finally { if (w != null) { w.Flush(); w.Close(); } } return result; }
public void AddFile(FileStruct fileStruct) { fileStructList.Add(fileStruct); }