Documentation

Guides and API reference for uploads, image transforms and video.

npm install @cd2/fileboy-react
API https://api.fileboy.io

Vue integration

Drop-in file uploaders and responsive images for Vue 3, with the same options and behaviour as the React package.

Installation

1
npm install @cd2/fileboy-vue

Install the plugin with your app's public key:

1
2
3
4
5
6
7
8
// main.ts
import { createApp } from "vue"
import { createFileboy } from "@cd2/fileboy-vue"
import App from "./App.vue"

createApp(App)
  .use(createFileboy({ publicKey: "YOUR_PUBLIC_KEY", uploadToken: "/api/fileboy/token" }))
  .mount("#app")

uploadToken is optional: see Upload tokens below. <FileboyProvider public-key="..."> does the same for one part of a page, and useFileboy() returns the client (uploadFile, buildImageUrl, getFileData...) inside a component's setup.

Uploaders

<FileUploader> holds one file and <MultiFileUploader> many. Both have a drop zone, and v-model holds the file id (or ids).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<script setup lang="ts">
import { ref } from "vue"
import { FileUploader, MultiFileUploader } from "@cd2/fileboy-vue"

const avatar = ref<string>()
const photos = ref<string[]>([])
</script>

<template>
  <FileUploader v-model="avatar" allow="image/*" capture="user" @uploaded="file => save(file)" />
  <MultiFileUploader
    v-model="photos"
    paste
    url-import
    folder-path="/photos"
    uploader-style="buttonWithProgress"
    @remove="id => forget(id)"
  />
</template>

Props

allow string | string[], optional

Mime types and extensions allowed, like "image/*" or ["image/*", ".pdf"]. Other files are refused with "Disallowed file given" before anything uploads. It also sets the file picker's accept.

uploader-style "default" | "minimal" | "buttonOnly" | "buttonWithProgress", optional, default is "default"

Which built-in look to use. buttonWithProgress is for the multi-uploader.

folder-id / folder-path string, optional

Upload into this folder. With the public key the folder must already exist: create it from your server.

file-name / metadata (info) => string / (info) => Record<string, string>, optional

The name and metadata to save each file with. info has file, rawName, baseName, ext and index.

paste boolean, optional

Upload files pasted anywhere on the page, like a screenshot. Pasted screenshots get a name with the time in it. Text pastes are left alone. Turn it on for one uploader per page.

capture boolean | "user" | "environment", optional

On phones, open the camera instead of the file picker: "environment" is the back camera, "user" the front one. Desktop browsers show the file picker.

url-import boolean, optional

Show a field to import a file from a link. Fileboy fetches it, so it must be public. Links to localhost and private or cloud metadata addresses are refused, in the browser and again by the API on the address it connects to.

upload-token string | () => Promise<string>, optional

Get a short-lived upload token before each upload: the URL of an endpoint on your server, or a function. Overrides the one given to the provider. See Upload tokens below.

theme { colors?: { button?: string, buttonText?: string }, fontFamily?: string }, optional

Colours and font for the built-in looks.

disabled boolean, optional

Stop new uploads.

namespace string, optional

Which provider to use, when the page has more than one app's key.

Events

uploaded IFileData (FileUploader) or IFileData[] (MultiFileUploader)

Once a file, or every file of a batch, has uploaded.

upload-start / upload-end ()

Around each upload, whether it worked or not.

remove (id: string)

Listening for it shows a remove button on each file. The file stays in Fileboy: delete it from your server if you need to.

Your own look

The default slot replaces the built-in look and gets the uploader's state: ids, files, error, isUploading, progress, dragging, openFileDialogue(), uploadFiles(files), uploadUrl(url), remove(id) and dropZone, the listeners for a drop target.

1
2
3
4
5
<MultiFileUploader v-slot="{ dropZone, dragging, openFileDialogue, isUploading, progress }">
  <div v-bind="dropZone" :class="{ over: dragging }" @click="openFileDialogue">
    {{ isUploading ? `${Math.round(progress?.percent ?? 0)}%` : "Drop files or click" }}
  </div>
</MultiFileUploader>

Images

<FileboyImage> (also exported as Image) builds the image URL. Give widths and sizes for a srcset, a saved preset, and placeholder-color (a colour, or "auto" for the image's own). Other attributes go on the <img>.

1
2
3
4
5
6
7
8
9
<FileboyImage
  :id="avatar"
  preset="card"
  :transforms="[{ type: 'format', format: 'auto' }]"
  :widths="[320, 640, 1280]"
  sizes="(max-width: 640px) 100vw, 640px"
  placeholder-color="auto"
  alt="Profile photo"
/>

Upload tokens

The uploaders upload straight from the browser with your public key, and allowed origins keep other websites from using it. To have your server decide who uploads, have it hand out a short-lived upload token, made with the private key, and give the provider its URL. The uploaders POST to it (with the page's cookies) before each upload and expect { "token": "..." } back.

1
2
3
4
5
6
7
8
// app/api/fileboy/token/route.ts (Next.js), or any server route
import { createUploadTokenHandler } from "@cd2/fileboy-next"

export const runtime = "nodejs"
export const POST = createUploadTokenHandler({
  // publicKey and privateKey default to FILEBOY_PUBLIC_KEY and FILEBOY_PRIVATE_KEY
  authorize: async request => isSignedIn(request),
})

Tokens last 60 seconds and only allow uploads. Without Next.js, make one with fileboy.createAccessToken({ upload: true }) from the Node library.

Files uploaded with a token are signed (signed: true). A token doesn't turn the public key off, so anyone with it can still upload unsigned files. To keep only files your users uploaded, check one is signed on your server before you save its id:

1
2
const file = await fileboy.fetchFileData(id) // @cd2/fileboy-node, with the private key
if (!file.signed) throw new Error("Upload with a token")

Related guides