98 lines
3.1 KiB
Swift
98 lines
3.1 KiB
Swift
import Foundation
|
|
|
|
struct AudioPlayerCommandProcessor: CommandProcessor {
|
|
|
|
let commandType: ShorthandMarkdownKey = .audioPlayer
|
|
|
|
let content: Content
|
|
|
|
let results: PageGenerationResults
|
|
|
|
init(content: Content, results: PageGenerationResults) {
|
|
self.content = content
|
|
self.results = results
|
|
}
|
|
|
|
func process(_ arguments: [String], markdown: Substring) -> String {
|
|
guard arguments.count == 2 else {
|
|
results.invalid(command: .audioPlayer, "Invalid audio player arguments")
|
|
return ""
|
|
}
|
|
let fileId = arguments[0]
|
|
let titleText = arguments[1]
|
|
|
|
guard content.isValidIdForFile(fileId) else {
|
|
results.invalid(command: .audioPlayer, "Invalid file id \(fileId) for audio player")
|
|
return ""
|
|
}
|
|
|
|
guard let file = content.file(fileId) else {
|
|
results.missingFiles.insert(fileId)
|
|
return ""
|
|
}
|
|
guard let data = file.dataContent() else {
|
|
results.issues.insert(.failedToLoadContent)
|
|
return ""
|
|
}
|
|
let songs: [Song]
|
|
do {
|
|
songs = try JSONDecoder().decode([Song].self, from: data)
|
|
} catch {
|
|
results.issues.insert(.failedToParseContent)
|
|
return ""
|
|
}
|
|
|
|
var playlist: [AudioPlayer.PlaylistItem] = []
|
|
var amplitude: [AmplitudeSong] = []
|
|
|
|
for song in songs {
|
|
guard let image = content.image(song.cover) else {
|
|
results.missing(file: song.cover, markdown: "Missing cover image \(song.cover) in \(file.id)")
|
|
continue
|
|
}
|
|
|
|
guard let audioFile = content.file(song.file) else {
|
|
results.missing(file: song.file, markdown: "Missing audio file \(song.file) in \(file.id)")
|
|
continue
|
|
}
|
|
#warning("Check if file is audio")
|
|
let coverUrl = image.absoluteUrl
|
|
|
|
let playlistItem = AudioPlayer.PlaylistItem(
|
|
index: playlist.count,
|
|
image: coverUrl,
|
|
name: song.name,
|
|
album: song.album,
|
|
track: song.track,
|
|
artist: song.artist)
|
|
|
|
let amplitudeSong = AmplitudeSong(
|
|
name: song.name,
|
|
artist: song.artist,
|
|
album: song.album,
|
|
track: "\(song.track)",
|
|
url: audioFile.absoluteUrl,
|
|
cover_art_url: coverUrl)
|
|
|
|
playlist.append(playlistItem)
|
|
amplitude.append(amplitudeSong)
|
|
}
|
|
|
|
let footerScript = AudioPlayerScript(items: amplitude).content
|
|
results.requiredFooters.insert(footerScript)
|
|
results.requiredHeaders.insert(.audioPlayerCss)
|
|
results.requiredHeaders.insert(.audioPlayerJs)
|
|
|
|
results.requiredIcons.formUnion([
|
|
.audioPlayerClose,
|
|
.audioPlayerPlaylist,
|
|
.audioPlayerNext,
|
|
.audioPlayerPrevious,
|
|
.audioPlayerPlay,
|
|
.audioPlayerPause
|
|
])
|
|
|
|
return AudioPlayer(playingText: titleText, items: playlist).content
|
|
}
|
|
}
|