def _handle_render(self, media_info): """ @param media_info: Information about the image @type media_info: Dictionary """ try: validation.media_id(self.media_id) except: self.size = 49 self.log.warning("requested image id is not valid: %s" % self.media_id) return self._serve_image_error("Invalid Image ID: %s" % self.media_id) return self._serve_image(media_info)
def _make_media_path(self, media_id, host, username=None, width=None, height=None, crop=None): """ Makes a path to an image. @param media_id: ID of the image @type media_id: String @param host: Host that holds the image @type host: String @param username: User who modified the image (if applicable) @type username: String @param width: Width of the render @type width: Integer @param height: Height of the render @type height: Integer @param crop: Whether or not the render is cropped @type crop: Integer """ try: media_id = validation.media_id(media_id) validation.required(host, "host") if username: username = validation.username(username, "username") if width: width = validation.cast_integer(width, "width") height = validation.cast_integer(height, "height") crop = validation.cast_boolean(crop, "crop") except errors.ValidationError, ex: return utils.return_deferred_error(ex.value)
def _make_media_path(self, media_id, host, username=None, width=None, height=None, crop=None): """ Makes a path to an image. @param media_id: ID of the image @type media_id: String @param host: Host that holds the image @type host: String @param username: User who modified the image (if applicable) @type username: String @param width: Width of the render @type width: Integer @param height: Height of the render @type height: Integer @param crop: Whether or not the render is cropped @type crop: Integer """ try: media_id = validation.media_id(media_id) validation.required(host, 'host') if username: username = validation.username(username, 'username') if width: width = validation.cast_integer(width, 'width') height = validation.cast_integer(height, 'height') crop = validation.cast_boolean(crop, 'crop') except errors.ValidationError, ex: return utils.return_deferred_error(ex.value)
def user_owns_image(self, media_id, owner_username): """ Determines if a particular user owns an image. @param media_id: Media ID to check. @type media_id: String @param owner_username: User to check ownership for. @type owner_username: String @return: Whether or not the specified user owns a copy of the image. @rtype: Boolean """ media_id = validation.media_id(media_id) owner_username = validation.username(owner_username) @stack def format_result(result): return result['zoto_user_owns_image'] d = self.app.db.query(""" select zoto_user_owns_image(zoto_get_user_id(%s), %s) """, (owner_username, media_id), single_row=True) d.addCallback(format_result) return d
def image_exists(self, media_id): """ Checks to see if a given image exists anywhere on the system. @param media_id: media identifier @type media_id: String @return: True or False @rtype: Boolean """ media_id = validation.media_id(media_id) d = self.app.db.query(""" SELECT zoto_media_exists(%s) AS image_exists """, (media_id, ), single_row=True) @stack def act(results): if results['image_exists']: return True else: return False d.addCallback(act) return d
def binary_exists(self, media_id): """ Checks to see if a media binary already exists on the system. """ try: media_id = validation.media_id(media_id) except errors.ValidationError, ex: return utils.return_deferred_error(ex.value)
def get_media_raw_data(self, media_id, username=None): """ Gets raw binary data of image @param media_id: Id if image to extract raw data from @type media_id: String @return: Raw Data of Image @rtype: (Deferred) String """ try: media_id = validation.media_id(media_id) except errors.ValidationError, ex: return utils.return_deferred_error(ex.value)
def _locate_media(self, media_id): """ Find out what server(s) are holding a particular media binary @param media_id: the media_id you are trying to find @type media_id: String @return: A list of hostnames that have the media_id @rtype: List """ try: media_id = validation.media_id(media_id) except errors.ValidationError, ex: return utils.return_deferred_error(ex.value)
def get_image_id(self, owner_username, media_id): """ Gets a numeric image_id based on the owner/media combination. @param owner_username: Owner of the image @type owner_username: String @param media_id: Hash identifier for the media @type media_id: String """ try: owner_username = validation.username(owner_username, 'owner_username') media_id = validation.media_id(media_id) except errors.ValidationError, ex: return utils.return_deferred_error(ex.value)
def clear_renders(self, media_id, owner_username, node): """ Clears out all the renders for a particular user's image. @param media_id: ID of the media being cleared @type media_id: String @param owner_username: User who owns the image @type owner_username: String """ try: media_id = validation.media_id(media_id) owner_username = validation.username(owner_username) validation.required(node, 'node') except errors.ValidationError, ex: return utils.return_deferred_error(ex.value)
def clear_renders(self, media_id, owner_username, node): """ Clears out all the renders for a particular user's image. @param media_id: ID of the media being cleared @type media_id: String @param owner_username: User who owns the image @type owner_username: String """ try: media_id = validation.media_id(media_id) owner_username = validation.username(owner_username) validation.required(node, "node") except errors.ValidationError, ex: return utils.return_deferred_error(ex.value)
def _generate_render_paths(self, media_id, host, username=None): """ Generates all possible render storage for the given host/media_id/username. @param media_id: ID of the media rendered @type media_id: String @param host: Host the render is stored on @type host: String @param username: Specific username, if applicable @type username: String (or None) """ try: media_id = validation.media_id(media_id) validation.required(host, 'host') if username: username = validation.username(username, 'username') except errors.ValidationError, ex: return utils.return_deferred_error(ex.value)
def _generate_render_paths(self, media_id, host, username=None): """ Generates all possible render storage for the given host/media_id/username. @param media_id: ID of the media rendered @type media_id: String @param host: Host the render is stored on @type host: String @param username: Specific username, if applicable @type username: String (or None) """ try: media_id = validation.media_id(media_id) validation.required(host, "host") if username: username = validation.username(username, "username") except errors.ValidationError, ex: return utils.return_deferred_error(ex.value)
def get_media_info(self, media_id, username=None, binary_data=False): """ Fetch the binary data of a given media_id @param media_id: the media_id you are trying to find @type media_id: String @param username: If specified find this user's most up to date version of the media (replaces our old filter-hash concept) @type username: String @return: binary media data, media type, and size @rtype: 3 element tuple """ try: media_id = validation.media_id(media_id) if username: username = validation.username(username, 'username') except errors.ValidationError, ex: return utils.returnDeferredError(ex.value)
def get_media_info(self, media_id, username=None, binary_data=False): """ Fetch the binary data of a given media_id @param media_id: the media_id you are trying to find @type media_id: String @param username: If specified find this user's most up to date version of the media (replaces our old filter-hash concept) @type username: String @return: binary media data, media type, and size @rtype: 3 element tuple """ try: media_id = validation.media_id(media_id) if username: username = validation.username(username, "username") except errors.ValidationError, ex: return utils.returnDeferredError(ex.value)
def _find_suitable_storage_targets(self, media_id, data, distribution_total): """ Find distribution_total nodes capable of storing the supplied binary data. @param media_id: Unique id for the media. @type media_id: String @param data: Binary data to store @type data: String @param distribution_total: Number of nodes to get @type distribution_total: Integer @return: Hosts @rtype: List """ try: media_id = validation.media_id(media_id) if md5(data).hexdigest() != media_id: raise errors.ValidationError("media_id doesn't match data!") except errors.ValidationError, ex: return utils.return_deferred_error(ex.value)
self.size = 24 ## ## Parse the url, making sure it is in the correct format. ## size, media_id = self.segments[0:2] media_id = media_id.replace('.jpg', '') self.media_id = media_id.replace('.jpeg', '') try: self.process_size_info(size) except Exception, ex: return self._serve_image_error(ex) try: self.media_id = validation.media_id(self.media_id) except errors.ValidationError, ex: return _self.serve_image_error("Invalid Image ID: %s" % self.media_id) def handle_fail(fail): return self._serve_image_error("Internal Server Error: %s" % fail.getErrorMessage()) @stack def act_can_serve(result): if not result['can_serve']: return self._serve_image_error("Can't serve image [%s:%s]" % (self.username, self.media_id)) d4 = self.app.api.mediahost.get_media_info(self.media_id, self.username) d4.addCallback(act_media_info) return d4 @stack
def set_user_image(self, owner_userid, media_id, filename, image_source, title, description, binary_data=None): """ Makes the link from an image to a user. @param owner_username: User who will own the image. @type owner_username: String @param media_id: Image being linked. @type media_id: String @param filename: Name of file @type filename: String @param image_source: Unknown @type image_source: Unknown @param title: Image Title @type title: String @param description: Image description @type description: String @return: Media ID of new image @rtype: String """ owner_userid = validation.cast_integer(owner_userid, 'owner_userid') media_id = validation.media_id(media_id) bytes = 0 source_id = self.app.api.mediahost.get_media_source_id(image_source) ## First make sure the image isn't already in their account, just inactive. d = self.app.db.query(""" SELECT status, image_id FROM user_images WHERE media_id = %s and owner_userid = %s """, (media_id, owner_userid), single_row=True) def handle_status(result): if not result: if binary_data: d2 = Deferred() d2.addCallback(discover_exif) d2.callback((0, binary_data)) else: d2 = self.app.api.mediahost.get_media_raw_data(media_id) d2.addCallback(discover_exif) # update the image's info (exif etc...) return d2 else: if result['status']: ## nothing to do return result['image_id'] else: info = { 'owner_userid': owner_userid, 'media_id': media_id, 'image_id': result['image_id'], 'title': title, } return set_active((0, "success"), info) @stack def handle_exif(exif_data, data): d4 = manip.get_dimensions(data) d4.addCallback(assign_to_user, exif_data, data) return d4 @stack def discover_exif(result): if result[0] != 0: self.log.warning("Error getting binary data: %s" % result[1]) raise errors.APIError(result[1]) data = result[1] self.log.debug("discovering exif for image %s with %s bytes of data" % (media_id, len(data))) d3 = manip.get_exif(data) d3.addCallback(handle_exif, data) return d3 @stack def assign_to_user(dimensions, exif_data, data): # blow off the exif crap for now, just get what we care about info = utils.filter_exif(exif_data) info['original_width'], info['original_height'] = dimensions if not info.get('datetime_taken') or info['datetime_taken'] == "0000-00-00 00:00:00": info['datetime_taken'] = None if info['rotate_bit'] > 0: info['rotate_bit'] = 'T' else: info['rotate_bit'] = 'F' if info['flash_fired'] % 2 == 0: info['flash_fired'] = 'F' else: info['flash_fired'] = 'T' info['media_id'] = media_id info['owner_userid'] = owner_userid info['title'] = utils.sql_escape(utils.check_n_chop(title, 30)) info['filename'] = utils.sql_escape(filename) info['description'] = utils.sql_escape(description) info['size_b'] = len(data) info['img_source'] = source_id info['license'] = None info['gps_location'] = None info['gps_altitude'] = None info['status'] = 0 @stack def user_assignment_txn(txn, info): try: query = """ INSERT INTO user_images ( media_id, owner_userid, title, filename, description, date_uploaded, date, status, camera_make, camera_model, fstop, exposure_time, focal_length, iso_speed, rotate_bit, flash_fired, original_width, original_height, size_b, img_source, license, gps_location, gps_altitude_m, total_views ) VALUES ( %(media_id)s, %(owner_userid)s, %(title)s, %(filename)s, %(description)s, DEFAULT, %(datetime_taken)s, %(status)s, %(camera_make)s, %(camera_model)s, %(fstop)s, %(exposure_time)s, %(focal_length)s, %(iso_speed)s, %(rotate_bit)s, %(flash_fired)s, %(original_width)s, %(original_height)s, %(size_b)s, %(img_source)s, %(license)s, %(gps_location)s, %(gps_altitude)s, 0 ) """ txn.execute(query, info) txn.execute("SELECT currval('user_images_image_id_seq') AS new_id") info['image_id'] = txn.fetchone()['new_id'] txn.execute(""" UPDATE user_images SET fulltext_index = to_tsvector('english', title || ' ' || description) WHERE image_id = %(image_id)s """, info) txn.execute(""" INSERT INTO user_image_permissions ( image_id ) VALUES ( %(image_id)s ) """, info) return info['image_id'] except psycopg2.IntegrityError, ex: column = str(ex).split('"', 3)[1] if column == 'user_image_pkey': self.log.warning("pkey violation") return media_id else: self.log.warning("error in user_insert txn: %s" % ex) raise ex except Exception, ex: self.log.warning(ex) self.log.warning("super query nova:\n%s" % txn.query) raise ex