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

Add filesize in media item view controlller #22984

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions WordPress/Classes/ViewRelated/Media/MediaItemViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ final class MediaItemViewController: UITableViewController {
var rows = [ImmuTableRow]()
rows.append(TextRow(title: NSLocalizedString("File name", comment: "Label for the file name for a media asset (image / video)"), value: media.filename ?? ""))
rows.append(TextRow(title: NSLocalizedString("File type", comment: "Label for the file type (.JPG, .PNG, etc) for a media asset (image / video)"), value: presenter.fileType ?? ""))
rows.append(TextRow(title: NSLocalizedString("File size", comment: "Label for the file size for a media asset"), value: presenter.fileSize ?? ""))

switch media.mediaType {
case .image, .video:
Expand Down Expand Up @@ -426,6 +427,25 @@ private struct MediaMetadataPresenter {

return (filename as NSString).pathExtension.uppercased()
}

var fileSize: String? {
guard let fileSizeInBytes = media.filesize as? Int else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest using ByteCountFormatter .

return nil
}

let sizeAbbreviations = ["bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
var sizeAbbreviationsIndex = 0
var capacity = Double(fileSizeInBytes)

while capacity > 1024 {
capacity /= 1024
sizeAbbreviationsIndex += 1
}

let formattedCapacity = String(format: "%4.2f", capacity)
let sizeAbbreviation = sizeAbbreviations[sizeAbbreviationsIndex]
return "\(formattedCapacity) \(sizeAbbreviation)"
}
}

/// Used to store media metadata and provide the ability to undo changes to
Expand Down