/// <summary> /// Handles the deletion of a chunk of the file /// </summary> /// <param name="currentFileChunk">The P2PChunk to be deleted</param> /// <param name="currentFile">The P2PFile to wich the chunk belongs</param> /// <returns></returns> public bool ChunkDeleter(P2PChunk currentFileChunk, P2PFile currentFile){ Listener listener = new Listener(this._port); int lastIndex = currentFileChunk.peers.Count - 1; //Sends a delete message to every peer with the chunk for (int i = lastIndex; i >= 0; i--) { if (_peers.TryGetValue(currentFileChunk.peers[i], out Peer currentReceiver)) { if (!currentReceiver.IsOnline()){ return false; } var deletionMessage = new FileDeletionMessage(currentReceiver) { type = TypeCode.REQUEST, statusCode = StatusCode.OK, port = _port, fileHash = currentFileChunk.hash, fullFileHash = currentFile.hash }; //Sends the message and waits for a response, //which will then overwrite the original sent message if (listener.SendAndAwaitResponse(ref deletionMessage, 2000)) { if (deletionMessage.type.Equals(TypeCode.RESPONSE)) { currentFileChunk.RemovePeer(deletionMessage.fromUuid); if (currentFileChunk.peers.Count == 0) { currentFile.RemoveChunk(currentFileChunk.hash); } if (deletionMessage.statusCode.Equals(StatusCode.FILE_NOT_FOUND)) { DiskHelper.ConsoleWrite("File not found at peer"); } } } } } return true; }
private void IdxFileAdded(IndexFile idxfile) { P2PFile file = new P2PFile(idxfile.GetHash()); file.AddPath(idxfile.paths); _p2P.UploadFile(file); }
private void RestoreOriginalFile(string path, P2PFile fileInformation) { DiskHelper.ConsoleWrite("File exist"); string pathWithoutExtension = (_path + @".hidden\incoming\" + fileInformation.hash); //Merge files var splitterLibrary = new SplitterLibrary(); if (!splitterLibrary.MergeFiles(_path + @".hidden\incoming\" + fileInformation.hash + @"\", pathWithoutExtension + ".aes", fileInformation.GetChunksAsString())) { _queue.Enqueue(fileInformation); return; } // Decrypt file var decryption = new FileEncryption(pathWithoutExtension, ".lzma"); if (!decryption.DoDecrypt(IdHandler.GetKeyMold())) { _queue.Enqueue(fileInformation); return; } DiskHelper.ConsoleWrite("File decrypted"); File.Delete(path); // Decompress file string pathToFileForCopying = Compressor.DecompressFile(pathWithoutExtension + ".lzma", pathWithoutExtension); DiskHelper.ConsoleWrite("File decompressed"); foreach (string filePath in _index.GetEntry(_fileHash).paths) { if (!Directory.Exists(Path.GetDirectoryName(filePath))) { Directory.CreateDirectory(Path.GetDirectoryName(filePath) ?? throw new NullReferenceException()); } try{ if (!File.Exists(filePath)) { File.Copy(pathToFileForCopying, filePath); DiskHelper.ConsoleWrite($"File saved to: {filePath}"); } } catch (Exception e) { logger.Error(e); } } }
/// <summary> /// Handles new files added by the user by uploading /// them to the network. /// </summary> /// <param name="idxfile">The file to be uploaded.</param> private static void Idx_FileAdded(IndexFile idxfile) { Console.WriteLine(@"Added: " + idxfile.GetHash()); P2PFile file = new P2PFile(idxfile.GetHash()); file.AddPath(idxfile.paths); _p2P.UploadFile(file); }