Fix tempFile

This commit is contained in:
Peter 2019-07-01 23:31:22 +03:00
parent 0d4394fdbb
commit b8a6f7d091

View File

@ -23,6 +23,7 @@ private final class TempBoxFileContext {
private struct TempBoxKey: Equatable, Hashable { private struct TempBoxKey: Equatable, Hashable {
let path: String? let path: String?
let fileName: String let fileName: String
let uniqueId: Int?
} }
public final class TempBoxFile { public final class TempBoxFile {
@ -42,7 +43,7 @@ private final class TempBoxContexts {
private var contexts: [TempBoxKey: TempBoxFileContext] = [:] private var contexts: [TempBoxKey: TempBoxFileContext] = [:]
func file(basePath: String, path: String, fileName: String) -> TempBoxFile { 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 let context: TempBoxFileContext
if let current = self.contexts[key] { if let current = self.contexts[key] {
context = current context = current
@ -68,13 +69,12 @@ private final class TempBoxContexts {
} }
func tempFile(basePath: String, fileName: String) -> TempBoxFile { 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 let id = self.nextId
self.nextId += 1 self.nextId += 1
let key = TempBoxKey(path: nil, fileName: fileName, uniqueId: id)
let context: TempBoxFileContext
let dirName = "\(id)" let dirName = "\(id)"
let dirPath = basePath + "/" + dirName let dirPath = basePath + "/" + dirName
var cleanName = fileName var cleanName = fileName
@ -85,9 +85,7 @@ private final class TempBoxContexts {
context = TempBoxFileContext(directory: dirPath, fileName: cleanName) context = TempBoxFileContext(directory: dirPath, fileName: cleanName)
self.contexts[key] = context self.contexts[key] = context
let _ = try? FileManager.default.createDirectory(atPath: dirPath, withIntermediateDirectories: true, attributes: nil) let _ = try? FileManager.default.createDirectory(atPath: dirPath, withIntermediateDirectories: true, attributes: nil)
}
let id = self.nextId
self.nextId += 1
context.subscribers.insert(id) context.subscribers.insert(id)
return TempBoxFile(key: key, id: id, path: context.path) return TempBoxFile(key: key, id: id, path: context.path)
} }