Exemple #1
0
    def item_enclosures(self, item):
        mime = mime_detector.fmt(item['format'])
        enclosure = [
            opdsEnclosure(reverse("opds_catalog:download", kwargs={"book_id": item['id'], "zip_flag": 0}), mime, "http://opds-spec.org/acquisition/open-access"), ]
        if not item['format'] in settings.NOZIP_FORMATS:
            mimezip = Mimetype.FB2_ZIP if mime==Mimetype.FB2 else "%s+zip"%mime
            enclosure += [opdsEnclosure(reverse("opds_catalog:download", kwargs={"book_id": item['id'], "zip_flag": 1}), mimezip, "http://opds-spec.org/acquisition/open-access")]
        enclosure += [opdsEnclosure(reverse("opds_catalog:cover", kwargs={"book_id": item['id']}), "image/jpeg", "http://opds-spec.org/image"),
                      opdsEnclosure(reverse("opds_catalog:thumb", kwargs={"book_id": item['id']}), "image/jpeg", "http://opds-spec.org/thumbnail"),
                      ]
        if (config.SOPDS_FB2TOEPUB != "") and (item['format'] == 'fb2'):
            enclosure += [
                opdsEnclosure(reverse("opds_catalog:convert", kwargs={"book_id": item['id'], "convert_type": "epub"}), Mimetype.EPUB, "http://opds-spec.org/acquisition/open-access")]
        if (config.SOPDS_FB2TOMOBI != "") and (item['format'] == 'fb2'):
            enclosure += [
                opdsEnclosure(reverse("opds_catalog:convert", kwargs={"book_id": item['id'], "convert_type": "mobi"}), Mimetype.MOBI, "http://opds-spec.org/acquisition/open-access")]

        return enclosure
Exemple #2
0
    def item_enclosures(self, item):
        mime = mime_detector.fmt(item['format'])
        enclosure = [
            opdsEnclosure(reverse("opds_catalog:download", kwargs={"book_id": item['id'], "zip_flag": 0}), mime, "http://opds-spec.org/acquisition/open-access"), ]
        if not item['format'] in settings.NOZIP_FORMATS:
            mimezip = Mimetype.FB2_ZIP if mime==Mimetype.FB2 else "%s+zip"%mime
            enclosure += [opdsEnclosure(reverse("opds_catalog:download", kwargs={"book_id": item['id'], "zip_flag": 1}), mimezip, "http://opds-spec.org/acquisition/open-access")]
        enclosure += [opdsEnclosure(reverse("opds_catalog:cover", kwargs={"book_id": item['id']}), "image/jpeg", "http://opds-spec.org/image"),
                      opdsEnclosure(reverse("opds_catalog:thumb", kwargs={"book_id": item['id']}), "image/jpeg", "http://opds-spec.org/thumbnail"),
                      ]
        if (config.SOPDS_FB2TOEPUB != "") and (item['format'] == 'fb2'):
            enclosure += [
                opdsEnclosure(reverse("opds_catalog:convert", kwargs={"book_id": item['id'], "convert_type": "epub"}), Mimetype.EPUB, "http://opds-spec.org/acquisition/open-access")]
        if (config.SOPDS_FB2TOMOBI != "") and (item['format'] == 'fb2'):
            enclosure += [
                opdsEnclosure(reverse("opds_catalog:convert", kwargs={"book_id": item['id'], "convert_type": "mobi"}), Mimetype.MOBI, "http://opds-spec.org/acquisition/open-access")]

        return enclosure
Exemple #3
0
def ConvertFB2(request, book_id, convert_type):
    """ Выдача файла книги после конвертации в EPUB или mobi """
    book = Book.objects.get(id=book_id)

    if book.format != 'fb2':
        raise Http404

    if config.SOPDS_AUTH and request.user.is_authenticated:
        bookshelf.objects.get_or_create(user=request.user, book=book)

    full_path = os.path.join(config.SOPDS_ROOT_LIB, book.path)
    if book.cat_type == opdsdb.CAT_INP:
        # Убираем из пути INPX и INP файл
        inp_path, zip_name = os.path.split(full_path)
        inpx_path, inp_name = os.path.split(inp_path)
        path, inpx_name = os.path.split(inpx_path)
        full_path = os.path.join(path, zip_name)

    if config.SOPDS_TITLE_AS_FILENAME:
        transname = utils.translit(book.title + '.' + book.format)
    else:
        transname = utils.translit(book.filename)

    transname = utils.to_ascii(transname)

    (n, e) = os.path.splitext(transname)
    dlfilename = "%s.%s" % (n, convert_type)

    if convert_type == 'epub':
        converter_path = config.SOPDS_FB2TOEPUB
    elif convert_type == 'mobi':
        converter_path = config.SOPDS_FB2TOMOBI
    content_type = mime_detector.fmt(convert_type)

    if book.cat_type == opdsdb.CAT_NORMAL:
        tmp_fb2_path = None
        file_path = os.path.join(full_path, book.filename)
    elif book.cat_type in [opdsdb.CAT_ZIP, opdsdb.CAT_INP]:
        try:
            fz = codecs.open(full_path, "rb")
        except FileNotFoundError:
            raise Http404
        z = zipfile.ZipFile(fz, 'r', allowZip64=True)
        z.extract(book.filename, config.SOPDS_TEMP_DIR)
        tmp_fb2_path = os.path.join(config.SOPDS_TEMP_DIR, book.filename)
        file_path = tmp_fb2_path

    tmp_conv_path = os.path.join(config.SOPDS_TEMP_DIR, dlfilename)
    popen_args = ("\"%s\" \"%s\" \"%s\"" %
                  (converter_path, file_path, tmp_conv_path))
    proc = subprocess.Popen(popen_args, shell=True, stdout=subprocess.PIPE)
    #proc = subprocess.Popen((converter_path.encode('utf8'),file_path.encode('utf8'),tmp_conv_path.encode('utf8')), shell=True, stdout=subprocess.PIPE)
    out = proc.stdout.readlines()

    if os.path.isfile(tmp_conv_path):
        fo = codecs.open(tmp_conv_path, "rb")
        s = fo.read()
        # HTTP Header
        response = HttpResponse()
        response["Content-Type"] = '%s; name="%s"' % (content_type, dlfilename)
        response["Content-Disposition"] = 'attachment; filename="%s"' % (
            dlfilename)
        response["Content-Transfer-Encoding"] = 'binary'
        response["Content-Length"] = str(len(s))
        response.write(s)
        fo.close()
    else:
        raise Http404

    try:
        if tmp_fb2_path:
            os.remove(tmp_fb2_path)
    except:
        pass
    try:
        os.remove(tmp_conv_path)
    except:
        pass

    return response
Exemple #4
0
def Download(request, book_id, zip_flag):
    """ Загрузка файла книги """
    book = Book.objects.get(id=book_id)

    if config.SOPDS_AUTH and request.user.is_authenticated:
        bookshelf.objects.get_or_create(user=request.user, book=book)

    full_path = os.path.join(config.SOPDS_ROOT_LIB, book.path)

    if book.cat_type == opdsdb.CAT_INP:
        # Убираем из пути INPX и INP файл
        inp_path, zip_name = os.path.split(full_path)
        inpx_path, inp_name = os.path.split(inp_path)
        path, inpx_name = os.path.split(inpx_path)
        full_path = os.path.join(path, zip_name)

    if config.SOPDS_TITLE_AS_FILENAME:
        transname = utils.translit(book.title + '.' + book.format)
    else:
        transname = utils.translit(book.filename)

    transname = utils.to_ascii(transname)

    if zip_flag == '1':
        dlfilename = transname + '.zip'
        content_type = Mimetype.FB2_ZIP if book.format == 'fb2' else Mimetype.ZIP
    else:
        dlfilename = transname
        content_type = mime_detector.fmt(book.format)

    response = HttpResponse()
    response["Content-Type"] = '%s; name="%s"' % (content_type, dlfilename)
    response["Content-Disposition"] = 'attachment; filename="%s"' % (
        dlfilename)
    response["Content-Transfer-Encoding"] = 'binary'

    z = None
    fz = None
    s = None
    book_size = book.filesize
    if book.cat_type == opdsdb.CAT_NORMAL:
        file_path = os.path.join(full_path, book.filename)
        book_size = os.path.getsize(file_path)
        try:
            fo = codecs.open(file_path, "rb")
        except FileNotFoundError:
            raise Http404
        s = fo.read()
    elif book.cat_type in [opdsdb.CAT_ZIP, opdsdb.CAT_INP]:
        try:
            fz = codecs.open(full_path, "rb")
        except FileNotFoundError:
            raise Http404
        z = zipfile.ZipFile(fz, 'r', allowZip64=True)
        book_size = z.getinfo(book.filename).file_size
        fo = z.open(book.filename)
        s = fo.read()

    if zip_flag == '1':
        dio = io.BytesIO()
        zo = zipfile.ZipFile(dio, 'w', zipfile.ZIP_DEFLATED)
        zo.writestr(transname, s)
        zo.close()
        buf = dio.getvalue()
        response["Content-Length"] = str(len(buf))
        response.write(buf)
    else:
        response["Content-Length"] = str(book_size)
        response.write(s)

    fo.close()
    if z: z.close()
    if fz: fz.close()

    return response
Exemple #5
0
def ConvertFB2(request, book_id, convert_type):
    """ Выдача файла книги после конвертации в EPUB или mobi """
    book = Book.objects.get(id=book_id)
    
    if book.format!='fb2':
        raise Http404

    if config.SOPDS_AUTH and request.user.is_authenticated:
        bookshelf.objects.get_or_create(user=request.user, book=book)

    full_path=os.path.join(config.SOPDS_ROOT_LIB,book.path)
    if book.cat_type==opdsdb.CAT_INP:
        # Убираем из пути INPX и INP файл
        inp_path, zip_name = os.path.split(full_path)
        inpx_path, inp_name = os.path.split(inp_path)
        path, inpx_name = os.path.split(inpx_path)
        full_path = os.path.join(path,zip_name)
            
    if config.SOPDS_TITLE_AS_FILENAME:
        transname=utils.translit(book.title+'.'+book.format)
    else:
        transname=utils.translit(book.filename)      
        
    transname = utils.to_ascii(transname)
      
    (n,e)=os.path.splitext(transname)
    dlfilename="%s.%s"%(n,convert_type)
    
    if convert_type=='epub':
        converter_path=config.SOPDS_FB2TOEPUB
    elif convert_type=='mobi':
        converter_path=config.SOPDS_FB2TOMOBI
    content_type=mime_detector.fmt(convert_type)

    if book.cat_type==opdsdb.CAT_NORMAL:
        tmp_fb2_path=None
        file_path=os.path.join(full_path, book.filename)
    elif book.cat_type in [opdsdb.CAT_ZIP, opdsdb.CAT_INP]:
        try:
            fz=codecs.open(full_path, "rb")
        except FileNotFoundError:
            raise Http404        
        z = zipfile.ZipFile(fz, 'r', allowZip64=True)
        z.extract(book.filename,config.SOPDS_TEMP_DIR)
        tmp_fb2_path=os.path.join(config.SOPDS_TEMP_DIR,book.filename)
        file_path=tmp_fb2_path        
        
    tmp_conv_path=os.path.join(config.SOPDS_TEMP_DIR,dlfilename)
    popen_args = ("\"%s\" \"%s\" \"%s\""%(converter_path,file_path,tmp_conv_path))
    proc = subprocess.Popen(popen_args, shell=True, stdout=subprocess.PIPE)
    #proc = subprocess.Popen((converter_path.encode('utf8'),file_path.encode('utf8'),tmp_conv_path.encode('utf8')), shell=True, stdout=subprocess.PIPE)
    out = proc.stdout.readlines()

    if os.path.isfile(tmp_conv_path):
        fo=codecs.open(tmp_conv_path, "rb")
        s=fo.read()
        # HTTP Header
        response = HttpResponse()
        response["Content-Type"]='%s; name="%s"'%(content_type,dlfilename)
        response["Content-Disposition"] = 'attachment; filename="%s"'%(dlfilename)
        response["Content-Transfer-Encoding"]='binary'    
        response["Content-Length"] = str(len(s))
        response.write(s)         
        fo.close()
    else:
        raise Http404

    try: 
        if tmp_fb2_path:
            os.remove(tmp_fb2_path)
    except: 
        pass
    try: 
        os.remove(tmp_conv_path)
    except: 
        pass

    return response
Exemple #6
0
def Download(request, book_id, zip_flag):
    """ Загрузка файла книги """
    book = Book.objects.get(id=book_id)

    if config.SOPDS_AUTH and request.user.is_authenticated:
        bookshelf.objects.get_or_create(user=request.user, book=book)

    full_path=os.path.join(config.SOPDS_ROOT_LIB,book.path)
    
    if book.cat_type==opdsdb.CAT_INP:
        # Убираем из пути INPX и INP файл
        inp_path, zip_name = os.path.split(full_path)
        inpx_path, inp_name = os.path.split(inp_path)
        path, inpx_name = os.path.split(inpx_path)
        full_path = os.path.join(path,zip_name)
        
    if config.SOPDS_TITLE_AS_FILENAME:
        transname=utils.translit(book.title+'.'+book.format)
    else:
        transname=utils.translit(book.filename)
        
    transname = utils.to_ascii(transname)
        
    if zip_flag == '1':
        dlfilename=transname+'.zip'   
        content_type= Mimetype.FB2_ZIP if book.format=='fb2' else Mimetype.ZIP
    else:    
        dlfilename=transname
        content_type = mime_detector.fmt(book.format)

    response = HttpResponse()
    response["Content-Type"]='%s; name="%s"'%(content_type,dlfilename)
    response["Content-Disposition"] = 'attachment; filename="%s"'%(dlfilename)
    response["Content-Transfer-Encoding"]='binary'

    z = None
    fz = None
    s = None
    book_size = book.filesize
    if book.cat_type==opdsdb.CAT_NORMAL:
        file_path=os.path.join(full_path, book.filename)
        book_size=os.path.getsize(file_path)
        try:
            fo=codecs.open(file_path, "rb")
        except FileNotFoundError:
            raise Http404
        s=fo.read()
    elif book.cat_type in [opdsdb.CAT_ZIP, opdsdb.CAT_INP]:
        try:
            fz=codecs.open(full_path, "rb")
        except FileNotFoundError:
            raise Http404
        z = zipfile.ZipFile(fz, 'r', allowZip64=True)
        book_size=z.getinfo(book.filename).file_size
        fo= z.open(book.filename)
        s=fo.read()

    if zip_flag=='1':
        dio = io.BytesIO()
        zo = zipfile.ZipFile(dio, 'w', zipfile.ZIP_DEFLATED)
        zo.writestr(transname,s)
        zo.close()
        buf = dio.getvalue()
        response["Content-Length"] = str(len(buf))
        response.write(buf)        
    else:
        response["Content-Length"] = str(book_size)
        response.write(s)

    fo.close()
    if z: z.close()
    if fz: fz.close()

    return response