From c19b0fd6f9db40ab2c56516207ea6ce0d2c2e3e0 Mon Sep 17 00:00:00 2001 From: Justus von Brandt Date: Sun, 15 Sep 2019 14:32:46 +0200 Subject: [PATCH] Create intermediate directories before writing to a file --- Haneke/DiskCache.swift | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Haneke/DiskCache.swift b/Haneke/DiskCache.swift index fa55e146..57e127b6 100644 --- a/Haneke/DiskCache.swift +++ b/Haneke/DiskCache.swift @@ -170,7 +170,16 @@ open class DiskCache { let path = self.path(forKey: key) let fileManager = FileManager.default let previousAttributes : [FileAttributeKey: Any]? = try? fileManager.attributesOfItem(atPath: path) - + + //If the data folder does not exist, it needs to be recreated + if (!fileManager.fileExists(atPath: self.path)) { + do { + try fileManager.createDirectory(atPath: self.path, withIntermediateDirectories: true, attributes: nil) + } catch { + Log.error(message: "Failed to create directory", error: error) + } + } + do { try data.write(to: URL(fileURLWithPath: path), options: Data.WritingOptions.atomicWrite) } catch {