-
Notifications
You must be signed in to change notification settings - Fork 20
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
9 changed files
with
441 additions
and
155 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 |
---|---|---|
@@ -1,5 +1,28 @@ | ||
// swift-tools-version:4.0 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "Shallows" | ||
name: "Shallows", | ||
products: [ | ||
// Products define the executables and libraries produced by a package, and make them visible to other packages. | ||
.library( | ||
name: "Shallows", | ||
targets: ["Shallows"]), | ||
], | ||
dependencies: [ | ||
// Dependencies declare other packages that this package depends on. | ||
// .package(url: /* package url */, from: "1.0.0"), | ||
], | ||
targets: [ | ||
// Targets are the basic building blocks of a package. A target can define a module or a test suite. | ||
// Targets can depend on other targets in this package, and on products in packages which this package depends on. | ||
.target( | ||
name: "Shallows", | ||
dependencies: []), | ||
.testTarget( | ||
name: "ShallowsTests", | ||
dependencies: ["Shallows"]), | ||
] | ||
) |
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 |
---|---|---|
|
@@ -5,15 +5,15 @@ Pod::Spec.new do |s| | |
s.description = <<-DESC | ||
Your description here. | ||
DESC | ||
s.homepage = "" | ||
s.homepage = "https://github.com/dreymonde/Shallows" | ||
s.license = { :type => "MIT", :file => "LICENSE" } | ||
s.author = { "Oleg Dreyman" => "[email protected]" } | ||
s.social_media_url = "" | ||
s.ios.deployment_target = "8.0" | ||
s.osx.deployment_target = "10.9" | ||
s.watchos.deployment_target = "2.0" | ||
s.tvos.deployment_target = "9.0" | ||
s.source = { :git => ".git", :tag => s.version.to_s } | ||
s.source = { :git => "https://github.com/dreymonde/Shallows.git", :tag => s.version.to_s } | ||
s.source_files = "Sources/**/*" | ||
s.frameworks = "Foundation" | ||
end |
Large diffs are not rendered by default.
Oops, something went wrong.
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,227 @@ | ||
// | ||
// DiskStorage.swift | ||
// Shallows | ||
// | ||
// Created by Олег on 18.01.2018. | ||
// Copyright © 2018 Shallows. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public struct FileURL { | ||
|
||
public let url: URL | ||
|
||
public init(_ url: URL) { | ||
self.url = url | ||
} | ||
|
||
public init(_ path: String) { | ||
self.init(URL.init(fileURLWithPath: path)) | ||
} | ||
|
||
} | ||
|
||
public struct Filename : RawRepresentable, Hashable, ExpressibleByStringLiteral { | ||
|
||
public var hashValue: Int { | ||
return rawValue.hashValue | ||
} | ||
|
||
public var rawValue: String | ||
|
||
public init(rawValue: String) { | ||
self.rawValue = rawValue | ||
} | ||
|
||
public init(stringLiteral value: String) { | ||
self.init(rawValue: value) | ||
} | ||
|
||
public init(unicodeScalarLiteral value: String) { | ||
self.init(rawValue: value) | ||
} | ||
|
||
public init(extendedGraphemeClusterLiteral value: String) { | ||
self.init(rawValue: value) | ||
} | ||
|
||
public func base64Encoded() -> String { | ||
guard let data = rawValue.data(using: .utf8) else { | ||
return rawValue | ||
} | ||
return data.base64EncodedString() | ||
} | ||
|
||
public struct Transform { | ||
|
||
private let transform: (Filename) -> String | ||
|
||
private init(transform: @escaping (Filename) -> String) { | ||
self.transform = transform | ||
} | ||
|
||
public static let base64: Transform = Transform(transform: { $0.base64Encoded() }) | ||
public static let notTransformed: Transform = Transform(transform: { $0.rawValue }) | ||
public static func custom(_ transform: @escaping (Filename) -> String) -> Transform { | ||
return Transform(transform: transform) | ||
} | ||
|
||
public func finalForm(of filename: Filename) -> String { | ||
return transform(filename) | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
public final class DiskFolderStorage : StorageProtocol { | ||
|
||
public typealias Key = Filename | ||
public typealias Value = Data | ||
|
||
public let storageName: String | ||
public let folderURL: URL | ||
|
||
private let diskStorage: Storage<FileURL, Data> | ||
|
||
public let transformFilename: Filename.Transform | ||
|
||
public init(folderURL: URL, | ||
diskStorage: Storage<FileURL, Data> = DiskStorage.main.asStorage(), | ||
transformFilename: Filename.Transform = .base64) { | ||
self.diskStorage = diskStorage | ||
self.folderURL = folderURL | ||
self.transformFilename = transformFilename | ||
self.storageName = "disk-\(folderURL.lastPathComponent)" | ||
} | ||
|
||
public func fileURL(forFilename filename: Filename) -> FileURL { | ||
let finalForm = transformFilename.finalForm(of: filename) | ||
return FileURL(folderURL.appendingPathComponent(finalForm)) | ||
} | ||
|
||
public func retrieve(forKey filename: Filename, completion: @escaping (Result<Data>) -> ()) { | ||
let fileURL = self.fileURL(forFilename: filename) | ||
diskStorage.retrieve(forKey: fileURL, completion: completion) | ||
} | ||
|
||
public func set(_ data: Data, forKey filename: Filename, completion: @escaping (Result<Void>) -> ()) { | ||
let fileURL = self.fileURL(forFilename: filename) | ||
diskStorage.set(data, forKey: fileURL, completion: completion) | ||
} | ||
|
||
} | ||
|
||
extension URL { | ||
|
||
public init(directory: FileManager.SearchPathDirectory, domainMask: FileManager.SearchPathDomainMask = .userDomainMask) { | ||
let urls = FileManager.default.urls(for: directory, in: domainMask) | ||
self = urls.first! | ||
} | ||
|
||
} | ||
|
||
extension DiskFolderStorage { | ||
|
||
public static func inDirectory(_ directory: FileManager.SearchPathDirectory, | ||
appending pathComponent: String, | ||
domainMask: FileManager.SearchPathDomainMask = .userDomainMask, | ||
diskStorage: Storage<FileURL, Data>, | ||
transformFilename: Filename.Transform) -> DiskFolderStorage { | ||
let directoryURL = URL(directory: directory, domainMask: domainMask).appendingPathComponent(pathComponent) | ||
return DiskFolderStorage(folderURL: directoryURL, | ||
diskStorage: diskStorage, | ||
transformFilename: transformFilename) | ||
} | ||
|
||
} | ||
|
||
public final class DiskStorage : StorageProtocol { | ||
|
||
public typealias Key = FileURL | ||
public typealias Value = Data | ||
|
||
public var storageName: String { | ||
return "disk" | ||
} | ||
|
||
private let queue: DispatchQueue = DispatchQueue(label: "disk-storage-queue", qos: .userInitiated) | ||
private let fileManager = FileManager.default | ||
|
||
fileprivate let creatingDirectories: Bool | ||
|
||
public init(creatingDirectories: Bool = true) { | ||
self.creatingDirectories = creatingDirectories | ||
} | ||
|
||
public func retrieve(forKey key: FileURL, completion: @escaping (Result<Data>) -> ()) { | ||
queue.async { | ||
do { | ||
let data = try Data.init(contentsOf: key.url) | ||
completion(succeed(with: data)) | ||
} catch { | ||
completion(fail(with: error)) | ||
} | ||
} | ||
} | ||
|
||
public enum Error : Swift.Error { | ||
case cantCreatFile | ||
case cantCreateDirectory(Swift.Error) | ||
} | ||
|
||
public func set(_ value: Data, forKey key: FileURL, completion: @escaping (Result<Void>) -> ()) { | ||
queue.async { | ||
do { | ||
try self.createDirectoryURLIfNotExisting(for: key) | ||
let path = key.url.path | ||
if self.fileManager.createFile(atPath: path, | ||
contents: value, | ||
attributes: nil) { | ||
completion(.success) | ||
} else { | ||
completion(fail(with: Error.cantCreatFile)) | ||
} | ||
} catch { | ||
completion(fail(with: error)) | ||
} | ||
} | ||
} | ||
|
||
public static func directoryURL(of fileURL: FileURL) -> URL { | ||
return fileURL.url.deletingLastPathComponent() | ||
} | ||
|
||
fileprivate func createDirectoryURLIfNotExisting(for fileURL: FileURL) throws { | ||
let directoryURL = DiskStorage.directoryURL(of: fileURL) | ||
if !fileManager.fileExists(atPath: directoryURL.path) { | ||
do { | ||
try fileManager.createDirectory(at: directoryURL, | ||
withIntermediateDirectories: true, | ||
attributes: nil) | ||
} catch { | ||
throw Error.cantCreateDirectory(error) | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
extension DiskStorage { | ||
|
||
public static let main = DiskStorage(creatingDirectories: true) | ||
|
||
public func folder(_ folderName: String, | ||
in directory: FileManager.SearchPathDirectory, | ||
domainMask: FileManager.SearchPathDomainMask = .userDomainMask, | ||
transformFilename: Filename.Transform = .base64) -> DiskFolderStorage { | ||
return DiskFolderStorage.inDirectory( | ||
directory, | ||
appending: folderName, | ||
diskStorage: self.asStorage(), | ||
transformFilename: transformFilename | ||
) | ||
} | ||
|
||
} |
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,36 @@ | ||
// | ||
// DiskStorageTests.swift | ||
// Shallows | ||
// | ||
// Created by Олег on 18.01.2018. | ||
// Copyright © 2018 Shallows. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
@testable import Shallows | ||
|
||
class DiskStorageTests: XCTestCase { | ||
|
||
override func setUp() { | ||
super.setUp() | ||
// Put setup code here. This method is called before the invocation of each test method in the class. | ||
} | ||
|
||
override func tearDown() { | ||
// Put teardown code here. This method is called after the invocation of each test method in the class. | ||
super.tearDown() | ||
} | ||
|
||
func testExample() { | ||
// This is an example of a functional test case. | ||
// Use XCTAssert and related functions to verify your tests produce the correct results. | ||
} | ||
|
||
func testPerformanceExample() { | ||
// This is an example of a performance test case. | ||
self.measure { | ||
// Put the code you want to measure the time of here. | ||
} | ||
} | ||
|
||
} |
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