-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
250 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import Foundation | ||
import UIKit | ||
import CoreData | ||
import os.log | ||
|
||
class CatalogParserDelegate: AmpacheXmlLibParser { | ||
|
||
let musicFoldersBeforeFetch: Set<MusicFolder> | ||
var musicFoldersParsed = Set<MusicFolder>() | ||
var musicFolderBuffer: MusicFolder? | ||
|
||
init(library: LibraryStorage, syncWave: SyncWave) { | ||
musicFoldersBeforeFetch = Set(library.getMusicFolders()) | ||
super.init(library: library, syncWave: syncWave) | ||
} | ||
|
||
override func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) { | ||
super.parser(parser, didStartElement: elementName, namespaceURI: namespaceURI, qualifiedName: qName, attributes: attributeDict) | ||
|
||
if(elementName == "catalog") { | ||
guard let id = attributeDict["id"] else { | ||
os_log("Found catalog with no id", log: log, type: .error) | ||
return | ||
} | ||
if let fetchedMusicFolder = library.getMusicFolder(id: id) { | ||
musicFolderBuffer = fetchedMusicFolder | ||
} else { | ||
musicFolderBuffer = library.createMusicFolder() | ||
musicFolderBuffer?.id = id | ||
} | ||
} | ||
} | ||
|
||
override func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) { | ||
switch(elementName) { | ||
case "name": | ||
musicFolderBuffer?.name = buffer | ||
case "catalog": | ||
parsedCount += 1 | ||
if let parsedmusicFolder = musicFolderBuffer { | ||
musicFoldersParsed.insert(parsedmusicFolder) | ||
} | ||
musicFolderBuffer = nil | ||
case "root": | ||
let removedMusicFolders = musicFoldersBeforeFetch.subtracting(musicFoldersParsed) | ||
removedMusicFolders.forEach{ library.deleteMusicFolder(musicFolder: $0) } | ||
default: | ||
break | ||
} | ||
|
||
super.parser(parser, didEndElement: elementName, namespaceURI: namespaceURI, qualifiedName: qName) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import XCTest | ||
@testable import Amperfy | ||
|
||
class CatalogParserTest: AbstractAmpacheTest { | ||
|
||
override func setUp() { | ||
super.setUp() | ||
xmlData = getTestFileData(name: "catalogs") | ||
recreateParserDelegate() | ||
} | ||
|
||
override func recreateParserDelegate() { | ||
parserDelegate = CatalogParserDelegate(library: library, syncWave: syncWave) | ||
} | ||
|
||
override func checkCorrectParsing() { | ||
XCTAssertEqual(library.musicFolderCount, 4) | ||
|
||
let musicFolders = library.getMusicFolders().sorted(by: {Int($0.id)! < Int($1.id)!} ) | ||
XCTAssertEqual(musicFolders[0].id, "1") | ||
XCTAssertEqual(musicFolders[0].name, "music") | ||
XCTAssertEqual(musicFolders[1].id, "2") | ||
XCTAssertEqual(musicFolders[1].name, "video") | ||
XCTAssertEqual(musicFolders[2].id, "3") | ||
XCTAssertEqual(musicFolders[2].name, "podcast") | ||
XCTAssertEqual(musicFolders[3].id, "4") | ||
XCTAssertEqual(musicFolders[3].name, "upload") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<root> | ||
<total_count>4</total_count> | ||
<catalog id="1"> | ||
<name><![CDATA[music]]></name> | ||
<type><![CDATA[local]]></type> | ||
<gather_types><![CDATA[music]]></gather_types> | ||
<enabled>1</enabled> | ||
<last_add>1627949046</last_add> | ||
<last_clean>1627949154</last_clean> | ||
<last_update>1626835896</last_update> | ||
<path><![CDATA[/mnt/files-music/ampache-test/music]]></path> | ||
<rename_pattern><![CDATA[%T - %t]]></rename_pattern> | ||
<sort_pattern><![CDATA[%a/%A]]></sort_pattern> | ||
</catalog> | ||
<catalog id="3"> | ||
<name><![CDATA[podcast]]></name> | ||
<type><![CDATA[local]]></type> | ||
<gather_types><![CDATA[podcast]]></gather_types> | ||
<enabled>1</enabled> | ||
<last_add>1617260634</last_add> | ||
<last_clean>1617260599</last_clean> | ||
<last_update>0</last_update> | ||
<path><![CDATA[/mnt/files-music/ampache-test/podcast]]></path> | ||
<rename_pattern><![CDATA[%T - %t]]></rename_pattern> | ||
<sort_pattern><![CDATA[%a/%A]]></sort_pattern> | ||
</catalog> | ||
<catalog id="4"> | ||
<name><![CDATA[upload]]></name> | ||
<type><![CDATA[local]]></type> | ||
<gather_types><![CDATA[music]]></gather_types> | ||
<enabled>1</enabled> | ||
<last_add>1617260634</last_add> | ||
<last_clean>1617260634</last_clean> | ||
<last_update>0</last_update> | ||
<path><![CDATA[/mnt/files-music/ampache-test/upload]]></path> | ||
<rename_pattern><![CDATA[%T - %t]]></rename_pattern> | ||
<sort_pattern><![CDATA[%a/%A]]></sort_pattern> | ||
</catalog> | ||
<catalog id="2"> | ||
<name><![CDATA[video]]></name> | ||
<type><![CDATA[local]]></type> | ||
<gather_types><![CDATA[clip]]></gather_types> | ||
<enabled>1</enabled> | ||
<last_add>1627949054</last_add> | ||
<last_clean>1627949144</last_clean> | ||
<last_update>0</last_update> | ||
<path><![CDATA[/mnt/files-music/ampache-test/video]]></path> | ||
<rename_pattern><![CDATA[%a - %t (%y)]]></rename_pattern> | ||
<sort_pattern><![CDATA[]]></sort_pattern> | ||
</catalog> | ||
</root> |