commit 54fdcd1c84b3c79cc01539eb3bd688f776a0db95
parent 5e0ff1a6a02540c4d7b1453e01c7975b071ae4c4
Author: William Casarin <jb55@jb55.com>
Date: Mon, 27 Mar 2023 11:30:41 -0400
Small refactor for video uploader
Diffstat:
3 files changed, 29 insertions(+), 12 deletions(-)
diff --git a/damus/Models/ImageUploadModel.swift b/damus/Models/ImageUploadModel.swift
@@ -9,10 +9,23 @@ import Foundation
import UIKit
+enum MediaUpload {
+ case image(UIImage)
+ case video(URL)
+
+ var is_image: Bool {
+ guard case .image = self else {
+ return true
+ }
+
+ return false
+ }
+}
+
class ImageUploadModel: NSObject, URLSessionTaskDelegate, ObservableObject {
@Published var progress: Double? = nil
- func start(media: Any, uploader: MediaUploader) async -> ImageUploadResult {
+ func start(media: MediaUpload, uploader: MediaUploader) async -> ImageUploadResult {
let res = await create_upload_request(mediaToUpload: media, mediaUploader: uploader, progress: self)
DispatchQueue.main.async {
self.progress = nil
diff --git a/damus/Views/AttachMediaUtility.swift b/damus/Views/AttachMediaUtility.swift
@@ -29,7 +29,7 @@ fileprivate func create_upload_body(mediaData: Data, boundary: String, mediaUplo
return body as Data
}
-func create_upload_request(mediaToUpload: Any, mediaUploader: MediaUploader, progress: URLSessionTaskDelegate) async -> ImageUploadResult {
+func create_upload_request(mediaToUpload: MediaUpload, mediaUploader: MediaUploader, progress: URLSessionTaskDelegate) async -> ImageUploadResult {
var mediaIsImage: Bool = false
var mediaData: Data?
guard let url = URL(string: mediaUploader.postAPI) else {
@@ -40,15 +40,19 @@ func create_upload_request(mediaToUpload: Any, mediaUploader: MediaUploader, pro
request.httpMethod = "POST";
let boundary = "Boundary-\(UUID().description)"
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
-
- if let imageToUpload = mediaToUpload as? UIImage {
- mediaData = imageToUpload.jpegData(compressionQuality: 0.8)
- mediaIsImage = true
- } else if let videoToUpload = mediaToUpload as? URL {
- mediaData = try? Data(contentsOf: videoToUpload)
+
+ switch mediaToUpload {
+ case .image(let img):
+ mediaData = img.jpegData(compressionQuality: 0.8)
+ case .video(let url):
+ do {
+ mediaData = try Data(contentsOf: url)
+ } catch {
+ return .failed(error)
+ }
}
- guard let mediaData = mediaData else {
+ guard let mediaData else {
return .failed(nil)
}
diff --git a/damus/Views/PostView.swift b/damus/Views/PostView.swift
@@ -168,7 +168,7 @@ struct PostView: View {
post = combinedAttributedString
}
- func handle_upload(media: Any) {
+ func handle_upload(media: MediaUpload) {
let uploader = get_media_uploader(damus_state.pubkey)
Task.init {
@@ -216,9 +216,9 @@ struct PostView: View {
.padding()
.sheet(isPresented: $attach_media) {
ImagePicker(sourceType: .photoLibrary, damusState: damus_state) { img in
- handle_upload(media: img)
+ handle_upload(media: .image(img))
} onVideoPicked: { url in
- handle_upload(media: url)
+ handle_upload(media: .video(url))
}
}
.onAppear() {