feat: add fileSize property to AssetEntity#1370
Open
dev-ale wants to merge 1 commit intofluttercandies:mainfrom
Open
feat: add fileSize property to AssetEntity#1370dev-ale wants to merge 1 commit intofluttercandies:mainfrom
dev-ale wants to merge 1 commit intofluttercandies:mainfrom
Conversation
Read file size from platform metadata at query time, without downloading files from iCloud or other cloud storage. - iOS/macOS: PHAssetResource.value(forKey: "fileSize") - Android: MediaStore.MediaColumns.SIZE - OHOS: photoAccessHelper.PhotoKeys.SIZE The property is nullable (Int?) and only set when the platform reports a value > 0. Closes fluttercandies#1366
AlexV525
reviewed
Feb 28, 2026
| DATE_ADDED, // 创建时间 | ||
| DATE_MODIFIED, // 修改时间 | ||
| MIME_TYPE, // mime type | ||
| MediaStore.MediaColumns.SIZE, // file size in bytes |
Member
There was a problem hiding this comment.
Follow how other fields are being imported.
| @property(nonatomic, assign) NSUInteger subtype; | ||
| @property(nonatomic, assign) BOOL favorite; | ||
| @property(nonatomic, assign) BOOL isLocallyAvailable; | ||
| @property(nonatomic, assign) long long fileSize; |
Comment on lines
+903
to
+904
| /// This value is read from metadata at query time, without downloading | ||
| /// the file from iCloud or other cloud storage. |
Member
There was a problem hiding this comment.
Could we clarify which file (compare to .file or .originalFile) the size indicated here refers to?
| // let date_taken = this.getAssetMember(photoAsset, photoAccessHelper.PhotoKeys.DATE_TAKEN); | ||
| let orientation = PhotoAssetHandler.getAssetMember(photoAsset, photoAccessHelper.PhotoKeys.ORIENTATION); | ||
| let is_favorite = PhotoAssetHandler.getAssetMember(photoAsset, photoAccessHelper.PhotoKeys.FAVORITE); | ||
| let file_size = PhotoAssetHandler.getAssetMember(photoAsset, photoAccessHelper.PhotoKeys.SIZE); |
Member
There was a problem hiding this comment.
Did you get any doc reference on this key?
|
I have tested this on both Android and iOS device and its working as expected, before this I had to run a separate process to fetch file size after getting image ids from the library. Using the same method that this PR used. Request to merge in stable release. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a
fileSizeproperty toAssetEntitythat reads the file size in bytes from platform metadata at query time, without downloading files from iCloud or other cloud storage.Motivation
Currently, the only way to get an asset's file size is via
asset.fileorasset.originFile, which triggers full file downloads on iOS (including from iCloud). This is prohibitively slow for apps that need to display or calculate file sizes for many assets — for example, photo declutter apps showing "freed space" estimates.Changes
lib/src/types/entity.dartfileSizefield (nullableint?) toAssetEntityconstructor, field declaration, andcopyWithlib/src/utils/convert_utils.dartdata['fileSize']inconvertMapToAsset()darwin/.../PMAssetPathEntity.hfileSizeproperty toPMAssetEntitydarwin/.../PMManager.mfileSizefromPHAssetResourcemetadata viavalue(forKey: "fileSize")darwin/.../PMConvertUtils.mfileSizein bothconvertPMAssetToMap:andconvertPHAssetToMap:core/entity/AssetEntity.ktfileSize: Longfieldcore/utils/IDBUtils.ktMediaStore.MediaColumns.SIZEtostoreImageKeysandstoreVideoKeysextension/CursorExtensions.ktSIZEcolumn from cursor, pass to entitycore/utils/ConvertUtils.ktfileSizeto mapPhotoAssetHandler.etsSIZE(already infetchColumns) and add['fileSize', file_size]to maptest/convert_utils_test.dartConvertUtils.convertMapToAssetfileSize parsing +AssetEntity.copyWithpreservationCHANGELOG.mdiOS implementation note
On iOS/macOS,
PHAssetResource.value(forKey: "fileSize")is used to read file size from the Photos database metadata. This is a well-known KVC technique that reads locally cached metadata — it does not trigger iCloud downloads. While thefileSizekey is not part of Apple's documented public API forPHAssetResource, it has been stable since the Photos framework was introduced and is widely used in the iOS ecosystem.The implementation selects the primary resource (
.photo,.video,.fullSizePhoto, or.fullSizeVideo) to report the most relevant file size, falling back to the first available resource.Usage
Testing
dart analyze lib/ test/— no issuesflutter test— all 15 tests pass (6 new tests added)Closes #1369
Addresses #1366