Beispiel #1
0
def upload(api, input_file=sys.stdin, verbose=False,
           encrypt=False, webui=False):
    """
    Upload and (optionally) encrypt a specified file.

    :param ipfsapi.Client api: The IPFS API client instance to use
    :param input_file: The file to upload
    :param bool verbose: Verbosity on/off
    :param bool encrypt: Encryption on/off

    :returns: A hash of the uploaded file
    :rtype: str
    """
    # Read stdin...
    if input_file is sys.stdin:
        if verbose:
            sys.stderr.write(
                'Waiting for standard input... ' +
                '(type your doc and press Ctrl+D)\n'
            )
            sys.stderr.flush()

        file_contents = sys.stdin.buffer.read()
        sys.stderr.write('\n')
    # ...or the specified file
    else:
        with open(input_file, 'rb') as f:
            file_contents = f.read()

    suffix = ''
    if encrypt or webui:  # WebUI implies encryption
        secret = Fernet.generate_key()
        if verbose:
            sys.stderr.write("Using secret %r...\n" % secret)
        cipher = Fernet(secret)
        file_contents = cipher.encrypt(ensure_bytes(file_contents))

        # Convert the secret to base58 for consistence
        secret58 = b58encode(secret)

        suffix += '/#'

        if webui:
            file_contents = render(ensure_unicode(
                file_contents), verbose=verbose)
            suffix += WEBUI_KEY_PREFIX

        suffix += secret58

    addr = api.add_bytes(ensure_bytes(file_contents)) + suffix

    return addr
Beispiel #2
0
 def test_str(self, cid):
     assert str(cid) == ensure_unicode(cid.encode())
Beispiel #3
0
 def __str__(self):
     return ensure_unicode(self.encode())