@@ -49,38 +49,40 @@ public func isLockFileName(_ filename: String) -> Bool {
4949///
5050/// Parse the original file name contained in a lock filename.
5151///
52- /// Example for Microsoft Office: `MyDoc.docx` is extracted from `~$MyDoc.docx`.
53- /// Example for LibreOffice: `MyDoc.odt` is extracted from `.~lock.MyDoc.odt#`.
54- /// Filename with <8 characters: Test.docx → lock file: ~$Test.docx
55- /// Filename with >8 characters: Document.docx → lock file: ~$cument.docx
56- /// Filename sandbox-style temporary naming like: Welcome123456.doc.sb-d215eb53-IBAwfU
52+ /// - Example for Microsoft Office: `MyDoc.docx` is extracted from `~$MyDoc.docx`.
53+ /// - Example for LibreOffice: `MyDoc.odt` is extracted from `.~lock.MyDoc.odt#`.
54+ /// - Filename with less than 8 characters like `Test.docx` will result in a lock file named `~$Test.docx`.
55+ /// - Filename with more than 8 characters like `Document.docx` will result in a lock file named `~$cument.docx`.
56+ /// - Filename sandbox-style temporary naming like `Welcome123456.doc.sb-d215eb53-IBAwfU`.
57+ ///
5758/// - Returns: Either the original file name parsed from the given lock file name or `nil`, if it is not a recognized lock file format.
5859///
5960public func originalFileName( fromLockFileName lockFilename: String , dbManager: FilesDatabaseManager ) -> String ? {
6061 let logger = FileProviderLogger ( category: " localfileutils " , log: dbManager. logger. log)
6162 logger. debug ( " Called originalFileName with lock filename: \( lockFilename) " )
6263
63- var targetFilePattern = lockFilename
64+ var targetFileSuffix = lockFilename
6465 if lockFilename. hasPrefix ( " ~$ " ) {
6566 let index = lockFilename. index ( lockFilename. startIndex, offsetBy: 2 )
66- targetFilePattern = String ( lockFilename [ index... ] )
67+ targetFileSuffix = String ( lockFilename [ index... ] )
6768 }
6869
6970 if lockFilename. hasPrefix ( " .~lock. " ) && lockFilename. hasSuffix ( " # " ) {
7071 let start = lockFilename. index ( lockFilename. startIndex, offsetBy: 7 )
7172 let end = lockFilename. index ( before: lockFilename. endIndex)
72- targetFilePattern = String ( lockFilename [ start..< end] )
73+ targetFileSuffix = String ( lockFilename [ start..< end] )
7374 }
7475
7576 if let sbRange = lockFilename. range ( of: " .sb- " ) {
76- targetFilePattern = String ( lockFilename [ ..< sbRange. lowerBound] )
77+ targetFileSuffix = String ( lockFilename [ ..< sbRange. lowerBound] )
7778 }
7879
79- logger. debug ( " Target filename is: \( targetFilePattern ) " )
80+ logger. debug ( " Target suffix is: \( targetFileSuffix ) " )
8081
81- guard let itemsMatchingMetadata = dbManager. itemsMetadataFromPattern ( pattern: targetFilePattern) else {
82- logger. debug ( " Could not find files in db matching pattern: \( targetFilePattern) " )
83- return targetFilePattern
82+ let itemsMatchingMetadata = dbManager. itemsMetadataByFileNameSuffix ( suffix: targetFileSuffix)
83+ if itemsMatchingMetadata. isEmpty {
84+ logger. debug ( " Could not find files in db matching suffix: \( targetFileSuffix) " )
85+ return targetFileSuffix
8486 }
8587
8688 for file in itemsMatchingMetadata {
0 commit comments