@autonomys/auto-drive 1.0.7 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +18 -0
- package/README.md +166 -23
- package/package.json +3 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Autonomys Network (autonomys.xyz)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
1. **Attribution**: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
2. **No Warranty**: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
15
|
+
|
|
16
|
+
3. **Limitation of Liability**: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
17
|
+
|
|
18
|
+
---
|
package/README.md
CHANGED
|
@@ -20,21 +20,21 @@ yarn add @autonomys/auto-drive
|
|
|
20
20
|
|
|
21
21
|
### How to use it?
|
|
22
22
|
|
|
23
|
-
###
|
|
23
|
+
### How to upload a file from filepath? (Not available in browser)
|
|
24
24
|
|
|
25
|
-
Here is an example of how to use the `
|
|
25
|
+
Here is an example of how to use the `uploadFileFromFilepath` method to upload a file with optional encryption and compression:
|
|
26
26
|
|
|
27
27
|
```typescript
|
|
28
|
-
import {
|
|
28
|
+
import { uploadFileFromFilepath } from '@autonomys/auto-drive'
|
|
29
29
|
|
|
30
|
-
const api =
|
|
30
|
+
const api = createAutoDriveApi({ apiKey: 'your-api-key' }) // Initialize your API instance with API key
|
|
31
31
|
const filePath = 'path/to/your/file.txt' // Specify the path to your file
|
|
32
32
|
const options = {
|
|
33
33
|
password: 'your-encryption-password', // Optional: specify a password for encryption
|
|
34
34
|
compression: true,
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
uploadFile(api, filePath, options)
|
|
37
|
+
const uploadObservable = uploadFile(api, filePath, options)
|
|
38
38
|
.then(() => {
|
|
39
39
|
console.log('File uploaded successfully!')
|
|
40
40
|
})
|
|
@@ -43,42 +43,185 @@ uploadFile(api, filePath, options)
|
|
|
43
43
|
})
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
-
###
|
|
47
|
-
|
|
48
|
-
Here is an example of how to use the `downloadFile` method to download a file from the server:
|
|
46
|
+
### How to upload [File](https://developer.mozilla.org/en-US/docs/Web/API/File) interface
|
|
49
47
|
|
|
50
48
|
```typescript
|
|
51
|
-
import {
|
|
49
|
+
import { uploadFileFromFilepath } from '@autonomys/auto-drive'
|
|
52
50
|
|
|
53
|
-
const api =
|
|
51
|
+
const api = createAutoDriveApi({ apiKey: 'your-api-key' }) // Initialize your API instance with API key
|
|
52
|
+
const filePath = 'path/to/your/file.txt' // Specify the path to your file
|
|
53
|
+
const options = {
|
|
54
|
+
password: 'your-encryption-password', // Optional: specify a password for encryption
|
|
55
|
+
compression: true,
|
|
56
|
+
}
|
|
54
57
|
|
|
55
|
-
|
|
56
|
-
.then((
|
|
57
|
-
console.log('
|
|
58
|
+
const uploadObservable = uploadFile(api, filePath, options)
|
|
59
|
+
.then(() => {
|
|
60
|
+
console.log('File uploaded successfully!')
|
|
58
61
|
})
|
|
59
62
|
.catch((error) => {
|
|
60
|
-
console.error('Error
|
|
63
|
+
console.error('Error uploading file:', error)
|
|
61
64
|
})
|
|
62
65
|
```
|
|
63
66
|
|
|
64
|
-
###
|
|
67
|
+
### How to upload a file from a custom interface?
|
|
65
68
|
|
|
66
|
-
|
|
69
|
+
Some times you might have a custom interface that doesn't fit either File or filepath. For those cases exists the interface GenericFile:
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
export interface GenericFile {
|
|
73
|
+
read(): AsyncIterable<Buffer> // A buffer generator function that will output the bytes of the file
|
|
74
|
+
name: string
|
|
75
|
+
mimeType?: string
|
|
76
|
+
size: number
|
|
77
|
+
path: string // Could be ignored in file upload
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
For more info about asynn generator visit [this website](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncGenerator).
|
|
82
|
+
|
|
83
|
+
You could upload any file that could be represented in that way. For example, uploading a file as a `Buffer`
|
|
67
84
|
|
|
68
85
|
```typescript
|
|
69
|
-
import {
|
|
86
|
+
import { uploadFile } from '@autonomys/auto-drive'
|
|
87
|
+
|
|
88
|
+
const api = createAutoDriveApi({ apiKey: 'your-api-key' }) // Initialize your API instance with API key
|
|
89
|
+
const buffer = Buffer.from(...);
|
|
90
|
+
const genericFile = {
|
|
91
|
+
read: async function *() {
|
|
92
|
+
yield buffer
|
|
93
|
+
},
|
|
94
|
+
name: "autonomys-whitepaper.pdf",
|
|
95
|
+
mimeType: "application/pdf",
|
|
96
|
+
size: 1234556,
|
|
97
|
+
path: "autonomys-whitepaper.pdf"
|
|
98
|
+
}
|
|
70
99
|
|
|
71
|
-
const
|
|
100
|
+
const options = {
|
|
101
|
+
password: 'your-encryption-password', // Optional: specify a password for encryption
|
|
102
|
+
compression: true,
|
|
103
|
+
}
|
|
72
104
|
|
|
73
|
-
|
|
74
|
-
.then((
|
|
75
|
-
console.log('
|
|
105
|
+
const uploadObservable = uploadFile(api, genericFile, options)
|
|
106
|
+
.then(() => {
|
|
107
|
+
console.log('File uploaded successfully!')
|
|
76
108
|
})
|
|
77
109
|
.catch((error) => {
|
|
78
|
-
console.error('Error
|
|
110
|
+
console.error('Error uploading file:', error)
|
|
79
111
|
})
|
|
80
112
|
```
|
|
81
113
|
|
|
114
|
+
### How to upload a folder from folder? (Not available in browser)
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
import { uploadFolderFromFolderPath } from '@autonomys/auto-drive'
|
|
118
|
+
|
|
119
|
+
const api = createAutoDriveApi({ apiKey: 'your-api-key' }) // Initialize your API instance with API key
|
|
120
|
+
const folderPath = 'path/to/your/folder' // Specify the path to your folder
|
|
121
|
+
|
|
122
|
+
const options = {
|
|
123
|
+
uploadChunkSize: 1024 * 1024, // Optional: specify the chunk size for uploads
|
|
124
|
+
password: 'your-encryption-password', // Optional: If folder is encrypted
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const uploadObservable = uploadFolderFromFolderPath(api, folderPath, options)
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
**Note: If a folder is tried to be encrypted a zip file would be generated and that file would be encrypted and uploaded.**
|
|
131
|
+
|
|
132
|
+
### Handle observables
|
|
133
|
+
|
|
134
|
+
Since uploads may take some time, specially in big-sized files. Uploads do implement `rxjs` observables so you could have feedback about the process or even show your users the progress of the upload.
|
|
135
|
+
|
|
136
|
+
For that reason when file upload functions return `PromisedObservable<UploadFileStatus>`:
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
export type UploadFileStatus = {
|
|
140
|
+
type: 'file'
|
|
141
|
+
progress: number
|
|
142
|
+
cid?: CID
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Being the cid only returned (and thus optional) when the upload is completed.
|
|
147
|
+
|
|
148
|
+
Similarly, for folder uploads the functions return `PromisedObservable<UploadFolderStatus>`
|
|
149
|
+
|
|
150
|
+
```ts
|
|
151
|
+
export type UploadFolderStatus = {
|
|
152
|
+
type: 'folder'
|
|
153
|
+
progress: number
|
|
154
|
+
cid?: CID
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
**e.g Show upload progress in React**
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
const [progress, setProgress] = useState<number>(0)
|
|
162
|
+
|
|
163
|
+
useEffect(async () => {
|
|
164
|
+
const finalStatus = await uploadFileFromInput(api, genericFile, options).forEach((status) => {
|
|
165
|
+
setProgress(status.progress)
|
|
166
|
+
})
|
|
167
|
+
})
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
**e.g Ignore observables**
|
|
171
|
+
|
|
172
|
+
Other users may want to not use the progress observability. For having a promise instead the field `promise` is a Promise that resolves into `UploadFileStatus` and `UploadFolderStatus` for files and folders respectively.
|
|
173
|
+
|
|
174
|
+
e.g
|
|
175
|
+
|
|
176
|
+
```ts
|
|
177
|
+
const status = await uploadFileFromInput(api, genericFile, options).promise
|
|
178
|
+
const cid = status.cid
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### Example Usage of Download
|
|
182
|
+
|
|
183
|
+
Here is an example of how to use the `downloadFile` method to download a file from the server:
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
import { downloadObject } from '@autonomys/auto-drive'
|
|
187
|
+
|
|
188
|
+
const api = createAutoDriveApi({ apiKey: 'your-api-key' }) // Initialize your API instance with API key
|
|
189
|
+
|
|
190
|
+
try {
|
|
191
|
+
const cid = ".."
|
|
192
|
+
const stream = await downloadObject(api, { cid })
|
|
193
|
+
let file = Buffer.alloc(0);
|
|
194
|
+
for await (const chunk of stream) {
|
|
195
|
+
file = Buffer.concat([file, chunk])
|
|
196
|
+
}
|
|
197
|
+
console.log('File downloaded successfully:', stream)
|
|
198
|
+
} catch (error) {
|
|
199
|
+
console.error('Error downloading file:', error)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### Example Usage of getRoots
|
|
205
|
+
|
|
206
|
+
Here is an example of how to use the `getRoots` method to retrieve the root directories:
|
|
207
|
+
|
|
208
|
+
```typescript
|
|
209
|
+
import { createAutoDriveApi, downloadObject } from '@autonomys/auto-drive'
|
|
210
|
+
import fs from 'fs'
|
|
211
|
+
|
|
212
|
+
const api = createAutoDriveApi({ apiKey: 'your-api-key' }) // Initialize your API instance with API key
|
|
213
|
+
|
|
214
|
+
try {
|
|
215
|
+
const stream = fs.createWriteStream('/path/to/file')
|
|
216
|
+
const asyncBuffer = await downloadObject(api, { cid })
|
|
217
|
+
for await (const buffer of asyncBuffer) {
|
|
218
|
+
stream.write(buffer)
|
|
219
|
+
}
|
|
220
|
+
} catch (error) {
|
|
221
|
+
console.error('Error downloading file:', error)
|
|
222
|
+
}
|
|
223
|
+
```
|
|
224
|
+
|
|
82
225
|
## License
|
|
83
226
|
|
|
84
227
|
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
|
|
@@ -93,4 +236,4 @@ If you have any questions or need support, feel free to reach out:
|
|
|
93
236
|
|
|
94
237
|
- **GitHub Issues**: [GitHub Issues Page](https://github.com/autonomys/auto-sdk/issues)
|
|
95
238
|
|
|
96
|
-
We appreciate your feedback and contributions!
|
|
239
|
+
We appreciate your feedback and contributions!
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"packageManager": "yarn@4.2.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
|
-
"version": "1.0.
|
|
6
|
+
"version": "1.0.8",
|
|
7
7
|
"author": {
|
|
8
8
|
"name": "Autonomys",
|
|
9
9
|
"url": "https://autonomys.net"
|
|
@@ -19,10 +19,10 @@
|
|
|
19
19
|
"typescript": "^5.6.3"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@autonomys/auto-dag-data": "^1.0.
|
|
22
|
+
"@autonomys/auto-dag-data": "^1.0.8",
|
|
23
23
|
"jszip": "^3.10.1",
|
|
24
24
|
"mime-types": "^2.1.35",
|
|
25
25
|
"zod": "^3.23.8"
|
|
26
26
|
},
|
|
27
|
-
"gitHead": "
|
|
27
|
+
"gitHead": "88b1b90467db7dd1e301387451ed727189f9808c"
|
|
28
28
|
}
|