rsfiles.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import hashlib
  2. import time
  3. import uuid
  4. import os.path
  5. try:
  6. from fsdb import Fsdb
  7. except ImportError:
  8. Fsdb = None
  9. from rscli.httputils import req
  10. from rscli import retroshare
  11. def get_fsdb(args):
  12. if Fsdb is None:
  13. raise Exception("ERROR: library Fsdb is needed for file publishing")
  14. store_dir = os.path.expanduser("~/.config/rscli/store/default")
  15. return Fsdb(store_dir, fmode="660")
  16. def filename_to_hash(args, dir_virtualname, filename):
  17. r = req(args, "/rsFiles/getSharedDirectories")
  18. dir_filename = [
  19. d["filename"] for d in r.json()["dirs"] if d["virtualname"] == dir_virtualname
  20. ][0]
  21. r = req(args, "/rsFiles/requestDirDetails", {"handle": 0})
  22. children = [
  23. c for c in r.json()["details"]["children"] if c["name"] != "[Extra List]"
  24. ]
  25. for possibile_root in children:
  26. r = req(
  27. args, "/rsFiles/requestDirDetails", {"handle": possibile_root["handle"]}
  28. )
  29. found = [
  30. c for c in r.json()["details"]["children"] if c["name"] == dir_filename
  31. ]
  32. if not found:
  33. raise Exception("Error: could not find shared file in RS")
  34. handle = found[0]["handle"]
  35. r = req(args, '/rsFiles/ForceDirectoryCheck')
  36. time.sleep(5)
  37. looking_for = filename.split(os.path.sep)
  38. hashlib
  39. for next_component in looking_for:
  40. r = req(args, '/rsFiles/requestDirDetails', { 'handle': handle })
  41. found = [c for c in r.json()['details']['children']
  42. if c['name'] == next_component]
  43. if not found:
  44. raise Exception('Error: could not find shared file in RS')
  45. handle = found[0]['handle']
  46. r = req(args, '/rsFiles/requestDirDetails', { 'handle': handle })
  47. filehash = r.json()['details']['hash']
  48. return filehash
  49. def file_publish(args, fnames):
  50. fsdb = get_fsdb(args)
  51. virtualname_path = os.path.join(fsdb.fsdbRoot, "virtualname.txt")
  52. if os.path.exists(virtualname_path):
  53. virtualname = open(virtualname_path).read().strip()
  54. else:
  55. virtualname = "rscli-%s" % uuid.uuid4()
  56. open(virtualname_path, "w").write(virtualname)
  57. r = req(args, "/rsFiles/getSharedDirectories")
  58. if virtualname not in [shared["virtualname"] for shared in r.json()["dirs"]]:
  59. r = req(
  60. args,
  61. "/rsFiles/addSharedDirectory",
  62. {
  63. "dir": {
  64. "filename": fsdb.fsdbRoot,
  65. "virtualname": virtualname,
  66. "shareflags": retroshare.DIR_FLAGS.ANONYMOUS_DOWNLOAD,
  67. }
  68. },
  69. )
  70. if not r.json()["retval"]:
  71. raise Exception("Error: could not create shared dir for default store")
  72. time.sleep(1)
  73. for fname in fnames:
  74. fsdb.add(fname)
  75. # print(filename_to_hash(args, virtualname, fsdb.get_file_path(digest)))
  76. h = hashlib.new("sha1")
  77. h.update(open(fname, "rb").read())
  78. yield h.hexdigest()
  79. r = req(args, "/rsFiles/ForceDirectoryCheck")
  80. def get_file_link(args, hash_digest, fname=None, size=None):
  81. fsdb = get_fsdb(args)
  82. if fname is None:
  83. # TODO: check file name on filesystem
  84. fname = os.path.basename(fsdb.get_file_path(hash_digest))
  85. if size is None:
  86. size = os.stat(fsdb.get_file_path(hash_digest)).st_size
  87. return "retroshare://file?name=%s&size=%d&hash=%s" % (fname, size, hash_digest)