Esempio 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);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// This is the runner function, needs to be called when the manager needs to run.
        /// </summary>
        public void Run()
        {
            _isStopped = false;
            while (_isRunning)
            {
                if (!_isRunning)
                {
                    break;
                }

                this._waitHandle.WaitOne();

                while (this._queue.TryDequeue(out string item))
                {
                    if (!_isRunning)
                    {
                        _queue.Enqueue(item);
                        _waitHandle.Set();
                        break;
                    }

                    _filesList.TryGetValue(item, out P2PFile currentFile);
                    if (currentFile == null)
                    {
                        return;
                    }

                    int lastIndexInChunks = currentFile.chunks.Count - 1;
                    for (int i = lastIndexInChunks; i >= 0; i--)
                    {
                        if (!_fileDeleter.ChunkDeleter(currentFile.chunks[i], currentFile))
                        {
                            _queue.Enqueue(currentFile.hash);
                            continue;
                        }
                        Console.WriteLine(@"Deleted: " + currentFile.hash);

                        if (currentFile.chunks.Count == 0)
                        {
                            _filesList.TryRemove(currentFile.hash, out _);
                        }
                    }

                    this._waitHandle.Reset();
                }

                _isStopped = true;
            }
        }