Ejemplo n.º 1
0
 def test_rar2list_returns_listing_with_one_file(self):
     from convert import call
     result = call('rar2list', str(rar_data / 'onefile.rar'))
     self.assertIn('fisier.txt', result)
     self.assertIn('04-09-12', result)
Ejemplo n.º 2
0
 def test_rar2list_with_unicode_characters_returns_contents(self):
     from convert import call
     result = call('rar2list', str(rar_data / 'diacritics.rar'))
     self.assertIn(u"director cu spații și diacritice".encode('utf-8'),
                   result)
Ejemplo n.º 3
0
 def test_list_7zip_with_unicode_chars_returns_contents(self):
     from convert import call
     result = call('list_7zip', str(sz_data / 'twofiles.7z'))
     self.assertIn('one.txt', result)
     self.assertIn('2 files, 0 folders', result)
Ejemplo n.º 4
0
def convert(name):
    start = time.time()
    document = getattr(flask.request.files.get("file", ""), "stream", None)
    if not document:
        import StringIO

        document = StringIO.StringIO(flask.request.data)
    with tempfile.NamedTemporaryFile(delete=False) as tmp:
        chunk = True
        while chunk:
            chunk = document.read(10)
            tmp.file.write(chunk)
        tmp.file.flush()
        tmp.file.seek(0)
        file_size = os.path.getsize(tmp.name)
        extra_params = flask.request.form.values()
        if not extra_params:
            extra_params = flask.request.args.values()
        needs_additional_files = getattr(converters.get(name), "additional_files", False)
        try:
            if needs_additional_files:
                shx_doc = getattr(flask.request.files.get("shx"), "stream", None)
                dbf_doc = getattr(flask.request.files.get("dbf"), "stream", None)
                tmp_shx = tempfile.NamedTemporaryFile()
                tmp_dbf = tempfile.NamedTemporaryFile()
                tmp_shx.file.write(shx_doc.read())
                tmp_dbf.file.write(dbf_doc.read())
                tmp_shx.file.flush()
                tmp_dbf.file.flush()
                tmp_shx.file.seek(0)
                tmp_dbf.file.seek(0)
                extra_params += [tmp_shx.name, tmp_dbf.name]
                response = call(name, tmp.name, list(extra_params))
                tmp_shx.close()
                tmp_dbf.close()
            else:
                response = call(name, tmp.name, list(extra_params))
        except ConversionError as exp:
            response = base64.b64encode(exp.output)
            status = 500
            content_type = converters.get(name).ct_output
        except NotImplementedError as exp:
            response = ""
            status = 404
            content_type = "text/plain"
        else:
            status = 200
            content_type = converters.get(name).ct_output
        finally:
            os.remove(tmp.name)
    duration = time.time() - start
    log_details = {"name": name, "size": file_size, "mime": content_type, "duration": duration}
    logger.info(
        (
            "\nPost conversion details:\n"
            "\tConverter: {name}\n"
            "\tFile size: {size} bytes\n"
            "\tMime-type: {mime}\n"
            "\tDuration:  {duration:.4f} seconds"
        ).format(**log_details)
    )
    return flask.Response(response, status=status, content_type=content_type)
Ejemplo n.º 5
0
 def test_list_7zip_returns_empty_list_for_empty_archive(self):
     from convert import call
     result = call('list_7zip', str(sz_data / 'empty.7z'))
     self.assertIn('0 files, 0 folders', result)