AppFlowy/frontend/appflowy_flutter/lib/util/string_extension.dart
Lucas.Xu 792573f46d
feat: optimize image block (#4553)
* feat: add tooltip for maximum image size

* feat: add maximum upload image size tooltip

* feat: limit image size to 10MB

* fix: disable copy link option for cloud image

* fix: disable copy link option for cloud image

* feat: use regex to match the appflowy.cloud image
2024-01-30 22:39:42 +08:00

39 lines
999 B
Dart

import 'dart:io';
import 'package:flutter/material.dart';
extension StringExtension on String {
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();
}
/// 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;
}
/// Returns if the string is a appflowy cloud url.
bool get isAppFlowyCloudUrl {
return RegExp(r'^(https:\/\/)(.*)(\.appflowy\.cloud\/)(.*)').hasMatch(this);
}
}