-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from makjac/makjac/chore-add-changelog-0.1.0
Makjac/chore add changelog 0.1.0
- Loading branch information
Showing
25 changed files
with
964 additions
and
112 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
31 changes: 31 additions & 0 deletions
31
android/src/main/kotlin/com/ecoala/flutter_file_info/FileMetadata.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.ecoala.flutter_file_info | ||
|
||
import java.nio.file.attribute.PosixFilePermission | ||
import java.util.Date | ||
|
||
/** | ||
* Represents metadata information about a file. | ||
* | ||
* @property filePath The path of the file. | ||
* @property fileName The name of the file. | ||
* @property fileExtension The extension of the file. | ||
* @property fileType The type of the file. | ||
* @property creationTime The creation time of the file. | ||
* @property modifiedTime The last modified time of the file. | ||
* @property accessedTime The last accessed time of the file. | ||
* @property sizeBytes The size of the file in bytes. | ||
* @property fileSize The size of the file as a human-readable string. | ||
* @property attributes The set of POSIX file permissions for the file. | ||
*/ | ||
data class FileMetadata( | ||
val filePath: String, | ||
val fileName: String?, | ||
val fileExtension: String?, | ||
val fileType: String?, | ||
val creationTime: Date?, | ||
val modifiedTime: Date?, | ||
val accessedTime: Date?, | ||
val sizeBytes: Long?, | ||
val fileSize: String?, | ||
val attributes: Set<PosixFilePermission>? | ||
) |
92 changes: 92 additions & 0 deletions
92
android/src/main/kotlin/com/ecoala/flutter_file_info/FileMetadataProvider.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package com.ecoala.flutter_file_info | ||
|
||
import android.annotation.TargetApi | ||
import android.os.Build | ||
import java.io.File | ||
import java.nio.file.Files | ||
import java.nio.file.Paths | ||
import java.nio.file.attribute.BasicFileAttributes | ||
import java.nio.file.attribute.PosixFilePermission | ||
import java.text.DecimalFormat | ||
import java.util.* | ||
|
||
/** | ||
* This class is responsible for providing file metadata. | ||
*/ | ||
class FileMetadataProvider { | ||
|
||
/** | ||
* Retrieves the metadata of a file specified by the given file path. | ||
* | ||
* @param filePath The path of the file. | ||
* @return The metadata of the file. | ||
*/ | ||
@TargetApi(Build.VERSION_CODES.O) | ||
fun getFileMetadata(filePath: String): FileMetadata { | ||
val file = File(filePath) | ||
val path = Paths.get(filePath) | ||
val attributes = Files.readAttributes(path, BasicFileAttributes::class.java) | ||
val posixAttributes: Set<PosixFilePermission> = try { | ||
Files.getPosixFilePermissions(path) | ||
} catch (e: UnsupportedOperationException) { | ||
emptySet() | ||
} | ||
|
||
val fileName = file.name | ||
val fileExtension = file.extension.takeIf { it.isNotEmpty() } | ||
val fileType = Files.probeContentType(path) | ||
val creationTime = Date(attributes.creationTime().toMillis()) | ||
val modifiedTime = Date(attributes.lastModifiedTime().toMillis()) | ||
val accessedTime = Date(attributes.lastAccessTime().toMillis()) | ||
val sizeBytes = attributes.size() | ||
val fileSize = humanReadableByteCountSI(sizeBytes) | ||
|
||
return FileMetadata( | ||
filePath = filePath, | ||
fileName = fileName, | ||
fileExtension = fileExtension, | ||
fileType = fileType, | ||
creationTime = creationTime, | ||
modifiedTime = modifiedTime, | ||
accessedTime = accessedTime, | ||
sizeBytes = sizeBytes, | ||
fileSize = fileSize, | ||
attributes = posixAttributes | ||
) | ||
} | ||
|
||
/** | ||
* Converts the given [FileMetadata] object to a map representation. | ||
* | ||
* @param metadata The [FileMetadata] object to convert. | ||
* @return A map containing the metadata of the file. | ||
*/ | ||
fun fileMetadataToMap(metadata: FileMetadata): Map<String, Any?> { | ||
return mapOf( | ||
"filePath" to metadata.filePath, | ||
"fileName" to metadata.fileName, | ||
"fileExtension" to metadata.fileExtension, | ||
"fileType" to metadata.fileType, | ||
"creationTime" to metadata.creationTime?.time, | ||
"modifiedTime" to metadata.modifiedTime?.time, | ||
"accessedTime" to metadata.accessedTime?.time, | ||
"sizeBytes" to metadata.sizeBytes, | ||
"fileSize" to metadata.fileSize, | ||
"androidAttributes" to metadata.attributes?.map { it.name } | ||
) | ||
} | ||
|
||
/** | ||
* Converts the given number of bytes into a human-readable string representation using the International System of Units (SI). | ||
* | ||
* @param bytes The number of bytes to convert. | ||
* @return A string representation of the given number of bytes in a human-readable format. | ||
*/ | ||
private fun humanReadableByteCountSI(bytes: Long): String { | ||
val unit = 1000 | ||
if (bytes < unit) return "$bytes B" | ||
val exp = (Math.log(bytes.toDouble()) / Math.log(unit.toDouble())).toInt() | ||
val pre = "kMGTPE"[exp - 1].toString() | ||
return DecimalFormat("#,##0.#").format(bytes / Math.pow(unit.toDouble(), exp.toDouble())) + " " + pre + "B" | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.