Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Gutenberg] Audio upload processor #15420

Merged
merged 7 commits into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 var name = "wp:audio"
etoledom marked this conversation as resolved.
Show resolved Hide resolved
static var id = "id"
etoledom marked this conversation as resolved.
Show resolved Hide resolved
static var src = "src"
etoledom marked this conversation as resolved.
Show resolved Hide resolved
}

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 @@ -315,6 +315,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 @@ -2783,6 +2784,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 @@ -10960,6 +10962,7 @@
children = (
FF2EC3BF2209A144006176E1 /* GutenbergImgUploadProcessor.swift */,
1E0462152566938300EB98EF /* GutenbergFileUploadProcessor.swift */,
1E672D94257663CE00421F13 /* GutenbergAudioUploadProcessor.swift */,
FF1B11E4238FDFE70038B93E /* GutenbergGalleryUploadProcessor.swift */,
91138454228373EB00FB02B7 /* GutenbergVideoUploadProcessor.swift */,
4629E4202440C5B20002E15C /* GutenbergCoverUploadProcessor.swift */,
Expand Down Expand Up @@ -12876,6 +12879,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