createItem method

Future<String> createItem(
  1. bool fileFlag,
  2. String itemName,
  3. String itemBody, {
  4. String? fileLoc,
})

Create a directory or a file

Implementation

Future<String> createItem(bool fileFlag, String itemName, String itemBody,
    {String? fileLoc}) async {
  String itemLoc = '';
  String itemSlug = '';
  String itemType = '';
  String contentType = '';

  /// Set up directory or file parameters
  if (fileFlag) {
    /// This is a file (resource)
    itemLoc = fileLoc!;
    itemSlug = itemName;
    contentType = 'text/turtle';
    itemType = '<http://www.w3.org/ns/ldp#Resource>; rel="type"';
  } else {
    /// This is a directory (container)
    itemSlug = itemName;
    contentType = 'application/octet-stream';
    itemType = '<http://www.w3.org/ns/ldp#BasicContainer>; rel="type"';
  }

  String encKeyUrl = webId.replaceAll('profile/card#me', itemLoc);
  String dPopToken =
      genDpopToken(encKeyUrl, rsaKeyPair, publicKeyJwk, 'POST');

  /// The POST request will create the item in the server
  final createResponse = await http.post(
    Uri.parse(encKeyUrl),
    headers: <String, String>{
      'Accept': '*/*',
      'Authorization': 'DPoP $accessToken',
      'Connection': 'keep-alive',
      'Content-Type': contentType,
      'Link': itemType,
      'Slug': itemSlug,
      'DPoP': dPopToken,
    },
    body: itemBody,
  );

  if (createResponse.statusCode == 200 || createResponse.statusCode == 201) {
    /// If the server did return a 200 OK response,
    /// then parse the JSON.
    return 'ok';
  } else {
    /// If the server did not return a 200 OK response,
    /// then throw an exception.
    throw Exception('Failed to create folder! Try again in a while.');
  }
}