Skip to Content
dimah-s3v2.8.1
Share Links

The Shared Page

Build the public page a share link opens, with a password form, a folder listing on ObjectList, and downloads.

A share link opens a page of your app, the one share({ url }) points at. It reads the token from the URL and calls the visitor actions, which need no session. This guide builds it in the Next.js App Router. SharedView from @dimah-s3/ui/share is the same page as one component, and useShareVisit from @dimah-s3/share/react holds its state (opening, the password, downloads, and a link that stops opening) for markup of your own.

Read the token

app/s/[token]/page.tsx
import { SharedView } from "./shared-view";

export default async function SharedPage({
  params,
}: {
  params: Promise<{ token: string }>;
}) {
  const { token } = await params;
  return <SharedView token={token} />;
}

openLink says what the link shares. A password-protected link answers locked: true and nothing more until the password is entered:

app/s/[token]/shared-view.tsx
"use client";

import type { ShareVisitorLink } from "@dimah-s3/share/client";
import { useEffect, useState } from "react";
import { s3Client } from "@/lib/s3-client";

export function SharedView({ token }: { token: string }) {
  const [access, setAccess] = useState<string>();
  const [link, setLink] = useState<ShareVisitorLink>();
  const [error, setError] = useState<unknown>();

  useEffect(() => {
    s3Client.share
      .openLink({ token, access })
      .then(({ link }) => setLink(link))
      .catch(setError);
  }, [token, access]);

  if (error) return <LinkError error={error} />;
  if (!link) return null;
  if (link.locked) return <PasswordForm token={token} onUnlock={setAccess} />;
  return link.type === "folder" ? (
    <SharedFolder token={token} access={access} />
  ) : (
    <SharedFile token={token} access={access} name={link.object.name} />
  );
}

Ask for the password

unlockLink trades the password for an access proof. Keep it in state and send it with every later call; it expires after an hour by default.

function PasswordForm({
  token,
  onUnlock,
}: {
  token: string;
  onUnlock: (access: string) => void;
}) {
  const [wrong, setWrong] = useState(false);
  return (
    <form
      action={async (data) => {
        try {
          const { access } = await s3Client.share.unlockLink({
            token,
            password: String(data.get("password")),
          });
          onUnlock(access);
        } catch {
          setWrong(true);
        }
      }}
    >
      <input name="password" type="password" required autoFocus />
      {wrong && <p role="alert">Wrong password</p>}
      <button type="submit">Open</button>
    </form>
  );
}

List a shared folder

shareObjectSource is a read-only useObjectList source over listShared, so ObjectList renders the folder with its breadcrumb, search, and sort. The top of the share is its root; folders above it stay hidden.

import { useObjectList } from "@dimah-s3/react";
import { downloadSharedFile, shareObjectSource } from "@dimah-s3/share/client";
import { ObjectList } from "@dimah-s3/ui";

function SharedFolder({ token, access }: { token: string; access?: string }) {
  const list = useObjectList({
    ...shareObjectSource(s3Client.share, { token, access }),
    queryKey: ["share", token, access],
  });
  return (
    <ObjectList
      list={list}
      rootLabel="Shared folder"
      renderActions={(entry) =>
        entry.kind === "file" ? (
          <button
            type="button"
            onClick={() =>
              downloadSharedFile(s3Client.share, entry.id, { token, access })
            }
          >
            Download
          </button>
        ) : null
      }
    />
  );
}

Download a shared file

downloadSharedFile asks downloadShared for a short-lived URL and sends the browser to it. Each call counts one download, and it resolves with the response, whose downloadsLeft keeps a counter on the page current. On an object link the file id is optional:

function SharedFile({
  token,
  access,
  name,
}: {
  token: string;
  access?: string;
  name: string;
}) {
  return (
    <button
      type="button"
      onClick={() =>
        downloadSharedFile(s3Client.share, undefined, { token, access })
      }
    >
      Download {name}
    </button>
  );
}

Pass disposition: "inline" to open a file in the tab instead. It stays inline only for a type on the route's inlineTypes.

Every refusal has its own code, and the visitor already holds the token, so you can say which one applies. useFormatError words every SHARE_* code, in your translations:

import { useFormatError } from "@dimah-s3/react";

function LinkError({ error }: { error: unknown }) {
  const formatError = useFormatError();
  return <p>{formatError(error)}</p>;
}

Render on the server

s3.share.api runs the visitor actions in process, so a Server Component can open the link before the page reaches the browser:

app/s/[token]/page.tsx
import { getS3 } from "@/lib/s3";

const { link } = await getS3().share.api.openLink({ token });

It runs your instance guard as openLink, like the HTTP call.

To say who sent the link, read its row with s3.share.links.findByToken(token) once openLink succeeds: its scope and target lead to your own records, such as the project a folder belongs to. It runs no guard and returns the link whatever its status, so show only what a visitor may see.

On this page