From e0faad1e591038ccebbd99a8df745f5217aebc13 Mon Sep 17 00:00:00 2001 From: Ozzieisaacs Date: Sun, 8 Dec 2019 09:40:54 +0100 Subject: [PATCH] Handle no write permission to tmp folder (#1060) --- cps/editbooks.py | 22 ++++++++++++++++------ cps/uploader.py | 2 +- cps/worker.py | 10 +++------- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/cps/editbooks.py b/cps/editbooks.py index f0156f71..acf20651 100644 --- a/cps/editbooks.py +++ b/cps/editbooks.py @@ -550,13 +550,19 @@ def upload(): flash( _("File extension '%(ext)s' is not allowed to be uploaded to this server", ext=file_ext), category="error") - return redirect(url_for('web.index')) + return Response(json.dumps({"location": url_for("web.index")}), mimetype='application/json') else: 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 - 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 authr = meta.author tags = meta.tags @@ -570,7 +576,8 @@ def upload(): if title != u'Unknown' and authr != u'Unknown': entry = helper.check_exists_book(authr, title) 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") # check if file path exists, otherwise create it, copy file to calibre path and delete temp file @@ -578,16 +585,19 @@ def upload(): try: os.makedirs(filepath) 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") - return redirect(url_for('web.index')) + return Response(json.dumps({"location": url_for("web.index")}), mimetype='application/json') try: copyfile(meta.file_path, saved_filename) 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") - return redirect(url_for('web.index')) + return Response(json.dumps({"location": url_for("web.index")}), mimetype='application/json') try: os.unlink(meta.file_path) 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), category="warning") diff --git a/cps/uploader.py b/cps/uploader.py index 79ac9417..b036b6dc 100644 --- a/cps/uploader.py +++ b/cps/uploader.py @@ -195,12 +195,12 @@ def upload(uploadfile): if not os.path.isdir(tmp_dir): os.mkdir(tmp_dir) - filename = uploadfile.filename filename_root, file_extension = os.path.splitext(filename) md5 = hashlib.md5() md5.update(filename.encode('utf-8')) tmp_file_path = os.path.join(tmp_dir, md5.hexdigest()) + log.debug("Temporary file: %s", tmp_file_path) uploadfile.save(tmp_file_path) meta = process(tmp_file_path, filename_root, file_extension) return meta diff --git a/cps/worker.py b/cps/worker.py index 5ccf2d8c..b508c437 100644 --- a/cps/worker.py +++ b/cps/worker.py @@ -437,19 +437,15 @@ class WorkerThread(threading.Thread): if self.last >= 20: self._delete_completed_tasks() # progress=100%, runtime=0, and status finished - log.info("Last " + str(self.last)) - log.info("Current " + str(self.current)) - log.info("id" + str(self.id)) - for i in range(0, len(self.queue)): - message = '%s:%s' % (i, self.queue[i].items()) - log.info(message) + log.debug("Last " + str(self.last)) + log.debug("Current " + str(self.current)) + log.debug("id" + str(self.id)) self.id += 1 starttime = datetime.now() self.queue.append({'starttime': starttime, 'taskType': TASK_UPLOAD}) 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}) - # self.UIqueue[self.current]['formStarttime'] = self.queue[self.current]['starttime'] self.last=len(self.queue) self.doLock.release()