private void OverwriteFileName(string commandString, string fileNameString, string zipLocation, string currentDirectory, string user, string id) { string str = String.Empty; if (String.Compare(commandString, Constants.FILE_COMMAND) == 0) { ZipArchive archive = ZipFile.OpenRead(zipLocation); foreach (ZipArchiveEntry entry in archive.Entries) { str = currentDirectory + "\\" + entry.Name; if (File.Exists(str)) { string extension = Path.GetExtension(str); string onlyName = Path.GetFileNameWithoutExtension(str); str = currentDirectory + "\\" + onlyName + user + extension; if (File.Exists(str)) { string timeStamp = DateTime.Now.ToString("yy-MM-dd_HH-mm-ss-ffffff"); str = currentDirectory + "\\" + onlyName + user + timeStamp + extension; } } try { entry.ExtractToFile(str, true); } catch (PathTooLongException) { FileCancel?.Invoke(id, Constants.NOTIFICATION_STATE.FILE_ERROR_REC); } } archive.Dispose(); } else if (String.Compare(commandString, Constants.DIR_COMMAND) == 0) { str = currentDirectory + "\\" + fileNameString; if (Directory.Exists(str)) { str += user; if (Directory.Exists(str)) { string timeStamp = DateTime.Now.ToString("yy-MM-dd_HH-mm-ss-ffffff"); str += timeStamp; } } try { ZipFile.ExtractToDirectory(zipLocation, str); } catch (PathTooLongException) { Directory.Delete(str, true); FileCancel?.Invoke(id, Constants.NOTIFICATION_STATE.FILE_ERROR_REC); } } }
public async Task Cancel([FromRoute] FileCancel fileCancel) { await _nodesService.CancelNode(fileCancel.NodeId, fileCancel.Body.Reason); }
private void ReceiveFromSocket(Socket handler) { handler.ReceiveTimeout = 2500; handler.SendTimeout = 2500; string zipLocation = String.Empty, fileNameString = String.Empty, ipSender = String.Empty, id = String.Empty; ZipArchive archive = null; long temp = 0; long zipFileSize = 0; FileStream fs = null; string user = String.Empty; bool zipToDelete = true; try { int received = 0; ipSender = ((IPEndPoint)handler.RemoteEndPoint).Address.ToString(); user = NeighborProtocol.GetInstance.GetUserFromIp(ipSender); if (String.IsNullOrEmpty(user)) { user = Constants.UTENTE_ANONIMO; } byte[] command = new byte[Constants.FILE_COMMAND.Length]; received = handler.Receive(command, 0, command.Length, SocketFlags.None, out SocketError sockError); if (sockError != SocketError.Success) { throw new SocketException(); } string commandString = Encoding.ASCII.GetString(command); byte[] fileNameLength = new byte[sizeof(int)]; received = handler.Receive(fileNameLength, 0, sizeof(int), SocketFlags.None, out sockError); if (sockError != SocketError.Success) { throw new SocketException(); } int fileNameDimension = BitConverter.ToInt32(fileNameLength, 0); byte[] fileNameAndFileLength = new byte[fileNameDimension + sizeof(long)]; received = handler.Receive(fileNameAndFileLength, 0, fileNameAndFileLength.Length, SocketFlags.None, out sockError); if (sockError != SocketError.Success) { throw new SocketException(); } fileNameString = Encoding.UTF8.GetString(fileNameAndFileLength, 0, fileNameDimension); long fileSize = BitConverter.ToInt64(fileNameAndFileLength, fileNameDimension); id = Guid.NewGuid().ToString(); if (settings.AutoAccept) { byte[] responseClient = Encoding.ASCII.GetBytes(Constants.ACCEPT_FILE); handler.Send(responseClient, 0, responseClient.Length, SocketFlags.None, out sockError); if (sockError != SocketError.Success) { throw new SocketException(); } } else { byte[] responseToClient = new byte[Constants.ACCEPT_FILE.Length]; string adjustedSize = SizeSuffix(fileSize); Acceptance?.Invoke(user, fileNameString, adjustedSize, id); while (mre.WaitOne()) { if (String.Compare(Receiver.idFileToAccept, id) == 0) { break; } } if (Receiver.accepted) { responseToClient = Encoding.ASCII.GetBytes(Constants.ACCEPT_FILE); handler.Send(responseToClient, 0, responseToClient.Length, SocketFlags.None, out sockError); if (sockError != SocketError.Success) { throw new SocketException(); } } else if (!Receiver.accepted) { responseToClient = Encoding.ASCII.GetBytes(Constants.DECLINE_FILE); handler.Send(responseToClient, 0, responseToClient.Length, SocketFlags.None, out sockError); if (sockError != SocketError.Success) { throw new SocketException(); } ReleaseResources(handler); return; } } byte[] zipCommandZipNameLength = new byte[Constants.ZIP_COMMAND.Length + sizeof(int)]; received = handler.Receive(zipCommandZipNameLength, 0, zipCommandZipNameLength.Length, SocketFlags.None, out sockError); if (sockError != SocketError.Success || received == 0) { throw new SocketException(); } int zipFileNameLength = BitConverter.ToInt32(zipCommandZipNameLength, Constants.ZIP_COMMAND.Length); byte[] zipNameAndZipLength = new byte[zipFileNameLength + sizeof(long)]; received = handler.Receive(zipNameAndZipLength, 0, zipNameAndZipLength.Length, SocketFlags.None, out sockError); if (sockError != SocketError.Success) { throw new SocketException(); } string zipFileName = Encoding.ASCII.GetString(zipNameAndZipLength, 0, zipFileNameLength); zipLocation = App.defaultFolder + "\\" + zipFileName; if (File.Exists(zipLocation)) { zipToDelete = false; handler.Send(Encoding.ASCII.GetBytes(Constants.DECLINE_FILE), 0, Constants.DECLINE_FILE.Length, SocketFlags.None, out sockError); if (sockError != SocketError.Success) { throw new SocketException(); } return; } handler.Send(Encoding.ASCII.GetBytes(Constants.ACCEPT_FILE), 0, Constants.ACCEPT_FILE.Length, SocketFlags.None, out sockError); if (sockError != SocketError.Success) { throw new SocketException(); } fs = new FileStream(zipLocation, FileMode.Create, FileAccess.Write); string senderID = user + "@" + ipSender; if (!NeighborProtocol.GetInstance.Neighbors.TryGetValue(senderID, out Neighbor neighbor)) { neighbor = new Neighbor(senderID, File.ReadAllBytes(App.currentDirectoryResources + "/anonimo.png")); } ReceivingFile rf1 = new ReceivingFile(neighbor, fileNameString, id); UpdateReceivingFiles?.Invoke(rf1); zipFileSize = BitConverter.ToInt64(zipNameAndZipLength, zipFileNameLength); int percentage = 0; byte[] data = new byte[Constants.PACKET_SIZE]; int bytesRec = 0; while (temp < zipFileSize) { if (zipFileSize - temp > Constants.PACKET_SIZE) { bytesRec = handler.Receive(data, 0, Constants.PACKET_SIZE, SocketFlags.None, out sockError); } else { bytesRec = handler.Receive(data, 0, (int)(zipFileSize - temp), SocketFlags.None, out sockError); } if (sockError == SocketError.Success) { temp += bytesRec; fs.Write(data, 0, bytesRec); fs.Flush(); ulong temporary = (ulong)temp * 100; int tempPercentage = (int)(temporary / (ulong)zipFileSize); if (tempPercentage > percentage) { UpdateProgress?.Invoke(id, tempPercentage); percentage = tempPercentage; } } else { throw new SocketException(); } if (bytesRec == 0) { break; } } if (temp != zipFileSize) { FileCancel?.Invoke(id, Constants.NOTIFICATION_STATE.CANCELED); fs.Close(); ReleaseResources(handler); return; } fs.Close(); string currentDirectory = String.Empty; while (String.IsNullOrEmpty(currentDirectory)) { if (!settings.DefaultDir) { FolderBrowserDialog fbd = new FolderBrowserDialog { Description = "Selezione la cartella in cui salvare il file" }; if (fbd.ShowDialog() == DialogResult.OK) { currentDirectory = fbd.SelectedPath; } } else { if (Directory.Exists(settings.DefaultDirPath)) { currentDirectory = settings.DefaultDirPath; } else { FolderBrowserDialog fbd = new FolderBrowserDialog { Description = "La cartella predefinita non è stata trovata, selezionane un altra" }; if (fbd.ShowDialog() == DialogResult.OK) { currentDirectory = fbd.SelectedPath; } } } } if (Settings.GetInstance.AutoRename) { OverwriteFileName(commandString, fileNameString, zipLocation, currentDirectory, user, id); } else { string str = String.Empty; RenamingFile rf = new RenamingFile(); if (String.Compare(commandString, Constants.FILE_COMMAND) == 0) { archive = ZipFile.OpenRead(zipLocation); foreach (ZipArchiveEntry entry in archive.Entries) { str = String.Empty; if (File.Exists(currentDirectory + "\\" + entry.Name)) { rf.SetFields(entry.Name, currentDirectory, 0); rf.Activate(); rf.Topmost = true; rf.Topmost = false; rf.WindowState = System.Windows.WindowState.Normal; rf.ShowDialog(); if (String.IsNullOrEmpty(rf.NewName)) { throw new Exception(); } str = currentDirectory + "//" + rf.NewName; } else { str = currentDirectory + "//" + entry.Name; } entry.ExtractToFile(str, true); } } else if (String.Compare(commandString, Constants.DIR_COMMAND) == 0) { if (Directory.Exists(currentDirectory + "\\" + fileNameString)) { rf.SetFields(fileNameString, currentDirectory, 1); rf.ShowDialog(); if (String.IsNullOrEmpty(rf.NewName)) { throw new Exception(); } str = currentDirectory + "//" + rf.NewName; } else { str = currentDirectory + "//" + fileNameString; } ZipFile.ExtractToDirectory(zipLocation, str); } } } catch (SocketException e) { Console.WriteLine("Receiver"); var st = new StackTrace(e, true); // Get the top stack frame var frame = st.GetFrame(st.FrameCount - 1); // Get the line number from the stack frame var line = frame.GetFileLineNumber(); Console.WriteLine("Error at line {0} ", line); Console.WriteLine(e.SocketErrorCode); if (zipFileSize == 0) { ReceivingFailure?.Invoke(fileNameString, ipSender, Constants.NOTIFICATION_STATE.NET_ERROR); } else { FileCancel?.Invoke(id, Constants.NOTIFICATION_STATE.REC_ERROR); } } catch (Exception e) { Console.WriteLine("Receiver"); var st = new StackTrace(e, true); // Get the top stack frame var frame = st.GetFrame(st.FrameCount - 1); // Get the line number from the stack frame var line = frame.GetFileLineNumber(); Console.WriteLine("Error at line {0} ", line); ReceivingFailure?.Invoke(fileNameString, ipSender, Constants.NOTIFICATION_STATE.FILE_ERROR_REC); } finally { if (fs != null) { fs.Close(); } if (archive != null) { archive.Dispose(); } if (!String.IsNullOrEmpty(zipLocation)) { if (File.Exists(zipLocation) && zipToDelete) { File.Delete(zipLocation); } } ReleaseResources(handler); } }