file upload: found a shortcut to find file hash
... just running sha1 directly
This commit is contained in:
parent
14249c61b1
commit
106f97bd10
2 changed files with 77 additions and 19 deletions
66
cli.py
66
cli.py
|
@ -1,4 +1,5 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
import hashlib
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import uuid
|
import uuid
|
||||||
|
@ -7,6 +8,8 @@ import requests
|
||||||
import argparse
|
import argparse
|
||||||
import os.path
|
import os.path
|
||||||
|
|
||||||
|
import retroshare
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from colors import color
|
from colors import color
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
@ -176,13 +179,15 @@ def main_channel_post(args):
|
||||||
return main_channel_post_v1(args)
|
return main_channel_post_v1(args)
|
||||||
|
|
||||||
|
|
||||||
def _file_publish(args, fnames):
|
def get_fsdb(args):
|
||||||
if Fsdb is None:
|
if Fsdb is None:
|
||||||
raise Exception('ERROR: library Fsdb is needed for file publishing')
|
raise Exception('ERROR: library Fsdb is needed for file publishing')
|
||||||
store_dir = os.path.expanduser('~/.config/rscli/store/default')
|
store_dir = os.path.expanduser('~/.config/rscli/store/default')
|
||||||
fsdb = Fsdb(store_dir, fmode='660')
|
return Fsdb(store_dir, fmode='660')
|
||||||
|
|
||||||
virtualname_path = os.path.join(store_dir, 'virtualname.txt')
|
def _file_publish(args, fnames):
|
||||||
|
fsdb = get_fsdb(args)
|
||||||
|
virtualname_path = os.path.join(fsdb.fsdbRoot, 'virtualname.txt')
|
||||||
if os.path.exists(virtualname_path):
|
if os.path.exists(virtualname_path):
|
||||||
virtualname = open(virtualname_path).read().strip()
|
virtualname = open(virtualname_path).read().strip()
|
||||||
else:
|
else:
|
||||||
|
@ -190,7 +195,7 @@ def _file_publish(args, fnames):
|
||||||
open(virtualname_path, 'w').write(virtualname)
|
open(virtualname_path, 'w').write(virtualname)
|
||||||
r = req(args, '/rsFiles/getSharedDirectories')
|
r = req(args, '/rsFiles/getSharedDirectories')
|
||||||
if virtualname not in [shared['virtualname'] for shared in r.json()['dirs']]:
|
if virtualname not in [shared['virtualname'] for shared in r.json()['dirs']]:
|
||||||
r = req(args, '/rsFiles/addSharedDirectory', {'dir':{ 'filename': store_dir,
|
r = req(args, '/rsFiles/addSharedDirectory', {'dir':{ 'filename': fsdb.fsdbRoot,
|
||||||
'virtualname': virtualname
|
'virtualname': virtualname
|
||||||
}})
|
}})
|
||||||
if not r.json()['retval']:
|
if not r.json()['retval']:
|
||||||
|
@ -213,27 +218,50 @@ def _file_publish(args, fnames):
|
||||||
|
|
||||||
for fname in fnames:
|
for fname in fnames:
|
||||||
digest = fsdb.add(fname)
|
digest = fsdb.add(fname)
|
||||||
r = req(args, '/rsFiles/ForceDirectoryCheck')
|
# correct implementation: check hash inside storage
|
||||||
time.sleep(5)
|
# unfortunately it is very slow because we can't give
|
||||||
# mo lo ricerchiamo va
|
# an hint to ForceDirectoryCheck for where to look
|
||||||
looking_for = os.path.relpath(fsdb.get_file_path(digest),
|
# for new files
|
||||||
store_dir).split(os.path.sep)
|
# after the comment, there is a valid implementation, which has the defect
|
||||||
for next_component in looking_for:
|
# of being very coupled to retroshare implementation
|
||||||
r = req(args, '/rsFiles/requestDirDetails', { 'handle': handle })
|
# r = req(args, '/rsFiles/ForceDirectoryCheck')
|
||||||
found = [c for c in r.json()['details']['children']
|
#time.sleep(5)
|
||||||
if c['name'] == next_component]
|
## mo lo ricerchiamo va
|
||||||
if not found:
|
#looking_for = os.path.relpath(fsdb.get_file_path(digest),
|
||||||
raise Exception('Error: could not find shared file in RS')
|
# fsdb.fsdbRoot).split(os.path.sep)
|
||||||
handle = found[0]['handle']
|
#hashlib
|
||||||
r = req(args, '/rsFiles/requestDirDetails', { 'handle': handle })
|
#for next_component in looking_for:
|
||||||
filehash = r.json()['details']['hash']
|
# r = req(args, '/rsFiles/requestDirDetails', { 'handle': handle })
|
||||||
yield filehash
|
# found = [c for c in r.json()['details']['children']
|
||||||
|
# if c['name'] == next_component]
|
||||||
|
# if not found:
|
||||||
|
# raise Exception('Error: could not find shared file in RS')
|
||||||
|
# handle = found[0]['handle']
|
||||||
|
#r = req(args, '/rsFiles/requestDirDetails', { 'handle': handle })
|
||||||
|
#filehash = r.json()['details']['hash']
|
||||||
|
#yield filehash
|
||||||
|
|
||||||
|
h = hashlib.new('sha1')
|
||||||
|
h.update(open(fname, 'rb').read())
|
||||||
|
yield h.hexdigest()
|
||||||
|
|
||||||
|
r = req(args, '/rsFiles/ForceDirectoryCheck')
|
||||||
|
|
||||||
|
def get_file_link(args, hash_digest, fname=None, size=None):
|
||||||
|
fsdb = get_fsdb(args)
|
||||||
|
if fname is None:
|
||||||
|
# TODO: check file name on filesystem
|
||||||
|
fname = os.path.basename(fsdb.get_file_path(hash_digest))
|
||||||
|
if size is None:
|
||||||
|
size = os.stat(fsdb.get_file_path(hash_digest)).st_size
|
||||||
|
return 'retroshare://file?name=%s&size=%d&hash=%s' % (fname, size, hash_digest)
|
||||||
|
|
||||||
|
|
||||||
def main_file_publish(args):
|
def main_file_publish(args):
|
||||||
ret = _file_publish(args, args.fnames)
|
ret = _file_publish(args, args.fnames)
|
||||||
for filehash, fname in zip(ret, args.fnames):
|
for filehash, fname in zip(ret, args.fnames):
|
||||||
print(color(filehash, fg='green') + ' \t%s' % fname)
|
print(color(filehash, fg='green') + ' \t%s' % fname)
|
||||||
|
print(' ' + get_file_link(args, filehash, fname=fname))
|
||||||
|
|
||||||
def get_parser():
|
def get_parser():
|
||||||
p = argparse.ArgumentParser()
|
p = argparse.ArgumentParser()
|
||||||
|
|
30
retroshare.py
Normal file
30
retroshare.py
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
import enum # py3.4
|
||||||
|
|
||||||
|
try:
|
||||||
|
IntFlag = enum.IntFlag
|
||||||
|
except AttributeError:
|
||||||
|
# requires py < 3.6
|
||||||
|
IntFlag = enum.Int
|
||||||
|
|
||||||
|
class RS_FILE_HINTS(IntFlag):
|
||||||
|
CACHE_deprecated = 0x00000001
|
||||||
|
EXTRA = 0x00000002
|
||||||
|
LOCAL = 0x00000004
|
||||||
|
REMOTE = 0x00000008
|
||||||
|
DOWNLOAD = 0x00000010
|
||||||
|
UPLOAD = 0x00000020
|
||||||
|
SPEC_ONLY = 0x01000000
|
||||||
|
NETWORK_WIDE = 0x00000080
|
||||||
|
|
||||||
|
BROWSABLE = 0x00000100
|
||||||
|
SEARCHABLE = 0x00000200
|
||||||
|
|
||||||
|
PERMISSION_MASK = 0x00000380
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# FileStorageFlags
|
||||||
|
DIR_FLAGS_ANONYMOUS_DOWNLOAD = 0x0080
|
||||||
|
DIR_FLAGS_BROWSABLE= 0x0400
|
||||||
|
DIR_FLAGS_ANONYMOUS_SEARCH= 0x0800
|
||||||
|
|
Loading…
Reference in a new issue