def processzip(self, name, full_path, file): rel_file = os.path.relpath(file, self.cfg.ROOT_LIB) if self.cfg.ZIPRESCAN or self.opdsdb.zipisscanned(rel_file, 1) == 0: cat_id = self.opdsdb.addcattree(rel_file, 1) try: z = zipf.ZipFile(file, 'r', allowZip64=True) filelist = z.namelist() for n in filelist: try: self.logger.debug('Start process ZIP file = ' + file + ' book file = ' + n) file_size = z.getinfo(n).file_size self.processfile(n, file, z.open(n), 1, file_size, cat_id=cat_id) except: self.logger.error('Error processing ZIP file = ' + file + ' book file = ' + n) z.close() self.arch_scanned += 1 except: self.logger.error('Error while read ZIP archive. File ' + file + ' corrupt.') self.bad_archives += 1 else: self.arch_skipped += 1 self.logger.debug('Skip ZIP archive ' + rel_file + '. Already scanned.')
def response_book_zip(self): """ Выдача файла книги в ZIP формате """ (book_name, book_path, reg_date, format, title, annotation, docdate, cat_type, cover, cover_type, fsize) = self.opdsdb.getbook(self.slice_value) if self.cfg.BOOK_SHELF and self.user != None: self.opdsdb.addbookshelf(self.user, self.slice_value) full_path = os.path.join(self.cfg.ROOT_LIB, book_path) if self.cfg.TITLE_AS_FN: transname = translit(title + '.' + format) else: transname = translit(book_name) # HTTP Header self.add_response_header([ ('Content-Type', 'application/zip; name="' + transname + '"') ]) self.add_response_header([ ('Content-Disposition', 'attachment; filename="' + transname + '.zip"') ]) self.add_response_header([('Content-Transfer-Encoding', 'binary')]) if cat_type == sopdsdb.CAT_NORMAL: file_path = os.path.join(full_path, book_name) dio = io.BytesIO() z = zipf.ZipFile(dio, 'w', zipf.ZIP_DEFLATED) z.write(file_path.encode('utf-8'), transname) z.close() buf = dio.getvalue() self.add_response_header([('Content-Length', str(len(buf)))]) self.add_response_binary(buf) elif cat_type == sopdsdb.CAT_ZIP: fz = codecs.open(full_path.encode("utf-8"), "rb") zi = zipf.ZipFile(fz, 'r', allowZip64=True) fo = zi.open(book_name) s = fo.read() fo.close() zi.close() fz.close() dio = io.BytesIO() zo = zipf.ZipFile(dio, 'w', zipf.ZIP_DEFLATED) zo.writestr(transname, s) zo.close() buf = dio.getvalue() self.add_response_header([('Content-Length', str(len(buf)))]) self.add_response_binary(buf)
def response_book_cover(self): """ Выдача Обложки На лету """ (book_name, book_path, reg_date, format, title, annotation, docdate, cat_type, cover, cover_type, fsize) = self.opdsdb.getbook(self.slice_value) c0 = 0 if format == 'fb2': full_path = os.path.join(self.cfg.ROOT_LIB, book_path) fb2 = sopdsparse.fb2parser(1) if cat_type == sopdsdb.CAT_NORMAL: file_path = os.path.join(full_path, book_name) fo = codecs.open(file_path.encode("utf-8"), "rb") fb2.parse(fo, 0) fo.close() elif cat_type == sopdsdb.CAT_ZIP: fz = codecs.open(full_path.encode("utf-8"), "rb") z = zipf.ZipFile(fz, 'r', allowZip64=True) fo = z.open(book_name) fb2.parse(fo, 0) fo.close() z.close() fz.close() if len(fb2.cover_image.cover_data) > 0: try: s = fb2.cover_image.cover_data dstr = base64.b64decode(s) ictype = fb2.cover_image.getattr('content-type') self.add_response_header([('Content-Type', ictype)]) self.add_response_binary(dstr) c0 = 1 except: c0 = 0 if c0 == 0: if os.path.exists(sopdscfg.NOCOVER_PATH): self.add_response_header([('Content-Type', 'image/jpeg')]) f = open(sopdscfg.NOCOVER_PATH, "rb") self.add_response_binary(f.read()) f.close() else: self.set_response_status('404 Not Found')
def response_book_file(self): """ Выдача файла книги """ (book_name, book_path, reg_date, format, title, annotation, docdate, cat_type, cover, cover_type, fsize) = self.opdsdb.getbook(self.slice_value) if self.cfg.BOOK_SHELF and self.user != None: self.opdsdb.addbookshelf(self.user, self.slice_value) full_path = os.path.join(self.cfg.ROOT_LIB, book_path) if self.cfg.TITLE_AS_FN: transname = translit(title + '.' + format) else: transname = translit(book_name) if format == "fb2": content_type = 'text/xml' elif format == "epub": content_type = 'application/epub+zip' elif format == "mobi": content_type = 'application/x-mobipocket-ebook' else: content_type = 'application/octet-stream' # HTTP Header self.add_response_header([ ('Content-Type', content_type + '; name="' + transname + '"') ]) self.add_response_header([ ('Content-Disposition', 'attachment; filename="' + transname + '"') ]) self.add_response_header([('Content-Transfer-Encoding', 'binary')]) if cat_type == sopdsdb.CAT_NORMAL: file_path = os.path.join(full_path, book_name) book_size = os.path.getsize(file_path.encode('utf-8')) self.add_response_header([('Content-Length', str(book_size))]) fo = codecs.open(file_path.encode("utf-8"), "rb") s = fo.read() self.add_response_binary(s) fo.close() elif cat_type == sopdsdb.CAT_ZIP: fz = codecs.open(full_path.encode("utf-8"), "rb") z = zipf.ZipFile(fz, 'r', allowZip64=True) book_size = z.getinfo(book_name).file_size self.add_response_header([('Content-Length', str(book_size))]) fo = z.open(book_name) s = fo.read() self.add_response_binary(s) fo.close() z.close() fz.close()
def response_book_convert(self): """ Выдача файла книги после конвертации в EPUB или mobi """ (book_name, book_path, reg_date, format, title, annotation, docdate, cat_type, cover, cover_type, fsize) = self.opdsdb.getbook(self.slice_value) if self.cfg.BOOK_SHELF and self.user != None: self.opdsdb.addbookshelf(self.user, self.slice_value) full_path = os.path.join(self.cfg.ROOT_LIB, book_path) (n, e) = os.path.splitext(book_name) if self.type_value == 93: convert_type = '.epub' converter_path = self.cfg.FB2TOEPUB_PATH content_type = 'application/epub+zip' elif self.type_value == 94: convert_type = '.mobi' converter_path = self.cfg.FB2TOMOBI_PATH content_type = 'application/x-mobipocket-ebook' else: content_type = 'application/octet-stream' if self.cfg.TITLE_AS_FN: transname = translit(title) + convert_type else: transname = translit(n) + convert_type if cat_type == sopdsdb.CAT_NORMAL: tmp_fb2_path = None file_path = os.path.join(full_path, book_name) elif cat_type == sopdsdb.CAT_ZIP: fz = codecs.open(full_path.encode("utf-8"), "rb") z = zipf.ZipFile(fz, 'r', allowZip64=True) z.extract(book_name, self.cfg.TEMP_DIR) tmp_fb2_path = os.path.join(self.cfg.TEMP_DIR, book_name) file_path = tmp_fb2_path tmp_conv_path = os.path.join(self.cfg.TEMP_DIR, transname) proc = subprocess.Popen(( "%s %s %s" % (converter_path, ("\"%s\"" % file_path), "\"%s\"" % 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 self.add_response_header([ ('Content-Type', content_type + '; name="' + transname + '"') ]) self.add_response_header([ ('Content-Disposition', 'attachment; filename="' + transname + '"') ]) self.add_response_header([('Content-Transfer-Encoding', 'binary')]) self.add_response_header([('Content-Length', str(len(s)))]) self.add_response_binary(s) fo.close() else: self.set_response_status('404 Not Found') try: os.remove(tmp_fb2_path.encode('utf-8')) except: pass try: os.remove(tmp_conv_path) except: pass