Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. import logging
  2. from errbot import botcmd, BotPlugin
  3. from config import BOT_DATA_DIR
  4. from os import path, mkdir, walk
  5. from io import open
  6. import shutil
  7.  
  8. FILESHARE_PATH = path.join(BOT_DATA_DIR, "public")
  9.  
  10. if not path.exists(FILESHARE_PATH):
  11. mkdir(FILESHARE_PATH)
  12.  
  13.  
  14. class FileShare(BotPlugin):
  15. min_err_version = '2.2.0-beta' # for file transfers
  16.  
  17. def target(self, name):
  18. full_path = path.join(FILESHARE_PATH, name)
  19. if full_path != path.abspath(full_path):
  20. logging.warn('Refused the filename "%s" is it an injection attempt?' % name)
  21. return ''
  22. return full_path
  23.  
  24. def callback_stream(self, stream):
  25. super(FileShare, self).callback_stream(stream)
  26. import pdb;pdb.set_trace()
  27. if not stream.name:
  28. logging.info("Anonymous stream, I can't save that")
  29. return
  30.  
  31. logging.debug('Receive the file "%s"' % stream.name)
  32. destination_path = self.target(stream.name)
  33. if not destination_path:
  34. self.send(stream.identity, "Invalid filename %s." % stream.name)
  35. return
  36. with open(destination_path, "wb") as destination:
  37. shutil.copyfileobj(stream, destination)
  38. self.send(stream.identity, "File %q well received." % stream.name)
  39.  
  40.  
  41. @botcmd
  42. def download(self, mess, args):
  43. target = self.target(args)
  44. if not target:
  45. return 'Invalid filename "%s"' % target
  46.  
  47. if not path.exists(target):
  48. return 'File not found %s' % args
  49. self.send_stream_request(mess.frm, open(target, 'rb'), name=args, size=path.getsize(target),
  50. stream_type='document')
  51. return 'File request sent'
  52.  
  53. @botcmd
  54. def upload(self, mess, args):
  55. return "upload your file"
  56.  
  57. @botcmd
  58. def upload_file(self, mess, args):
  59. target = '/home/beast/Downloads/errbot.pdf'
  60. self.send_stream_request(mess.frm, open(target, 'rb'), name=args, size=path.getsize(target),
  61. stream_type='document')
  62. return "file uploaded successfully"
  63.  
  64. @botcmd
  65. def ls(self, mess, args):
  66. return '\n'.join(['\n'.join([n for n in f]) for p, _, f in walk(FILESHARE_PATH)])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement