allow handling multiple file name patterns
This commit is contained in:
@@ -260,8 +260,16 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
|
||||
|
||||
func getMetadataFromFileName(path: URL) {
|
||||
// Beautiful, is it not?
|
||||
let pattern = "c([0-9]+(?:x[0-9]+)?) \\(v([0-9]+)\\) - p([0-9]+(?:-[0-9]+)?)"
|
||||
let titlePattern = "^(.*) - c([0-9]+(?:x[0-9]+)?)"
|
||||
let patterns: [(NSRegularExpression, NSRegularExpression, Int, Int, Int)] = [
|
||||
(try! NSRegularExpression(pattern: #"c([0-9]+(?:x[0-9]+)?) \(v([0-9]+)\) - p([0-9]+(?:-[0-9]+)?)"#), try! NSRegularExpression(pattern: #"^(.*) - c([0-9]+(?:x[0-9]+)?) \(v([0-9]+)\) - p([0-9]+(?:-[0-9]+)?)"#), 3, 1, 2),
|
||||
(try! NSRegularExpression(pattern: "v([0-9]+) p([0-9]+(?:-[0-9]+)?) - c([0-9]+(?:x[0-9]+)?)"), try! NSRegularExpression(pattern: "^(.*) - v([0-9]+) p([0-9]+(?:-[0-9]+)?) - c([0-9]+(?:x[0-9]+)?)"), 2, 3, 1),
|
||||
]
|
||||
var pattern: NSRegularExpression! = nil
|
||||
var titlePattern: NSRegularExpression! = nil
|
||||
|
||||
var pageRegexPosition: Int! = nil
|
||||
var chapterRegexPosition: Int! = nil
|
||||
var volumeRegexPosition: Int! = nil
|
||||
|
||||
var currentVolume: UInt32! = nil
|
||||
var currentChapter: (UInt32, UInt32?)!
|
||||
@@ -281,11 +289,75 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
|
||||
|
||||
var titleValue: String = ""
|
||||
|
||||
|
||||
do {
|
||||
let regex = try NSRegularExpression(pattern: pattern)
|
||||
let titleRegex = try NSRegularExpression(pattern: titlePattern)
|
||||
let dir = path.appendingPathComponent("images")
|
||||
|
||||
var isDir: ObjCBool = false
|
||||
if !fileManager.fileExists(atPath: dir.path, isDirectory: &isDir) || !isDir.boolValue {
|
||||
print("Subdirectory images not found in \"\(path)\", returning")
|
||||
return
|
||||
}
|
||||
|
||||
let testFilename = try fileManager.contentsOfDirectory(
|
||||
at: dir,
|
||||
includingPropertiesForKeys: [.isRegularFileKey],
|
||||
options: [.skipsHiddenFiles]
|
||||
).first
|
||||
if testFilename == nil {
|
||||
print("Empty images dir, returning")
|
||||
return
|
||||
}
|
||||
|
||||
for (currentPattern, currentTitlePattern, currentPageRegexPosition, currentChapterRegexPosition, currentVolumeRegexPosition) in patterns {
|
||||
let filename = testFilename!.lastPathComponent
|
||||
let range = NSRange(filename.startIndex..., in: filename)
|
||||
if let match = currentTitlePattern.firstMatch(in: filename, range: range) {
|
||||
if (filename as NSString).substring(with: match.range(at: 1)) == "" {
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
if let match = currentPattern.firstMatch(in: filename, range: range) {
|
||||
if (filename as NSString).substring(with: match.range(at: currentPageRegexPosition)) == "" {
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
if let match = currentPattern.firstMatch(in: filename, range: range) {
|
||||
if (filename as NSString).substring(with: match.range(at: currentChapterRegexPosition)) == "" {
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
if let match = currentPattern.firstMatch(in: filename, range: range) {
|
||||
if (filename as NSString).substring(with: match.range(at: currentVolumeRegexPosition)) == "" {
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
pattern = currentPattern
|
||||
titlePattern = currentTitlePattern
|
||||
|
||||
pageRegexPosition = currentPageRegexPosition
|
||||
chapterRegexPosition = currentChapterRegexPosition
|
||||
volumeRegexPosition = currentVolumeRegexPosition
|
||||
break
|
||||
}
|
||||
|
||||
if pattern == nil || titlePattern == nil || pageRegexPosition == nil || chapterRegexPosition == nil || volumeRegexPosition == nil {
|
||||
print("Could not find pattern, returning")
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
let regex = pattern!
|
||||
let titleRegex = titlePattern!
|
||||
|
||||
let fileURLs = try fileManager.contentsOfDirectory(
|
||||
at: dir,
|
||||
includingPropertiesForKeys: [.isRegularFileKey],
|
||||
@@ -297,18 +369,21 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
|
||||
if i == 0 {
|
||||
if let match = titleRegex.firstMatch(in: filename, range: range) {
|
||||
titleValue = (filename as NSString).substring(with: match.range(at: 1))
|
||||
}
|
||||
} else {
|
||||
print("Did not find match for title, regex did not handle this case.")
|
||||
return
|
||||
}
|
||||
}
|
||||
if let match = regex.firstMatch(in: filename, range: range) {
|
||||
let chapterStr = (filename as NSString).substring(with: match.range(at: 1))
|
||||
let chapterStr = (filename as NSString).substring(with: match.range(at: chapterRegexPosition))
|
||||
let chapterParts = chapterStr.split(separator: "x", maxSplits: 1)
|
||||
let chapter: (UInt32, UInt32?) = (
|
||||
UInt32(chapterParts[0])!, chapterParts.count > 1 ? UInt32(chapterParts[1])! : nil
|
||||
)
|
||||
|
||||
let volume: UInt32 = UInt32((filename as NSString).substring(with: match.range(at: 2)))!
|
||||
let volume: UInt32 = UInt32((filename as NSString).substring(with: match.range(at: volumeRegexPosition)))!
|
||||
|
||||
let pageStr = (filename as NSString).substring(with: match.range(at: 3))
|
||||
let pageStr = (filename as NSString).substring(with: match.range(at: pageRegexPosition))
|
||||
let pageParts = pageStr.split(separator: "-", maxSplits: 1)
|
||||
let (page, doublePage) = (
|
||||
UInt32(pageParts[0])!, pageParts.count > 1
|
||||
@@ -410,12 +485,20 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
|
||||
}
|
||||
currentVolume = volume
|
||||
currentChapter = chapter
|
||||
} else {
|
||||
print("no regex match on \(filename), returning")
|
||||
return
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
print("failed reading image file names")
|
||||
}
|
||||
|
||||
if volumes.count == 0 {
|
||||
print("Why no metadata from file name?")
|
||||
return
|
||||
}
|
||||
|
||||
let title = builder.create(string: titleValue)
|
||||
let originalLanguage = builder.create(string: "Japanese")
|
||||
let countryOfOrigin = builder.create(string: "Japan")
|
||||
@@ -1074,22 +1157,16 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
|
||||
return
|
||||
}
|
||||
progress = newProgress
|
||||
let path = getImagePath(progress: progress) //{
|
||||
let path = getImagePath(progress: progress)
|
||||
imageLoader.loadImage(at: path, scaling: scaling, screenSize: UIScreen.main.bounds.size) { [weak self] image in
|
||||
self?.imageView.image = image
|
||||
self?.updateInfo()
|
||||
}
|
||||
// } else {
|
||||
// return
|
||||
//}
|
||||
|
||||
for _ in 0 ... preloadCount {
|
||||
newProgress = getProgressIndicesFromTurn(turn: .next, progress: newProgress)
|
||||
let path = getImagePath(progress: newProgress) // {
|
||||
let path = getImagePath(progress: newProgress)
|
||||
imageLoader.preloadImage(at: path, scaling: scaling, screenSize: UIScreen.main.bounds.size)
|
||||
// } else {
|
||||
// print("could not preload image")
|
||||
//}
|
||||
}
|
||||
|
||||
saveLocalState()
|
||||
|
||||
Reference in New Issue
Block a user