IF YOU WOULD LIKE TO GET AN ACCOUNT, please write an email to s dot adaszewski at gmail dot com. User accounts are meant only to report issues and/or generate pull requests. This is a purpose-specific Git hosting for ADARED projects. Thank you for your understanding!
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

__main__.py 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import aiohttp
  2. from aiohttp import web
  3. from argparse import ArgumentParser
  4. import ssl
  5. import json
  6. def options_fetch_blocks(request):
  7. return web.Response(headers={ 'Access-Control-Allow-Origin': '*' })
  8. async def post_fetch_blocks(request):
  9. body = await request.read()
  10. body = json.loads(body)
  11. proxy_host = body['keepProxyHost']
  12. arv_token = body['arvToken']
  13. segments = body['segments']
  14. use_ssl = body['useSsl'] \
  15. if 'useSsl' in body \
  16. else True
  17. protocol = 'https://' \
  18. if use_ssl \
  19. else 'http://'
  20. name = body['name'] \
  21. if 'name' in body \
  22. else None
  23. content_type = body['contentType'] \
  24. if 'contentType' in body \
  25. else 'application/octet-stream'
  26. res = web.StreamResponse()
  27. res.content_type = content_type
  28. if name:
  29. res.headers['Content-Disposition'] = 'attachment; filename*=UTF-8\'\'"' + name + '"'
  30. else:
  31. res.headers['Content-Disposition'] = 'inline'
  32. res.headers['Access-Control-Allow-Origin'] = '*'
  33. await res.prepare(request)
  34. async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(verify_ssl=False)) as session:
  35. for seg in segments:
  36. url = protocol + proxy_host + '/' + seg[0]
  37. async with session.get(url, headers={ 'Authorization': 'OAuth2 ' + arv_token }) as response:
  38. block = await response.read()
  39. block = block[seg[1]:seg[2]]
  40. await res.write(block)
  41. return res
  42. def get_index(request):
  43. return web.Response(text='Use /fetch-blocks to stream files from Keep')
  44. def create_parser():
  45. parser = ArgumentParser()
  46. parser.add_argument('--port', type=int, default=50080)
  47. parser.add_argument('--ssl-cert', type=str, default=None)
  48. return parser
  49. def main():
  50. parser = create_parser()
  51. args = parser.parse_args()
  52. app = web.Application()
  53. app.add_routes([
  54. web.get('/', get_index),
  55. web.post('/fetch-blocks', post_fetch_blocks),
  56. web.options('/fetch-blocks', options_fetch_blocks)
  57. ])
  58. if args.ssl_cert:
  59. ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
  60. ssl_context.load_cert_chain(args.ssl_cert)
  61. else:
  62. ssl_context = None
  63. web.run_app(app, port=args.port, ssl_context=ssl_context)
  64. if __name__ == '__main__':
  65. main()