From b8a6f7d091b16dfafbf6ea70783bf0ec77d1c086 Mon Sep 17 00:00:00 2001 From: Peter <> Date: Mon, 1 Jul 2019 23:31:22 +0300 Subject: [PATCH] Fix tempFile --- submodules/Postbox/Postbox/TempBox.swift | 36 +++++++++++------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/submodules/Postbox/Postbox/TempBox.swift b/submodules/Postbox/Postbox/TempBox.swift index dbbf1748a0..202f11d832 100644 --- a/submodules/Postbox/Postbox/TempBox.swift +++ b/submodules/Postbox/Postbox/TempBox.swift @@ -23,6 +23,7 @@ private final class TempBoxFileContext { private struct TempBoxKey: Equatable, Hashable { let path: String? let fileName: String + let uniqueId: Int? } public final class TempBoxFile { @@ -42,7 +43,7 @@ private final class TempBoxContexts { private var contexts: [TempBoxKey: TempBoxFileContext] = [:] func file(basePath: String, path: String, fileName: String) -> TempBoxFile { - let key = TempBoxKey(path: path, fileName: fileName) + let key = TempBoxKey(path: path, fileName: fileName, uniqueId: nil) let context: TempBoxFileContext if let current = self.contexts[key] { context = current @@ -68,26 +69,23 @@ private final class TempBoxContexts { } func tempFile(basePath: String, fileName: String) -> TempBoxFile { - let key = TempBoxKey(path: nil, fileName: fileName) - let context: TempBoxFileContext - if let current = self.contexts[key] { - context = current - } else { - let id = self.nextId - self.nextId += 1 - let dirName = "\(id)" - let dirPath = basePath + "/" + dirName - var cleanName = fileName - if cleanName.hasPrefix("..") { - cleanName = "__" + String(cleanName[cleanName.index(cleanName.startIndex, offsetBy: 2)]) - } - cleanName = cleanName.replacingOccurrences(of: "/", with: "_") - context = TempBoxFileContext(directory: dirPath, fileName: cleanName) - self.contexts[key] = context - let _ = try? FileManager.default.createDirectory(atPath: dirPath, withIntermediateDirectories: true, attributes: nil) - } let id = self.nextId self.nextId += 1 + + let key = TempBoxKey(path: nil, fileName: fileName, uniqueId: id) + let context: TempBoxFileContext + + let dirName = "\(id)" + let dirPath = basePath + "/" + dirName + var cleanName = fileName + if cleanName.hasPrefix("..") { + cleanName = "__" + String(cleanName[cleanName.index(cleanName.startIndex, offsetBy: 2)]) + } + cleanName = cleanName.replacingOccurrences(of: "/", with: "_") + context = TempBoxFileContext(directory: dirPath, fileName: cleanName) + self.contexts[key] = context + let _ = try? FileManager.default.createDirectory(atPath: dirPath, withIntermediateDirectories: true, attributes: nil) + context.subscribers.insert(id) return TempBoxFile(key: key, id: id, path: context.path) }