def get(self, url_sid): # check user login if (core_util.is_missing(url_sid)): logging.error("[ImageSearchHandler] missing session id") self.error(401) return sid = str(urllib.unquote(url_sid)) login = littlecircle.Login.get_by_sid(sid) if (login is None or login.is_valid() == False): logging.error("[ImageSearchHandler] invalid session id: {}".format(sid)) self.error(401) return list = littlecircle.Photo.query( littlecircle.Photo.deletedDate == None).order( littlecircle.Photo.uploadDate).fetch() array = [] for obj in list: array.append(obj.to_dict(login)) logging.info("[ImageSearchHandler] number of photo: {}".format(len(array))) self.response.headers['Content-Type'] = 'application/json' self.response.out.write(json.dumps(array))
def object_decoder(obj): if (obj is None): return None fb_id = str(obj['fb_id']) logging.info("[User] fb_id: {}".format(fb_id)) if (core_util.is_missing(fb_id)): return None bday = obj['birthday'] birthday = None if (core_util.is_missing(bday) is False): birthday = datetime.datetime.strptime(bday, '%Y-%m-%d').date() logging.info("[User] birthday: {}".format(birthday)) return User( key=ndb.Key(User, "{}{}".format(User.FB_ID_INIT, fb_id)), firstName=obj['first_name'], lastName=obj['last_name'], gender=obj['gender'], email=obj['email'], birthday=birthday )
def get(self): r = littlecircle.Resp() sid = self.request.get('sid') if (core_util.is_missing(sid) or sid.isdigit() == False): logging.error("[UserLogoutHandler] missing sid") else: login = littlecircle.Login.get_by_id(int(sid)) if (login is None): logging.error("[UserLogoutHandler] cannot find login: {}".format(sid)) else: login.status = False login.put() r.status = 1 self.response.headers['Content-Type'] = 'application/json' logging.info("[UserLogoutHandler] status: {}".format(r.status)) self.response.out.write(r.to_json())
def get(self, resource): resource = str(urllib.unquote(resource)) if (core_util.is_missing(resource)): logging.error("[ImageDownloadHandler] missing resource") self.error(404) return blob_info = blobstore.BlobInfo.get(resource) if (blob_info is None): logging.error("[ImageDownloadHandler] cannot find image: {}".format(resource)) self.error(404) return size = blob_info.size name = blob_info.filename logging.info("[ImageDownloadHandler] {} ({})".format(name, size)) self.send_blob(blob_info, save_as=name)
def get(self, url_sid, url_photo): # get the photo ID blob_key = str(urllib.unquote(url_photo)) if (core_util.is_missing(blob_key)): logging.error("[ImageDeleteHandler] missing id") self.error(404) return img = ndb.Key(littlecircle.Photo, blob_key).get() if (img is None): logging.error("[ImageDeleteHandler] missing image 123: {}".format(blob_key)) self.error(404) return # check user login sid = str(urllib.unquote(url_sid)) login = littlecircle.Login.get_by_sid(sid) if (login is None or login.is_valid() == False): logging.error("[ImageDeleteHandler] invalid session id: {}".format(sid)) self.error(401) return # check if the photo is belonged to the user k1 = img.owner k2 = login.user logging.info("[ImageDeleteHandler] owner: {}, login: {}".format(k1.id(), k2.id())) if (k1 != k2): logging.info("[ImageDeleteHandler] permission denied, image: {}".format(blob_key)) self.error(550) # permission denied return # delete photo (set inactive) logging.info("[ImageDeleteHandler] delete image: {}".format(blob_key)) img.deletedBy = k2 img.deletedDate = datetime.datetime.now() img.put() self.response.headers['Content-Type'] = 'application/json' self.response.out.write(littlecircle.Resp(status=1).to_json())
def post(self): r = littlecircle.Resp() user = self.request.get('user') if (core_util.is_missing(user)): logging.error("[UserSyncHandler] missing user") else: logging.info("[UserSyncHandler] user: {}".format(user)) try: u = json.loads(user, object_hook=littlecircle.User.object_decoder) if (u is None): logging.error("[UserSyncHandler] cannot restore user from JSON") else: u.put() logging.info("[UserSyncHandler] user name: {}".format(u.name())) r.status = 1 except: logging.error("[UserSyncHandler] except: {}".format(sys.exc_info())) r.status = 0 self.response.headers['Content-Type'] = 'application/json' logging.info("[UserSyncHandler] status: {}".format(r.status)) self.response.out.write(r.to_json())
def get(self): r = UserLoginResp() userId = self.request.get('user_id') if (core_util.is_missing(userId)): logging.error("[UserLoginHandler] missing user_id") else: userKey = ndb.Key(littlecircle.User, "{}{}".format(littlecircle.User.FB_ID_INIT, userId)) user = userKey.get() if (user is None): logging.error("[UserLoginHandler] cannot find user: {}", userId) else: # logout previous session ID(s) ''' due to the quota limitation in free GAE, here we delete instead of set inactive (reduced security) ''' # keys = littlecircle.Login.query(littlecircle.Login.user==userKey) # list = keys.fetch() # for obj in list: # obj.status = False # ndb.put_multi(keys) ndb.delete_multi(littlecircle.Login.query(littlecircle.Login.user==userKey).fetch(keys_only=True)) # create login record and return session ID k = littlecircle.Login( user=userKey, status=True ).put() r.status = 1 r.userId = userId r.sid = str(k.id()) logging.info("[UserLoginHandler] user {} login".format(userId)) self.response.headers['Content-Type'] = 'application/json' logging.info("[UserLoginHandler] status: {}".format(r.status)) self.response.out.write(r.to_json())
def post(self): # check user login sid = self.request.get('sid') login = littlecircle.Login.get_by_sid(sid) if (login is None or login.is_valid() == False): logging.error("[UploadHandler] invalid session id: {}".format(sid)) self.error(401) return # check user exists user = login.user.get() if (user is None): logging.error("[UploadHandler] cannot get user, sid: {}".format(sid)) self.error(401) return upload_files = self.get_uploads('file') # 'file' is file upload field in the form blob_info = upload_files[0] blob_key = blob_info.key() size = blob_info.size logging.info("[UploadHandler] file size: {}".format(size)) # make preview img = images.Image(blob_key=blob_key) img.resize(width=LITTLECIRCLE_PREVIEW_W) preview = img.execute_transforms(output_encoding=images.JPEG, quality=LITTLECIRCLE_IMG_Q, parse_source_metadata=True) # try to get geo location and date time of the photo env = os.environ['SERVER_SOFTWARE'] dev = (core_util.is_missing(env) == False) and (env.split('/')[0] == 'Development') meta = img.get_original_metadata() logging.debug("[UploadHandler] env: {}, dev: {}, meta: {}".format(env, dev, meta)) # date time dt = None if (dev == True): dt = datetime.datetime.now() elif ('DateTime' in meta): dt = core_util.exif_datetime_to_datetime(meta['DateTime']) # location loc = None if (dev == True): loc = ndb.GeoPt(22.4182277, 114.2080536) # The Chinese University of Hong Kong elif ('GPSLatitude' in meta and 'GPSLongitude' in meta): loc = ndb.GeoPt(meta['GPSLatitude'], meta['GPSLongitude']) # orientation orientation = 1 if ('Orientation' in meta): orientation = meta['Orientation'] if (orientation == 3): logging.info("[UploadHandler] rotate 180") img.rotate(180) ''' Due to GAE's limitation, parse_source_metadata will only be done in execute_transforms it makes preview is generated before img.rotate, therefore, we need to rotate it. But no need to parse_source_metadata again ''' preview = img.execute_transforms(output_encoding=images.JPEG, quality=LITTLECIRCLE_IMG_Q, parse_source_metadata=False) # summary logging.info("[UploadHandler] photo taken at {} in location {}, orientation: {}".format(dt, loc, orientation)) # make thumbnail img.resize(width=LITTLECIRCLE_THUMBNAIL_W) thumb = img.execute_transforms(output_encoding=images.JPEG, quality=LITTLECIRCLE_IMG_Q) # save photo information p = littlecircle.Photo( key=ndb.Key(littlecircle.Photo, str(blob_key)), owner=user.key, #event=eventKey, size=size, geo=loc, photoDate=dt, preview=preview, thumbnail=thumb ) k = p.put() logging.info("[UploadHandler] photo saved, key: {}".format(k.string_id())) self.response.headers['Content-Type'] = 'application/json' self.response.out.write(json.dumps(p.to_dict(login)))
def get(self): self.response.headers['Content-Type'] = 'image/jpeg' # get the photo ID blob_key = self.request.get('id') if (core_util.is_missing(blob_key)): logging.error("[ImageViewHandler] missing id") self.error(404) return # check user login if (littlecircle.Login.is_valid_sid(self.request.get('sid')) == False): logging.error("[ImageViewHandler] invalid session id") self.error(401) return # get the photo size required size = self.request.get('size') logging.info("[ImageViewHandler] key: {} size: {}".format(blob_key, size)) if size == '2': logging.info("[ImageViewHandler] send size: {}".format(blob_key)) self.redirect('/download/%s' % blob_key) elif size == '1': logging.info("[ImageViewHandler] send preview") try: # get the stored photo img = ndb.Key(littlecircle.Photo, blob_key).get() if (img is None): raise Exception("[ImageViewHandler] cannot find image: {}".format(blob_key)) else: # get the stored preview preview = img.preview if (preview is None): logging.info("[ImageViewHandler] preview not found, try to make it") fullImg = images.Image(blob_key=blob_key) fullImg.resize(width=LITTLECIRCLE_PREVIEW_W) preview = fullImg.execute_transforms(output_encoding=images.JPEG, quality=LITTLECIRCLE_IMG_Q) if (preview is None): raise Exception("[ImageViewHandler] cannot make preview: {}".format(blob_key)) logging.debug("[ImageViewHandler] save back the preview") img.preview = preview img.put() else: logging.debug("[ImageViewHandler] found stored preview") self.response.out.write(preview) except: logging.error("[ImageViewHandler] except: {}".format(sys.exc_info())) self.error(404) else: logging.info("[ImageViewHandler] send thumbnail") try: # get the stored photo img = ndb.Key(littlecircle.Photo, blob_key).get() if (img is None): raise Exception("[ImageViewHandler] cannot find image: {}".format(blob_key)) else: # get the stored thumbnail thumbnail = img.thumbnail if (thumbnail is None): logging.info("[ImageViewHandler] thumbnail not found, try to make it") fullImg = images.Image(blob_key=blob_key) fullImg.resize(width=LITTLECIRCLE_THUMBNAIL_W) thumbnail = fullImg.execute_transforms(output_encoding=images.JPEG, quality=LITTLECIRCLE_IMG_Q) if (thumbnail is None): raise Exception("[ImageViewHandler] cannot make thumbnail: {}".format(blob_key)) logging.debug("[ImageViewHandler] save back the thumbnail") img.thumbnail = thumbnail img.put() else: logging.debug("[ImageViewHandler] found stored thumbnail") self.response.out.write(thumbnail) except: logging.error("[ImageViewHandler] except: {}".format(sys.exc_info())) self.error(404)