def update(self): self.error = False self.response.setHeader('Content-Type', 'application/json') upload_file = self.request.form['file'] size = self.request.getHeader('X-File-Size') thumbnail_width = int(self.request.getHeader('X-thumbnail-width')) thumbnail_height = int(self.request.getHeader('X-thumbnail-height')) self.filename = self.request.getHeader('X-File-Name') self.identifier = generate_random_identifier() media = Media() media.id = self.identifier media.size = size media.filename = self.filename # Check maximum file size first: db_util = getUtility(IDbUtility) if int(media.size) > db_util.max_allowed_packet: # Problem: Database does not allow BLOBs of this size. self.error = True self.message = 'The selected file is too big. Please contact your system administrator.' self.title = 'Upload failed.' upload_file.close() return # Read data: chunk = upload_file.read() while chunk != '': # Initialise our data array if media.data == None: media.data = bytearray() # Add data media.data += chunk # Check maximum length if len(media.data) > int(media.size): # Cancel upload here so uploader cannot throw an endless stream of bytes at us self.error = True self.message = 'The selected file is bigger than specified.' self.title = 'Upload failed.' upload_file.close() return chunk = upload_file.read() mime_type = getUtility(IMimeTypeGetter)(name=self.filename, data=media.data) if not mime_type: mime_type = upload_file.headers('Content-Type') media.mime_type = mime_type thumbnail_file = None self.has_thumbnail = False if mime_type in ('image/jpeg', 'image/png', 'image/gif'): thumbnail_file = self.create_thumbnail(upload_file, (thumbnail_width, thumbnail_height)) self.has_thumbnail = True chunk = thumbnail_file.read() while chunk != '': if media.thumbnail == None: media.thumbnail = bytearray() media.thumbnail += chunk chunk = thumbnail_file.read() thumbnail_file.close() upload_file.close() session = db_util.Session() session.add(media) session.commit()
def post_order_traverse(self, mode): if mode == 'save': source_type = self.op_setobject_type self.relation.source_table = source_type.get_table_name() self.relation.target_table = Media.get_table_name() self.relation.create_relation('LIST')