Back to Nappakura
SOURCE / PINNED RELEASE

Made of little things.

Nappakura

Release
a75ca0f8b596…
Author-recorded commit
4463126213fa…
License
LICENSE
Author’s source reference
nostr://npub182jczunncwe0jn6frpqwq3e0qjws7yqqnc3auccqv9nte2dnd63scjm4rf/wss%3A%2F%2Fgit.napplet.soy%2F/n-8db03001b62

Archive hash verified: b448d77b767ae754…. The source-to-build association is the author’s claim; it has not been independently rebuilt.

src/files.ts
import { fs } from '@napplet/sdk';
const CHUNK = 192 * 1024;
export async function importFile(): Promise<{
  name: string;
  bytes: Uint8Array;
}> {
  const result = await fs.pickFile({
    accept: [
      { extension: '.obj' },
      { extension: '.stl' },
      { extension: '.nappakura' },
    ],
    description: 'Open a model or Nappakura project',
  });
  const entry = result.entries[0];
  if (!entry) throw new Error('No file selected.');
  const chunks: Uint8Array[] = [];
  let offset = 0;
  while (true) {
    const part = await fs.read(entry.path, { offset, length: CHUNK });
    const bytes = Uint8Array.from(atob(part.data), (c) => c.charCodeAt(0));
    chunks.push(bytes);
    offset += bytes.length;
    if (offset > 8 * 1024 * 1024)
      throw new Error('Choose a file smaller than 8 MiB.');
    if (part.eof) break;
    if (!bytes.length) throw new Error('The host returned an incomplete file.');
  }
  const bytes = new Uint8Array(offset);
  let start = 0;
  for (const chunk of chunks) {
    bytes.set(chunk, start);
    start += chunk.length;
  }
  return { name: entry.path.split('/').pop() || 'model.obj', bytes };
}
export async function exportFile(
  bytes: Uint8Array,
  name: string,
  signal?: AbortSignal,
): Promise<void> {
  if (bytes.length > 8 * 1024 * 1024)
    throw new Error(
      'Export exceeds 8 MiB. Reduce the model or number of sheets.',
    );
  if (signal?.aborted) throw new Error('Export cancelled.');
  const result = await fs.pickSaveFile({
    suggestedName: name,
    description: 'Save your exported file',
  });
  const entry = result.entries[0];
  if (!entry || signal?.aborted) throw new Error('Export cancelled.');
  for (let offset = 0; offset < bytes.length; offset += CHUNK) {
    if (signal?.aborted)
      throw new Error(
        'Export stopped; an incomplete file may remain in the host panel.',
      );
    let binary = '';
    for (const byte of bytes.subarray(offset, offset + CHUNK))
      binary += String.fromCharCode(byte);
    const result = await fs.write(entry.path, btoa(binary), {
      mode: offset === 0 ? 'replace' : 'append',
    });
    if (result.bytesWritten !== Math.min(CHUNK, bytes.length - offset))
      throw new Error(
        'The host could not finish writing this export. Try again.',
      );
  }
}