Пример #1
0
        public string GetFilePathSync(string path)
        {
			var documentsPath = DocumentConstants.DocumentsPath;
			var pathToFile =  Path.Combine(documentsPath, "" + Waardes.Instance.GeselecteerdeBox, path.Substring(1, path.Length -1));
            EmptyDecryptedFolder();


			if(!File.Exists(pathToFile)) //Does not exist
            {
                var explorer = new RemoteExplorer();
                var fileBytes = explorer.GetFile(path);

                if(fileBytes == null) {
                    return null;
                }

				if (!Directory.Exists(pathToFile.Substring(0, pathToFile.LastIndexOf("/"))))
                {
					Directory.CreateDirectory(pathToFile.Substring(0, pathToFile.LastIndexOf("/")));
                }

				if (pathToFile != null)
					File.WriteAllBytes(pathToFile, fileBytes);
            }



            // moeten buiten de root zitten
            if (path.Count(e => e == '/') > 1)
            {
                int index = path.IndexOf('/', path.IndexOf('/') + 1);
                var rootFolder = path.Substring(0, index);
                var folder = GetFolder(rootFolder).Result;

                if (folder.HasCryptoKeys)
                {
					try{
						Rijndael rijndael = Rijndael.Create(); 
                    	rijndael.Key = folder.Key; 
                    	rijndael.IV = folder.IV; 
							
                    	var decpath = Path.Combine(documentsPath, "decrypted", path.Substring(1, path.Length - 1));
                    	if (!Directory.Exists(decpath.Substring(0, decpath.LastIndexOf("/"))))
                    	{
                    	    Directory.CreateDirectory(decpath.Substring(0, decpath.LastIndexOf("/")));
                    	}

						using (var stream = new CryptoStream(File.OpenRead(pathToFile), rijndael.CreateDecryptor(), CryptoStreamMode.Read))
                    	using(var fs = File.OpenWrite(decpath))
                    	{
                    	    stream.CopyTo(fs);
                    	}
                    	return decpath;
					}catch (Exception ex){
						Insights.Report(ex);
						return pathToFile;
					}
                }
            }
			
			return pathToFile;
        }