registerFile method

String? registerFile(
  1. String filePath
)

Register a file to be served and get the URL

Implementation

String? registerFile(String filePath) {
  try {
    if (_server == null) {
      debugPrint('⚠️ Server not started, cannot register file');
      return null;
    }

    final file = File(filePath);
    if (!file.existsSync()) {
      debugPrint('⚠️ File does not exist: $filePath');
      return null;
    }

    // Create a unique path based on file path hash
    final hash = filePath.hashCode.abs().toRadixString(36);
    final extension = filePath.split('.').last;
    final urlPath = '/art/$hash.$extension';

    // Cache the mapping
    _fileCache[urlPath] = filePath;

    final port = _server!.port;
    final url = 'http://0.0.0.0:$port$urlPath';

    debugPrint('📝 Registered: $filePath -> $url');
    return url;
  } catch (e) {
    debugPrint('❌ Error registering file: $e');
    return null;
  }
}