remove the hardcoded download

This commit is contained in:
Leo Arias 2017-12-03 18:12:32 +00:00
parent 2da65dbcf9
commit da183cddc9
1 changed files with 15 additions and 22 deletions

37
ipfs
View File

@ -23,7 +23,6 @@
import hashlib import hashlib
import os import os
import sys import sys
import tempfile


import ipfsapi import ipfsapi


@ -63,11 +62,13 @@ class IPFS_method():
if message is None: if message is None:
return 0 return 0
if message['number'] == 600: if message['number'] == 600:
uri = message['URI'][0]
filename = message['Filename'][0]
try: try:
self.fetch(message) self.fetch(uri, filename)
except Exception as e: except Exception as e:
self.send_uri_failure({ self.send_uri_failure({
'URI': self.uri, 'URI': uri,
'Message': e.__class__.__name__ + ": " + str(e)}) 'Message': e.__class__.__name__ + ": " + str(e)})
else: else:
return 100 return 100
@ -104,18 +105,14 @@ class IPFS_method():
result[item].append(value.strip()) result[item].append(value.strip())
return result return result


def fetch(self, message): def fetch(self, uri, filename):
self.uri = message['URI'][0] ipfs_file_path = uri.replace('ipfs://', '', 1)
self.filename = message['Filename'][0]


self.send_status({'URI': self.uri, 'Message': 'Waiting for stats'}) self.send_status({'URI': uri, 'Message': 'Waiting for stats'})
ipfs = ipfsapi.connect('127.0.0.1', 5001) ipfs = ipfsapi.connect('127.0.0.1', 5001)
inrelease = ( stat = ipfs.object_stat(ipfs_file_path)
'QmabnVr8k4uFdwQ8dW2P3jEUxdVPF8FcAgc2i8q7T2ArMg/'
'dists/xenial/InRelease')
stat = ipfs.object_stat(inrelease)
self.send_uri_start({ self.send_uri_start({
'URI': self.uri, 'URI': uri,
# FIXME We can't get the real size without downloading the file. # FIXME We can't get the real size without downloading the file.
# https://github.com/ipfs/go-ipfs/issues/2071 # https://github.com/ipfs/go-ipfs/issues/2071
# --elopio - 20171203 # --elopio - 20171203
@ -123,30 +120,26 @@ class IPFS_method():


# XXX IPFS downloads the file to the current directory. # XXX IPFS downloads the file to the current directory.
# --elopio - 20171203 # --elopio - 20171203
tmp_dir = tempfile.gettempdir()
os.chdir(tmp_dir)
fetched_file_path = os.path.join(
tmp_dir, os.path.basename(inrelease))
cwd = os.getcwd() cwd = os.getcwd()
os.chdir(os.path.dirname(filename))
try: try:
ipfs.get(inrelease) ipfs.get(ipfs_file_path)
finally: finally:
os.chdir(cwd) os.chdir(cwd)
os.rename(fetched_file_path, self.filename)


hash_md5 = hashlib.md5() hash_md5 = hashlib.md5()
hash_sha256 = hashlib.sha256() hash_sha256 = hashlib.sha256()
hash_sha512 = hashlib.sha512() hash_sha512 = hashlib.sha512()
with open(self.filename, 'rb') as fetched_file: with open(filename, 'rb') as fetched_file:
for chunk in iter(lambda: fetched_file.read(4096), b''): for chunk in iter(lambda: fetched_file.read(4096), b''):
hash_md5.update(chunk) hash_md5.update(chunk)
hash_sha256.update(chunk) hash_sha256.update(chunk)
hash_sha512.update(chunk) hash_sha512.update(chunk)


self.send_uri_done({ self.send_uri_done({
'URI': self.uri, 'URI': uri,
'Filename': self.filename, 'Filename': filename,
'Size': os.stat(self.filename).st_size, 'Size': os.stat(filename).st_size,
'MD5-Hash': hash_md5.hexdigest(), 'MD5-Hash': hash_md5.hexdigest(),
'MD5Sum-Hash': hash_md5.hexdigest(), 'MD5Sum-Hash': hash_md5.hexdigest(),
'SHA256-Hash': hash_sha256.hexdigest(), 'SHA256-Hash': hash_sha256.hexdigest(),