Skip to content
Open
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
4 changes: 3 additions & 1 deletion Sources/Disk+Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public extension Disk {
/// - path: file location to store the data (i.e. "Folder/file.json")
/// - encoder: custom JSONEncoder to encode value
/// - Throws: Error if there were any issues encoding the struct or writing it to disk
static func save<T: Encodable>(_ value: T, to directory: Directory, as path: String, encoder: JSONEncoder = JSONEncoder()) throws {
@discardableResult
static func save<T: Encodable>(_ value: T, to directory: Directory, as path: String, encoder: JSONEncoder = JSONEncoder()) throws -> URL {
if path.hasSuffix("/") {
throw createInvalidFileNameForStructsError()
}
Expand All @@ -40,6 +41,7 @@ public extension Disk {
let data = try encoder.encode(value)
try createSubfoldersBeforeCreatingFile(at: url)
try data.write(to: url, options: .atomic)
return url
} catch {
throw error
}
Expand Down
4 changes: 3 additions & 1 deletion Sources/Disk+Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ public extension Disk {
/// - directory: user directory to store the file in
/// - path: file location to store the data (i.e. "Folder/file.mp4")
/// - Throws: Error if there were any issues writing the given data to disk
static func save(_ value: Data, to directory: Directory, as path: String) throws {
@discardableResult
static func save(_ value: Data, to directory: Directory, as path: String) throws -> URL {
do {
let url = try createURL(for: path, in: directory)
try createSubfoldersBeforeCreatingFile(at: url)
try value.write(to: url, options: .atomic)
return url
} catch {
throw error
}
Expand Down
4 changes: 3 additions & 1 deletion Sources/Disk+UIImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public extension Disk {
/// - directory: user directory to store the image file in
/// - path: file location to store the data (i.e. "Folder/file.png")
/// - Throws: Error if there were any issues writing the image to disk
static func save(_ value: UIImage, to directory: Directory, as path: String) throws {
@discardableResult
static func save(_ value: UIImage, to directory: Directory, as path: String) throws -> URL {
do {
var imageData: Data
if path.suffix(4).lowercased() == ".png" {
Expand Down Expand Up @@ -97,6 +98,7 @@ public extension Disk {
let url = try createURL(for: path, in: directory)
try createSubfoldersBeforeCreatingFile(at: url)
try imageData.write(to: url, options: .atomic)
return url
} catch {
throw error
}
Expand Down
6 changes: 5 additions & 1 deletion Sources/Disk+[Data].swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,21 @@ public extension Disk {
/// - directory: user directory to store the files in
/// - path: folder location to store the data files (i.e. "Folder/")
/// - Throws: Error if there were any issues creating a folder and writing the given [Data] to files in it
static func save(_ value: [Data], to directory: Directory, as path: String) throws {
@discardableResult
static func save(_ value: [Data], to directory: Directory, as path: String) throws -> [URL] {
do {
let folderUrl = try createURL(for: path, in: directory)
try createSubfoldersBeforeCreatingFile(at: folderUrl)
try FileManager.default.createDirectory(at: folderUrl, withIntermediateDirectories: false, attributes: nil)
var urls: [URL] = []
for i in 0..<value.count {
let data = value[i]
let dataName = "\(i)"
let dataUrl = folderUrl.appendingPathComponent(dataName, isDirectory: false)
try data.write(to: dataUrl, options: .atomic)
urls.append(dataUrl)
}
return urls
} catch {
throw error
}
Expand Down
6 changes: 5 additions & 1 deletion Sources/Disk+[UIImage].swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ public extension Disk {
/// - directory: user directory to store the images in
/// - path: folder location to store the images (i.e. "Folder/")
/// - Throws: Error if there were any issues creating a folder and writing the given images to it
static func save(_ value: [UIImage], to directory: Directory, as path: String) throws {
@discardableResult
static func save(_ value: [UIImage], to directory: Directory, as path: String) throws -> [URL] {
do {
let folderUrl = try createURL(for: path, in: directory)
try createSubfoldersBeforeCreatingFile(at: folderUrl)
try FileManager.default.createDirectory(at: folderUrl, withIntermediateDirectories: false, attributes: nil)
var urls: [URL] = []
for i in 0..<value.count {
let image = value[i]
var imageData: Data
Expand Down Expand Up @@ -71,7 +73,9 @@ public extension Disk {
}
let imageUrl = folderUrl.appendingPathComponent(imageName, isDirectory: false)
try imageData.write(to: imageUrl, options: .atomic)
urls.append(imageUrl)
}
return urls
} catch {
throw error
}
Expand Down