import * as fs from 'node:fs'; import * as path from 'node:path'; import * as p from '@clack/prompts'; import { type CreateBookmarkRequestParentId, ResponseError } from '@fbrc/sdk'; import { api } from './utils/client'; import { getRandomDelegatedWorkspaceId, randomString } from './utils/random'; const DATA_DIR = path.join(__dirname, 'data'); const MIME_TYPES: Record = { '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.gif': 'image/gif', '.webp': 'image/webp', '.svg': 'image/svg+xml', '.pdf': 'application/pdf', '.txt': 'text/plain', '.md': 'text/markdown', '.json': 'application/json', }; function getMimeType(filename: string): string { const ext = path.extname(filename).toLowerCase(); return MIME_TYPES[ext] ?? 'application/octet-stream'; } function randomFilename(originalPath: string): string { const ext = path.extname(originalPath); return `${randomString(12)}${ext}`; } export async function addDummyFiles() { const workspaceId = await getRandomDelegatedWorkspaceId(); if (!workspaceId) { p.log.error('No delegated workspace found'); return; } let entries: fs.Dirent[]; try { entries = fs.readdirSync(DATA_DIR, { withFileTypes: true }); } catch (err) { p.log.error(`Could not read data dir ${DATA_DIR}: ${err}`); return; } const files = entries.filter((e) => e.isFile()); if (files.length === 0) { p.log.warn(`No files in ${DATA_DIR}`); return; } const s = p.spinner(); s.start('Uploading dummy files'); for (const dirent of files) { const filePath = path.join(DATA_DIR, dirent.name); const buffer = fs.readFileSync(filePath); const size = buffer.length; const mimeType = getMimeType(dirent.name); const filename = randomFilename(dirent.name); s.message(`Aquiring presigned URL for ${filename}`); const presigned = await api.uploads.getUrl( { filename, size }, { workspaceId }, ); s.message(`Aquired presigned URL for ${filename}`); const putHeaders: Record = {}; const contentDisposition = presigned.headers['Content-Disposition']; if (contentDisposition) putHeaders['Content-Disposition'] = contentDisposition; const etag = presigned.headers.ETag; if (etag) putHeaders.ETag = etag; s.message(`Uploading ${filename}`); const putRes = await fetch(presigned.url, { method: 'PUT', headers: putHeaders, body: buffer, }); s.message(`Uploaded ${filename} (${size} bytes)`); if (!putRes.ok) { p.log.error(`Upload failed for ${filename}: ${putRes.status}`); continue; } const filePathStored = new URL(presigned.url).pathname.replace(/^\//, ''); // get or create test space const testSpace = await getOrCreateTestSpace(workspaceId); if (!testSpace) { p.log.error('Failed to get or create test space'); continue; } s.message(`Creating file in Test Space`); await api.files.create( { attachment: { path: filePathStored, filename }, parentId: testSpace.id, mimeType, }, { workspaceId }, ); p.log.info(`Created file ${filename} in Test Space of ${testSpace.id}`); } s.stop(`Done uploading dummy files in ${workspaceId}`); } export async function getOrCreateTestSpace(workspaceId: string) { const space = await api.spaces.create( { name: 'Test', copyFolderId: '', moveFolderId: '', }, { workspaceId }, ); if (!space) { p.log.error('Failed to create test space'); return null; } return space; } // export async function listFiles(workspaceId: string) { // const files = await api.( // { // parentId: '@alias::inbox', // }, // { workspaceId }, // ); // return files; // }