示例#1
0
 public static async Task <IpfsFileListing> GetFileListingAsync()
 {
     if (_FileListing == null)
     {
         _FileListing = await IpfsFileListing.Load();
     }
     return(_FileListing);
 }
示例#2
0
        public static async Task <IpfsFileListing> Load()
        {
            IpfsFileListing returnValue = new IpfsFileListing();

            try
            {
                //Load the file listing if we have one locally
                if (File.Exists(ListingFilename))
                {
                    using (StreamReader reader = new StreamReader(File.OpenRead(ListingFilename)))
                    {
                        string json = reader.ReadToEnd();
                        returnValue = JsonConvert.DeserializeObject <IpfsFileListing>(json);
                    }
                }

                string localPeerId = IpfsApiWrapper.GetPeerId();

                //Validate that the local file matches the remote file

                /*{
                 *  string listingFileHash = await IpfsApiWrapper.ResolveAsync(localPeerId);
                 *  IpfsFileListing remoteFileListing = await FetchListingFile(listingFileHash, false);
                 *  if (remoteFileListing != returnValue)
                 *  {
                 *      returnValue.Dirty = true;
                 *  }
                 * }*/

                PeerListing peerListing = await IpfsApiWrapper.GetPeerListingAsync();

                foreach (string peer in peerListing.Peers)
                {
                    IpfsFileListing peerFileListing = null;
                    if (peer != localPeerId)
                    {
                        string listingFileHash = await IpfsApiWrapper.ResolveAsync(peer);

                        peerFileListing = await FetchListingFile(listingFileHash, false);
                    }
                    else if (File.Exists(ListingFilename))
                    {
                        string listingFileHash = await IpfsApiWrapper.ResolveAsync();

                        peerFileListing = await FetchListingFile(listingFileHash, true);
                    }

                    returnValue.MergeFile(peerFileListing);
                }
            }
            catch
            {
            }

            await returnValue.Sync();

            return(returnValue);
        }
示例#3
0
 private void MergeFile(IpfsFileListing other)
 {
     foreach (IpfsFile file in other._Files)
     {
         string mfLocalFilename = file.LocalFilename;
         if (!_Files.Any(o => o.LocalFilename == mfLocalFilename))
         {
             Dirty = true;
             _Files.Add(file);
         }
         else
         {
             ResolveConflict(file);
         }
     }
 }
示例#4
0
        private static async Task <IpfsFileListing> FetchListingFile(string listingFileHash, bool keep = false)
        {
            IpfsFileListing returnValue = null;

            try
            {
                string localListingFilename = keep ? ListingFilename : Path.GetTempFileName();
                if (!string.IsNullOrEmpty(listingFileHash) && await IpfsApiWrapper.Get(listingFileHash, localListingFilename))
                {
                    using (StreamReader reader = new StreamReader(File.OpenRead(localListingFilename)))
                    {
                        string json = reader.ReadToEnd();
                        returnValue = JsonConvert.DeserializeObject <IpfsFileListing>(json);
                    }
                }
            }
            catch
            {
                //In case the peer's file hasn't yet been published to ipns
            }
            returnValue.Dirty = false;
            return(returnValue);
        }