Merge branch 'fix-image-db-upgrade' into 'master'

Fix image db upgrade

Closes #585, #649, and #607

See merge request openlp/openlp!230
This commit is contained in:
Tim Bentley 2020-08-18 06:47:02 +00:00
commit e2a65a3264
4 changed files with 23 additions and 7 deletions

View File

@ -575,6 +575,7 @@ class ImageMediaItem(MediaManagerItem):
service_item.theme = -1 service_item.theme = -1
missing_items_file_names = [] missing_items_file_names = []
images = [] images = []
existing_images = []
# Expand groups to images # Expand groups to images
for bitem in items: for bitem in items:
if isinstance(bitem.data(0, QtCore.Qt.UserRole), ImageGroups) or bitem.data(0, QtCore.Qt.UserRole) is None: if isinstance(bitem.data(0, QtCore.Qt.UserRole), ImageGroups) or bitem.data(0, QtCore.Qt.UserRole) is None:
@ -590,8 +591,10 @@ class ImageMediaItem(MediaManagerItem):
for image in images: for image in images:
if not image.file_path.exists(): if not image.file_path.exists():
missing_items_file_names.append(str(image.file_path)) missing_items_file_names.append(str(image.file_path))
else:
existing_images.append(image)
# We cannot continue, as all images do not exist. # We cannot continue, as all images do not exist.
if not images: if not existing_images:
if not remote: if not remote:
critical_error_message_box( critical_error_message_box(
translate('ImagePlugin.MediaItem', 'Missing Image(s)'), translate('ImagePlugin.MediaItem', 'Missing Image(s)'),
@ -607,7 +610,7 @@ class ImageMediaItem(MediaManagerItem):
QtWidgets.QMessageBox.No: QtWidgets.QMessageBox.No:
return False return False
# Continue with the existing images. # Continue with the existing images.
for image in images: for image in existing_images:
name = image.file_path.name name = image.file_path.name
thumbnail_path = self.generate_thumbnail_path(image) thumbnail_path = self.generate_thumbnail_path(image)
service_item.add_from_image(image.file_path, name, thumbnail_path) service_item.add_from_image(image.file_path, name, thumbnail_path)

View File

@ -85,7 +85,12 @@ def upgrade_3(session, metadata):
thumb_path = AppLocation.get_data_path() / 'images' / 'thumbnails' thumb_path = AppLocation.get_data_path() / 'images' / 'thumbnails'
for row in results.fetchall(): for row in results.fetchall():
file_path = json.loads(row.file_path, cls=OpenLPJSONDecoder) file_path = json.loads(row.file_path, cls=OpenLPJSONDecoder)
hash = sha256_file_hash(file_path) if file_path.exists():
hash = sha256_file_hash(file_path)
else:
log.warning('{image} does not exists, so no sha256 hash added.'.format(image=str(file_path)))
# set a fake "hash" to allow for the upgrade to go through. The image will be marked as invalid
hash = 'NONE'
sql = 'UPDATE image_filenames SET file_hash = \'{hash}\' WHERE id = {id}'.format(hash=hash, id=row.id) sql = 'UPDATE image_filenames SET file_hash = \'{hash}\' WHERE id = {id}'.format(hash=hash, id=row.id)
conn.execute(sql) conn.execute(sql)
# rename thumbnail to use file hash # rename thumbnail to use file hash

View File

@ -234,7 +234,10 @@ class PresentationMediaItem(MediaManagerItem):
self.application.set_busy_cursor() self.application.set_busy_cursor()
self.main_window.display_progress_bar(len(row_list)) self.main_window.display_progress_bar(len(row_list))
for item in items: for item in items:
self.clean_up_thumbnails(item.data(QtCore.Qt.UserRole)) file_path = item.data(QtCore.Qt.UserRole)
# cleaning thumbnails depends on being able to calculate sha256 hash of the actual file
if file_path.exists():
self.clean_up_thumbnails(file_path)
self.main_window.increment_progress_bar() self.main_window.increment_progress_bar()
self.main_window.finished_progress_bar() self.main_window.finished_progress_bar()
for row in row_list: for row in row_list:

View File

@ -26,7 +26,7 @@ import pytest
from pathlib import Path from pathlib import Path
from unittest.mock import Mock, MagicMock, patch from unittest.mock import Mock, MagicMock, patch
from openlp.core.common import ThemeLevel from openlp.core.common import ThemeLevel, is_win
from openlp.core.common.enum import ServiceItemType from openlp.core.common.enum import ServiceItemType
from openlp.core.common.registry import Registry from openlp.core.common.registry import Registry
from openlp.core.lib.formattingtags import FormattingTags from openlp.core.lib.formattingtags import FormattingTags
@ -563,16 +563,21 @@ def test_to_dict_text_item(state_media, settings, service_item_env):
service_item.add_icon = MagicMock() service_item.add_icon = MagicMock()
FormattingTags.load_tags() FormattingTags.load_tags()
line = convert_file_service_item(TEST_PATH, 'serviceitem-song-linked-audio.osj') line = convert_file_service_item(TEST_PATH, 'serviceitem-song-linked-audio.osj')
service_item.set_from_service(line, Path('/test/')) if is_win():
fake_path = Path('c:\\test\\')
else:
fake_path = Path('/test/')
service_item.set_from_service(line, fake_path)
# WHEN: to_dict() is called # WHEN: to_dict() is called
result = service_item.to_dict() result = service_item.to_dict()
# THEN: The correct dictionary should be returned # THEN: The correct dictionary should be returned
expected_fake_path = str(fake_path / 'amazing_grace.mp3')
expected_dict = { expected_dict = {
'audit': ['Amazing Grace', ['John Newton'], '', ''], 'audit': ['Amazing Grace', ['John Newton'], '', ''],
'backgroundAudio': ['/test/amazing_grace.mp3'], 'backgroundAudio': [expected_fake_path],
'capabilities': [2, 1, 5, 8, 9, 13, 15], 'capabilities': [2, 1, 5, 8, 9, 13, 15],
'footer': ['Amazing Grace', 'Written by: John Newton'], 'footer': ['Amazing Grace', 'Written by: John Newton'],
'fromPlugin': False, 'fromPlugin': False,