Exemplo n.º 1
0
 def wrapped_function(*args, **kwargs):
     if request.method == 'POST':
         if len(request.files.keys()) > 0:
             for key in request.files:
                 result = clarifai_api.tag_images(
                     request.files[key])
                 if result['results'][0]['result']['tag'][
                         'classes'][0] == 'nsfw':
                     result = result['results'][0]['result']['tag'][
                         'probs']
                     if result[0] > 0.7:
                         abort(400)
         elif request.json is not None:
             for key in request.json:
                 if util.is_base64(request.json[key]):
                     base64String = request.json[key]
                     file = open('temp.jpg', 'w+')
                     file.write(base64String.decode('base64'))
                     result = clarifai_api.tag_images(file)
                     file.close()
                 if result['results'][0]['result']['tag'][
                         'classes'][0] == 'nsfw':
                     result = result['results'][0]['result']['tag'][
                         'probs']
                     if result[0] > 0.7:
                         abort(400)
     try:
         os.system('rm temp.jpg')
     except:
         pass
     # resp = current_app.make_default_options_response()
     resp = make_response(f(*args, **kwargs))
     return resp
Exemplo n.º 2
0
 def testBase64Data(self, data):
     files = []
     for d in data:
         f = tempfile.TemporaryFile()
         if util.is_base64(d):
             f.write(d.decode('base64'))
             files.append(f)
     return self.testFilesAgainstApi(files)
Exemplo n.º 3
0
    def get(self):
        """URL parameters:
      start_time: float, seconds since the epoch
      key: string that should appear in the first app log
    """
        start_time = util.get_required_param(self, 'start_time')
        if not util.is_float(start_time):
            self.abort(400,
                       "Couldn't convert start_time to float: %r" % start_time)
        start_time = float(start_time)

        key = util.get_required_param(self, 'key')
        if not util.is_base64(key):
            self.abort(400, 'key is not base64: %r' % key)
        key = urllib.unquote(key)

        # the propagate task logs the poll task's URL, which includes the source
        # entity key as a query param. exclude that with this heuristic.
        key_re = re.compile('[^=]' + key)

        self.response.headers['Content-Type'] = 'text/html; charset=utf-8'

        offset = None
        for log in logservice.fetch(start_time=start_time,
                                    end_time=start_time + 120,
                                    offset=offset,
                                    include_app_logs=True,
                                    version_ids=['2', '3', '4', '5', '6',
                                                 '7']):
            first_lines = '\n'.join([
                line.message.decode('utf-8')
                for line in log.app_logs[:min(10, len(log.app_logs))]
            ])
            if log.app_logs and key_re.search(first_lines):
                # found it! render and return
                self.response.out.write("""\
<html>
<body style="font-family: monospace; white-space: pre">
""")
                self.response.out.write(sanitize(log.combined))
                self.response.out.write('<br /><br />')
                for a in log.app_logs:
                    msg = a.message.decode('utf-8')
                    # don't sanitize poll task URLs since they have a key= query param
                    msg = linkify_datastore_keys(
                        util.linkify(
                            cgi.escape(msg if msg.startswith(
                                'Created by this poll:') else sanitize(msg))))
                    self.response.out.write(
                        '%s %s %s<br />' %
                        (datetime.datetime.utcfromtimestamp(a.time),
                         LEVELS[a.level], msg.replace('\n', '<br />')))
                self.response.out.write('</body>\n</html>')
                return

            offset = log.offset

        self.response.out.write('No log found!')
Exemplo n.º 4
0
 def test_base64_data(cls, data):
     """
         This Method will test base64 data and return nsfw evalation
     """
     files = []
     for file_obj in data:
         image_file = tempfile.NamedTemporaryFile("wb+")
         if util.is_base64(file_obj):
             image_file.write(base64.decodestring(str.encode(file_obj)))
             image_file.seek(0, 0)
             files.append(image_file)
     return cls.test_files_against_api(files)
Exemplo n.º 5
0
  def get(self):
    """URL parameters:
      start_time: float, seconds since the epoch
      key: string that should appear in the first app log
    """
    start_time = util.get_required_param(self, 'start_time')
    if not util.is_float(start_time):
      self.abort(400, "Couldn't convert start_time to float: %r" % start_time)
    start_time = float(start_time)

    key = util.get_required_param(self, 'key')
    if not util.is_base64(key):
      self.abort(400, 'key is not base64: %r' % key)
    key = urllib.unquote(key)

    # the propagate task logs the poll task's URL, which includes the source
    # entity key as a query param. exclude that with this heuristic.
    key_re = re.compile('[^=]' + key)

    self.response.headers['Content-Type'] = 'text/html; charset=utf-8'

    offset = None
    for log in logservice.fetch(start_time=start_time, end_time=start_time + 120,
                                offset=offset, include_app_logs=True,
                                version_ids=['2', '3', '4', '5', '6', '7']):
      first_lines = '\n'.join([line.message.decode('utf-8') for line in
                               log.app_logs[:min(10, len(log.app_logs))]])
      if log.app_logs and key_re.search(first_lines):
        # found it! render and return
        self.response.out.write("""\
<html>
<body style="font-family: monospace; white-space: pre">
""")
        self.response.out.write(sanitize(log.combined))
        self.response.out.write('<br /><br />')
        for a in log.app_logs:
          msg = a.message.decode('utf-8')
          # don't sanitize poll task URLs since they have a key= query param
          msg = linkify_datastore_keys(util.linkify(cgi.escape(
              msg if msg.startswith('Created by this poll:') else sanitize(msg))))
          self.response.out.write('%s %s %s<br />' %
              (datetime.datetime.utcfromtimestamp(a.time), LEVELS[a.level],
               msg.replace('\n', '<br />')))
        self.response.out.write('</body>\n</html>')
        return

      offset = log.offset

    self.response.out.write('No log found!')
Exemplo n.º 6
0
 def test_is_base64(self):
   for arg in '', 'asdf', '1', '1===', '_-aglzfmJyaWQtZ3lyDgsSB1R3aXR0ZXIiAXQM':
     self.assertTrue(util.is_base64(arg), `arg`)
   for arg in 0, 12.2, ')(,.",\'",[---', None, self:
     self.assertFalse(util.is_base64(arg), `arg`)