2025-03-10 01:57:37 +01:00
|
|
|
import Hummingbird
|
|
|
|
|
2025-03-12 23:43:19 +01:00
|
|
|
/// A controller type that provides information about the *DocC* archives containers.
|
2025-03-10 01:57:37 +01:00
|
|
|
struct ArchiveController<Context: RequestContext> {
|
|
|
|
|
2025-03-12 08:32:12 +01:00
|
|
|
// MARK: Properties
|
|
|
|
|
2025-03-26 00:43:05 +01:00
|
|
|
private let archivesPath: String
|
2025-03-12 08:32:12 +01:00
|
|
|
private let fileService: any FileServicing
|
|
|
|
|
|
|
|
// MARK: Initialisers
|
|
|
|
|
|
|
|
/// Initialises this controller.
|
|
|
|
/// - Parameters:
|
2025-03-26 00:43:05 +01:00
|
|
|
/// - archivesPath: A path in the local file system where the *DocC* archive contained are located.
|
2025-03-12 23:43:19 +01:00
|
|
|
/// - fileService: A service that interfaces with the local file system.
|
2025-03-12 08:32:12 +01:00
|
|
|
init(
|
2025-03-26 00:43:05 +01:00
|
|
|
_ archivesPath: String,
|
2025-03-12 08:32:12 +01:00
|
|
|
fileService: any FileServicing = FileService()
|
|
|
|
) {
|
2025-03-26 00:43:05 +01:00
|
|
|
self.archivesPath = archivesPath
|
2025-03-12 08:32:12 +01:00
|
|
|
self.fileService = fileService
|
|
|
|
}
|
|
|
|
|
2025-03-10 01:57:37 +01:00
|
|
|
// MARK: Functions
|
|
|
|
|
2025-03-12 08:32:12 +01:00
|
|
|
/// Registers the controller to a given router.
|
|
|
|
/// - Parameter router: A router to register this controller to.
|
2025-03-10 01:57:37 +01:00
|
|
|
func register(to router: Router<Context>) {
|
2025-03-12 23:43:19 +01:00
|
|
|
router.get(.archives, use: listArchives)
|
2025-03-10 01:57:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Helpers
|
|
|
|
|
|
|
|
private extension ArchiveController {
|
|
|
|
|
|
|
|
// MARK: Functions
|
|
|
|
|
2025-03-12 23:43:19 +01:00
|
|
|
@Sendable func listArchives(
|
2025-03-10 01:57:37 +01:00
|
|
|
_ request: Request,
|
|
|
|
context: Context
|
2025-03-12 23:43:19 +01:00
|
|
|
) async throws (HTTPError) -> ArchiveList {
|
|
|
|
do {
|
|
|
|
let nameArchives = try await fileService
|
2025-03-26 00:43:05 +01:00
|
|
|
.listItems(in: archivesPath)
|
2025-03-12 23:43:19 +01:00
|
|
|
.filter { $0.hasSuffix(.suffixArchive) }
|
|
|
|
.map { $0.dropLast(String.suffixArchive.count) }
|
|
|
|
.map(String.init)
|
|
|
|
.sorted { $0 < $1 }
|
|
|
|
|
|
|
|
return .init(nameArchives)
|
|
|
|
} catch .folderNotFound {
|
|
|
|
throw .init(.notFound)
|
|
|
|
} catch .folderPathEmpty, .folderNotDirectory {
|
|
|
|
throw .init(.unprocessableContent)
|
|
|
|
} catch {
|
|
|
|
throw .init(.badRequest)
|
|
|
|
}
|
2025-03-10 01:57:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2025-03-12 08:32:12 +01:00
|
|
|
|
|
|
|
// MARK: - RouterPath+Constants
|
|
|
|
|
|
|
|
private extension RouterPath {
|
|
|
|
static let archives: RouterPath = .init("archives")
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - String+Constants
|
|
|
|
|
|
|
|
private extension String {
|
|
|
|
static let suffixArchive: String = ".doccarchive"
|
|
|
|
}
|