Beispiel #1
0
        /// <summary>
        /// Recover the files from the list of string: filename and CIDs: download, merge and decrypt
        /// </summary>
        /// <param name="cidArray"></param>
        public async void RecoverFilesMergeDecrypt(List <string> cidArray)
        {
            //We connect
            var ipfs = new IpfsClient(ipfsServer);

            if (cidArray.Count >= 0)
            {
                // First need to get the right folder. For ease of test, the part of the file are downloaded
                // on a folder filename-extension, i.e. test.txt to blockget/test-txt
                string fileName    = Path.GetFileNameWithoutExtension(cidArray[0]);
                string extensionPt = Path.GetExtension(cidArray[0]);
                //string extension = extensionPt.Skip(1).ToArray();
                string extension = extensionPt.Remove(0, 1);
                //string inputFolder = Globals.BLOCKGET_FILE_FOLDER + fileName + "-" + extension + "\\";
                string   inputFolder        = Globals.OPERATION_FOLDER + fileName + "-" + extension + "\\";
                string[] SplittedFilesArray = new string[Globals.SPLIT_NB];
                string   baseFileName       = Path.GetFileNameWithoutExtension(cidArray[0]);
                int      i = 0;
                //Create input forlder for download and merge
                Directory.CreateDirectory(inputFolder);
                foreach (string s in cidArray.Skip(1)) // We want to skip the first (contre intuitive, not 0)
                {
                    try
                    {
                        //SplittedFilesArray[i] = inputFolder + baseFileName + "." +
                        //        i.ToString().PadLeft(5, Convert.ToChar("0")) + extensionPt + ".tmp";
                        SplittedFilesArray[i] = inputFolder + baseFileName + extensionPt + ".aes." +
                                                i.ToString().PadLeft(5, Convert.ToChar("0")) + ".tmp";
                        //We download bytes => File
                        Stream result = await ipfs.FileSystem.ReadFileAsync(s);

                        //We want to save them in the SaveFolder + cidArray folder
                        //Could also try: Stream result = await ipfs.FileSystem.ReadFileAsync(s).Result;
                        using (FileStream DestinationStream = File.Create(SplittedFilesArray[i])) //TODO ERROR: System.IO.DirectoryNotFoundException: 'Could not find a part of the path 'C:\Blockget\ifmalesuperfherodresedlikewomen-jpg\ifmalesuperfherodresedlikewomen.jpg.aes.00000.tmp'.'
                        {
                            await result.CopyToAsync(DestinationStream);
                        }
                        i++;
                    } //System.Net.Http.HttpRequestException: 'invalid path "": path does not begin with '/''
                    catch (HttpRequestException e)
                    {
                        Console.WriteLine(e.Message);
                        Console.WriteLine("FAIL! Don't forget to run IPFS on your computer");
                        break;
                    }
                }
                // We merge
                string pathEncryptedFile = Split.MergeFile(inputFolder);
                //We decrypt
                //string pathEncryptedFile = inputFolder + fileName + ".aes"; //Globals.BLOCKGET_FILE_FOLDER + "\\" + cidArray[0];
                //string pathEncryptedFile = inputFolder + baseFileName + extensionPt + ".aes";
                string outputPath = Encryption.DecryptFile(pathEncryptedFile);
                // Test if the file downloaded is the same as the one already present
                if (File.Exists(cidArray[0]) && Filesys.compare2FilesByBlocks(outputPath, cidArray[0]))
                {
                    Console.WriteLine("{0} and {1} have been compared bit by bit and are the same", outputPath, cidArray[0]);
                }
                else
                {
                    Console.WriteLine("{0} and {1} have been compared bit by bit and are different, we download and store the file in the Blockget Folder", outputPath, cidArray[0]);
                    // Could use the Path.Combine method to safely append the file name to the path.
                    // Will overwrite if the destination file already exists.
                    File.Copy(outputPath, cidArray[0], true);
                }
                //We delete folder and file downloaded
                File.Delete(outputPath);
                Directory.Delete(inputFolder);
            }
            else
            {
                Console.WriteLine("Problem this entry is empty");
            }
        }