Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Folder upload feature at cascade and sense provider #1129

Merged
merged 2 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ const result = await provider.uploadToCascade(file);
if (result) {
console.log(result.ipfs_url);
}

// upload folder
const results = await provider.uploadFolderToCascade("./examples");

if (results.length > 0) {
for (const result of results) {
if (result) {
console.log("File Name:", result.file_name);
console.log("IPFS Url:", result.ipfs_url);
console.log("Result Id:", result.result_id);
}
}
}
```

### Upload files to Sense Protocol
Expand All @@ -68,6 +81,19 @@ const result = await provider.uploadToSense(file);
if (result) {
console.log(result.ipfs_url);
}

// upload folder
const results = await provider.uploadFolderToSense("./examples");

if (results.length > 0) {
for (const result of results) {
if (result) {
console.log("File Name:", result.file_name);
console.log("IPFS Url:", result.ipfs_url);
console.log("Result Id:", result.result_id);
}
}
}
```

### Retrieve activation ticket transaction id
Expand Down
99 changes: 98 additions & 1 deletion docs/learn/other-guides/utilize-ipfs-storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,101 @@ const file = createReadStream('./path-to-your-file');

const { result_id, ipfs_url } = await provider.uploadToCascade(file);
console.log(result_id, ipfs_url);


// upload folder
const results = await provider.uploadFolderToCascade("./examples");

if (results.length > 0) {
for (const result of results) {
if (result) {
console.log("File Name:", result.file_name);
console.log("IPFS Url:", result.ipfs_url);
console.log("Result Id:", result.result_id);
}
}
}
```

**Using Cascade**

````js
```js
import { CascadeUploader } from '@lukso/data-provider-cascade';


const provider = new CascadeUploader(import.meta.env.CASCADE_API_KEY);
```

React Example

```ts
import React, { useCallback, useMemo, useRef, useState } from "react";
import { CascadeUploader } from "@lukso/data-provider-cascade";
import { urlResolver } from "./shared";

export interface Props {
apiKey: string;
}

export default function UploadLocal({ apiKey }: Props) {
const provider = useMemo(
() => new CascadeUploader(apiKey),
[]
);
const fileInput = useRef<HTMLInputElement>(null);
const [url, setUrl] = useState("");
const [hash, setHash] = useState("");
const [imageUrl, setImageUrl] = useState("");

const upload = useCallback(async () => {
const file = fileInput?.current?.files?.item(0) as File;
const formData = new FormData();
formData.append("file", file); // FormData keys are called fields
const { hash, url } = await provider.upload(file);
setUrl(url);
setHash(hash);
const destination = urlResolver.resolveUrl(url);
setImageUrl(destination);
}, []);

return (
<div>
<input ref={fileInput} type="file" accept="image/*" />
<button onClick={upload}>Upload</button>
<div className="url">{url}</div>
<div>
<img className="image" src={imageUrl} alt="uploaded image" />
</div>
</div>
);
}
```

Can use above component like following.

```js
<Upload client:only="react" apiKey="import.meta.env.CASCADE_API_KEY" />
```

API endpoint example

```ts
import type { APIContext } from 'astro';
import { CascadeUploader } from '@lukso/data-provider-cascade';

export async function POST({ request }: APIContext) {
const formData = await request.formData();
const file = formData.get('file');

const provider = new CascadeUploader(import.meta.env.CASCADE_API_KEY);

const { hash, url } = await provider.upload(file);
return new Response(JSON.stringify({ Hash: url }), {
headers: { contentType: 'application/json' },
});
}
```

### Sense

```ts
Expand All @@ -155,6 +239,19 @@ const file = createReadStream('./path-to-your-file');

const { result_id, ipfs_url } = await provider.uploadToSense(file);
console.log(result_id, ipfs_url);

// upload folder
const results = await provider.uploadFolderToSense("./examples");

if (results.length > 0) {
for (const result of results) {
if (result) {
console.log("File Name:", result.file_name);
console.log("IPFS Url:", result.ipfs_url);
console.log("Result Id:", result.result_id);
}
}
}
````

**Using Sense**
Expand Down
Loading