feat: bud-04 (mirror)

feat: migrate void.cat
This commit is contained in:
2024-12-16 17:20:26 +00:00
parent 5e92134b5c
commit c024cd4a4e
10 changed files with 228 additions and 74 deletions

View File

@ -15,14 +15,14 @@ export default function Button({
if (!onClick) return;
try {
setLoading(true);
onClick(e);
await onClick(e);
} finally {
setLoading(false);
}
}
return (
<button
className={`py-2 px-4 rounded-md border-0 text-sm font-semibold bg-neutral-700 hover:bg-neutral-600 ${className} ${props.disabled ? "opacity-50" : ""}`}
className={`py-2 px-4 rounded-md border-0 text-sm font-semibold bg-neutral-700 hover:bg-neutral-600 ${className} ${props.disabled || loading ? "opacity-50" : ""}`}
onClick={doClick}
{...props}
disabled={loading || (props.disabled ?? false)}

File diff suppressed because one or more lines are too long

View File

@ -50,6 +50,18 @@ export class Blossom {
}
}
async mirror(url: string) {
const rsp = await this.#req("mirror", "PUT", "mirror", JSON.stringify({ url }), undefined, {
"content-type": "application/json"
});
if (rsp.ok) {
return (await rsp.json()) as BlobDescriptor;
} else {
const text = await rsp.text();
throw new Error(text);
}
}
async list(pk: string) {
const rsp = await this.#req(`list/${pk}`, "GET", "list");
if (rsp.ok) {
@ -76,6 +88,7 @@ export class Blossom {
term: string,
body?: BodyInit,
tags?: Array<Array<string>>,
headers?: Record<string, string>,
) {
throwIfOffline();
@ -100,6 +113,7 @@ export class Blossom {
method,
body,
headers: {
...headers,
accept: "application/json",
authorization: await auth(url, method),
},

View File

@ -17,6 +17,7 @@ export default function Upload() {
const [toUpload, setToUpload] = useState<File>();
const [self, setSelf] = useState<AdminSelf>();
const [error, setError] = useState<string>();
const [bulkPrgress, setBulkProgress] = useState<number>();
const [results, setResults] = useState<Array<object>>([]);
const [listedFiles, setListedFiles] = useState<Nip96FileList>();
const [adminListedFiles, setAdminListedFiles] = useState<Nip96FileList>();
@ -26,7 +27,8 @@ export default function Upload() {
const login = useLogin();
const pub = usePublisher();
const myLegacyFiles = login ? (Report as Record<string, Array<string>>)[login.pubkey] : [];
const legacyFiles = Report as Record<string, Array<string>>;
const myLegacyFiles = login ? legacyFiles[login.pubkey] : [];
const url = import.meta.env.VITE_API_URL || `${location.protocol}//${location.host}`;
async function doUpload() {
@ -110,6 +112,20 @@ export default function Upload() {
}
}
async function migrateLegacy() {
if (!pub) return;
const uploader = new Blossom(url, pub);
let ctr = 0;
for (const f of myLegacyFiles) {
try {
await uploader.mirror(`https://void.cat/d/${f}`);
} catch (e) {
console.error(e);
}
setBulkProgress(ctr++ / myLegacyFiles.length);
}
}
useEffect(() => {
listUploads(listedPage);
}, [listedPage]);
@ -190,13 +206,14 @@ export default function Upload() {
<div className="flex flex-col gap-4 font-bold">
You have {myLegacyFiles.length.toLocaleString()} files which can be migrated from void.cat
<div className="flex gap-2">
<Button>
<Button onClick={() => migrateLegacy()}>
Migrate Files
</Button>
<Button onClick={() => setShowLegacy(true)}>
Show Files
<Button onClick={() => setShowLegacy(s => !s)}>
{!showLegacy ? "Show Files" : "Hide Files"}
</Button>
</div>
{bulkPrgress !== undefined && <progress value={bulkPrgress} />}
</div>
)}
{showLegacy && (