|
| 1 | +import { |
| 2 | + ActionFunctionArgs, |
| 3 | + LoaderFunctionArgs, |
| 4 | + MetaFunction, |
| 5 | + redirect, |
| 6 | +} from "@remix-run/node"; |
| 7 | +import { Form, useLoaderData } from "@remix-run/react"; |
| 8 | +import { eq } from "drizzle-orm"; |
| 9 | +import { parseFeed } from "@rowanmanning/feed-parser"; |
| 10 | + |
| 11 | +import { authenticator } from "~/services/auth.server"; |
| 12 | +import { adminOnlyLoader } from "~/util/adminOnlyLoader"; |
| 13 | +import { User } from "~/types/User"; |
| 14 | + |
| 15 | +export const meta: MetaFunction = () => { |
| 16 | + return [ |
| 17 | + { title: "Import RSS/Atom Links - Regex Zone" }, |
| 18 | + { name: "description", content: "Import JSON link dump from Pinboard.in" }, |
| 19 | + ]; |
| 20 | +}; |
| 21 | + |
| 22 | +export const action = async ({ |
| 23 | + request, |
| 24 | +}: ActionFunctionArgs) => { |
| 25 | + |
| 26 | + const user: User | null = await authenticator.isAuthenticated(request); |
| 27 | + if (!user) { |
| 28 | + return redirect("/auth/"); |
| 29 | + } |
| 30 | + if (!user.isAdmin) { |
| 31 | + throw new Response("Unauthorized", { status: 401 }); |
| 32 | + } |
| 33 | + |
| 34 | + const formData = await request.formData(); |
| 35 | + |
| 36 | + const formDataValue = formData.get("feedurl"); |
| 37 | + if (!formDataValue) { |
| 38 | + throw new Response("No url specified", { status: 400 }); |
| 39 | + } |
| 40 | + |
| 41 | + const feedUrl = formDataValue as string; |
| 42 | + try { |
| 43 | + new URL(feedUrl); |
| 44 | + } catch (e) { |
| 45 | + throw new Response("Invalid URL", { status: 400 }); |
| 46 | + } |
| 47 | + |
| 48 | + const feedResponse = await fetch(feedUrl); |
| 49 | + if (!feedResponse.ok) { |
| 50 | + throw new Response("Error fetching feed", { status: 400 }); |
| 51 | + } |
| 52 | + |
| 53 | + const feedtext = await feedResponse.text(); |
| 54 | + let feed:Feed; |
| 55 | + |
| 56 | + try { |
| 57 | + feed = await parseFeed(feedtext); |
| 58 | + } catch (err: unknown) { |
| 59 | + throw new Response("Error parsing feed", { status: 400 }); |
| 60 | + } |
| 61 | + |
| 62 | + let count = 0; |
| 63 | + for (const entry of feed.items) { |
| 64 | + const url = entry.url; |
| 65 | + if (!url) { |
| 66 | + continue; |
| 67 | + } |
| 68 | + const existing = await dborm.select().from(regex_link).where(eq(regex_link.rxl_url, url)); |
| 69 | + if (existing.length > 0) { |
| 70 | + console.log(`found ${url}, stopping...`); |
| 71 | + break; |
| 72 | + } |
| 73 | + const values = { |
| 74 | + rxl_url: entry.url, |
| 75 | + rxl_title: entry.title || "(untitled)", |
| 76 | + rxl_tags: entry.categories[0].term.split(' '), |
| 77 | + rxl_created_at: new Date(entry.published || "1970-01-01T00:00:00Z"), |
| 78 | + } |
| 79 | + console.log(JSON.stringify(values, null, 2)); |
| 80 | + await dborm.insert(regex_link).values(values); |
| 81 | + count++; |
| 82 | + } |
| 83 | + |
| 84 | + const session = await cookieStorage.getSession(request.headers.get("Cookie")); |
| 85 | + session.flash("message", { type: 'info', message: `You uploaded ${count} entries from ${feedUrl}!` }); |
| 86 | + return redirect('/links/', { headers: { "Set-Cookie": await cookieStorage.commitSession(session) } }); |
| 87 | + |
| 88 | + /** |
| 89 | + * LATER: it would be nice if Remix supported action responses but `shouldRevalidate` doesn't seem to work |
| 90 | + * |
| 91 | + console.log("about to return"); |
| 92 | + return new Response(`You uploaded ${data.length} bytes`, { |
| 93 | + status: 200, |
| 94 | + headers: { |
| 95 | + 'Content-type': "text/plain; charset=utf-8", |
| 96 | + } |
| 97 | + }); |
| 98 | + */ |
| 99 | + |
| 100 | +}; |
| 101 | + |
| 102 | +export function loader(args: LoaderFunctionArgs) { |
| 103 | + const user = adminOnlyLoader(args); |
| 104 | + |
| 105 | + return { |
| 106 | + user, |
| 107 | + url: process.env.BOOKMARK_FEED_URL, |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +import type { ShouldRevalidateFunction } from "@remix-run/react"; |
| 112 | +import { cookieStorage } from "~/services/session.server"; |
| 113 | +import { dborm } from "~/db/connection.server"; |
| 114 | +import { regex_link } from "~/db/schema"; |
| 115 | +import { Feed } from "@rowanmanning/feed-parser/lib/feed/base"; |
| 116 | + |
| 117 | +export const shouldRevalidate: ShouldRevalidateFunction = (params) => { |
| 118 | + console.log("shouldRevalidate", params); // never called |
| 119 | + return params.defaultShouldRevalidate; |
| 120 | +}; |
| 121 | + |
| 122 | +export default function Import() { |
| 123 | + |
| 124 | + const data = useLoaderData<typeof loader>(); |
| 125 | + |
| 126 | + const url = data.url; |
| 127 | + |
| 128 | + |
| 129 | + return ( |
| 130 | + <> |
| 131 | + <h1 className="py-2">Import RSS/Atom Feed Links</h1> |
| 132 | + <div className="d-flex justify-content-center"> |
| 133 | + <Form method="post" className="border p-3" action="/links/import-feed.html" encType="multipart/form-data"> |
| 134 | + <div className="mb-3"> |
| 135 | + <label htmlFor="feedurl" className="form-label">RSS/Atom Feed</label> |
| 136 | + <input className="form-control" type="url" id="feedurl" name="feedurl" value={url}/> |
| 137 | + </div> |
| 138 | + <button type="submit" className="btn btn-primary">Import</button> |
| 139 | + </Form> |
| 140 | + </div> |
| 141 | + </> |
| 142 | + ); |
| 143 | +} |
0 commit comments