Files
GitHawk/Classes/Models/FilePath.swift
Ivan Magda 6c3c451677 Add PDF support (#1596)
* Add string extension for searching pdf files (#1552)

* Create RepositoryWebViewController (#1552)

* Initial presenting of the pdf files (#1552)

* refactor: Rename String+Resource -> String+BinaryFile (#1552)

* Build destination URL (#1552)

* Configure title (#1552)

* Encode file path

* Use EmptyView for errors

* Replace UIWebView with WKWebView

* Review fixes

* refactor: Make binarySuffix computed property

* Unify if-else path

* Group guard statements
2018-03-11 15:59:51 -04:00

81 lines
1.7 KiB
Swift

//
// FilePath.swift
// Freetime
//
// Created by Ryan Nystrom on 12/3/17.
// Copyright © 2017 Ryan Nystrom. All rights reserved.
//
import Foundation
struct FilePath {
static private let joiner = "/"
let components: [String]
var baseComponents: [String]? {
let count = components.count
guard count > 1 else { return nil }
return Array(components[0..<count-1])
}
var path: String {
return components.joined(separator: FilePath.joiner)
}
var basePath: String? {
return baseComponents?.joined(separator: FilePath.joiner)
}
var current: String? {
return components.last
}
var fileExtension: String? {
let components = current?.components(separatedBy: ".") ?? []
if components.count > 1 {
return components.last
} else {
return nil
}
}
func appending(_ component: String) -> FilePath {
return FilePath(components: components + [component])
}
}
// MARK: - FilePath (BinaryFile) -
extension FilePath {
private static let supportedBinaries = [
"pdf": "application/pdf"
]
// MARK: Public API
/// A Boolean value indicating whether a string has binary file suffix.
///
/// Supported types: **pdf**.
var hasBinarySuffix: Bool {
return binarySuffix != nil
}
/// Returns mime type for the supported binary files.
var mimeType: String? {
guard let type = binarySuffix else { return nil }
return FilePath.supportedBinaries[type]
}
// MARK: Private API
private var binarySuffix: String? {
return FilePath.supportedBinaries.keys.first(where: { path.hasSuffix($0) })
}
}