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 os
import sys
import tempfile

import ipfsapi

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

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

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)
inrelease = (
'QmabnVr8k4uFdwQ8dW2P3jEUxdVPF8FcAgc2i8q7T2ArMg/'
'dists/xenial/InRelease')
stat = ipfs.object_stat(inrelease)
stat = ipfs.object_stat(ipfs_file_path)
self.send_uri_start({
'URI': self.uri,
'URI': uri,
# FIXME We can't get the real size without downloading the file.
# https://github.com/ipfs/go-ipfs/issues/2071
# --elopio - 20171203
@ -123,30 +120,26 @@ class IPFS_method():

# XXX IPFS downloads the file to the current directory.
# --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()
os.chdir(os.path.dirname(filename))
try:
ipfs.get(inrelease)
ipfs.get(ipfs_file_path)
finally:
os.chdir(cwd)
os.rename(fetched_file_path, self.filename)

hash_md5 = hashlib.md5()
hash_sha256 = hashlib.sha256()
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''):
hash_md5.update(chunk)
hash_sha256.update(chunk)
hash_sha512.update(chunk)

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