Last active
May 11, 2020 12:50
-
-
Save dagronf/267514677f6a8849c4be90dd006f95c0 to your computer and use it in GitHub Desktop.
Temporary file and folder support for Files (https://github.com/JohnSundell/Files)
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
public extension Folder { | |
/// Create a new uniquely-named file within this folder. | |
/// - Parameter prefix: (optional) prefix to add the temporary file name | |
/// - Parameter fileExtension: (optional) file extension (without the `.`) to use for the created file | |
/// - Parameter contents: (optional) the data to write to the file | |
/// - throws: `WriteError` if a new file couldn't be created. | |
func createTemporaryFile(prefix: String? = nil, fileExtension: String? = nil, contents: Data? = nil) throws -> File { | |
var tempFilename = "" | |
if let prefix = prefix { | |
tempFilename += prefix + "_" | |
} | |
tempFilename += ProcessInfo.processInfo.globallyUniqueString | |
if let fileExtension = fileExtension { | |
tempFilename += "." + fileExtension | |
} | |
return try self.createFile(at: tempFilename, contents: contents) | |
} | |
/// Create a new uniquely-named folder within this folder. | |
/// - Parameter prefix: (optional) prefix to add the temporary folder name | |
/// - throws: `WriteError` if a new folder couldn't be created. | |
func createTemporarySubfolder(prefix: String? = nil) throws -> Folder { | |
var tempFolderName = "" | |
if let prefix = prefix { | |
tempFolderName += prefix + "_" | |
} | |
tempFolderName += ProcessInfo.processInfo.globallyUniqueString | |
return try self.createSubfolder(named: tempFolderName) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment