Skip to content

Commit

Permalink
feat: 使用imagur上传本地图片并获取连接
Browse files Browse the repository at this point in the history
  • Loading branch information
liwuzeng_17080006 committed Nov 4, 2019
1 parent 428aa7f commit 5da04ff
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 49 deletions.
42 changes: 22 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,45 @@ npm install -g .
```shell
# send 跟上本地文件路径或线上文件链接
send path/to/file/demo.jpg
# or
# or 从网络图片上传
send http://file/link.jpg
# or 从剪贴板上传,仅支持macOS
send -c

# 选项
Options:
--hash add hash at end of filename
-p, --path specific file storage path, default: it-platform/
-c, --clipboard 从剪贴板中上传

send path/to/file/demo.jpg --hash
# 会在文件名后面拼上随机字符串,如下:
# http://link-of-file/path/demo-2ker3kd.jpg

send path/to/file/demo.jpg --path
# 指定文件存在桶下那个目录中,默认为:it-platform
send path/to/file/demo.jpg --path somewhere/
# 指定文件存在桶下那个目录中,默认为:it-platform/
```

## 配置

配置文件在`~/.sendrc`,可在该文件中自定义配置,如下:

__已弃用__

```yaml
# 支持以下配置:
S3_ACCESS_ID: '' # S3的登录ID
S3_ACCESS_SECRET: '' # S3的登录秘钥
S3_BUCKET_NAME: '' # 目标桶(Bucket)名
S3_REGION: '' # S3所在地区名称,默认为:cn-north-1
S3_PATH: '' # 文件存储路径(Bucket下路径)
# S3_ACCESS_ID: '' # S3的登录ID
# S3_ACCESS_SECRET: '' # S3的登录秘钥
# S3_BUCKET_NAME: '' # 目标桶(Bucket)名
# S3_REGION: '' # S3所在地区名称,默认为:cn-north-1
# S3_PATH: '' # 文件存储路径(Bucket下路径)
```

__请使用__

```yaml
# imagur client ID
# 申请地址:https://api.imgur.com/oauth2/addclient
# 或者使用:1dfa83c47f8a089
IMAGUR_CLIENT_ID:''
```

### 查看配置
Expand All @@ -51,13 +63,3 @@ send config --show
# or
vim ~/.sendrc
```

### 修改配置

```shell
send config -e
# or
send config --edit
# or 具体配置某项
send config -e S3_ACCESS_ID 1231231231
```
62 changes: 39 additions & 23 deletions lib/aws-s3.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,45 @@
const AWS = require('aws-sdk')
const ext = require('ext-name')
// const AWS = require('aws-sdk')
// const ext = require('ext-name')
const CONFIG = require('./aws-config')
const FormData = require('form-data')
const got = require('got')

AWS.config.update({
region: CONFIG.S3_REGION,
accessKeyId: CONFIG.S3_ACCESS_ID,
secretAccessKey: CONFIG.S3_ACCESS_SECRET
})
// AWS.config.loadFromPath('/app/config.json')

const s3 = new AWS.S3()
// const s3 = new AWS.S3({
// region: CONFIG.S3_REGION
// })

module.exports = function saveToS3 (name, file) {
let fileType = ext(name)[0] || {}
if ((fileType.mime || '').indexOf('text') !== -1) {
fileType.mime += ';charset=utf-8'
}
return new Promise((resolve, reject) => {
s3.upload({
Bucket: CONFIG.S3_BUCKET_NAME,
Key: name,
Body: file,
ContentType: fileType.mime
}, (err, data) => {
if (err) reject(err)
else resolve(data)
})
// module.exports = function saveToS3 (name, file) {
// let fileType = ext(name)[0] || {}
// if ((fileType.mime || '').indexOf('text') !== -1) {
// fileType.mime += ';charset=utf-8'
// }
// return new Promise((resolve, reject) => {
// s3.upload({
// Bucket: CONFIG.S3_BUCKET_NAME,
// Key: name,
// Body: file,
// ContentType: fileType.mime
// }, (err, data) => {
// if (err) reject(err)
// else resolve(data)
// })
// })
// }

const api = 'https://api.imgur.com/3/image'

module.exports = async function saveToImagur (name, file) {
const data = new FormData()
data.append('image', file)
let { body } = await got(api, {
method: 'POST',
body: data,
headers: {
Authorization: `Client-ID ${CONFIG.IMAGUR_CLIENT_ID}`
}
})
body = JSON.parse(body)
return body.data.link
}
7 changes: 4 additions & 3 deletions lib/send-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ class Send {

try {
const res = await s3(name, content)
clipboardy.writeSync(res.Location)
clipboardy.writeSync(res)
spinner.succeed(`${logger.prefix} Upload complete!`)
logger.succ(`File link: ${res.Location}`)
logger.succ(`File link: ${res}`)
logger.succ(chalk.bold('Image URL had copyed, use CMD + C to paste.'))
} catch (err) {
spinner.fail(`${logger.prefix} Upload failed!${err}`)
spinner.fail(`${logger.prefix} Upload failed! ${err}`)
err.stack && console.log(err.stack)
}
}

Expand Down
45 changes: 44 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "send-file",
"version": "1.3.4",
"description": "send localfile to aws",
"version": "1.4.0",
"description": "get local image link",
"main": "index.js",
"bin": {
"send": "./bin/send.js",
Expand All @@ -28,6 +28,7 @@
"commander": "^2.19.0",
"execa": "^1.0.0",
"ext-name": "^5.0.0",
"form-data": "^2.5.1",
"got": "^9.6.0",
"ora": "^3.1.0",
"prompts": "^2.0.3",
Expand Down

0 comments on commit 5da04ff

Please sign in to comment.