Exemple #1
0
  def post(self):
    # Check the user has been blocked or not
    user = model.get_user_by_ip(self.request.remote_ip)
    if user is None:
      uid = model.add_user(self.request.remote_ip)
    else:
      if user['blocked']:
        raise tornado.web.HTTPError(403, 'You are on our blacklist.')
      else:
        uid = user['id']

    # Check whether password is required
    expected_password = self.settings['password']
    if expected_password and \
      not compare_digest(self.get_argument('password'), expected_password):
        raise tornado.web.HTTPError(403, 'You need a valid password to post.')

    files = self.request.files
    if not files:
      raise tornado.web.HTTPError(400, 'upload your image please')

    ret = OrderedDict()
    for filelist in files.values():
      for file in filelist:
        m = hashlib.sha1()
        m.update(file['body'])
        h = m.hexdigest()
        model.add_image(uid, h, file['filename'], len(file['body']))
        d = h[:2]
        f = h[2:]
        p = os.path.join(self.settings['datadir'], d)
        if not os.path.exists(p):
          os.mkdir(p, 0o750)
        fpath = os.path.join(p, f)
        if not os.path.exists(fpath):
          try:
            with open(fpath, 'wb') as img_file:
              img_file.write(file['body'])
          except IOError:
            logging.exception('failed to open the file: %s', fpath)
            ret[file['filename']] = 'FAIL'
            self.set_status(500)
            continue

        ftype = mimetypes.guess_type(fpath)[0]
        ext = None
        if ftype:
          ext = guess_extension(ftype)
        if ext:
          f += ext
        ret[file['filename']] = '%s/%s/%s' % (
          self.request.full_url().rstrip('/'), d, f)

    if len(ret) > 1:
      for item in ret.items():
        self.write('%s: %s\n' % item)
    elif ret:
      img_url = tuple(ret.values())[0]
      self.write("%s\n" % img_url)
    logging.info('%s posted: %s', self.request.remote_ip, ret)
Exemple #2
0
  def post(self):
    # Check the user has been blocked or not
    user = model.get_user_by_ip(self.request.remote_ip)
    if user is None:
      uid = model.add_user(self.request.remote_ip)
    else:
      if user['blocked']:
        raise tornado.web.HTTPError(403, 'You are on our blacklist.')
      else:
        uid = user['id']

    files = self.request.files
    if not files:
      raise tornado.web.HTTPError(400, 'upload your image please')

    ret = OrderedDict()
    for filelist in files.values():
      for file in filelist:
        m = hashlib.sha1()
        m.update(file['body'])
        h = m.hexdigest()
        model.add_image(uid, h)
        d = h[:2]
        f = h[2:]
        p = os.path.join(self.settings['datadir'], d)
        if not os.path.exists(p):
          os.mkdir(p, 0o750)
        fpath = os.path.join(p, f)
        if not os.path.exists(fpath):
          open(fpath, 'wb').write(file['body'])

        ftype = mimetypes.guess_type(fpath)[0]
        ext = None
        if ftype:
          ext = mimetypes.guess_extension(ftype)
        if ext:
          f += ext
          ret[file['filename']] = '%s/%s/%s\n' % (
            self.request.full_url().rstrip('/'), d, f
          )
    if len(ret) > 1:
      for i in ret.items():
        self.write('%s: %s'% i)
    elif ret:
      self.write(tuple(ret.values())[0])
Exemple #3
0
    def post(self):
        # Check the user has been blocked or not
        user = model.get_user_by_ip(self.request.remote_ip)
        if user is None:
            uid = model.add_user(self.request.remote_ip)
        else:
            if user["blocked"]:
                raise tornado.web.HTTPError(403, "You are on our blacklist.")
            else:
                uid = user["id"]

        files = self.request.files
        if not files:
            raise tornado.web.HTTPError(400, "upload your image please")

        ret = OrderedDict()
        for filelist in files.values():
            for file in filelist:
                # FIXME: avoid forgery
                m = hashlib.sha1()
                m.update(file["body"])
                h = m.hexdigest()
                model.add_image(uid, h)
                d = h[:2]
                f = h[2:]
                p = os.path.join(self.settings["datadir"], d)
                if not os.path.exists(p):
                    os.mkdir(p, 0o750)
                fpath = os.path.join(p, f)
                if not os.path.exists(fpath):
                    open(fpath, "wb").write(file["body"])

                ftype = mimetypes.guess_type(fpath)[0]
                ext = None
                if ftype:
                    ext = mimetypes.guess_extension(ftype)
                if ext:
                    f = f + ext
                    ret[file["filename"]] = "%s/%s/%s\n" % (self.request.full_url().rstrip("/"), d, f)
        if len(ret) > 1:
            for i in ret.items():
                self.write("%s: %s" % i)
        elif ret:
            self.write(tuple(ret.values())[0])