Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

ipfs.files.exists? #780

Closed
aphelionz opened this issue Mar 8, 2017 · 5 comments
Closed

ipfs.files.exists? #780

aphelionz opened this issue Mar 8, 2017 · 5 comments
Labels
kind/support A question or request for support

Comments

@aphelionz
Copy link
Contributor

Another possible "exists" request.

I'm working with ipfs.files.cat and I may have the need to detect whether a hash exists in the local repo before trying to get it from a gateway. Since the browser node isn't autoatically peering and retrieving files from the DHT yet (or I don't know how to manually implement it yet (or both)), I need to create a workflow that's something like:

  1. Try to attempt to grab the content locally
  2. Catch the exception and grab it from a dedicated gateway I have hosted on AWS

However, looking at the code for cat, I see the pull stream calls at the bottom:

        pull(
          exporter(hash, self._ipldResolver),
          pull.collect((err, files) => {
            if (err) {
              return callback(err)
            }
            callback(null, toStream.source(files[0].content))
          })
        )

I don't think that pull.colllect call is fired if the hash doesn't exist, so when I create a promise chain like below, it never fails or resolves.

        this.ipfs.files.cat(e.detail)
          .then(this._processObjectStream)
          .then(this._fireBodyEvent.bind(this, "file-read"))
          .catch(this._getFileFromServer.bind(this));

Not sure exactly how to proceed. Any advice or workaround is appreciated. Thank you!

@daviddias
Copy link
Member

@aphelionz I believe that is what ipfs refs local is for, although it just lists the hashes of objects, it has no notion of 'files'.

» ipfs refs local --help
USAGE
  ipfs refs local - List all local references.

SYNOPSIS
  ipfs refs local

DESCRIPTION

  Displays the hashes of all local objects.

A files.exists might be indeed useful, but also misleading, it should include options for:

  • local
  • check only root (avoid recursively checking all the leafs)

Please consider making a proposal to https://github.com/ipfs/interface-ipfs-core/tree/master/API/files with a PR.

@aphelionz
Copy link
Contributor Author

Right, I'm still getting the nomenclature right. Checking to see if the hash exists locally should be enough. Let me try with that and I'll report back and close the issue if it suffices.

@aphelionz
Copy link
Contributor Author

@diasdavid Though, should the pull() function have an error state that throws and therefore doesnt break the promise chain? Or am I using that / reading that incorrectly?

@daviddias daviddias added the status/deferred Conscious decision to pause or backlog label Mar 9, 2017
@daviddias daviddias added the kind/support A question or request for support label Jul 7, 2017
@daviddias
Copy link
Member

@aphelionz apologize for the delay. The reason is that right now we don't have timeouts intentionally, which means that the pull/promise chain will wait forever.

See: https://github.com/ipfs/interface-ipfs-core/issues/58

Did you consider to add this call to the interface definition?

@daviddias
Copy link
Member

Created the issue to get tracked there: ipfs-inactive/interface-js-ipfs-core#146

@daviddias daviddias removed the status/deferred Conscious decision to pause or backlog label Jul 7, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind/support A question or request for support
Projects
None yet
Development

No branches or pull requests

2 participants