Ejemplo n.º 1
0
        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);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Main running function for UploadManager, needs to be called for it to run.
        /// </summary>
        public void Run()
        {
            _isStopped = false;
            this._waitHandle.Set();

            while (_isRunning)
            {
                this._waitHandle.WaitOne();

                if (!_isRunning)
                {
                    break;
                }

                while (this._queue.TryDequeue(out P2PFile file))
                {
                    bool uploaded = true;

                    if (!_peers.Any(p => p.Value.IsOnline()))
                    {
                        this._queue.Enqueue(file);
                        break;
                    }



                    if (!_isRunning)
                    {
                        this._queue.Enqueue(file);
                        break;
                    }

                    string filePath           = file.paths[0];
                    string compressedFilePath = this._path + @".hidden\" + file.hash;

                    // Compress file
                    bool compressionCompleted = Compressor.CompressFile(filePath, compressedFilePath);

                    if (!compressionCompleted)
                    {
                        this._queue.Enqueue(file);
                        continue;
                    }

                    // Encrypt file
                    var encryption = new FileEncryption(compressedFilePath, ".lzma");

                    bool encryptionCompleted = encryption.DoEncrypt(IdHandler.GetKeyMold());

                    if (!encryptionCompleted)
                    {
                        this._queue.Enqueue(file);
                        continue;
                    }

                    _hiddenFolder.Remove(compressedFilePath + ".lzma");

                    string encryptedFilePath = compressedFilePath + ".aes";

                    // Initialize splitter
                    var splitter = new SplitterLibrary();

                    List <string> chunks =
                        splitter.SplitFile(encryptedFilePath, file.hash, _path + @".hidden\splitter\");
                    file.AddChunk(chunks);

                    FileUploader uploader = new FileUploader(_ports, _peers);

                    int i = 0;
                    foreach (var chunk in file.chunks)
                    {
                        string path = _path + @".hidden\splitter\" + chunk.hash;

                        //3 is used, because the network is relatively small. 10 is the default.
                        if (!uploader.Push(chunk, path, 3, i))
                        {
                            uploaded = false;
                        }
                        i++;
                    }

                    if (!uploaded)
                    {
                        this._queue.Enqueue(file);
                    }

                    if (uploaded)
                    {
                        Console.WriteLine();
                        DiskHelper.ConsoleWrite($"The file {file.hash} was successfully sent to all \n");
                    }
                }

                this._waitHandle.Reset();
            }

            _isStopped = true;
        }