Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ const editor = window.editor = {
} else if (file.mediaEntryID) {
img.src = videoPreviewURL
img.dataset.media_comment_id = file.mediaEntryID
img.dataset.media_attachment_id = file.mediaAttachmentID
delete img.dataset.uploading
overlay.progressSVG.classList.add('is-hidden')
overlay.removeButton.classList.remove('is-hidden')
Expand Down Expand Up @@ -176,11 +177,8 @@ const editor = window.editor = {
}
for (let img of clone.querySelectorAll('[data-media_comment_id]')) {
let mediaID = img.dataset.media_comment_id
if (editor.featureFlags.includes('rce_enhancements')) {
img.outerHTML = `<div id="media_object_${mediaID}" style="width: 768px; height: 432px;"><iframe src="/media_objects_iframe/${mediaID}" width="100%" height="100%"></iframe></div>`
} else {
img.outerHTML = `<a id="media_comment_${mediaID}" class="instructure_inline_media_comment video_comment" href="/media_objects/${mediaID}">this is a media comment</a>`
}
let attachmentID = img.dataset.media_attachment_id
img.outerHTML = `<div id="media_object_${mediaID}" style="width: 768px; height: 432px;"><iframe src="/media_attachments_iframe/${attachmentID}" width="100%" height="100%"></iframe></div>`
}
let html = clone.innerHTML
// backspaces can leave behind empty line breaks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,18 +397,19 @@ extension RichContentEditorViewController: UIImagePickerControllerDelegate, UINa
let string = CoreWebView.jsString(url.absoluteString)
webView.evaluateJavaScript("editor.insertVideoPlaceholder(\(string))")
}
UploadMedia(type: .video, url: url, file: file, context: context).fetch { [weak self] mediaID, error in
self?.updateFile(file, error: error, mediaID: mediaID)
UploadMedia(type: .video, url: url, file: file, context: context).fetch { [weak self] media, error in
self?.updateFile(file, error: error, media: media)
}
}

private func updateFile(_ file: File, error: Error?, mediaID: String? = nil) {
private func updateFile(_ file: File, error: Error?, media: MediaEntry? = nil) {
let context = env.uploadManager.viewContext
context.performAndWait { [weak self] in
do {
guard let file = try? context.existingObject(with: file.objectID) as? File else { return }
file.uploadError = error?.localizedDescription ?? file.uploadError
file.mediaEntryID = mediaID
file.mediaEntryID = media?.mediaID
file.mediaAttachmentID = media?.attachmentID
try context.save()
} catch {
self?.showError(error)
Expand All @@ -421,6 +422,7 @@ extension RichContentEditorViewController: UIImagePickerControllerDelegate, UINa
"localFileURL": file.localFileURL?.absoluteString,
"url": file.url?.absoluteString,
"mediaEntryID": file.mediaEntryID,
"mediaAttachmentID": file.mediaAttachmentID,
"uploadError": file.uploadError,
"uploadErrorTitle": String(localized: "Failed Upload", bundle: .core),
"bytesSent": file.bytesSent,
Expand Down
1 change: 1 addition & 0 deletions Core/Core/Features/AudioVideo/APIMediaComment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ struct PostCompleteMediaUploadRequest: APIRequestable {
struct Response: Codable {
struct MediaObject: Codable {
let media_id: String
let attachment_id: String?
}
let media_object: MediaObject
}
Expand Down
19 changes: 15 additions & 4 deletions Core/Core/Features/AudioVideo/UploadMedia.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class UploadMedia: NSObject, URLSessionDelegate, URLSessionDataDelegate {
let database = UploadManager.shared.database
var mediaAPI: API?
var task: APITask?
var callback: (String?, Error?) -> Void = { _, _ in }
var callback: (MediaEntry?, Error?) -> Void = { _, _ in }
let file: File?
let url: URL
let type: MediaCommentType
Expand All @@ -45,7 +45,7 @@ public class UploadMedia: NSObject, URLSessionDelegate, URLSessionDataDelegate {
}
}

public func fetch(_ callback: @escaping (String?, Error?) -> Void) {
public func fetch(_ callback: @escaping (MediaEntry?, Error?) -> Void) {
self.callback = callback
upload()
}
Expand Down Expand Up @@ -121,11 +121,22 @@ public class UploadMedia: NSObject, URLSessionDelegate, URLSessionDataDelegate {

func completeUpload(mediaID: String) {
guard let context = context else {
return self.callback(mediaID, nil)
return self.callback(MediaEntry(mediaID: mediaID, attachmentID: nil), nil)
}
let request = PostCompleteMediaUploadRequest(mediaID: mediaID, context: context, type: type)
task = env.api.makeRequest(request) { response, _, error in
self.callback(response?.media_object.media_id, error)
let entry = response.flatMap({
MediaEntry(
mediaID: $0.media_object.media_id,
attachmentID: $0.media_object.attachment_id
)
})
self.callback(entry, error)
}
}
}

public struct MediaEntry {
public let mediaID: String
public let attachmentID: String?
}
4 changes: 2 additions & 2 deletions Core/Core/Features/AudioVideo/UploadMediaComment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ public class UploadMediaComment {
try client.save()
self.placeholderID = placeholder.id
UploadMediaComment.placeholderSuffix += 1
self.uploader.fetch { mediaID, error in
guard error == nil, let mediaID = mediaID else {
self.uploader.fetch { entry, error in
guard error == nil, let mediaID = entry?.mediaID else {
self.callback(nil, error)
return
}
Expand Down
1 change: 1 addition & 0 deletions Core/Core/Features/Files/Model/Entities/File.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ final public class File: NSManagedObject {
@NSManaged public var modifiedAt: Date?
@NSManaged public var mimeClass: String?
@NSManaged public var mediaEntryID: String?
@NSManaged public var mediaAttachmentID: String?
@NSManaged public var lockedForUser: Bool
@NSManaged public var lockExplanation: String?
@NSManaged public var previewURL: URL?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,7 @@
<attribute name="locked" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
<attribute name="lockedForUser" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
<attribute name="lockExplanation" optional="YES" attributeType="String"/>
<attribute name="mediaAttachmentID" optional="YES" attributeType="String"/>
<attribute name="mediaEntryID" optional="YES" attributeType="String"/>
<attribute name="mimeClass" optional="YES" attributeType="String"/>
<attribute name="modifiedAt" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class RichContentEditorViewControllerTests: CoreTestCase, RichContentEditorDeleg

override func setUp() {
super.setUp()
api.mock(GetEnabledFeatureFlagsRequest(context: context), value: ["rce_enhancements"])
controller = RichContentEditorViewController
.create(env: environment, context: context, uploadTo: .myFiles)
controller.delegate = self
Expand Down
24 changes: 13 additions & 11 deletions Core/CoreTests/Features/AudioVideo/UploadMediaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,20 @@ class UploadMediaTests: CoreTestCase {
func testCompleteUploadSuccess() {
let context = Context(.course, id: "1")
let expectation = XCTestExpectation(description: "callback was called")
let response = PostCompleteMediaUploadRequest.Response(media_object: .init(media_id: "10"))
let response = PostCompleteMediaUploadRequest.Response(media_object: .init(media_id: "10", attachment_id: "34"))
api.mock(PostCompleteMediaUploadRequest(mediaID: "1", context: context, type: .audio), value: response)
var error: Error?
var mediaID: String?
var entry: MediaEntry?
let upload = UploadMedia(type: .audio, url: URL(string: "data:text/plain,abcde")!, context: context)
upload.callback = {
mediaID = $0
entry = $0
error = $1
expectation.fulfill()
}
upload.completeUpload(mediaID: "10")
wait(for: [expectation], timeout: 1)
XCTAssertEqual(mediaID, "10")
XCTAssertEqual(entry?.mediaID, "10")
XCTAssertEqual(entry?.attachmentID, "34")
XCTAssertNil(error)
}

Expand All @@ -107,35 +108,36 @@ class UploadMediaTests: CoreTestCase {
let expectation = XCTestExpectation(description: "callback was called")
api.mock(PostCompleteMediaUploadRequest(mediaID: "1", context: context, type: .audio), error: NSError.internalError())
var error: Error?
var mediaID: String?
var entry: MediaEntry?
let upload = UploadMedia(type: .audio, url: URL(string: "data:text/plain,abcde")!, context: context)
upload.callback = {
mediaID = $0
entry = $0
error = $1
expectation.fulfill()
}
upload.completeUpload(mediaID: "10")
wait(for: [expectation], timeout: 1)
XCTAssertNil(mediaID)
XCTAssertNil(entry)
XCTAssertNotNil(error)
}

func testCompleteUploadNoContext() {
let context = Context(.course, id: "1")
let expectation = XCTestExpectation(description: "callback was called")
let response = PostCompleteMediaUploadRequest.Response(media_object: .init(media_id: "1"))
let response = PostCompleteMediaUploadRequest.Response(media_object: .init(media_id: "1", attachment_id: "23"))
api.mock(PostCompleteMediaUploadRequest(mediaID: "1", context: context, type: .audio), value: response)
var error: Error?
var mediaID: String?
var entry: MediaEntry?
let upload = UploadMedia(type: .audio, url: URL(string: "data:text/plain,abcde")!, context: nil)
upload.callback = {
mediaID = $0
entry = $0
error = $1
expectation.fulfill()
}
upload.completeUpload(mediaID: "2")
wait(for: [expectation], timeout: 1)
XCTAssertEqual(mediaID, "2")
XCTAssertEqual(entry?.mediaID, "2")
XCTAssertNil(entry?.attachmentID)
XCTAssertNil(error)
}
}
1 change: 0 additions & 1 deletion Core/TestsFoundation/UITestCases/CoreUITestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,6 @@ open class CoreUITestCase: XCTestCase {
]), value: course)
mockData(GetContextPermissionsRequest(context: .course(course.id.value)), value: .make())
mockData(GetEnabledFeatureFlagsRequest(context: .course(course.id.value)), value: [
"rce_enhancements",
"new_gradebook",
"assignment_attempts"
])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,14 +327,14 @@ extension SubmissionButtonPresenter {
reportSuccess()
} }
}
let createSubmission = { (mediaID: String?, error: Error?) in
let createSubmission = { (media: MediaEntry?, error: Error?) in
guard error == nil else { return doneUploading(error) }
CreateSubmission(
context: .course(assignment.courseID),
assignmentID: assignment.id,
userID: userID,
submissionType: .media_recording,
mediaCommentID: mediaID,
mediaCommentID: media?.mediaID,
mediaCommentType: type,
mediaCommentSource: source
).fetch(environment: env) { _, _, error in doneUploading(error) }
Expand Down
Loading