Skip to content

Commit

Permalink
Merge pull request #15420 from wordpress-mobile/gutenberg/audio-block…
Browse files Browse the repository at this point in the history
…-processor

[Gutenberg] Audio upload processor
  • Loading branch information
etoledom authored Dec 7, 2020
2 parents b0fd5d0 + 8005bc5 commit 5a3d37e
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 6 deletions.
3 changes: 3 additions & 0 deletions WordPress/Classes/Services/PostCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ class PostCoordinator: NSObject {

let videoPostUploadProcessor = VideoUploadProcessor(mediaUploadID: mediaUploadID, remoteURLString: remoteURLStr, videoPressID: media.videopressGUID)
aztecProcessors.append(videoPostUploadProcessor)
} else if media.mediaType == .audio {
let gutenbergAudioProcessor = GutenbergAudioUploadProcessor(mediaUploadID: gutenbergMediaUploadID, serverMediaID: mediaID, remoteURLString: remoteURLStr)
gutenbergProcessors.append(gutenbergAudioProcessor)
} else if let remoteURL = URL(string: remoteURLStr) {
let documentTitle = remoteURL.lastPathComponent
let documentUploadProcessor = DocumentUploadProcessor(mediaUploadID: mediaUploadID, remoteURLString: remoteURLStr, title: documentTitle)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import Foundation
import Aztec

class GutenbergAudioUploadProcessor: Processor {
private struct AudioBlockKeys {
static let name = "wp:audio"
static let id = "id"
static let src = "src"
}

let mediaUploadID: Int32
let remoteURLString: String
let serverMediaID: Int

init(mediaUploadID: Int32, serverMediaID: Int, remoteURLString: String) {
self.mediaUploadID = mediaUploadID
self.serverMediaID = serverMediaID
self.remoteURLString = remoteURLString
}

lazy var fileHtmlProcessor = HTMLProcessor(for: "audio", replacer: { (audio) in
var attributes = audio.attributes

attributes.set(.string(self.remoteURLString), forKey: AudioBlockKeys.src)

var html = "<audio "
let attributeSerializer = ShortcodeAttributeSerializer()
html += attributeSerializer.serialize(attributes)
html += "></audio>"
return html
})

lazy var fileBlockProcessor = GutenbergBlockProcessor(for: AudioBlockKeys.name, replacer: { fileBlock in
guard let mediaID = fileBlock.attributes[AudioBlockKeys.id] as? Int,
mediaID == self.mediaUploadID else {
return nil
}
var block = "<!-- \(AudioBlockKeys.name) "
var attributes = fileBlock.attributes
attributes[AudioBlockKeys.id] = self.serverMediaID
if let jsonData = try? JSONSerialization.data(withJSONObject: attributes, options: .sortedKeys),
let jsonString = String(data: jsonData, encoding: .utf8) {
block += jsonString
}
block += " -->"
block += self.fileHtmlProcessor.process(fileBlock.content)
block += "<!-- /\(AudioBlockKeys.name) -->"
return block
})

func process(_ text: String) -> String {
return fileBlockProcessor.process(text)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class GutenbergFilesAppMediaSource: NSObject {
}

func presentPicker(origin: UIViewController, filters: [Gutenberg.MediaType], allowedTypesOnBlog: [String], multipleSelection: Bool, callback: @escaping MediaPickerDidPickMediaCallback) {

let uttypeFilters = filters.contains(.any) ? allowedTypesOnBlog : filters.compactMap { $0.typeIdentifier }
let uttypeFilters = filters.contains(.any) ? allowedTypesOnBlog : allTypesFrom(allowedTypesOnBlog, conformingTo: filters)

mediaPickerCallback = callback
let docPicker = UIDocumentPickerViewController(documentTypes: uttypeFilters, in: .import)
Expand All @@ -22,6 +21,10 @@ class GutenbergFilesAppMediaSource: NSObject {

origin.present(docPicker, animated: true)
}

private func allTypesFrom(_ allTypes: [String], conformingTo filters: [Gutenberg.MediaType]) -> [String] {
return filters.map { $0.filterTypesConformingTo(allTypes: allTypes) }.reduce([], +)
}
}

extension GutenbergFilesAppMediaSource: UIDocumentPickerDelegate {
Expand Down Expand Up @@ -59,14 +62,25 @@ extension GutenbergFilesAppMediaSource: UIDocumentPickerDelegate {
}

extension Gutenberg.MediaType {
var typeIdentifier: String? {
func filterTypesConformingTo(allTypes: [String]) -> [String] {
guard let uttype = typeIdentifier else {
return []
}
return getTypesFrom(allTypes, conformingTo: uttype)
}

private func getTypesFrom(_ allTypes: [String], conformingTo uttype: CFString) -> [String] {
return allTypes.filter { UTTypeConformsTo($0 as CFString, uttype) }
}

private var typeIdentifier: CFString? {
switch self {
case .image:
return String(kUTTypeImage)
return kUTTypeImage
case .video:
return String(kUTTypeMovie)
return kUTTypeMovie
case .audio:
return String(kUTTypeAudio)
return kUTTypeAudio
case .other, .any: // needs to be specified by the blog's allowed types.
return nil
}
Expand Down
4 changes: 4 additions & 0 deletions WordPress/WordPress.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@
1E4F2E712458AF8500EB73E7 /* GutenbergWebNavigationViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E4F2E702458AF8500EB73E7 /* GutenbergWebNavigationViewController.swift */; };
1E5D00102493CE240004B708 /* GutenGhostView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E5D000F2493CE240004B708 /* GutenGhostView.swift */; };
1E5D00142493E8C90004B708 /* GutenGhostView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 1E5D00132493E8C90004B708 /* GutenGhostView.xib */; };
1E672D95257663CE00421F13 /* GutenbergAudioUploadProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E672D94257663CE00421F13 /* GutenbergAudioUploadProcessor.swift */; };
1E9D544D23C4C56300F6A9E0 /* GutenbergRollout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E9D544C23C4C56300F6A9E0 /* GutenbergRollout.swift */; };
24ADA24C24F9A4CB001B5DAE /* RemoteFeatureFlagStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24ADA24B24F9A4CB001B5DAE /* RemoteFeatureFlagStore.swift */; };
24B1AE3124FEC79900B9F334 /* RemoteFeatureFlagTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24B1AE3024FEC79900B9F334 /* RemoteFeatureFlagTests.swift */; };
Expand Down Expand Up @@ -2770,6 +2771,7 @@
1E4F2E702458AF8500EB73E7 /* GutenbergWebNavigationViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GutenbergWebNavigationViewController.swift; sourceTree = "<group>"; };
1E5D000F2493CE240004B708 /* GutenGhostView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GutenGhostView.swift; sourceTree = "<group>"; };
1E5D00132493E8C90004B708 /* GutenGhostView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = GutenGhostView.xib; sourceTree = "<group>"; };
1E672D94257663CE00421F13 /* GutenbergAudioUploadProcessor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GutenbergAudioUploadProcessor.swift; sourceTree = "<group>"; };
1E9D544C23C4C56300F6A9E0 /* GutenbergRollout.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GutenbergRollout.swift; sourceTree = "<group>"; };
2262D835FA89938EBF63EADF /* Pods-WordPressShareExtension.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WordPressShareExtension.debug.xcconfig"; path = "../Pods/Target Support Files/Pods-WordPressShareExtension/Pods-WordPressShareExtension.debug.xcconfig"; sourceTree = "<group>"; };
24ADA24B24F9A4CB001B5DAE /* RemoteFeatureFlagStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoteFeatureFlagStore.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -10931,6 +10933,7 @@
children = (
FF2EC3BF2209A144006176E1 /* GutenbergImgUploadProcessor.swift */,
1E0462152566938300EB98EF /* GutenbergFileUploadProcessor.swift */,
1E672D94257663CE00421F13 /* GutenbergAudioUploadProcessor.swift */,
FF1B11E4238FDFE70038B93E /* GutenbergGalleryUploadProcessor.swift */,
91138454228373EB00FB02B7 /* GutenbergVideoUploadProcessor.swift */,
4629E4202440C5B20002E15C /* GutenbergCoverUploadProcessor.swift */,
Expand Down Expand Up @@ -12845,6 +12848,7 @@
17BD4A192101D31B00975AC3 /* NavigationActionHelpers.swift in Sources */,
B55FFCFA1F034F1A0070812C /* String+Ranges.swift in Sources */,
7E846FF320FD37BD00881F5A /* ActivityCommentRange.swift in Sources */,
1E672D95257663CE00421F13 /* GutenbergAudioUploadProcessor.swift in Sources */,
9AA0ADB1235F11700027AB5D /* AsyncOperation.swift in Sources */,
FF8791BB1FBAF4B500AD86E6 /* MediaService+Swift.swift in Sources */,
D8212CB920AA77AD008E8AE8 /* ReaderActionHelpers.swift in Sources */,
Expand Down

0 comments on commit 5a3d37e

Please sign in to comment.