Esempio n. 1
0
  def handle_entry(self, message):

    entry = Entry(author='Julian')
    raw, entry.content = self.get_content(message)

    if entry.content is None:
      logging.error("Failed to find message body")
      logging.error(message)
      return

    matches = re.search("diaryentry(\d+)", raw)
    if matches is None:
      logging.error("received mail that wasn't a diary entry")
      logging.error(raw)
      return

    entry.date = datetime.date.fromtimestamp(int(matches.group(1)))
    entry.put()

    num_attachments = 0

    # fall back to raw mail message for attachment parsing
    for part in message.original.walk():
      content_type = part.get_content_type()
      if content_type not in ["text/plain", "text/html", "multipart/mixed",
                              "multipart/alternative"]:
        attachment = Attachment(name=part.get_param("name"),
                                content_type=content_type)

        # store attachment in blobstore
        bucket = '/infinite-diary.appspot.com'
        filename = os.path.join(bucket, 'attachments',
                                time.strftime('%Y-%m-%d_%H-%M'),
                                str(num_attachments))

        with gcs.open(filename, 'w') as f:
          f.write(base64.b64decode(part.get_payload()))

        attachment.content = blobstore.create_gs_key('/gs' + filename)
        attachment.entry = entry.key()
        attachment.thumbnail = images.get_serving_url(attachment.content,
                                                      size=400)

        attachment.put()
        num_attachments += 1
Esempio n. 2
0
    def get_attachment(self, page, filename):
        """Return an attachment from the cloud store, storing it locally"""

        if not self.token:
            log.debug("No token")
            return None

        # get the folder contents
        metadata = self.get_metadata(page)
        if not metadata:
            return None

        target = None
        for i in metadata['contents']:
            if not i['is_dir']:
                if os.path.basename(i['path']) == filename:
                    target = i['path']
                    break

        if not target:
            return None

        get_url = _urls.files % (target, urllib.urlencode({"access_token": self.token}))
        log.debug(get_url)
        r = fetch(get_url)

        if r['status'] == 200:
            metadata = json.loads(r['x-dropbox-metadata'])

            # mtime is taken from cloud store metadata
            mtime = parse_dropbox_date(metadata['modified'])

            id   = os.path.join(page.lower(),filename.lower())
            params = {
                "id"       : id,
                "path"     : page,
                "filename" : filename,
                "mtime"    : mtime,
                "data"     : r['data'],
                "mime_type": metadata['mime_type'],
            }
            a = Attachment(**params)
            a.put()
            return a
        return None
Esempio n. 3
0
  def post(self):         
    """create attachment or update attachment for given attachment_id"""     
    
    logging.info("POST: _method:%s attachment_id: %s" % (self.request.get("_method"), self.request.get("attachment_id")))
     
    _method = self.request.headers.get(METHOD_OVERRIDE_HEADER, None)
    if _method == 'DELETE':
      self.handle_delete()        
    else:    
      self.do_delete()
      attachment = Attachment(attachment_id = self.request.get("attachment_id"),
                              content_type = self.request.get("content_type"),
                              data = self.request.get("data"))   
      attachment.put()

      # return xml
      path = os.path.join(os.path.dirname(__file__), 'templates','attachment.xml')
      self.response.out.write(template.render(path, {'attachment': attachment}))