2023-03-13 19:55:39 +08:00
|
|
|
import 'package:clock/clock.dart';
|
2021-08-02 04:46:16 +08:00
|
|
|
import 'package:nc_photos/account.dart';
|
|
|
|
import 'package:nc_photos/api/api_util.dart' as api_util;
|
2021-04-10 12:28:12 +08:00
|
|
|
import 'package:nc_photos/entity/file.dart';
|
2022-10-15 22:29:18 +08:00
|
|
|
import 'package:nc_photos/entity/file_descriptor.dart';
|
2021-11-12 19:44:31 +08:00
|
|
|
import 'package:nc_photos/remote_storage_util.dart' as remote_storage_util;
|
2023-05-07 22:00:33 +08:00
|
|
|
import 'package:np_api/np_api.dart' as api;
|
2023-08-27 18:58:05 +08:00
|
|
|
import 'package:np_platform_util/np_platform_util.dart';
|
2023-08-26 00:37:17 +08:00
|
|
|
import 'package:np_string/np_string.dart';
|
2022-01-31 17:47:37 +08:00
|
|
|
import 'package:path/path.dart' as path_lib;
|
2021-04-10 12:28:12 +08:00
|
|
|
|
2022-10-15 22:29:18 +08:00
|
|
|
bool isSupportedMime(String mime) => supportedFormatMimes.contains(mime);
|
2021-10-07 04:32:36 +08:00
|
|
|
|
2022-10-15 22:29:18 +08:00
|
|
|
bool isSupportedFormat(FileDescriptor file) =>
|
|
|
|
isSupportedMime(file.fdMime ?? "");
|
2021-04-10 12:28:12 +08:00
|
|
|
|
2022-05-06 17:16:56 +08:00
|
|
|
bool isSupportedImageMime(String mime) =>
|
2022-10-15 22:29:18 +08:00
|
|
|
supportedImageFormatMimes.contains(mime);
|
2022-05-06 17:16:56 +08:00
|
|
|
|
2022-10-15 22:29:18 +08:00
|
|
|
bool isSupportedImageFormat(FileDescriptor file) =>
|
|
|
|
isSupportedImageMime(file.fdMime ?? "");
|
2021-05-06 14:29:46 +08:00
|
|
|
|
2023-04-13 23:32:31 +08:00
|
|
|
bool isSupportedVideoMime(String mime) =>
|
|
|
|
supportedVideoFormatMimes.contains(mime);
|
|
|
|
|
2022-10-15 22:29:18 +08:00
|
|
|
bool isSupportedVideoFormat(FileDescriptor file) =>
|
2023-04-13 23:32:31 +08:00
|
|
|
isSupportedVideoMime(file.fdMime ?? "");
|
2021-05-06 19:36:20 +08:00
|
|
|
|
2022-04-22 03:18:57 +08:00
|
|
|
bool isMetadataSupportedMime(String mime) =>
|
|
|
|
_metadataSupportedFormatMimes.contains(mime);
|
|
|
|
|
2022-10-15 22:29:18 +08:00
|
|
|
bool isMetadataSupportedFormat(FileDescriptor file) =>
|
|
|
|
isMetadataSupportedMime(file.fdMime ?? "");
|
2021-05-07 04:11:31 +08:00
|
|
|
|
2022-10-15 22:29:18 +08:00
|
|
|
bool isTrash(Account account, FileDescriptor file) =>
|
|
|
|
file.fdPath.startsWith(api_util.getTrashbinPath(account));
|
2021-08-02 04:46:16 +08:00
|
|
|
|
2023-04-13 23:32:31 +08:00
|
|
|
bool isAlbumFile(Account account, FileDescriptor file) =>
|
|
|
|
file.fdPath.startsWith(remote_storage_util.getRemoteAlbumsDir(account));
|
2021-11-12 19:44:31 +08:00
|
|
|
|
2023-05-07 22:00:33 +08:00
|
|
|
bool isNcAlbumFile(Account account, FileDescriptor file) =>
|
|
|
|
file.fdPath.startsWith("${api.ApiPhotos.path}/");
|
|
|
|
|
2021-11-16 01:55:39 +08:00
|
|
|
/// Return if [file] is located under [dir]
|
|
|
|
///
|
|
|
|
/// Return false if [file] is [dir] itself (since it's not "under")
|
|
|
|
///
|
|
|
|
/// See [isOrUnderDir]
|
2023-04-13 23:32:31 +08:00
|
|
|
bool isUnderDir(FileDescriptor file, FileDescriptor dir) =>
|
|
|
|
file.fdPath.startsWith("${dir.fdPath}/");
|
2021-11-16 01:55:39 +08:00
|
|
|
|
|
|
|
/// Return if [file] is [dir] or located under [dir]
|
|
|
|
///
|
|
|
|
/// See [isUnderDir]
|
2023-04-13 23:32:31 +08:00
|
|
|
bool isOrUnderDir(FileDescriptor file, FileDescriptor dir) =>
|
|
|
|
file.fdPath == dir.fdPath || isUnderDir(file, dir);
|
2021-11-16 01:55:39 +08:00
|
|
|
|
2021-11-16 23:06:22 +08:00
|
|
|
/// Convert a stripped path to a full path
|
|
|
|
///
|
|
|
|
/// See [File.strippedPath]
|
2021-12-25 19:55:48 +08:00
|
|
|
String unstripPath(Account account, String strippedPath) {
|
|
|
|
final p = strippedPath == "." ? "" : strippedPath;
|
|
|
|
return "${api_util.getWebdavRootUrlRelative(account)}/$p".trimRightAny("/");
|
|
|
|
}
|
2021-11-16 23:06:22 +08:00
|
|
|
|
2021-06-14 21:51:29 +08:00
|
|
|
/// For a path "remote.php/dav/files/foo/bar.jpg", return foo
|
2021-11-13 05:13:02 +08:00
|
|
|
CiString getUserDirName(File file) {
|
2021-06-14 21:51:29 +08:00
|
|
|
if (file.path.startsWith("remote.php/dav/files/")) {
|
2021-09-15 14:58:06 +08:00
|
|
|
const beg = "remote.php/dav/files/".length;
|
2021-06-14 21:51:29 +08:00
|
|
|
final end = file.path.indexOf("/", beg);
|
|
|
|
if (end != -1) {
|
2021-11-13 05:13:02 +08:00
|
|
|
return file.path.substring(beg, end).toCi();
|
2021-06-14 21:51:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
throw ArgumentError("Invalid path: ${file.path}");
|
|
|
|
}
|
|
|
|
|
2021-10-08 15:01:27 +08:00
|
|
|
String renameConflict(String filename, int conflictCount) {
|
2022-01-31 17:47:37 +08:00
|
|
|
final temp =
|
|
|
|
"${path_lib.basenameWithoutExtension(filename)} ($conflictCount)";
|
|
|
|
if (path_lib.extension(filename).isEmpty) {
|
2021-10-08 15:01:27 +08:00
|
|
|
return temp;
|
|
|
|
} else {
|
2022-01-31 17:47:37 +08:00
|
|
|
return "$temp${path_lib.extension(filename)}";
|
2021-10-08 15:01:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-02 04:34:40 +08:00
|
|
|
/// Return if this file is the no media marker
|
|
|
|
///
|
|
|
|
/// A no media marker marks the parent dir and its sub dirs as not containing
|
|
|
|
/// media files of interest
|
2022-02-08 03:22:41 +08:00
|
|
|
bool isNoMediaMarker(File file) => isNoMediaMarkerPath(file.path);
|
|
|
|
|
|
|
|
/// See [isNoMediaMarker]
|
|
|
|
bool isNoMediaMarkerPath(String path) {
|
|
|
|
final filename = path_lib.basename(path);
|
2022-01-02 04:34:40 +08:00
|
|
|
return filename == ".nomedia" || filename == ".noimage";
|
|
|
|
}
|
|
|
|
|
2022-08-20 18:35:59 +08:00
|
|
|
/// Return if there's missing metadata in [file]
|
2022-08-27 23:32:31 +08:00
|
|
|
///
|
|
|
|
/// Current this function will check both [File.metadata] and [File.location]
|
2022-08-20 18:35:59 +08:00
|
|
|
bool isMissingMetadata(File file) =>
|
2022-08-27 23:32:31 +08:00
|
|
|
isSupportedImageFormat(file) &&
|
|
|
|
(file.metadata == null || file.location == null);
|
2022-08-20 18:35:59 +08:00
|
|
|
|
2022-10-29 19:05:23 +08:00
|
|
|
DateTime getBestDateTime({
|
|
|
|
DateTime? overrideDateTime,
|
|
|
|
DateTime? dateTimeOriginal,
|
|
|
|
DateTime? lastModified,
|
2023-03-13 19:55:39 +08:00
|
|
|
}) =>
|
|
|
|
overrideDateTime ?? dateTimeOriginal ?? lastModified ?? clock.now().toUtc();
|
2022-10-29 19:05:23 +08:00
|
|
|
|
2022-10-15 22:29:18 +08:00
|
|
|
final supportedFormatMimes = [
|
2021-04-10 12:28:12 +08:00
|
|
|
"image/jpeg",
|
|
|
|
"image/png",
|
|
|
|
"image/webp",
|
2021-04-15 05:09:31 +08:00
|
|
|
"image/heic",
|
2021-06-22 13:24:37 +08:00
|
|
|
"image/gif",
|
2021-08-05 15:04:12 +08:00
|
|
|
"video/mp4",
|
2021-09-20 16:53:42 +08:00
|
|
|
"video/quicktime",
|
2023-08-27 18:58:05 +08:00
|
|
|
if ([NpPlatform.android, NpPlatform.web].contains(getRawPlatform()))
|
|
|
|
"video/webm",
|
2021-04-10 12:28:12 +08:00
|
|
|
];
|
2021-05-07 04:11:31 +08:00
|
|
|
|
2022-10-15 22:29:18 +08:00
|
|
|
final supportedImageFormatMimes =
|
|
|
|
supportedFormatMimes.where((f) => f.startsWith("image/")).toList();
|
|
|
|
|
2023-04-13 23:32:31 +08:00
|
|
|
final supportedVideoFormatMimes =
|
|
|
|
supportedFormatMimes.where((f) => f.startsWith("video/")).toList();
|
|
|
|
|
2021-05-07 04:11:31 +08:00
|
|
|
const _metadataSupportedFormatMimes = [
|
|
|
|
"image/jpeg",
|
|
|
|
"image/heic",
|
|
|
|
];
|