Skip to content

Instantly share code, notes, and snippets.

@dagronf
Last active May 11, 2020 12:50
Show Gist options
  • Save dagronf/267514677f6a8849c4be90dd006f95c0 to your computer and use it in GitHub Desktop.
Save dagronf/267514677f6a8849c4be90dd006f95c0 to your computer and use it in GitHub Desktop.
Temporary file and folder support for Files (https://github.com/JohnSundell/Files)
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