From 5da04ff3475931dc58dd62f6765cb44db04adcf8 Mon Sep 17 00:00:00 2001 From: liwuzeng_17080006 Date: Mon, 4 Nov 2019 11:23:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BD=BF=E7=94=A8imagur=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E6=9C=AC=E5=9C=B0=E5=9B=BE=E7=89=87=E5=B9=B6=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E8=BF=9E=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 42 +++++++++++++++++--------------- lib/aws-s3.js | 62 +++++++++++++++++++++++++++++------------------ lib/send-file.js | 7 +++--- package-lock.json | 45 +++++++++++++++++++++++++++++++++- package.json | 5 ++-- 5 files changed, 112 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 7a31408..2cf3c95 100644 --- a/README.md +++ b/README.md @@ -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:'' ``` ### 查看配置 @@ -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 -``` diff --git a/lib/aws-s3.js b/lib/aws-s3.js index 1301b12..a8644eb 100644 --- a/lib/aws-s3.js +++ b/lib/aws-s3.js @@ -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 } diff --git a/lib/send-file.js b/lib/send-file.js index df70d87..4130281 100644 --- a/lib/send-file.js +++ b/lib/send-file.js @@ -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) } } diff --git a/package-lock.json b/package-lock.json index 06584c4..882052d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "send-file", - "version": "1.2.2", + "version": "1.3.5", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -100,6 +100,11 @@ "integrity": "sha1-bIw/uCfdQ+45GPJ7gngqt2WKb9k=", "dev": true }, + "asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=" + }, "aws-sdk": { "version": "2.406.0", "resolved": "http://10.32.1.103:4873/aws-sdk/-/aws-sdk-2.406.0.tgz", @@ -269,6 +274,14 @@ "resolved": "http://10.32.1.103:4873/color-name/-/color-name-1.1.3.tgz", "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=" }, + "combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "requires": { + "delayed-stream": "~1.0.0" + } + }, "commander": { "version": "2.19.0", "resolved": "http://10.32.1.103:4873/commander/-/commander-2.19.0.tgz", @@ -375,6 +388,11 @@ "resolved": "http://10.32.1.103:4873/defer-to-connect/-/defer-to-connect-1.0.2.tgz", "integrity": "sha1-S651ijFLA0rjOQK1qsJajdaoYz4=" }, + "delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=" + }, "doctrine": { "version": "3.0.0", "resolved": "http://10.32.1.103:4873/doctrine/-/doctrine-3.0.0.tgz", @@ -805,6 +823,16 @@ "integrity": "sha1-VRIrZTbqSWtLRIk+4mCBQdENmRY=", "dev": true }, + "form-data": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-2.5.1.tgz", + "integrity": "sha512-m21N3WOmEEURgk6B9GLOE4RuWOFf28Lhh9qGYeNlGq4VDXUlJy2th2slBNU8Gp8EzloYZOibZJ7t5ecIrFSjVA==", + "requires": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.6", + "mime-types": "^2.1.12" + } + }, "fs.realpath": { "version": "1.0.0", "resolved": "http://10.32.1.103:4873/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -1250,6 +1278,21 @@ "resolved": "http://10.32.1.103:4873/mime-db/-/mime-db-1.38.0.tgz", "integrity": "sha1-GiqrFtqesWe0nG5N8tnGjWPY4q0=" }, + "mime-types": { + "version": "2.1.24", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.24.tgz", + "integrity": "sha512-WaFHS3MCl5fapm3oLxU4eYDw77IQM2ACcxQ9RIxfaC3ooc6PFuBMGZZsYpvoXS5D5QTWPieo1jjLdAm3TBP3cQ==", + "requires": { + "mime-db": "1.40.0" + }, + "dependencies": { + "mime-db": { + "version": "1.40.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.40.0.tgz", + "integrity": "sha512-jYdeOMPy9vnxEqFRRo6ZvTZ8d9oPb+k18PKoYNYUe2stVEBPPwsln/qWzdbmaIvnhZ9v2P+CuecK+fpUfsV2mA==" + } + } + }, "mimic-fn": { "version": "1.2.0", "resolved": "http://10.32.1.103:4873/mimic-fn/-/mimic-fn-1.2.0.tgz", diff --git a/package.json b/package.json index 85abbf1..0e29c34 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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",