Skip to content

Commit

Permalink
Let CopyProcessor mapping return nil too
Browse files Browse the repository at this point in the history
...to discard a track
  • Loading branch information
fwcd committed Apr 30, 2024
1 parent 44de5fd commit 91bd57d
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions Sources/MusicLibrary/Process/CopyProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation

/// A library processor copying tracks to a location using a provided mapping.
public struct CopyProcessor: LibraryProcessor {
private let mapping: (Track) -> URL
private let mapping: (Track) -> URL?

public init(mapping: @escaping (Track) -> URL) {
self.mapping = mapping
Expand All @@ -14,19 +14,18 @@ public struct CopyProcessor: LibraryProcessor {
}

var newTracks: [Int: Track] = [:]
for (id, track) in library.tracks {
for (i, (id, track)) in library.tracks.enumerated() {
let oldURL = track.url
let newURL = mapping(track)

progress.increment(message: "Copying to \(newURL)...")

if let oldURL {
if let newURL, let oldURL {
progress.update(current: i, message: "Copying to \(newURL)...")
try FileManager.default.copyItem(at: oldURL, to: newURL)
}

var newTrack = track
newTrack.url = newURL
newTracks[id] = newTrack
var newTrack = track
newTrack.url = newURL
newTracks[id] = newTrack
}
}

library.tracks = newTracks
Expand Down

0 comments on commit 91bd57d

Please sign in to comment.