writeFile static method

Future<bool> writeFile(
  1. String relativePath,
  2. String content, {
  3. PodContentType contentType = PodContentType.json,
  4. bool createParentDirs = true,
})

Write a file to the POD.

relativePath - Path relative to the data directory. content - Content to write. contentType - MIME type of the content. createParentDirs - Whether to create parent directories if missing.

Returns true if write was successful.

Implementation

static Future<bool> writeFile(
  String relativePath,
  String content, {
  PodContentType contentType = PodContentType.json,
  bool createParentDirs = true,
}) async {
  if (!await PodAuth.isLoggedIn()) {
    debugPrint('PodFileSystem.writeFile() - not logged in');
    return false;
  }

  try {
    final url = await PodPath.getFileUrl(relativePath);

    // Check if parent directory exists and create if needed.

    if (createParentDirs) {
      final parentPath = PodPath.getParentPath(
        PodPath.getFilePath(relativePath),
      );
      await _ensureDirectoryExists(parentPath);
    }

    final response = await PodHttp.put(
      url,
      content,
      contentType: contentType,
    );

    if (response.isSuccess) {
      debugPrint('PodFileSystem.writeFile() - success: $relativePath');
      return true;
    } else {
      debugPrint(
        'PodFileSystem.writeFile() - error ${response.statusCode}: ${response.body}',
      );
      return false;
    }
  } catch (e) {
    debugPrint('PodFileSystem.writeFile() - exception: $e');
    return false;
  }
}