Skip to content

Commit

Permalink
[trello.com/c/nuH8o1Pr] extension duplication fix
Browse files Browse the repository at this point in the history
  • Loading branch information
just-software-dev committed Dec 27, 2024
1 parent 65fffc3 commit e56979c
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions Adamant/Modules/Chat/ViewModel/ChatViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
32 changes: 30 additions & 2 deletions CommonKit/Sources/CommonKit/Helpers/String+utilites.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
}
}
40 changes: 40 additions & 0 deletions CommonKit/Sources/CommonKit/Models/FileResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit e56979c

Please sign in to comment.