Pick up files added to the Blog folder in Drive, and name nginx errors
Opening Tools → Media → Files (or a Choose a file picker) now looks in the shared drive's Blog folder, at most every 30 seconds, and adds anything put there in Drive itself: shared as anyone with the link, previewed, Google Docs, Sheets and Slides included, folders and shortcuts left out. A file Drive won't share is named in a note and tried again every ten minutes. A reply that isn't the API's (nginx's own error page) now says what it was: a 413 names nginx's upload limit, a 502 the service, a 504 the timeout, instead of "Server did not return JSON". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
5f13273e69
commit
6191322fea
6 changed files with 143 additions and 25 deletions
|
|
@ -13,7 +13,8 @@
|
|||
file" (a field with `files: true`, fields.tsx)
|
||||
|
||||
A link field stores the file's Drive link, a plain URL, so nothing
|
||||
else needs to know where it came from.
|
||||
else needs to know where it came from. Files put in the Drive
|
||||
folder by hand are listed too (the server picks them up).
|
||||
═══════════════════════════════════════════════════════════════ */
|
||||
|
||||
import {
|
||||
|
|
@ -54,20 +55,19 @@ export const FILE_HELP =
|
|||
|
||||
const ACCEPT = ".pdf,.docx,.xlsx,.pptx,.mp3,.m4a,.zip";
|
||||
|
||||
/* An icon per kind of file; Google's own Docs, Sheets and Slides
|
||||
(application/vnd.google-apps.*) count as their Office kin. */
|
||||
const TYPE_ICONS: [RegExp, typeof FileIcon][] = [
|
||||
[/pdf$/, FilePdf],
|
||||
[/wordprocessing|google-apps\.document$/, FileDoc],
|
||||
[/spreadsheet/, FileXls],
|
||||
[/presentation/, FilePpt],
|
||||
[/^audio\//, FileAudio],
|
||||
[/zip$/, FileZip],
|
||||
];
|
||||
|
||||
function TypeIcon({ mime }: { mime: string }) {
|
||||
const Icon = mime === "application/pdf"
|
||||
? FilePdf
|
||||
: mime.includes("wordprocessing")
|
||||
? FileDoc
|
||||
: mime.includes("spreadsheet")
|
||||
? FileXls
|
||||
: mime.includes("presentation")
|
||||
? FilePpt
|
||||
: mime.startsWith("audio/")
|
||||
? FileAudio
|
||||
: mime === "application/zip"
|
||||
? FileZip
|
||||
: FileIcon;
|
||||
const Icon = TYPE_ICONS.find(([test]) => test.test(mime))?.[1] ?? FileIcon;
|
||||
return <Icon aria-hidden="true" weight="bold" className="h-8 w-8 text-link" />;
|
||||
}
|
||||
|
||||
|
|
@ -76,6 +76,8 @@ function TypeIcon({ mime }: { mime: string }) {
|
|||
export function useDriveFiles(query: string) {
|
||||
const [items, setItems] = useState<DriveFile[] | null>(null);
|
||||
const [ready, setReady] = useState(true);
|
||||
// Files in the Drive folder that Drive wouldn't share publicly.
|
||||
const [refused, setRefused] = useState<string[]>([]);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const reload = useCallback(() => {
|
||||
|
|
@ -85,12 +87,13 @@ export function useDriveFiles(query: string) {
|
|||
.then((data) => {
|
||||
setItems(data.files);
|
||||
setReady(data.ready);
|
||||
setRefused(data.refused ?? []);
|
||||
})
|
||||
.catch((err) => setError(err instanceof ApiError ? err.message : "Couldn't load the files."));
|
||||
}, [query]);
|
||||
|
||||
useEffect(reload, [reload]);
|
||||
return { items, ready, error, reload, setItems };
|
||||
return { items, ready, refused, error, reload, setItems };
|
||||
}
|
||||
|
||||
/* ── Upload ──────────────────────────────────────────────────── */
|
||||
|
|
@ -203,7 +206,7 @@ export function FilePicker({
|
|||
}) {
|
||||
const { user } = useAuth();
|
||||
const [query, setQuery] = useState("");
|
||||
const { items, ready, error, setItems } = useDriveFiles(query);
|
||||
const { items, ready, refused, error, setItems } = useDriveFiles(query);
|
||||
const dialogRef = useRef<HTMLDialogElement>(null);
|
||||
|
||||
useEffect(() => {
|
||||
|
|
@ -259,6 +262,7 @@ export function FilePicker({
|
|||
|
||||
<div className="overflow-y-auto px-5 py-4">
|
||||
{!ready && <p className="mb-3 text-sm text-danger">Google Drive isn't set up on the server, so nothing can be uploaded.</p>}
|
||||
<RefusedNote names={refused} />
|
||||
{error ? (
|
||||
<p className="text-sm text-danger">{error}</p>
|
||||
) : items === null ? (
|
||||
|
|
@ -272,6 +276,19 @@ export function FilePicker({
|
|||
);
|
||||
}
|
||||
|
||||
/* Files someone put in the Drive folder that Drive wouldn't share
|
||||
with anyone who has the link, so they aren't listed. */
|
||||
export function RefusedNote({ names }: { names: string[] }) {
|
||||
if (names.length === 0) return null;
|
||||
return (
|
||||
<p className="mb-3 text-sm text-danger">
|
||||
In the Drive folder but not listed, because Drive wouldn't share {names.length === 1 ? "it" : "them"} with anyone
|
||||
who has the link: {names.join(", ")}. Check the sharing settings (docs/google-drive-and-mail.md); they're tried
|
||||
again every ten minutes.
|
||||
</p>
|
||||
);
|
||||
}
|
||||
|
||||
/* "Choose a file" under a link field. */
|
||||
export function ChooseFileButton({ value, onPick }: { value?: string | null; onPick: (file: DriveFile) => void }) {
|
||||
const [open, setOpen] = useState(false);
|
||||
|
|
|
|||
|
|
@ -29,6 +29,13 @@ export class ApiError extends Error {
|
|||
}
|
||||
}
|
||||
|
||||
/* nginx's answers of its own, in words that point at the fix. */
|
||||
const NOT_THE_API: Record<number, string> = {
|
||||
413: "Too large for the web server: nginx's upload limit for this address is lower than the file (HTTP 413). The nginx block for it is in the docs.",
|
||||
502: "The API isn't answering (HTTP 502). Check that the ngu-api service is running.",
|
||||
504: "The API took too long to answer (HTTP 504). Try again; if it keeps happening, nginx's timeout for this address is too short.",
|
||||
};
|
||||
|
||||
async function request(path, options: RequestInit = {}) {
|
||||
const response = await fetch(`${BASE}${path}`, {
|
||||
...options,
|
||||
|
|
@ -39,9 +46,11 @@ async function request(path, options: RequestInit = {}) {
|
|||
|
||||
const type = response.headers.get("content-type") ?? "";
|
||||
if (!type.includes("application/json")) {
|
||||
// Usually the SPA fallback returning index.html for a URL the
|
||||
// API doesn't serve. Parsing it would throw something useless.
|
||||
throw new ApiError("Server did not return JSON.", {
|
||||
// Not the API's answer: the SPA fallback returning index.html for
|
||||
// a URL the API doesn't serve, or nginx's own error page when the
|
||||
// request never reached the API (nothing in its journal then).
|
||||
// Parsing it would throw something useless; say what it was.
|
||||
throw new ApiError(NOT_THE_API[response.status] ?? `The server didn't answer as expected (HTTP ${response.status}).`, {
|
||||
status: response.status,
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ import {
|
|||
FILE_HELP,
|
||||
FileList,
|
||||
FileUploadButton,
|
||||
RefusedNote,
|
||||
useDriveFiles,
|
||||
} from "../../components/admin/DriveFiles.tsx";
|
||||
import {
|
||||
|
|
@ -142,7 +143,7 @@ function CopyPath({ url, label = "Copy path" }: { url: string; label?: string })
|
|||
function FilesTab() {
|
||||
const { user } = useAuth();
|
||||
const [query, setQuery] = useState("");
|
||||
const { items, ready, error, setItems } = useDriveFiles(query);
|
||||
const { items, ready, refused, error, setItems } = useDriveFiles(query);
|
||||
|
||||
return (
|
||||
<>
|
||||
|
|
@ -167,6 +168,7 @@ function FilesTab() {
|
|||
</p>
|
||||
)}
|
||||
<div className="mt-6">
|
||||
<RefusedNote names={refused} />
|
||||
{error ? (
|
||||
<p className="text-sm text-danger">{error}</p>
|
||||
) : items === null ? (
|
||||
|
|
@ -217,7 +219,7 @@ export default function AdminMedia() {
|
|||
<p className="text-sm text-muted">
|
||||
{tab === "images"
|
||||
? "Uploaded images. Image fields in every editor pick from here."
|
||||
: "Files in the shared Google Drive, for pages to link to. Link fields pick from here with Choose a file."}
|
||||
: "Files in the shared Google Drive's Blog folder, for pages to link to, including any put there in Drive itself. Link fields pick from here with Choose a file."}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue