importer chnges
This commit is contained in:
@@ -74,6 +74,7 @@ struct Importer {
|
||||
var archives = [(url:URL,signature:String,folder:URL)]()
|
||||
var archiveForFile = [String:URL]()
|
||||
var archiveSongIDs = [URL:[Int64]]()
|
||||
var archiveOrigins = [URL:URL]()
|
||||
func importSignature(_ audio: URL,cdg: URL? = nil) throws -> String {
|
||||
func signature(_ url: URL) throws -> String {
|
||||
let attrs = try FileManager.default.attributesOfItem(atPath:url.path)
|
||||
@@ -82,13 +83,19 @@ struct Importer {
|
||||
return try signature(audio)+"|"+(cdg.map { try signature($0) } ?? "no-cdg")+"|copy=\(copyMedia)"
|
||||
}
|
||||
func remember(_ audio:URL,_ signature:String,_ id:Int64) throws {
|
||||
var originalPath = audio.path
|
||||
if let folder = archiveForFile[audio.resolvingSymlinksInPath().path],let zip = archiveOrigins[folder] {
|
||||
let relative = String(audio.resolvingSymlinksInPath().path.dropFirst(folder.resolvingSymlinksInPath().path.count+1))
|
||||
originalPath = zip.path+"/"+relative
|
||||
}
|
||||
try library.recordOriginalName(song:id,name:audio.deletingPathExtension().lastPathComponent,path:originalPath)
|
||||
if let archive = archiveForFile[audio.resolvingSymlinksInPath().path] { archiveSongIDs[archive,default:[]].append(id) }
|
||||
else { try library.rememberImport(path:audio.standardizedFileURL.path,signature:signature,songs:[id]) }
|
||||
}
|
||||
for zip in files where zip.pathExtension.lowercased() == "zip" {
|
||||
if cancelled() { throw CancellationError() }
|
||||
let signature = try importSignature(zip)
|
||||
if let existing = try library.importedSongs(path:zip.standardizedFileURL.path,signature:signature) {
|
||||
if let existing = try library.importedSongs(path:zip.standardizedFileURL.path,signature:signature),try library.originalNamesRecorded(existing) {
|
||||
songIDs.formUnion(existing);state.completed += existing.count;state.skipped += existing.count
|
||||
report("Already imported: "+zip.lastPathComponent,force:state.completed == existing.count);continue
|
||||
}
|
||||
@@ -96,7 +103,7 @@ struct Importer {
|
||||
let folder = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
|
||||
try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
|
||||
temporary.append(folder)
|
||||
archives.append((zip,signature,folder))
|
||||
archives.append((zip,signature,folder));archiveOrigins[folder] = zip
|
||||
// Validate names before extraction; reject path traversal and symlinks afterwards.
|
||||
let details = try process(["-Z", "-l", zip.path])
|
||||
guard !details.split(separator: "\n").contains(where: { $0.hasPrefix("l") }) else { throw LibraryError.message("ZIP symlinks are unsupported") }
|
||||
@@ -165,7 +172,7 @@ struct Importer {
|
||||
let signature = try importSignature(audio,cdg:cdg)
|
||||
if let existing = try library.importedSongs(path:audio.standardizedFileURL.path,signature:signature) {
|
||||
songIDs.formUnion(existing)
|
||||
if let archive { archiveSongIDs[archive,default:[]].append(contentsOf:existing) }
|
||||
for id in existing { try remember(audio,signature,id) }
|
||||
state.completed += 1;state.skipped += 1
|
||||
report("Already imported: "+audio.lastPathComponent,force:state.completed == 1);continue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user