public DownloadItem(DBDownItem item, string hostname, int port, string pathFolderOnClient, DownloadManager par) { //ct = ts.Token; _hostName = hostname; _port = port; parrent = par; if (item.FolderOnClient == null) { FolderOnClient = pathFolderOnClient; } else { FolderOnClient = item.FolderOnClient; } if (item.IsDownComplete) { if (File.Exists(item.PathOnClient)) { PathOnClient = item.PathOnClient; PathOnServer = item.PathOnServer; Name = new FileInfo(item.PathOnClient).Name; FullSize = new FileInfo(item.PathOnClient).Length; currSize = FullSize; IsDownComplete = true; IsPause = false; ProgressBarProccess = 100; pauseStopIcon = "CheckMark.png"; return; } } Task.Run(() => CheckDBFile(item)); }
public void SaveToDataBase() { foreach (DownloadItem item in items) { DBDownItem obj = new DBDownItem() { FolderOnClient = item.FolderOnClient, PathOnClient = item.PathOnClient, PathOnServer = item.PathOnServer, FullSize = item.FullSize, Name = item.Name, IsDownComplete = item.IsDownComplete }; DBInjector dbi = new DBInjector(); if (dbi.Servers.ToList().Where(a => a.host == _host && a.port == _port).ToList().Count <= 0) { dbi.Servers.Add(new ServerItem() { host = _host, port = _port, DBDownItems = new List <DBDownItem>() }); dbi.SaveChanges(); } int idServer = dbi.Servers.ToList().Where(a => a.host == _host && a.port == _port).FirstOrDefault().ServerItemId; if (dbi.DBDownItems.ToList().Where(a => a.ServerItemId == idServer && a.PathOnClient == item.PathOnClient || a.PathOnServer == item.PathOnServer).ToList().Count <= 0) { dbi.DBDownItems.Add(obj); dbi.SaveChanges(); dbi.Servers.FirstOrDefault(a => a.host == _host && a.port == _port).DBDownItems.Add(obj); dbi.SaveChanges(); } else { DBDownItem it = dbi.DBDownItems.FirstOrDefault(a => a.ServerItemId == idServer && a.PathOnClient == item.PathOnClient || a.PathOnServer == item.PathOnServer); it.Name = obj.Name; it.FolderOnClient = obj.FolderOnClient; it.FullSize = obj.FullSize; it.IsDownComplete = obj.IsDownComplete; it.PathOnClient = obj.PathOnClient; it.PathOnServer = obj.PathOnServer; dbi.SaveChanges(); } } }
void CheckDBFile(DBDownItem item) { // Connecting client.Connect(_hostName, _port); PathOnServer = item.PathOnServer; string name = Environment.MachineName + PathOnServer; byte[] bytes = Encoding.UTF8.GetBytes(name); client.GetStream().Write(bytes, 0, bytes.Length); bytes = new byte[2048]; client.GetStream().Read(bytes, 0, bytes.Length); string mess = Encoding.UTF8.GetString(bytes); // Connecting //Check file exist on server mess = $"/FileExist {item.PathOnServer}"; bytes = Encoding.UTF8.GetBytes(mess); client.GetStream().Write(bytes, 0, bytes.Length); bytes = new byte[2048]; client.GetStream().Read(bytes, 0, bytes.Length); mess = Encoding.UTF8.GetString(bytes); mess = mess.Replace("\0", ""); //Check file exist on server if (mess == "true") { if (File.Exists(item.PathOnClient)) { currSize = new FileInfo(item.PathOnClient).Length; IsPause = false; pauseStopIcon = "play.png"; //Take info about file mess = $"/GetFile {PathOnServer}"; bytes = Encoding.UTF8.GetBytes(mess); client.GetStream().Write(bytes, 0, bytes.Length); bytes = new byte[500000]; Task.Run(() => ServerTimeOut()); client.GetStream().Read(bytes, 0, bytes.Length); IsTimeOut = false; Emigration.EmigrationObject takedinfo; takedinfo = ByteArrayToObject(bytes) as Emigration.EmigrationObject; if (takedinfo.type == "file") { Emigration.EmigrationFileInfo efi = (Emigration.EmigrationFileInfo)takedinfo.message; name = efi.FileName; FullSize = efi.Size; if (DriveInfo.GetDrives().Where(a => a.Name == FolderOnClient).ToList().Count > 0) { PathOnClient = FolderOnClient + name; } else { PathOnClient = FolderOnClient + @"\" + name; } bytes = new byte[currSize]; client.GetStream().Read(bytes, 0, bytes.Length); try { using (fs = new FileStream(PathOnClient, FileMode.OpenOrCreate)) { fs.Position = fs.Length; ProgressBarProccess = CalculateValueProgressBar(); while (currSize < FullSize) { //if (ct.IsCancellationRequested) //{ // break; //} if (IsPause) { bytes = new byte[1]; client.GetStream().Read(bytes, 0, bytes.Length); fs.WriteByte(bytes[0]); currSize++; if (currSize % 1024 == 0) { ProgressBarProccess = CalculateValueProgressBar(); } } else { Thread.Sleep(1000); } } ProgressBarProccess = 100; IsDownComplete = true; pauseStopIcon = "CheckMark.png"; parrent.SaveToDataBase(); client.Close(); client = null; } } catch (Exception ex) { parrent.Message($"Can't open file {PathOnClient}, probably file busy.", "Client: ERROR"); Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() => DeleteDownload())); } } } else { PathOnServer = item.PathOnServer; IsPause = false; pauseStopIcon = "play.png"; IsDownComplete = false; ProgressBarProccess = 0; client.Close(); client = new TcpClient(); ProcessOfDownload(); } } else { DBInjector dbi = new DBInjector(); dbi.DBDownItems.Remove(dbi.DBDownItems.FirstOrDefault(a => a.PathOnServer == item.PathOnServer && a.ServerItemId == item.ServerItemId)); dbi.SaveChanges(); parrent.Message($"File {PathOnServer} is not exist on server...", "Server: Error"); DeleteDownload(); } }