Skip to content

Commit

Permalink
Merge pull request #111 from KenEucker/develop
Browse files Browse the repository at this point in the history
2.0.1
  • Loading branch information
KenEucker committed Dec 31, 2021
2 parents 86c128c + 00c1774 commit 020927b
Show file tree
Hide file tree
Showing 20 changed files with 17,388 additions and 3,395 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ lib/
src/coverage/
dist/
webpack.config.js
example
imgur.js
imgur.node.js
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ src/coverage
*.code-workspace
lib
dist
.DS_Store
.DS_Store
.env
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ lib/
package-lock.json
.github
src/coverage/
example
imgur.js
imgur.node.js
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,17 @@ import { ImgurClient } from 'imgur';
// CommonJS syntax
const { ImgurClient } = require('imgur');

let client;
// browser script include
const client = new imgur({ clientId: env.CLIENT_ID });

// if you already have an access token acquired
client = new ImgurClient({ accessToken: process.env.ACCESS_TOKEN });
const client = new ImgurClient({ accessToken: process.env.ACCESS_TOKEN });

// or your client ID
client = new ImgurClient({ clientId: process.env.CLIENT_ID });
const client = new ImgurClient({ clientId: process.env.CLIENT_ID });

// or your username/password/client id to retrieve an access token automatically:
client = new ImgurClient({
const client = new ImgurClient({
username: process.env.USERNAME,
password: process.env.PASSWORD,
clientId: process.env.CLIENT_ID,
Expand Down Expand Up @@ -75,12 +76,12 @@ If you want to provide metadata, such as a title, description, etc., then pass a
// multiple images via an array of absolute paths
const responses = await client.upload([
{
stream: createReadStream('/home/kai/dank-meme.jpg'),
image: createReadStream('/home/kai/dank-meme.jpg'),
title: 'Meme',
description: 'Dank Meme',
},
{
stream: createReadStream('/home/kai/cat.mp4'),
image: createReadStream('/home/kai/cat.mp4'),
title: 'A Cat Movie',
description: 'Caturday',
},
Expand All @@ -93,7 +94,6 @@ Acceptable key/values match what [the Imgur API expects](https://apidocs.imgur.c
| Key | Description |
| --------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| `image` | A string that is a URL pointing to a remote image (up to 10MB) |
| `stream` | A readable stream that is to be piped to the upload method |
| `base64` | A base 64 object that is to be placed in the the upload form |
| `album` | The id of the album you want to add the media to. For anonymous albums, album should be the deletehash that is returned at creation |
| `type` | The type of the media that's being transmitted; `stream`, `base64` or `url` |
Expand Down
5 changes: 5 additions & 0 deletions example/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const env = {
"CLIENT_ID": "YOUR-CLIENT-ID",
"ACCESS_TOKEN": "YOUR-ACCESS-TOKEN",
"ALBUM": "YOUR-ALBUM"
}
70 changes: 70 additions & 0 deletions example/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<html>

<head>

<meta charset="utf-8">
<meta type="title" content="Imgur API test">
<title>Imgur API test</title>
<script src="../dist/imgur.js"></script>
<script src="env.js"></script>

</head>

<body>

<img id="image" src="" style="min-width: 25vw;" />
<video id="video" src="" controls></video>

<div>
<label for="uploadImage">Upload Image</label>
<input id="uploadImage" type="file" rel="image">
<label for="uploadVideo">Upload Video</label>
<input id="uploadVideo" type="file" rel="video">
</div>

<script defer>
const album = env.ALBUM;
let client;

const getImageStream = (img) => {
const canvas = document.createElement("canvas")
canvas.width = canvas.height = 100
const ctx = canvas.getContext("2d")
ctx.drawImage(img, 0, 0)
return canvas.toDataURL()
}

const upload = (e) => {
const file = e.target.files.length ? e.target.files[0] : null
const targetEl = document.getElementById(e.target.getAttribute('rel'))
client.upload({
album,
image: file,
type: 'stream'
})
.then((response) => {
const r = response.length ? response[0] : response
const i = r.success ? r.data : null
if (i) {
targetEl.src = i.link
}
})
}

const init = () => {
client = new imgur({
accessToken: env.ACCESS_TOKEN,
clientId: env.CLIENT_ID,
})

const uploadImageEl = document.getElementById('uploadImage')
const uploadVideoEl = document.getElementById('uploadVideo')
uploadImageEl.addEventListener('change', upload)
uploadVideoEl.addEventListener('change', upload)
}
init()
</script>

</body>

</html>
16 changes: 16 additions & 0 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { ImgurClient } = require('../dist/imgur.node');
const { createReadStream } = require('fs');
const { join } = require('path');
require('dotenv').config();

const album = process.env.ALBUM;
const imgur = new ImgurClient({
accessToken: process.env.ACCESS_TOKEN,
clientId: process.env.CLIENT_ID,
});

const imageStream = createReadStream(join(__dirname, 'small.jpg'));
const videoStream = createReadStream(join(__dirname, 'small.mp4'));

imgur.upload({ album, image: imageStream, type: 'stream' }).then(console.log);
imgur.upload({ album, image: videoStream, type: 'stream' }).then(console.log);
Binary file added example/small.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/small.mp4
Binary file not shown.
Loading

0 comments on commit 020927b

Please sign in to comment.