PageRenderTime 87ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 0ms

/openphoto.py

https://github.com/RyanBalfanz/OpenPhoto
Python | 172 lines | 161 code | 1 blank | 10 comment | 0 complexity | fb90c0dab875598dd68c66f8b356773b MD5 | raw file
  1. #!/usr/bin/env python
  2. """
  3. A simple web server that can handle photo uploads from the OpenPhoto iOS application.
  4. Uses the Tornado web server: http://www.tornadoweb.org
  5. Created by Kyle Buza on 2/5/11.
  6. Copyright 2010 BuzaMoto. All rights reserved.
  7. Permission is hereby granted, free of charge, to any person
  8. obtaining a copy of this software and associated documentation
  9. files (the "Software"), to deal in the Software without
  10. restriction, including without limitation the rights to use,
  11. copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the
  13. Software is furnished to do so, subject to the following
  14. conditions:
  15. The above copyright notice and this permission notice shall be
  16. included in all copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  19. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. OTHER DEALINGS IN THE SOFTWARE.
  25. """
  26. import os
  27. import json
  28. import base64
  29. import tornado.web
  30. import tornado.auth
  31. import tornado.escape
  32. import tornado.httpserver
  33. import tornado.ioloop
  34. import tornado.options
  35. from tornado.options import define, options
  36. from sqlalchemy import desc
  37. from sqlalchemy import create_engine
  38. from elixir import *
  39. from photo import *
  40. define("port", default=25006, help="Port to run the server on.", type=int)
  41. #Change this to a location that you'd like your photos stored on your filesystem.
  42. PHOTO_DIR_PREFIX = 'static/data/'
  43. class Application(tornado.web.Application):
  44. def __init__(self):
  45. handlers = [
  46. (r"/hello", HelloHandler),
  47. (r"/upload", UploadHandlerLocal),
  48. (r"/comment", CommentHandler)
  49. ]
  50. settings = dict(
  51. static_path= os.path.join(os.path.dirname(__file__), "static"),
  52. template_path=os.path.join(os.path.dirname(os.path.abspath(__file__)), "templates"),
  53. )
  54. tornado.web.Application.__init__(self, handlers, **settings)
  55. class HelloHandler(tornado.web.RequestHandler):
  56. def get(self):
  57. res = { 'code' : 200, 'status' : 'success', 'title' : 'My OpenPhoto Test Server'}
  58. self.write(tornado.escape.json_encode(res))
  59. class CommentHandler(tornado.web.RequestHandler):
  60. def post(self):
  61. photo_id = self.get_argument("photo_id", None)
  62. comment = self.get_argument("comment", None)
  63. if photo_id == None or comment == None:
  64. res = { 'status' : 'failure'}
  65. self.write(tornado.escape.json_encode(res))
  66. return
  67. _photo = Photo.get_by(photo_id=photo_id)
  68. if not _photo:
  69. res = { 'status' : 'nonexistent'}
  70. self.write(tornado.escape.json_encode(res))
  71. return
  72. _photo.comment = comment
  73. session.commit()
  74. res = { 'status' : 'success'}
  75. self.write(tornado.escape.json_encode(res))
  76. class UploadHandlerLocal(tornado.web.RequestHandler):
  77. def post(self):
  78. photo_id = self.get_argument("photo_id", None)
  79. user_id = self.get_argument("user_id", None)
  80. event_id = self.get_argument("eventID", None)
  81. comment = self.get_argument("comment", "")
  82. photo_width = self.get_argument("photo_width", None)
  83. photo_height = self.get_argument("photo_height", None)
  84. thumb_width = self.get_argument("thumb_width", None)
  85. thumb_height = self.get_argument("thumb_height", None)
  86. imgdata_b64 = self.get_argument("photo_b64", None)
  87. imgdata_thumb_b64 = self.get_argument("photo_thumb_b64", None)
  88. latitude = self.get_argument("latitude", 0)
  89. longitude = self.get_argument("longitude", 0)
  90. if photo_id == None or \
  91. event_id == None or \
  92. user_id == None or \
  93. imgdata_b64 == None or \
  94. imgdata_thumb_b64 == None or \
  95. photo_width == None or \
  96. photo_height == None or \
  97. thumb_height == None or \
  98. thumb_width == None:
  99. res = { 'status' : 'failure'}
  100. self.write(tornado.escape.json_encode(res))
  101. return
  102. _photo = Photo.get_by(photo_id=photo_id)
  103. if _photo == None:
  104. imgbytes = base64.decodestring(imgdata_b64)
  105. thumbbytes = base64.decodestring(imgdata_thumb_b64)
  106. try:
  107. f = open(PHOTO_DIR_PREFIX+photo_id+'.png', "w")
  108. f.write(imgbytes)
  109. f.close()
  110. f = open(PHOTO_DIR_PREFIX+photo_id+'_thumb.png', "w")
  111. f.write(thumbbytes)
  112. f.close()
  113. except Exception, e:
  114. res = { 'status' : 'failure'}
  115. self.write(tornado.escape.json_encode(res))
  116. return
  117. else:
  118. res = { 'status' : 'exists'}
  119. self.write(tornado.escape.json_encode(res))
  120. return
  121. try:
  122. _photo = Photo(photo_id, event_id, user_id, photo_width, photo_height, thumb_width, thumb_height, latitude, longitude, comment)
  123. session.commit()
  124. except Exception, e:
  125. res = { 'status' : 'failure'}
  126. self.write(tornado.escape.json_encode(res))
  127. return
  128. res = { 'status' : 'success'}
  129. self.write(tornado.escape.json_encode(res))
  130. def main():
  131. tornado.options.parse_command_line()
  132. http_server = tornado.httpserver.HTTPServer(Application())
  133. http_server.listen(options.port)
  134. tornado.ioloop.IOLoop.instance().start()
  135. def init_db():
  136. metadata.bind = create_engine("sqlite:///openphoto.sqlite", pool_recycle=3600)
  137. setup_all()
  138. create_all()
  139. if __name__ == "__main__":
  140. init_db()
  141. main()