Skip to content

An open-source cross-platform alternative to AirDrop

fix: ignore unknown or invalid mtime reading files on Android

Tien Do Namc4e1edb2b83f
diff --git a/app/android/app/src/main/kotlin/org/localsend/localsend_app/FastDocumentFile.kt b/app/android/app/src/main/kotlin/org/localsend/localsend_app/FastDocumentFile.kt
index b8d1454a..b46998e9 100644
--- a/app/android/app/src/main/kotlin/org/localsend/localsend_app/FastDocumentFile.kt
+++ b/app/android/app/src/main/kotlin/org/localsend/localsend_app/FastDocumentFile.kt
@@ -18,7 +18,7 @@ class FastDocumentFile(
val uri: Uri,
val name: String,
val size: Long,
- val lastModified: Long,
+ val lastModified: Long?,
) {
val isDirectory: Boolean = mime == MIME_TYPE_DIR
val isFile: Boolean = !isDirectory && mime.isNotBlank()
@@ -58,7 +58,7 @@ class FastDocumentFile(
),
name = cursor.getString(2),
size = cursor.getLong(3),
- lastModified = cursor.getLong(4)
+ lastModified = readLastModified(cursor, 4)
)
)
}
@@ -75,6 +75,16 @@ class FastDocumentFile(
companion object {
const val TAG = "FastDocumentFile"
+ /**
+ * Treats null, 0 (File.lastModified() errors) and negative sentinels as unknown.
+ */
+ private fun readLastModified(cursor: Cursor, columnIndex: Int): Long? {
+ if (cursor.isNull(columnIndex)) {
+ return null
+ }
+ return cursor.getLong(columnIndex).takeIf { it > 0L }
+ }
+
/**
* Create a FastDocumentFile from a tree Uri.
*/
@@ -96,7 +106,7 @@ class FastDocumentFile(
),
name = "",
size = 0,
- lastModified = 0,
+ lastModified = null,
)
}
@@ -123,7 +133,7 @@ class FastDocumentFile(
uri = documentUri,
name = cursor.getString(1),
size = cursor.getLong(2),
- lastModified = cursor.getLong(3),
+ lastModified = readLastModified(cursor, 3),
)
} else {
null
diff --git a/app/android/app/src/main/kotlin/org/localsend/localsend_app/MainActivity.kt b/app/android/app/src/main/kotlin/org/localsend/localsend_app/MainActivity.kt
index 79de8571..ccc9a080 100644
--- a/app/android/app/src/main/kotlin/org/localsend/localsend_app/MainActivity.kt
+++ b/app/android/app/src/main/kotlin/org/localsend/localsend_app/MainActivity.kt
@@ -16,6 +16,10 @@ import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
+import java.text.SimpleDateFormat
+import java.util.Date
+import java.util.Locale
+import java.util.TimeZone
private const val CHANNEL = "org.localsend.localsend_app/localsend"
@@ -397,7 +401,7 @@ class MainActivity : FlutterActivity() {
name = documentFile.name,
size = documentFile.size,
uri = uri.toString(),
- lastModified = documentFile.lastModified,
+ lastModified = documentFile.lastModified?.toRfc3339(),
)
)
}
@@ -421,7 +425,7 @@ class MainActivity : FlutterActivity() {
name = file.name,
size = file.size,
uri = file.uri.toString(),
- lastModified = file.lastModified,
+ lastModified = file.lastModified?.toRfc3339(),
),
)
}
@@ -501,9 +505,9 @@ data class FileInfo(
val name: String,
val size: Long,
val uri: String,
- val lastModified: Long
+ val lastModified: String?
) {
- fun toMap(): Map<String, Any> {
+ fun toMap(): Map<String, Any?> {
return mapOf(
"name" to name,
"size" to size,
@@ -512,3 +516,10 @@ data class FileInfo(
)
}
}
+
+/// Formats milliseconds since epoch as an RFC 3339 string in UTC.
+private fun Long.toRfc3339(): String {
+ val format = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US)
+ format.timeZone = TimeZone.getTimeZone("UTC")
+ return format.format(Date(this))
+}
diff --git a/app/lib/provider/selection/selected_sending_files_provider.dart b/app/lib/provider/selection/selected_sending_files_provider.dart
index 2ed37bca..ae2aa154 100644
--- a/app/lib/provider/selection/selected_sending_files_provider.dart
+++ b/app/lib/provider/selection/selected_sending_files_provider.dart
@@ -247,7 +247,7 @@ class AddAndroidDirectoryAction extends AsyncReduxAction<SelectedSendingFilesNot
path: file.uri,
bytes: null,
// SAF only provides milliseconds, so there is no point statting in Rust.
- lastModified: DateTime.fromMillisecondsSinceEpoch(file.lastModified, isUtc: true).toIso8601String(),
+ lastModified: file.lastModified,
lastAccessed: null,
);
diff --git a/app/lib/util/native/channel/android_channel.dart b/app/lib/util/native/channel/android_channel.dart
index 0aedcfdd..d9bdde5f 100644
--- a/app/lib/util/native/channel/android_channel.dart
+++ b/app/lib/util/native/channel/android_channel.dart
@@ -104,7 +104,9 @@ class FileInfo with FileInfoMappable {
final String name;
final int size;
final String uri;
- final int lastModified;
+
+ /// RFC 3339 in UTC. Null when the document provider does not know it.
+ final String? lastModified;
FileInfo({
required this.name,
diff --git a/app/lib/util/native/channel/android_channel.mapper.dart b/app/lib/util/native/channel/android_channel.mapper.dart
index 8437d9ee..3b7ee832 100644
--- a/app/lib/util/native/channel/android_channel.mapper.dart
+++ b/app/lib/util/native/channel/android_channel.mapper.dart
@@ -179,8 +179,8 @@ class FileInfoMapper extends ClassMapperBase<FileInfo> {
static const Field<FileInfo, int> _f$size = Field('size', _$size);
static String _$uri(FileInfo v) => v.uri;
static const Field<FileInfo, String> _f$uri = Field('uri', _$uri);
- static int _$lastModified(FileInfo v) => v.lastModified;
- static const Field<FileInfo, int> _f$lastModified = Field(
+ static String? _$lastModified(FileInfo v) => v.lastModified;
+ static const Field<FileInfo, String> _f$lastModified = Field(
'lastModified',
_$lastModified,
);
@@ -259,7 +259,7 @@ extension FileInfoValueCopy<$R, $Out> on ObjectCopyWith<$R, FileInfo, $Out> {
abstract class FileInfoCopyWith<$R, $In extends FileInfo, $Out>
implements ClassCopyWith<$R, $In, $Out> {
- $R call({String? name, int? size, String? uri, int? lastModified});
+ $R call({String? name, int? size, String? uri, String? lastModified});
FileInfoCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(Then<$Out2, $R2> t);
}
@@ -272,12 +272,17 @@ class _FileInfoCopyWithImpl<$R, $Out>
late final ClassMapperBase<FileInfo> $mapper =
FileInfoMapper.ensureInitialized();
@override
- $R call({String? name, int? size, String? uri, int? lastModified}) => $apply(
+ $R call({
+ String? name,
+ int? size,
+ String? uri,
+ Object? lastModified = $none,
+ }) => $apply(
FieldCopyWithData({
if (name != null) #name: name,
if (size != null) #size: size,
if (uri != null) #uri: uri,
- if (lastModified != null) #lastModified: lastModified,
+ if (lastModified != $none) #lastModified: lastModified,
}),
);
@override
diff --git a/app/lib/util/native/cross_file_converters.dart b/app/lib/util/native/cross_file_converters.dart
index c9704107..52070576 100644
--- a/app/lib/util/native/cross_file_converters.dart
+++ b/app/lib/util/native/cross_file_converters.dart
@@ -69,7 +69,7 @@ class CrossFileConverters {
path: file.uri,
bytes: null,
// SAF only provides milliseconds, so there is no point statting in Rust.
- lastModified: DateTime.fromMillisecondsSinceEpoch(file.lastModified, isUtc: true).toIso8601String(),
+ lastModified: file.lastModified,
lastAccessed: null,
);
}