diff --git a/Adamant/Modules/Chat/View/Subviews/ChatMedia/Content/Views/ChatFileContainerView/FileListContentView.swift b/Adamant/Modules/Chat/View/Subviews/ChatMedia/Content/Views/ChatFileContainerView/FileListContentView.swift index 9a27186e2..6780c0e10 100644 --- a/Adamant/Modules/Chat/View/Subviews/ChatMedia/Content/Views/ChatFileContainerView/FileListContentView.swift +++ b/Adamant/Modules/Chat/View/Subviews/ChatMedia/Content/Views/ChatFileContainerView/FileListContentView.swift @@ -228,10 +228,7 @@ private extension FileListContentView { let fileType = chatFile.file.extension.map { ".\($0)" } ?? .empty let fileName = chatFile.file.name ?? .adamant.chat.unknownTitle.uppercased() - nameLabel.text = fileName.contains(fileType) - ? fileName - : "\(fileName.uppercased())\(fileType.uppercased())" - + nameLabel.text = "\(fileName)\(fileType)".withoutFileExtensionDuplication() sizeLabel.text = formatSize(chatFile.file.size) additionalLabel.text = fileType.uppercased() diff --git a/Adamant/Modules/Chat/View/Subviews/FilesToolBarView/FilesToolbarCollectionViewCell.swift b/Adamant/Modules/Chat/View/Subviews/FilesToolBarView/FilesToolbarCollectionViewCell.swift index a7b0306b3..bbba2e6c2 100644 --- a/Adamant/Modules/Chat/View/Subviews/FilesToolBarView/FilesToolbarCollectionViewCell.swift +++ b/Adamant/Modules/Chat/View/Subviews/FilesToolBarView/FilesToolbarCollectionViewCell.swift @@ -80,12 +80,10 @@ final class FilesToolbarCollectionViewCell: UICollectionViewCell { imageView.image = file.preview ?? defaultImage removeBtn.tag = tag - let fileType = file.extenstion ?? "" + let fileType = file.extenstion ?? .empty let fileName = file.name ?? "UNKNWON" - nameLabel.text = fileName.contains(fileType) - ? fileName - : "\(fileName.uppercased()).\(fileType.uppercased())" + nameLabel.text = "\(fileName).\(fileType)" additionalLabel.text = fileType.uppercased() additionalLabel.isHidden = file.preview != nil diff --git a/Adamant/Modules/Chat/ViewModel/ChatViewModel.swift b/Adamant/Modules/Chat/ViewModel/ChatViewModel.swift index 612dccc74..202505026 100644 --- a/Adamant/Modules/Chat/ViewModel/ChatViewModel.swift +++ b/Adamant/Modules/Chat/ViewModel/ChatViewModel.swift @@ -1635,9 +1635,11 @@ private extension ChatViewModel { dialog.send(.progress(true)) let files: [FileResult] = chatFiles.compactMap { file in - guard file.isCached, - !file.isBusy, - let fileDTO = try? filesStorage.getFile(with: file.file.id).get() + guard + file.isCached, + !file.isBusy, + let fileDTO = try? filesStorage.getFile(with: file.file.id).get(), + let filename = file.file.name else { return nil } @@ -1656,7 +1658,7 @@ private extension ChatViewModel { previewUrl: nil, previewExtension: nil, size: file.file.size, - name: file.file.name, + namePossiblyWithExtension: filename, extenstion: file.file.extension, resolution: nil, data: data diff --git a/CommonKit/Sources/CommonKit/Helpers/String+utilites.swift b/CommonKit/Sources/CommonKit/Helpers/String+utilites.swift index f56be87ec..2fd6525dd 100644 --- a/CommonKit/Sources/CommonKit/Helpers/String+utilites.swift +++ b/CommonKit/Sources/CommonKit/Helpers/String+utilites.swift @@ -42,7 +42,35 @@ public extension String { ?? self } - subscript (i: Int) -> Character { - return self[index(startIndex, offsetBy:i)] + subscript(i: Int) -> Character { + return self[index(startIndex, offsetBy: i)] + } + + func separateFileExtension() -> (name: String, extension: String?) { + guard let dotIndex = lastIndex(of: ".") else { + return (name: self, extension: nil) + } + + return ( + name: .init(self[startIndex ..< dotIndex]), + extension: .init(self[dotIndex ..< endIndex].dropFirst()) + ) + } + + func withoutFileExtensionDuplication() -> String { + let dotsCount = count { $0 == "." } + guard dotsCount > 1 else { return self } + + var nameAndExtension = separateFileExtension() + var filename = nameAndExtension.name + guard let ext = nameAndExtension.extension else { return self } + + for _ in 1 ..< dotsCount { + nameAndExtension = filename.separateFileExtension() + guard nameAndExtension.extension == ext else { return "\(filename).\(ext)" } + filename = nameAndExtension.name + } + + return "\(filename).\(ext)" } } diff --git a/CommonKit/Sources/CommonKit/Models/FileResult.swift b/CommonKit/Sources/CommonKit/Models/FileResult.swift index b712af13c..85374c1d6 100644 --- a/CommonKit/Sources/CommonKit/Models/FileResult.swift +++ b/CommonKit/Sources/CommonKit/Models/FileResult.swift @@ -89,3 +89,43 @@ public struct FileResult: Sendable { self.mimeType = mimeType } } + +public extension FileResult { + init( + assetId: String? = nil, + url: URL, + type: FileType, + preview: UIImage?, + previewUrl: URL?, + previewExtension: String?, + size: Int64, + namePossiblyWithExtension: String, + extenstion: String?, + resolution: CGSize?, + data: Data? = nil, + duration: Float64? = nil, + mimeType: String? = nil + ) { + let nameWithExtension = namePossiblyWithExtension.separateFileExtension() + + let name = nameWithExtension.extension == extenstion + ? nameWithExtension.name + : namePossiblyWithExtension + + self.init( + assetId: assetId, + url: url, + type: type, + preview: preview, + previewUrl: previewUrl, + previewExtension: previewExtension, + size: size, + name: name, + extenstion: extenstion, + resolution: resolution, + data: data, + duration: duration, + mimeType: mimeType + ) + } +} diff --git a/FilesPickerKit/Sources/FilesPickerKit/Helpers/FilesPickerKit.swift b/FilesPickerKit/Sources/FilesPickerKit/Helpers/FilesPickerKit.swift index 866717fe3..cdc556dab 100644 --- a/FilesPickerKit/Sources/FilesPickerKit/Helpers/FilesPickerKit.swift +++ b/FilesPickerKit/Sources/FilesPickerKit/Helpers/FilesPickerKit.swift @@ -86,13 +86,13 @@ public final class FilesPickerKit: FilesPickerProtocol { public func getFileResult(for url: URL) throws -> FileResult { try createFileResult( from: url, - name: url.lastPathComponent, + name: url.lastPathComponent.separateFileExtension().name, extension: url.pathExtension ) } public func getFileResult(for image: UIImage) throws -> FileResult { - let fileName = "\(imagePrefix)\(String.random(length: 4)).\(previewExtension)" + let fileName = "\(imagePrefix)\(String.random(length: 4))" let newUrl = try storageKit.getTempUrl(for: image, name: fileName)