From afb60fbf5cc1e8fc0ced2e8909bc04010b131337 Mon Sep 17 00:00:00 2001 From: Morgan Pretty Date: Fri, 11 Oct 2024 15:04:59 +1100 Subject: [PATCH] Fixed a couple of bugs with attachment path creation --- SessionMessagingKit/Database/Models/Attachment.swift | 10 +++++----- SessionUtilitiesKit/Media/UTType+Utilities.swift | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/SessionMessagingKit/Database/Models/Attachment.swift b/SessionMessagingKit/Database/Models/Attachment.swift index 3bf47732a..a87e68d66 100644 --- a/SessionMessagingKit/Database/Models/Attachment.swift +++ b/SessionMessagingKit/Database/Models/Attachment.swift @@ -610,10 +610,6 @@ extension Attachment { } public static func originalFilePath(id: String, mimeType: String, sourceFilename: String?) -> String? { - // Store the file in a subdirectory whose name is the uniqueId of this attachment, - // to avoid collisions between multiple attachments with the same name - let attachmentFolder: String = Attachment.attachmentsFolder.appending("/\(id)") - if let sourceFilename: String = sourceFilename, !sourceFilename.isEmpty { // Ensure that the filename is a valid filesystem name, // replacing invalid characters with an underscore. @@ -647,6 +643,10 @@ extension Attachment { targetFileExtension = targetFileExtension.lowercased() if !targetFileExtension.isEmpty { + // Store the file in a subdirectory whose name is the uniqueId of this attachment, + // to avoid collisions between multiple attachments with the same name + let attachmentFolder: String = Attachment.attachmentsFolder.appending("/\(id)") + guard case .success = Result(try FileSystem.ensureDirectoryExists(at: attachmentFolder)) else { return nil } @@ -660,7 +660,7 @@ extension Attachment { UTType.fileExtensionDefault ).lowercased() - return attachmentFolder.appending("/\(id).\(targetFileExtension)") + return Attachment.attachmentsFolder.appending("/\(id).\(targetFileExtension)") } public static func localRelativeFilePath(from originalFilePath: String?) -> String? { diff --git a/SessionUtilitiesKit/Media/UTType+Utilities.swift b/SessionUtilitiesKit/Media/UTType+Utilities.swift index 6ee882361..7765a2833 100644 --- a/SessionUtilitiesKit/Media/UTType+Utilities.swift +++ b/SessionUtilitiesKit/Media/UTType+Utilities.swift @@ -123,7 +123,7 @@ public extension UTType { let mimeType: String = preferredMIMEType, let fileExtension: String = UTType.genericExtensionTypesToMimeTypes .first(where: { _, value in value == mimeType })? - .value + .key else { return preferredFilenameExtension } return fileExtension