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

Fix/remove photo #159

Merged
merged 3 commits into from
Sep 11, 2024
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
2 changes: 1 addition & 1 deletion Kabinett/Application/KabinettApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
31 changes: 31 additions & 0 deletions Kabinett/Service/Letter/FirestorageLetterManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
}
10 changes: 8 additions & 2 deletions Kabinett/Service/Letter/FirestoreLetterManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Loading