Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/catto-labs/drive into main
Browse files Browse the repository at this point in the history
  • Loading branch information
pnxl committed Aug 13, 2023
2 parents 26b2117 + 90dac64 commit 098c72f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 29 deletions.
69 changes: 48 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,11 @@ You can use the API as unauthenticated (anonymous) or [authenticated](#how-to-au
| Remove uploaded file later - with no self destruct option | ❌ No | ✅ Yes |
| Access private files - if user is allowed | ❌ No | ✅ Yes |

### How to authenticate ?
### Where to find the API token ?

If you do have an account, head over the "My Account" page and
grab your "API Token" from there.

Now, if every requests you make to the API, just add the `Authorization` header, where its value is the API token you got from earlier.

#### Simple example using `fetch`

```typescript
const response = await fetch("https://drive.cattolabs.com/api/...", {
// ...
headers: {
authorization: "api-token-you-got-here"
}
});
```

### Get a workspace ID

> TODO
Expand All @@ -49,24 +36,28 @@ To use [drive.cattolabs.com](https://drive.cattolabs.com) with ShareX, you have

Head over to "Destinations" > "Custom uploader settings" ;

### Manual
From here, you can either [create a new custom uploader manually](#manual) or simply use a pre-made one we give you: [anonymous upload](#pre-made-anonymously-upload-public-files).

#### Manual

- Click on "New" to create a new uploader. Name it as you want, we'll go with `drive.cattolabs.com` here ;
- Set the "Destination type" to `Image uploader` and `File uploader`, we currently don't support `Text uploader` ;
- Set the "Method" to `PUT` ;
- Set the "Request URL" to `https://kcpxeoxkmblpivpgwdsm.supabase.co/functions/v1/upload-file`, (for some reason, ShareX throws on redirections so `https://drive.cattolabs.com/api/files` doesn't work) ;
- Set the "Request URL" to `https://kcpxeoxkmblpivpgwdsm.supabase.co/functions/v1/upload-file`, (for some reason, ShareX throws an error on redirection so `https://drive.cattolabs.com/api/workspace/upload` doesn't work) ;
- Keep "URL parameters" empty ;
- For "Headers", leave empty if you want to upload as anonymous, else add `Authorization` and as value, [your API key](#using-the-api) ;
- For "Headers", leave empty ;
- Keep "Body" as `Form data (multipart/form-data)`, below you're able to add form data parameters

> `private` as name and `0` as value to make all your uploaded files public.
> If you're authenticated, uploaded files will be at the root of your drive by default, you can override that behavior by adding `workspace_id` as name and [any workspace ID](#get-a-workspace-id) as value to make all uploads go there.
> `private` as name and `0` as value to make all your uploaded files public. If you're authenticated to an account, the default value will be `1` and `0` if you're anonymous.
> If you want to upload to an account, pass the `api_token` parameter followed by the value of the [account's API token](#where-to-find-the-api-token).
> By default, uploaded files will be in the root workspace of the account.
> You can override that behavior by adding `workspace_id` as name and [any workspace ID](#get-a-workspace-id) as value to make the upload go there.
- Set the "File form name" to `files` ;
- Set the "URL" to `https://drive.cattolabs.com/api/file/{json:data.uploaded[0].id}`
- Set the "Error message" to `{json:message}`

### Pre-made: Anonymously upload public files
#### Pre-made: Anonymously upload public files

```json
{
Expand All @@ -85,7 +76,43 @@ Head over to "Destinations" > "Custom uploader settings" ;
}
```

## Developing
### Integration with cURL

You can simply send files through your terminal using cURL.

In these examples, let's say you want to upload two files located at `./screen1.png` and `/tmp/image.jpg`.

#### Upload as anonymous

```bash
curl -L \
-X PUT \
-F '[email protected]' \
-F 'files=@/tmp/image.jpg' \
https://drive.cattolabs.com/api/workspace/upload
```

#### Upload as authenticated to a certain workspace

```bash
curl -L \
-X PUT \
-F "api_token=YOUR_API_TOKEN" \
-F 'workspace_id=SOME-WORKSPACE-ID-123456789' \
-F 'private=0' \
-F '[email protected]' \
-F 'files=@/tmp/image.jpg' \
https://drive.cattolabs.com/api/workspace/upload
```

If you want to upload to your root workspace, you can remove the `workspace_id`
parameter, it'll take the root workspace by default.
`workspace_id` can be found using [this guide](#get-a-workspace-id).

`private` defaults to `1` if not passed. If you want to make publicly
accessible uploads, you might have to change it to `private=0`.

## Run locally

```bash
pnpm dev
Expand Down
4 changes: 1 addition & 3 deletions src/utils/files.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,12 @@ export const makeFileUpload = async (files: FileList | Array<File>, options?: {
}

// Only add headers for authenticated users.
const headers: Record<string, string> = {};
if (auth.profile) {
headers.authorization = auth.profile.api_token;
body.set("api_token", auth.profile.api_token);
}

const response = await fetch("/api/workspace/upload", {
method: "PUT",
headers,
body
});

Expand Down
9 changes: 4 additions & 5 deletions supabase/functions/upload-file/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ const json = <T>(data: T, options?: { status: number }) => new Response(
*/
serve(async (req: Request) => {
if (req.method === "PUT") {
const api_token = req.headers.get("authorization");

let user_profile: UserProfile | undefined;
if (api_token) (user_profile = await getUserProfile(api_token));

const formData = await req.formData();
const formDataFiles = formData.getAll("files") as File[];
const formDataApiToken = formData.get("api_token") as string | undefined;

let user_profile: UserProfile | undefined;
if (formDataApiToken) (user_profile = await getUserProfile(formDataApiToken));

const workspaceId = (formData.get("workspace_id") as string | undefined) ?? user_profile?.root_workspace_id ?? undefined;

Expand Down

0 comments on commit 098c72f

Please sign in to comment.