Handle no write permission to tmp folder (#1060)

pull/1107/head
Ozzieisaacs 5 years ago
parent fda0ab1e86
commit e0faad1e59

@ -550,13 +550,19 @@ def upload():
flash( flash(
_("File extension '%(ext)s' is not allowed to be uploaded to this server", _("File extension '%(ext)s' is not allowed to be uploaded to this server",
ext=file_ext), category="error") ext=file_ext), category="error")
return redirect(url_for('web.index')) return Response(json.dumps({"location": url_for("web.index")}), mimetype='application/json')
else: else:
flash(_('File to be uploaded must have an extension'), category="error") flash(_('File to be uploaded must have an extension'), category="error")
return redirect(url_for('web.index')) return Response(json.dumps({"location": url_for("web.index")}), mimetype='application/json')
# extract metadata from file # extract metadata from file
meta = uploader.upload(requested_file) try:
meta = uploader.upload(requested_file)
except (IOError, OSError):
log.error("File %s could not saved to temp dir", requested_file.filename)
flash(_(u"File %(filename)s could not saved to temp dir",
filename= requested_file.filename), category="error")
return Response(json.dumps({"location": url_for("web.index")}), mimetype='application/json')
title = meta.title title = meta.title
authr = meta.author authr = meta.author
tags = meta.tags tags = meta.tags
@ -570,7 +576,8 @@ def upload():
if title != u'Unknown' and authr != u'Unknown': if title != u'Unknown' and authr != u'Unknown':
entry = helper.check_exists_book(authr, title) entry = helper.check_exists_book(authr, title)
if entry: if entry:
book_html = flash(_(u"Uploaded book probably exists in the library, consider to change before upload new: ") log.info("Uploaded book probably exists in library")
flash(_(u"Uploaded book probably exists in the library, consider to change before upload new: ")
+ Markup(render_title_template('book_exists_flash.html', entry=entry)), category="warning") + Markup(render_title_template('book_exists_flash.html', entry=entry)), category="warning")
# check if file path exists, otherwise create it, copy file to calibre path and delete temp file # check if file path exists, otherwise create it, copy file to calibre path and delete temp file
@ -578,16 +585,19 @@ def upload():
try: try:
os.makedirs(filepath) os.makedirs(filepath)
except OSError: except OSError:
log.error("Failed to create path %s (Permission denied)", filepath)
flash(_(u"Failed to create path %(path)s (Permission denied).", path=filepath), category="error") flash(_(u"Failed to create path %(path)s (Permission denied).", path=filepath), category="error")
return redirect(url_for('web.index')) return Response(json.dumps({"location": url_for("web.index")}), mimetype='application/json')
try: try:
copyfile(meta.file_path, saved_filename) copyfile(meta.file_path, saved_filename)
except OSError: except OSError:
log.error("Failed to store file %s (Permission denied)", saved_filename)
flash(_(u"Failed to store file %(file)s (Permission denied).", file=saved_filename), category="error") flash(_(u"Failed to store file %(file)s (Permission denied).", file=saved_filename), category="error")
return redirect(url_for('web.index')) return Response(json.dumps({"location": url_for("web.index")}), mimetype='application/json')
try: try:
os.unlink(meta.file_path) os.unlink(meta.file_path)
except OSError: except OSError:
log.error("Failed to delete file %(file)s (Permission denied)", meta.file_path)
flash(_(u"Failed to delete file %(file)s (Permission denied).", file= meta.file_path), flash(_(u"Failed to delete file %(file)s (Permission denied).", file= meta.file_path),
category="warning") category="warning")

@ -195,12 +195,12 @@ def upload(uploadfile):
if not os.path.isdir(tmp_dir): if not os.path.isdir(tmp_dir):
os.mkdir(tmp_dir) os.mkdir(tmp_dir)
filename = uploadfile.filename filename = uploadfile.filename
filename_root, file_extension = os.path.splitext(filename) filename_root, file_extension = os.path.splitext(filename)
md5 = hashlib.md5() md5 = hashlib.md5()
md5.update(filename.encode('utf-8')) md5.update(filename.encode('utf-8'))
tmp_file_path = os.path.join(tmp_dir, md5.hexdigest()) tmp_file_path = os.path.join(tmp_dir, md5.hexdigest())
log.debug("Temporary file: %s", tmp_file_path)
uploadfile.save(tmp_file_path) uploadfile.save(tmp_file_path)
meta = process(tmp_file_path, filename_root, file_extension) meta = process(tmp_file_path, filename_root, file_extension)
return meta return meta

@ -437,19 +437,15 @@ class WorkerThread(threading.Thread):
if self.last >= 20: if self.last >= 20:
self._delete_completed_tasks() self._delete_completed_tasks()
# progress=100%, runtime=0, and status finished # progress=100%, runtime=0, and status finished
log.info("Last " + str(self.last)) log.debug("Last " + str(self.last))
log.info("Current " + str(self.current)) log.debug("Current " + str(self.current))
log.info("id" + str(self.id)) log.debug("id" + str(self.id))
for i in range(0, len(self.queue)):
message = '%s:%s' % (i, self.queue[i].items())
log.info(message)
self.id += 1 self.id += 1
starttime = datetime.now() starttime = datetime.now()
self.queue.append({'starttime': starttime, 'taskType': TASK_UPLOAD}) self.queue.append({'starttime': starttime, 'taskType': TASK_UPLOAD})
self.UIqueue.append({'user': user_name, 'formStarttime': starttime, 'progress': "100 %", 'taskMess': taskMessage, self.UIqueue.append({'user': user_name, 'formStarttime': starttime, 'progress': "100 %", 'taskMess': taskMessage,
'runtime': '0 s', 'stat': STAT_FINISH_SUCCESS,'id': self.id, 'taskType': TASK_UPLOAD}) 'runtime': '0 s', 'stat': STAT_FINISH_SUCCESS,'id': self.id, 'taskType': TASK_UPLOAD})
# self.UIqueue[self.current]['formStarttime'] = self.queue[self.current]['starttime']
self.last=len(self.queue) self.last=len(self.queue)
self.doLock.release() self.doLock.release()

Loading…
Cancel
Save