2024-01-30 14:39:42 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
2024-03-06 15:31:30 +00:00
|
|
|
import 'package:appflowy/shared/patterns/common_patterns.dart';
|
2024-05-07 04:13:52 +00:00
|
|
|
import 'package:appflowy_editor/appflowy_editor.dart';
|
|
|
|
import 'package:flowy_infra/theme_extension.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2024-03-06 15:31:30 +00:00
|
|
|
|
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);
|
2024-05-07 04:13:52 +00:00
|
|
|
|
|
|
|
/// Returns the color of the string.
|
|
|
|
///
|
|
|
|
/// ONLY used for the cover.
|
|
|
|
Color? coverColor(BuildContext context) {
|
|
|
|
// try to parse the color from the tint id,
|
|
|
|
// if it fails, try to parse the color as a hex string
|
|
|
|
return FlowyTint.fromId(this)?.color(context) ?? tryToColor();
|
|
|
|
}
|
2023-09-04 09:28:19 +00:00
|
|
|
}
|