Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a couple of bugs with attachment path creation #1039

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions SessionMessagingKit/Database/Models/Attachment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand All @@ -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? {
Expand Down
2 changes: 1 addition & 1 deletion SessionUtilitiesKit/Media/UTType+Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down