I am trying to upload my imageUrl to firestore but its being uploaded to firebase with a extra field videoUrl being attached to it but I am only uploading a image and within my imageUploader only a Uiimage is classified not a video so why is my imageUrl post being uploaded with a extra field as the videoUrl and I didn’t upload a video with the image post nor is it written that way in my code. What is wrong with my code and how do I rewrite this code to upload my image to firestore without the videoUrl field
class UploadPostViewModel: NSObject, ObservableObject {
@Published var didUploadPost = false
@Published var isLoading = false
@Published var error: Error?
@Published var videos = [Video]()
@Published var mediaPreview: Movie?
@Published var profileImage: Image?
@Published var text = ""
@Published var selectedImage: PhotosPickerItem? {
didSet {
Task { await loadImage(fromItem: selectedImage) }
Task { try await uploadVideo() }
}
}
private var uiImage: UIImage?
func uploadPost(caption: String) async throws {
guard let uid = Auth.auth().currentUser?.uid else { return }
var videoUrl: String? = nil
let items = selectedImage
if let videoData = try await items?.loadTransferable(type: Data.self) {
videoUrl = (try await VideoUploader.uploadVideo(withData: videoData))!
}
var imageUrl: String? = nil
if let image = uiImage {
imageUrl = try await ImageUploader.uploadImage(image: image, type: .post)
let post = Post(
ownerUid: uid,
text: text,
videoUrl: videoUrl, likes: 0,
replyCount: 23,
imageUrl: imageUrl, timestamp: Timestamp()
)
try await PostService.uploadPost(post)
self.didUploadPost = true
}
}
func loadImage(fromItem item: PhotosPickerItem?) async {
guard let item = item else { return }
guard let data = try? await item.loadTransferable(type: Data.self) else { return }
guard let uiImage = UIImage(data: data) else { return }
self.uiImage = uiImage
self.profileImage = Image(uiImage: uiImage)
}
func uploadVideo() async throws {
guard let items = selectedImage else { return }
guard let videoData = try await items.loadTransferable(type: Data.self) else { return }
guard let videoUrl = try await VideoUploader.uploadVideo(withData: videoData) else { return }
try await Firestore.firestore().collection("videos").document().setData(["videoUrl": videoUrl])
}
}
import Foundation
import Firebase
import FirebaseStorage
enum UploadType {
case profile
case thread
case message
case post
var filePath: StorageReference {
let filename = NSUUID().uuidString
switch self {
case .profile:
return Storage.storage().reference(withPath: "/profile_images/\(filename)")
case .thread:
return Storage.storage().reference(withPath: "/post_images/\(filename)")
case .message:
return Storage.storage().reference(withPath: "/message_images/\(filename)")
case .post:
return Storage.storage().reference(withPath: "/post_images/\(filename)")
}
}
}
struct ImageUploader {
static func uploadImage(image: UIImage, type: UploadType) async throws -> String? {
guard let imageData = image.jpegData(compressionQuality: 0.5) else { return nil }
let ref = type.filePath
do {
let _ = try await ref.putDataAsync(imageData)
let url = try await ref.downloadURL()
return url.absoluteString
} catch {
print("DEBUG: Failed to upload image \(error.localizedDescription)")
return nil
}
}
}




