server.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env python2
  2. # -*- coding: utf-8 -*-
  3. from flask import Flask, request, json
  4. import os, argparse
  5. import common
  6. args = None
  7. app = Flask(__name__)
  8. logger = common.genlogger()
  9. def getargs():
  10. parser = argparse.ArgumentParser()
  11. parser.add_argument("--ip", default="0.0.0.0") # default: listen on all ip
  12. parser.add_argument("--port", default="3000")
  13. parser.add_argument("--limit", type=int, default=12) # [GB]
  14. parser.add_argument("--dir", default=common.BAG_PATH)
  15. args = parser.parse_args()
  16. args.dir = common.normpath(args.dir)
  17. common.gendir(args.dir) # create directory
  18. return args
  19. @app.route("/status", methods=["GET"])
  20. def status():
  21. res = { "limit" : args.limit, "dir" : args.dir }
  22. return json.dumps(res)
  23. @app.route("/upload", methods=["POST"])
  24. def upload():
  25. reqform = request.form
  26. reqfile = request.files["file"]
  27. utime, hsh = reqform["utime"], reqform["hash"]
  28. # save file
  29. savepath = "{}/{}".format(args.dir, reqfile.filename)
  30. logger.info("receive file: {}".format(reqfile.filename))
  31. logger.debug("unixtime: {}, hash: {}".format(utime, hsh))
  32. logger.info("-> save path: {}".format(savepath))
  33. reqfile.save(savepath)
  34. # check hash
  35. res = { "success" : common.chkhash(reqform["hash"], savepath) }
  36. if not res["success"]:
  37. logger.error("hash check failed, remove file: {}".format(savepath))
  38. os.remove(savepath)
  39. return json.dumps(res)
  40. if __name__ == "__main__":
  41. args = getargs()
  42. app.config['MAX_CONTENT_LENGTH'] = args.limit * 1024**3
  43. app.run(host=args.ip, port=args.port)