コード例 #1
0
ファイル: pdf_to_djvu_cgi.py プロジェクト: Pikse/phetools
def handle_query(params, start_response):
    print >> sys.stderr, params

    answer = '200 OK'

    if not params['ia_id']:
        answer = '400 BAD REQUEST'
        text = json.dumps({
            'error' : 1,
            'text' : 'No ia identifier provided in request',
        })
    else:
        # Check it now to provide directly feedback to user
        ia_files = pdf_to_djvu.get_ia_files(params['ia_id'])
        if not ia_files.get('pdf', None) or not ia_files.get('xml', None):
            answer = '400 BAD REQUEST'
            text = json.dumps(
                { 'error' : 2,
                  'text' : "invalid ia identifier, I can't locate needed files",
              })

    if answer == '200 OK':
        queue_pdf_to_djvu(params['ia_id'])
        text = json.dumps({
            'error' : 0,
            'text' : 'item conversion will start soon',
        })

    start_response(answer, [('Content-Type', 'text/plain; charset=UTF-8'),
                            ('Content-Length', str(len(text))),
                            ('Access-Control-Allow-Origin', '*')])
    return [ text ]
コード例 #2
0
ファイル: pdf_to_djvu_cgi.py プロジェクト: Pikse/phetools
def handle_get(environ, params, start_response):
    if not params['ia_id']:
        text = json.dumps({
            'error' : 1,
            'text' : 'No ia identifier provided in request',
        })
        start_response("400 BAD REQUEST",
                       [('Content-Type', 'text/plain; charset=UTF-8'),
                        ('Content-Length', str(len(text))),
                        ('Access-Control-Allow-Origin', '*')])
        return [ text ]

    ia_files = pdf_to_djvu.get_ia_files(params['ia_id'])
    if not ia_files.get('pdf', None) or not ia_files.get('xml', None):
        text = json.dumps(
            { 'error' : 2,
              'text' : "invalid ia identifier, can't locate the needed files",
          })
        start_response("400 BAD REQUEST",
                       [('Content-Type', 'text/plain; charset=UTF-8'),
                        ('Content-Length', str(len(text))),
                        ('Access-Control-Allow-Origin', '*')])
        return [ text ]

    djvu_name = os.path.expanduser('~/cache/ia_pdf/')
    djvu_name += ia_files['pdf']['name'][:-3] + 'djvu'
    if not os.path.exists(djvu_name):
        text = json.dumps(
            { 'error' : 3,
              'text' : "Can't locate djvu file, ia id is valid, perhaps conversion failed or is in progress",
          })
        start_response("400 BAD REQUEST",
                       [('Content-Type', 'text/plain; charset=UTF-8'),
                        ('Content-Length', str(len(text))),
                        ('Access-Control-Allow-Origin', '*')])
        return [ text ]

    if 'wsgi.file_wrapper' in environ:
        return environ['wsgi.file_wrapper'](djvu_name, 1024)
    else:
        size = os.path.getsize(djvu_name)
        fd = open(djvu_name)
        save_name = ia_files['pdf']['name'][:-3].encode('utf-8') + 'djvu'
        start_response("200 OK",
                       [('Content-Type', 'application/octet-stream'),
                        ('Content-Length', str(size)),
                        ('Content-Disposition', 'filename=%s' % save_name),
                        ('Access-Control-Allow-Origin', '*')])

        def file_wrapper(fileobj, block_size=1024):
            try:
                data = fileobj.read(block_size)
                while data:
                    yield data
                    data = fileobj.read(block_size)
            finally:
                fileobj.close()

        return file_wrapper(fd, 1024)