-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// DomainError.swift | ||
// Domain | ||
// | ||
// Created by 이동현 on 11/18/24. | ||
// | ||
|
||
import Foundation | ||
|
||
public enum DomainError { | ||
case cannotWriteFile | ||
case cannotCreateDirectory | ||
case cannotFindDirectory | ||
} | ||
|
||
extension DomainError: LocalizedError { | ||
public var errorDescription: String? { | ||
switch self { | ||
case .cannotWriteFile: | ||
return "파일을 쓸 수 없습니다." | ||
case .cannotCreateDirectory: | ||
return "디렉터리를 생성할 수 없습니다." | ||
case .cannotFindDirectory: | ||
return "디렉터리를 찾을 수 없습니다." | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// AirplaINImageData.swift | ||
// Domain | ||
// | ||
// Created by 이동현 on 11/18/24. | ||
// | ||
|
||
import Foundation | ||
|
||
public struct AirplaINImageData { | ||
let width: CGFloat | ||
let height: CGFloat | ||
let imageData: Data | ||
|
||
public init( | ||
width: CGFloat, | ||
height: CGFloat, | ||
imageData: Data | ||
) { | ||
self.width = width | ||
self.height = height | ||
self.imageData = imageData | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
Domain/Domain/Sources/Interface/UseCase/AddPhotoUseCaseInterface.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// AddPhotoUseCaseInterface.swift | ||
// Domain | ||
// | ||
// Created by 이동현 on 11/18/24. | ||
// | ||
|
||
import Foundation | ||
|
||
public protocol AddPhotoUseCaseInterface { | ||
|
||
/// 사진 오브젝트를 추가합니다. | ||
/// - Parameters: | ||
/// - imageData: 추가할 사진의 데이터 | ||
/// - position: 사진을 추가할 위치 (origin) | ||
/// - size: 사진 객체 | ||
/// - Returns: | ||
func addPhoto( | ||
airplainImageData: AirplaINImageData, | ||
position: CGPoint | ||
) throws -> PhotoObject | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// AddPhotoUseCase.swift | ||
// Domain | ||
// | ||
// Created by 이동현 on 11/18/24. | ||
// | ||
|
||
import Foundation | ||
|
||
public final class AddPhotoUseCase: AddPhotoUseCaseInterface { | ||
private let fileManager: FileManager | ||
private let photoDirectory: URL | ||
|
||
public init(fileManager: FileManager) throws { | ||
self.fileManager = fileManager | ||
guard | ||
let documentDirectory = fileManager | ||
.urls(for: .documentDirectory, in: .userDomainMask) | ||
.first | ||
else { throw DomainError.cannotCreateDirectory } | ||
photoDirectory = documentDirectory.appending(path: "photos") | ||
|
||
if !fileManager.fileExists(atPath: photoDirectory.path()) { | ||
do { | ||
try fileManager.createDirectory(at: photoDirectory, withIntermediateDirectories: true) | ||
} catch { | ||
throw DomainError.cannotCreateDirectory | ||
} | ||
} | ||
} | ||
|
||
public func addPhoto( | ||
airplainImageData: AirplaINImageData, | ||
position: CGPoint | ||
) throws -> PhotoObject { | ||
let uuid = UUID() | ||
let photoname = "\(uuid.uuidString).jpg" | ||
let photoURL = photoDirectory.appending(path: photoname) | ||
let imageSize = CGSize(width: airplainImageData.width, height: airplainImageData.height) | ||
|
||
do { | ||
try airplainImageData.imageData.write(to: photoURL) | ||
} catch { | ||
throw DomainError.cannotWriteFile | ||
} | ||
|
||
let photoObject = PhotoObject( | ||
id: uuid, | ||
position: position, | ||
size: imageSize, | ||
photoURL: photoURL) | ||
|
||
return photoObject | ||
} | ||
} |