2024-01-30 14:39:42 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
2023-09-04 09:28:19 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
2024-03-06 15:31:30 +00:00
|
|
|
import 'package:appflowy/shared/patterns/common_patterns.dart';
|
|
|
|
|
2024-01-30 14:39:42 +00:00
|
|
|
extension StringExtension on String {
|
2023-09-04 09:28:19 +00:00
|
|
|
static const _specialCharacters = r'\/:*?"<>| ';
|
|
|
|
|
|
|
|
/// Encode a string to a file name.
|
|
|
|
///
|
|
|
|
/// Normalizes the string to remove special characters and replaces the "\/:*?"<>|" with underscores.
|
|
|
|
String toFileName() {
|
|
|
|
final buffer = StringBuffer();
|
|
|
|
for (final character in characters) {
|
|
|
|
if (_specialCharacters.contains(character)) {
|
|
|
|
buffer.write('_');
|
|
|
|
} else {
|
|
|
|
buffer.write(character);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buffer.toString();
|
|
|
|
}
|
2024-01-30 14:39:42 +00:00
|
|
|
|
|
|
|
/// Returns the file size of the file at the given path.
|
|
|
|
///
|
|
|
|
/// Returns null if the file does not exist.
|
|
|
|
int? get fileSize {
|
|
|
|
final file = File(this);
|
|
|
|
if (file.existsSync()) {
|
|
|
|
return file.lengthSync();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-03-06 15:31:30 +00:00
|
|
|
/// Returns true if the string is a appflowy cloud url.
|
|
|
|
bool get isAppFlowyCloudUrl => appflowyCloudUrlRegex.hasMatch(this);
|
2023-09-04 09:28:19 +00:00
|
|
|
}
|