mirror of
git://github.com/kovidgoyal/calibre.git
synced 2026-05-05 23:23:46 +02:00
Fix #856158 (Truncating files when adding from withing library directory)
This commit is contained in:
parent
30f29c6117
commit
8cb2e17254
3 changed files with 25 additions and 9 deletions
|
|
@ -8,7 +8,7 @@
|
|||
__docformat__ = 'restructuredtext en'
|
||||
|
||||
# Imports {{{
|
||||
import os, shutil, uuid, json, glob, time, tempfile
|
||||
import os, shutil, uuid, json, glob, time
|
||||
from functools import partial
|
||||
|
||||
import apsw
|
||||
|
|
@ -16,7 +16,7 @@
|
|||
from calibre import isbytestring, force_unicode, prints
|
||||
from calibre.constants import (iswindows, filesystem_encoding,
|
||||
preferred_encoding)
|
||||
from calibre.ptempfile import PersistentTemporaryFile
|
||||
from calibre.ptempfile import PersistentTemporaryFile, SpooledTemporaryFile
|
||||
from calibre.db.schema_upgrades import SchemaUpgrade
|
||||
from calibre.library.field_metadata import FieldMetadata
|
||||
from calibre.ebooks.metadata import title_sort, author_to_author_sort
|
||||
|
|
@ -805,7 +805,7 @@ def cover(self, path, as_file=False, as_image=False,
|
|||
shutil.copyfileobj(f, pt)
|
||||
return pt.name
|
||||
if as_file:
|
||||
ret = tempfile.SpooledTemporaryFile(SPOOL_SIZE)
|
||||
ret = SpooledTemporaryFile(SPOOL_SIZE)
|
||||
shutil.copyfileobj(f, ret)
|
||||
ret.seek(0)
|
||||
else:
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
__copyright__ = '2011, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||
__docformat__ = 'restructuredtext en'
|
||||
|
||||
import textwrap, re, os, errno
|
||||
import textwrap, re, os, errno, shutil
|
||||
|
||||
from PyQt4.Qt import (Qt, QDateEdit, QDate, pyqtSignal, QMessageBox,
|
||||
QIcon, QToolButton, QWidget, QLabel, QGridLayout, QApplication,
|
||||
|
|
@ -33,8 +33,9 @@
|
|||
from calibre.library.comments import comments_to_html
|
||||
from calibre.gui2.dialogs.tag_editor import TagEditor
|
||||
from calibre.utils.icu import strcmp
|
||||
from calibre.ptempfile import PersistentTemporaryFile
|
||||
from calibre.ptempfile import PersistentTemporaryFile, SpooledTemporaryFile
|
||||
from calibre.gui2.languages import LanguagesEdit as LE
|
||||
from calibre.db.backend import SPOOL_SIZE
|
||||
|
||||
def save_dialog(parent, title, msg, det_msg=''):
|
||||
d = QMessageBox(parent)
|
||||
|
|
@ -43,8 +44,6 @@ def save_dialog(parent, title, msg, det_msg=''):
|
|||
d.setStandardButtons(QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel)
|
||||
return d.exec_()
|
||||
|
||||
|
||||
|
||||
'''
|
||||
The interface common to all widgets used to set basic metadata
|
||||
class BasicMetadataWidget(object):
|
||||
|
|
@ -731,8 +730,12 @@ def commit(self, db, id_):
|
|||
else:
|
||||
old_extensions.add(ext)
|
||||
for ext in new_extensions:
|
||||
db.add_format(id_, ext, open(paths[ext], 'rb'), notify=False,
|
||||
index_is_id=True)
|
||||
with SpooledTemporaryFile(SPOOL_SIZE) as spool:
|
||||
with open(paths[ext], 'rb') as f:
|
||||
shutil.copyfileobj(f, spool)
|
||||
spool.seek(0)
|
||||
db.add_format(id_, ext, spool, notify=False,
|
||||
index_is_id=True)
|
||||
dbfmts = db.formats(id_, index_is_id=True)
|
||||
db_extensions = set([f.lower() for f in (dbfmts.split(',') if dbfmts
|
||||
else [])])
|
||||
|
|
|
|||
|
|
@ -181,4 +181,17 @@ def __exit__(self, *args):
|
|||
|
||||
|
||||
|
||||
class SpooledTemporaryFile(tempfile.SpooledTemporaryFile):
|
||||
|
||||
def __init__(self, max_size=0, suffix="", prefix="", dir=None, mode='w+b',
|
||||
bufsize=-1):
|
||||
if prefix == None:
|
||||
prefix = ''
|
||||
if suffix is None:
|
||||
suffix = ''
|
||||
if dir is None:
|
||||
dir = base_dir()
|
||||
tempfile.SpooledTemporaryFile.__init__(self, max_size=max_size, suffix=suffix,
|
||||
prefix=prefix, dir=dir, mode=mode, bufsize=bufsize)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue