Read/Write Files

Solid Pods can store regular files (e.g., PDFs, photos, etc.) in addition to storing structured data as Things and SolidDatasets (see Read/Write Data to store structured data).

The solid-client library provides various file handling functions. Files are represented as Blobs.

Access Control

Like other data stored in a Solid Pod, each file is a Resource with a distinct URL, which may or may not include the file extension, (e.g., .jpg extension for an image file). The same access control mechanism applies to these files as applies to any other Resource in the Pod.

Retrieve a File

To read a file, you can use getFile() to fetch the file content at the specified URL. The getFile() returns the content as a Blob. Once fetched, you can decode appropriately.

import {
  getFile,
  isRawData,
  getContentType,
  getSourceUrl,
} from "@inrupt/solid-client";

const file = await getFile(
  "https://example.com/some/interesting/file"
);
// file is a Blob (see https://developer.mozilla.org/docs/Web/API/Blob)
console.log(
  `Fetched a ${getContentType(file)} file from ${getSourceUrl(file)}.`
);
console.log(`The file is ${isRawData(file) ? "not " : ""}a dataset.`);

The above example uses:

Note

You can use getFile() to retrieve a file that contains structured data. In this case, getContentType() on the returned Blob might return text/turtle; charset=UTF-8 if the user’s Pod server defaults to returning structured data in that format. The isRawData() on the Blob returns false.

Write a File

When writing a file to a Pod, you can:

Write a File to a Specific URL

To specify the file’s destination URL during the save, use overwriteFile(). Once saved, you know the exact URL of the saved file.

Note

  • If a file already exists at that URL, the function overwrites the existing file.

  • When saving the file to the destination URL, the Solid server creates any intermediate Container as needed.

import { overwriteFile } from "@inrupt/solid-client";

const savedFile = await overwriteFile(
  "https://example.com/some/new/file",
  new Blob(["This is a plain piece of text"], { type: "plain/text" })
  // Or in Node:
  // Buffer.from("This is a plain piece of text", "utf8"), { type: "plain/text" })
);
console.log("File saved!");

In the example, if the some and new Containers do not exist when saving the file, the Solid server creates them both in order to save the file to the specified URL.

Save a File into a Parent Container

To specify only the URL for the parent Container during the save, i.e., the Solid server determines the name of your file in the Container, use saveFileInContainer(). To determine the saved filename, you can use getSourceUrl.

Note

  • With saveFileInContainer(), you do not control the name, and thus the URL, of your file. That is,

    • Although saveFileInContainer() can accept a slug as an option, there is no guarantee about how the Solid server will use the slug, if at all.

    • Even if the server uses the slug as the file name, if the slug matches an already existing file in the specified Container, the server creates a new name for your file. That is, the function does not overwrite existing files.

  • When saving the file into the specified Container, the Container must exist. Otherwise, the save operation fails.

import { saveFileInContainer, getSourceUrl } from "@inrupt/solid-client";

const savedFile = await saveFileInContainer(
  "https://example.com/some/folder",
  new Blob(["This is a plain piece of text"], { type: "plain/text" }),
  { slug: "new-file" }
);

console.log(`File saved at ${getSourceUrl(savedFile)}`);

After saving the file, the example uses getSourceUrl on the returned file to determine the saved filename.

Delete a File

To delete a file, you can use deleteFile() to remove the file at the specified URL.

import { deleteFile } from "@inrupt/solid-client";

await deleteFile(
  "https://example.com/some/boring/file"
);
console.log("File deleted !");