MediaModel.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*****************************************************************************
  2. * MediaModel.swift
  3. *
  4. * Copyright © 2018 VLC authors and VideoLAN
  5. * Copyright © 2018 Videolabs
  6. *
  7. * Authors: Soomin Lee <bubu@mikan.io>
  8. *
  9. * Refer to the COPYING file of the official project for license.
  10. *****************************************************************************/
  11. protocol MediaModel: MLBaseModel where MLType == VLCMLMedia { }
  12. extension MediaModel {
  13. func append(_ item: VLCMLMedia) {
  14. if !files.contains { $0 == item } {
  15. files.append(item)
  16. }
  17. }
  18. func delete(_ items: [VLCMLObject]) {
  19. do {
  20. for case let media as VLCMLMedia in items {
  21. if let mainFile = media.mainFile() {
  22. try FileManager.default.removeItem(atPath: mainFile.mrl.path)
  23. }
  24. }
  25. medialibrary.reload()
  26. }
  27. catch let error as NSError {
  28. assertionFailure("MediaModel: Delete failed: \(error.localizedDescription)")
  29. }
  30. }
  31. }
  32. // MARK: - Helpers
  33. extension VLCMLMedia {
  34. static func == (lhs: VLCMLMedia, rhs: VLCMLMedia) -> Bool {
  35. return lhs.identifier() == rhs.identifier()
  36. }
  37. }
  38. // MARK: - ViewModel
  39. extension VLCMLMedia {
  40. @objc func mediaDuration() -> String {
  41. return String(format: "%@", VLCTime(int: Int32(duration())))
  42. }
  43. @objc func formatSize() -> String {
  44. return ByteCountFormatter.string(fromByteCount: Int64(mainFile()?.size() ?? 0),
  45. countStyle: .file)
  46. }
  47. }
  48. // MARK: - CoreSpotlight
  49. extension VLCMLMedia {
  50. func coreSpotlightAttributeSet() -> CSSearchableItemAttributeSet {
  51. let attributeSet = CSSearchableItemAttributeSet(itemContentType: "public.audiovisual-content")
  52. attributeSet.title = title
  53. attributeSet.metadataModificationDate = Date()
  54. attributeSet.addedDate = Date()
  55. attributeSet.duration = NSNumber(value: duration() / 1000)
  56. attributeSet.streamable = 0
  57. attributeSet.deliveryType = 0
  58. attributeSet.local = 1
  59. attributeSet.playCount = NSNumber(value: playCount())
  60. if isThumbnailGenerated() {
  61. attributeSet.thumbnailData = UIImage(contentsOfFile: thumbnail.path)?.jpegData(compressionQuality: 0.9)
  62. }
  63. attributeSet.codecs = codecs()
  64. attributeSet.languages = languages()
  65. if let audioTracks = audioTracks {
  66. for track in audioTracks {
  67. attributeSet.audioBitRate = NSNumber(value: track.bitrate())
  68. attributeSet.audioChannelCount = NSNumber(value: track.nbChannels())
  69. attributeSet.audioSampleRate = NSNumber(value: track.sampleRate())
  70. }
  71. }
  72. if let albumTrack = albumTrack {
  73. if let genre = albumTrack.genre {
  74. attributeSet.genre = genre.name
  75. }
  76. if let artist = albumTrack.artist {
  77. attributeSet.artist = artist.name
  78. }
  79. attributeSet.audioTrackNumber = NSNumber(value:albumTrack.trackNumber())
  80. if let album = albumTrack.album {
  81. attributeSet.artist = album.title
  82. }
  83. }
  84. return attributeSet
  85. }
  86. func codecs() -> [String] {
  87. var codecs = [String]()
  88. if let videoTracks = videoTracks {
  89. for track in videoTracks {
  90. codecs.append(track.codec)
  91. }
  92. }
  93. if let audioTracks = audioTracks {
  94. for track in audioTracks {
  95. codecs.append(track.codec)
  96. }
  97. }
  98. if let subtitleTracks = subtitleTracks {
  99. for track in subtitleTracks {
  100. codecs.append(track.codec)
  101. }
  102. }
  103. return codecs
  104. }
  105. func languages() -> [String] {
  106. var languages = [String]()
  107. if let videoTracks = videoTracks {
  108. for track in videoTracks where track.language != "" {
  109. languages.append(track.language)
  110. }
  111. }
  112. if let audioTracks = audioTracks {
  113. for track in audioTracks where track.language != "" {
  114. languages.append(track.language)
  115. }
  116. }
  117. if let subtitleTracks = subtitleTracks {
  118. for track in subtitleTracks where track.language != "" {
  119. languages.append(track.language)
  120. }
  121. }
  122. return languages
  123. }
  124. func updateCoreSpotlightEntry() {
  125. if !KeychainCoordinator.passcodeLockEnabled {
  126. let groupIdentifier = ProcessInfo.processInfo.environment["GROUP_IDENTIFIER"]
  127. let item = CSSearchableItem(uniqueIdentifier: "\(identifier())", domainIdentifier: groupIdentifier, attributeSet: coreSpotlightAttributeSet())
  128. CSSearchableIndex.default().indexSearchableItems([item], completionHandler: nil)
  129. }
  130. }
  131. }