diff --git a/src/calibre/srv/library_broker.py b/src/calibre/srv/library_broker.py index dcaf3318d1..5f5d5b8851 100644 --- a/src/calibre/srv/library_broker.py +++ b/src/calibre/srv/library_broker.py @@ -62,6 +62,18 @@ def library_id_from_path(path, existing=frozenset()): return make_library_id_unique(library_id, existing) +def correct_case_of_last_path_component(original_path): + prefix, basename = os.path.split(original_path) + q = basename.lower() + equals = tuple(x for x in os.listdir(prefix) if x.lower() == q) + if len(equals) > 1: + if basename not in equals: + basename = equals[0] + else: + basename = equals[0] + return os.path.join(prefix, basename) + + class LibraryBroker(object): def __init__(self, libraries): @@ -82,9 +94,10 @@ class LibraryBroker(object): seen.add(path) if is_samefile or not LibraryDatabase.exists_at(path): continue - library_id = library_id_from_path(original_path, self.lmap) + corrected_path = correct_case_of_last_path_component(original_path) + library_id = library_id_from_path(corrected_path, self.lmap) self.lmap[library_id] = path - self.library_name_map[library_id] = basename(original_path) + self.library_name_map[library_id] = basename(corrected_path) self.original_path_map[path] = original_path self.loaded_dbs = {} self.category_caches, self.search_caches, self.tag_browser_caches = ( @@ -195,11 +208,11 @@ class GuiLibraryBroker(LibraryBroker): if library_path not in self.original_path_map: self.original_path_map[library_path] = original_library_path db = self.init_library(library_path, False) - library_id = library_id_from_path(library_path, self.lmap) + corrected_path = correct_case_of_last_path_component(original_library_path) + library_id = library_id_from_path(corrected_path, self.lmap) db.new_api.server_library_id = library_id self.lmap[library_id] = library_path - self.library_name_map[library_id] = basename( - original_library_path) + self.library_name_map[library_id] = basename(corrected_path) self.loaded_dbs[library_id] = db return db @@ -226,10 +239,10 @@ class GuiLibraryBroker(LibraryBroker): break else: # A new library - library_id = self.gui_library_id = library_id_from_path( - newloc, self.lmap) + corrected_path = correct_case_of_last_path_component(original_path) + library_id = self.gui_library_id = library_id_from_path(corrected_path, self.lmap) self.lmap[library_id] = newloc - self.library_name_map[library_id] = basename(original_path) + self.library_name_map[library_id] = basename(corrected_path) self.original_path_map[newloc] = original_path self.loaded_dbs[library_id] = db db.new_api.server_library_id = library_id diff --git a/src/calibre/srv/tests/routes.py b/src/calibre/srv/tests/routes.py index 1250f003a3..0395de48fd 100644 --- a/src/calibre/srv/tests/routes.py +++ b/src/calibre/srv/tests/routes.py @@ -8,12 +8,13 @@ __copyright__ = '2015, Kovid Goyal ' import os from calibre.srv.tests.base import BaseTest from polyglot.builtins import itervalues, filter +from tempfile import TemporaryDirectory class TestRouter(BaseTest): def test_library_id_construction(self): - from calibre.srv.library_broker import library_id_from_path + from calibre.srv.library_broker import library_id_from_path, correct_case_of_last_path_component self.ae(library_id_from_path('as'), 'as') self.ae(library_id_from_path('as/'), 'as') self.ae(library_id_from_path('as////'), 'as') @@ -21,6 +22,11 @@ class TestRouter(BaseTest): if os.sep == '\\': self.ae(library_id_from_path('as/' + os.sep), 'as') self.ae(library_id_from_path('X:' + os.sep), 'X') + with TemporaryDirectory() as tdir: + path = os.path.join(tdir, 'Test') + os.mkdir(path) + self.ae(correct_case_of_last_path_component(os.path.join(tdir, 'test')), path) + self.ae(correct_case_of_last_path_component(os.path.join(tdir, 'Test')), path) def test_route_construction(self): ' Test route construction '