diff --git a/Kabinett/Application/KabinettApp.swift b/Kabinett/Application/KabinettApp.swift index b52aaa10..1b0a1d0b 100644 --- a/Kabinett/Application/KabinettApp.swift +++ b/Kabinett/Application/KabinettApp.swift @@ -60,8 +60,8 @@ struct KabinettApp: App { let writerManager = FirestoreWriterManager() let writerStorageManager = FirestorageWriterManager() let authManager = AuthManager(writerManager: writerManager) - let letterManager = FirestoreLetterManager() let letterStorageManager = FirestorageLetterManager() + let letterManager = FirestoreLetterManager(storageManager: letterStorageManager) // MARK: - UseCase Dependencies let profileUseCase = DefaultProfileUseCase( diff --git a/Kabinett/Service/Letter/FirestorageLetterManager.swift b/Kabinett/Service/Letter/FirestorageLetterManager.swift index a119e077..7addae1f 100644 --- a/Kabinett/Service/Letter/FirestorageLetterManager.swift +++ b/Kabinett/Service/Letter/FirestorageLetterManager.swift @@ -7,9 +7,18 @@ import Foundation import FirebaseStorage +import os final class FirestorageLetterManager { let storage = Storage.storage() + private let logger: Logger + + init() { + self.logger = Logger( + subsystem: "co.kr.codegrove.Kabinett", + category: "FirestorageLetterManager" + ) + } func convertPhotoToUrl(photoContents: [Data]) async throws -> [String] { let storageRef = storage.reference() @@ -51,4 +60,26 @@ final class FirestorageLetterManager { return .failure(error) } } + + func deleteData(urls: [String], path: String) async throws { + for url in urls { + guard let fileName = extractFileName(from: url) else { + logger.error("Failed to extract FileName: \(url)") + throw NSError(domain: "DeleteDataError", code: -1, userInfo: [NSLocalizedDescriptionKey: "Failed to extract file name for URL: \(url)"]) + } + + let storageRef = storage.reference().child("\(path)/\(fileName)") + try await storageRef.delete() + } + } + + private func extractFileName(from url: String) -> String? { + guard let urlComponents = URLComponents(string: url), + let filePath = urlComponents.path.components(separatedBy: "/").last, + let decodedFilePath = filePath.removingPercentEncoding else { + return nil + } + + return decodedFilePath.components(separatedBy: "?").first ?? decodedFilePath + } } diff --git a/Kabinett/Service/Letter/FirestoreLetterManager.swift b/Kabinett/Service/Letter/FirestoreLetterManager.swift index ac2d9dfd..b9338811 100644 --- a/Kabinett/Service/Letter/FirestoreLetterManager.swift +++ b/Kabinett/Service/Letter/FirestoreLetterManager.swift @@ -32,13 +32,14 @@ final class FirestoreLetterManager { private let logger: Logger private let db = Firestore.firestore() + private let storageManager: FirestorageLetterManager - init( - ) { + init(storageManager: FirestorageLetterManager) { self.logger = Logger( subsystem: "co.kr.codegrove.Kabinett", category: "FirebaseFirestoreManager" ) + self.storageManager = storageManager } actor SafeListeners { @@ -308,6 +309,11 @@ final class FirestoreLetterManager { if documentSnapshot.exists { try await validateLetter(userId: userId, letterId: letterId, letterType: type) + + if let photoUrls = documentSnapshot.data()?["photoContents"] as? [String] { + try await storageManager.deleteData(urls: photoUrls, path: "Users/photoContents") + } + try await documentRef.delete() removeSucceeded = true } else {