download.py 936 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #! /usr/bin/env python
  2. import os, sys
  3. import cgi, cgitb
  4. cgitb.enable()
  5. PKGDIR = os.environ.get('PKGDIR', os.path.abspath('../packages'))
  6. form = cgi.FieldStorage()
  7. def getvalue(x):
  8. v = form.getvalue(x)
  9. if not v:
  10. print("Status: 413\ncontent-type: text/plain\n\nmissing %s\n" % x)
  11. return v
  12. pkgname = getvalue('pkgname')
  13. pkgver = getvalue('pkgver')
  14. pkgfile = getvalue('pkgfile')
  15. filename = os.path.join(PKGDIR, pkgname, pkgver, pkgfile)
  16. if not os.path.exists(filename):
  17. filename = filename + '.tarfile'
  18. if not os.path.exists(filename):
  19. print("Status: 404\ncontent-type: text/plain\n\nInvalid package %r\n" % filename)
  20. length = os.stat(filename).st_size
  21. print("Content-Type: application/octet-stream")
  22. print("Content-Disposition: attachment; filename=f.bin")
  23. print("Content-length: %s" % length)
  24. print("")
  25. with open(filename, 'rb') as f:
  26. while True:
  27. buf = f.read(8192)
  28. if buf:
  29. sys.stdout.write(buf)
  30. else:
  31. break