Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example: fix
custom-provider
example (#3854)
- Loading branch information
Showing
14 changed files
with
176 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Uppy + Companion + Custom Provider Example | ||
|
||
This example uses @uppy/companion with a dummy custom provider. | ||
This serves as an illustration on how integrating custom providers would work | ||
|
||
## Run it | ||
|
||
**Note**: this example is using `fetch`, which is only available on Node.js 18+. | ||
|
||
First, you want to set up your environment variable. You can copy the content of | ||
`.env.example` and save it in a file named `.env`. You can modify in there all | ||
the information needed for the app to work that should not be committed | ||
(Google keys, Unsplash keys, etc.). | ||
|
||
```sh | ||
[ -f .env ] || cp .env.example .env | ||
``` | ||
|
||
To run the example, from the root directory of this repo, run the following commands: | ||
|
||
```sh | ||
corepack yarn install | ||
corepack yarn build | ||
corepack yarn workspace @uppy-example/custom-provider start | ||
``` |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,32 @@ | ||
{ | ||
"name": "@uppy-example/custom-provider", | ||
"version": "0.0.0", | ||
"type": "module", | ||
"dependencies": { | ||
"@babel/core": "^7.2.2", | ||
"@uppy/companion-client": "workspace:*", | ||
"@uppy/core": "workspace:*", | ||
"@uppy/dashboard": "workspace:*", | ||
"@uppy/google-drive": "workspace:*", | ||
"@uppy/provider-views": "workspace:*", | ||
"@uppy/tus": "workspace:*", | ||
"babelify": "^10.0.0", | ||
"preact": "^10.5.13" | ||
}, | ||
"engines": { | ||
"node": ">=18.0.0" | ||
}, | ||
"devDependencies": { | ||
"@uppy/companion": "workspace:*", | ||
"body-parser": "^1.18.2", | ||
"budo": "^11.3.2", | ||
"dotenv": "^16.0.1", | ||
"express": "^4.16.2", | ||
"express-session": "^1.15.6", | ||
"npm-run-all": "^4.1.2", | ||
"preact": "^10.5.13", | ||
"request": "2.88.2" | ||
"vite": "^2.7.1" | ||
}, | ||
"private": true, | ||
"scripts": { | ||
"copy": "cp ../../packages/uppy/dist/uppy.min.css .", | ||
"start": "npm-run-all --serial copy --parallel start:*", | ||
"start:client": "budo client/main.js:bundle.js -- -t babelify", | ||
"start:server": "node server/index.js" | ||
"start": "npm-run-all --parallel start:server start:client", | ||
"start:client": "vite", | ||
"start:server": "node server/index.cjs" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
const { Readable } = require('node:stream') | ||
|
||
const BASE_URL = 'https://api.unsplash.com' | ||
|
||
function adaptData (res) { | ||
const data = { | ||
username: null, | ||
items: [], | ||
nextPagePath: null, | ||
} | ||
|
||
const items = res | ||
items.forEach((item) => { | ||
const isFolder = !!item.published_at | ||
data.items.push({ | ||
isFolder, | ||
icon: isFolder ? item.cover_photo.urls.thumb : item.urls.thumb, | ||
name: item.title || item.description, | ||
mimeType: isFolder ? null : 'image/jpeg', | ||
id: item.id, | ||
thumbnail: isFolder ? item.cover_photo.urls.thumb : item.urls.thumb, | ||
requestPath: item.id, | ||
modifiedDate: item.updated_at, | ||
size: null, | ||
}) | ||
}) | ||
|
||
return data | ||
} | ||
|
||
/** | ||
* an example of a custom provider module. It implements @uppy/companion's Provider interface | ||
*/ | ||
class MyCustomProvider { | ||
static version = 2 | ||
|
||
authProvider = 'myunsplash' | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
async list ({ token, directory }) { | ||
const path = directory ? `/${directory}/photos` : '' | ||
|
||
const resp = await fetch(`${BASE_URL}/collections${path}`, { | ||
headers:{ | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}) | ||
if (!resp.ok) { | ||
throw new Error(`Errornous HTTP response (${resp.status} ${resp.statusText})`) | ||
} | ||
return adaptData(await resp.json()) | ||
} | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
async download ({ id, token }) { | ||
const resp = await fetch(`${BASE_URL}/photos/${id}`, { | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}) | ||
|
||
if (!resp.ok) { | ||
throw new Error(`Errornous HTTP response (${resp.status} ${resp.statusText})`) | ||
} | ||
return { stream: Readable.fromWeb(resp.body) } | ||
} | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
async size ({ id, token }) { | ||
const resp = await fetch(`${BASE_URL}/photos/${id}`, { | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}) | ||
|
||
if (!resp.ok) { | ||
throw new Error(`Errornous HTTP response (${resp.status} ${resp.statusText})`) | ||
} | ||
|
||
const { size } = await resp.json() | ||
return size | ||
} | ||
} | ||
|
||
module.exports = MyCustomProvider |
Oops, something went wrong.