feat: use tiny oss (#375)

close #313
This commit is contained in:
Libin YANG 2024-08-31 11:27:26 +08:00 committed by GitHub
parent eb77ada3d7
commit 91df77e6b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 203 additions and 579 deletions

745
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -19,7 +19,6 @@
"dependencies": { "dependencies": {
"@element-plus/icons-vue": "^2.3.1", "@element-plus/icons-vue": "^2.3.1",
"@vueuse/core": "^11.0.1", "@vueuse/core": "^11.0.1",
"ali-oss": "^6.21.0",
"axios": "^1.7.4", "axios": "^1.7.4",
"buffer-from": "^1.1.2", "buffer-from": "^1.1.2",
"class-variance-authority": "^0.7.0", "class-variance-authority": "^0.7.0",
@ -46,6 +45,7 @@
"radix-vue": "^1.9.4", "radix-vue": "^1.9.4",
"tailwind-merge": "^2.5.2", "tailwind-merge": "^2.5.2",
"tailwindcss-animate": "^1.0.7", "tailwindcss-animate": "^1.0.7",
"tiny-oss": "^0.5.1",
"uuid": "^10.0.0", "uuid": "^10.0.0",
"vue": "^3.4.38" "vue": "^3.4.38"
}, },

View File

@ -30,6 +30,7 @@ const formAliOSS = ref({
region: ``, region: ``,
path: ``, path: ``,
cdnHost: ``, cdnHost: ``,
useSSL: true,
}) })
const formTxCOS = ref({ const formTxCOS = ref({
@ -380,6 +381,9 @@ function uploadImage(params) {
<el-form-item label="Bucket 所在区域" :required="true"> <el-form-item label="Bucket 所在区域" :required="true">
<el-input v-model.trim="formAliOSS.region" placeholder="如oss-cn-shenzhen" /> <el-input v-model.trim="formAliOSS.region" placeholder="如oss-cn-shenzhen" />
</el-form-item> </el-form-item>
<el-form-item label="UseSSL" :required="true">
<el-switch v-model="formAliOSS.useSSL" active-text="" inactive-text="" />
</el-form-item>
<el-form-item label="自定义 CDN 域名" :required="false"> <el-form-item label="自定义 CDN 域名" :required="false">
<el-input v-model.trim="formAliOSS.cdnHost" placeholder="如https://imagecdn.alidaodao.com可不填" /> <el-input v-model.trim="formAliOSS.cdnHost" placeholder="如https://imagecdn.alidaodao.com可不填" />
</el-form-item> </el-form-item>

View File

@ -1,5 +1,5 @@
import CryptoJS from 'crypto-js' import CryptoJS from 'crypto-js'
import OSS from 'ali-oss' import OSS from 'tiny-oss'
import * as Minio from 'minio' import * as Minio from 'minio'
import COS from 'cos-js-sdk-v5' import COS from 'cos-js-sdk-v5'
import Buffer from 'buffer-from' import Buffer from 'buffer-from'
@ -167,23 +167,24 @@ async function qiniuUpload(file) {
// AliOSS File Upload // AliOSS File Upload
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
async function aliOSSFileUpload(content, filename) { async function aliOSSFileUpload(file) {
const dateFilename = getDateFilename(filename) const dateFilename = getDateFilename(file.name)
const { region, bucket, accessKeyId, accessKeySecret, cdnHost, path } const { region, bucket, accessKeyId, accessKeySecret, useSSL, cdnHost, path }
= JSON.parse(localStorage.getItem(`aliOSSConfig`)) = JSON.parse(localStorage.getItem(`aliOSSConfig`))
const buffer = Buffer(content, `base64`) const dir = path ? `${path}/${dateFilename}` : dateFilename
const dir = `${path}/${dateFilename}` const secure = useSSL === undefined || useSSL
const protocol = secure ? `https` : `http`
const client = new OSS({ const client = new OSS({
region, region,
bucket, bucket,
accessKeyId, accessKeyId,
accessKeySecret, accessKeySecret,
secure,
}) })
try { try {
const res = await client.put(dir, buffer) await client.put(dir, file)
if (cdnHost === ``) return cdnHost ? `${cdnHost}/${dir}` : `${protocol}://${bucket}.${region}.aliyuncs.com/${dir}`
return res.url
return `${cdnHost}/${path === `` ? dateFilename : dir}`
} }
catch (e) { catch (e) {
return Promise.reject(e) return Promise.reject(e)
@ -290,7 +291,7 @@ async function formCustomUpload(content, file) {
util: { util: {
axios: fetch, // axios 实例 axios: fetch, // axios 实例
CryptoJS, // 加密库 CryptoJS, // 加密库
OSS, // ali-oss OSS, // ali-oss(tiny-oss)
COS, // cos-js-sdk-v5 COS, // cos-js-sdk-v5
Buffer, // buffer-from Buffer, // buffer-from
uuidv4, // uuid uuidv4, // uuid
@ -315,7 +316,7 @@ function fileUpload(content, file) {
!imgHost && localStorage.setItem(`imgHost`, `default`) !imgHost && localStorage.setItem(`imgHost`, `default`)
switch (imgHost) { switch (imgHost) {
case `aliOSS`: case `aliOSS`:
return aliOSSFileUpload(content, file.name) return aliOSSFileUpload(file)
case `minio`: case `minio`:
return minioFileUpload(content, file.name) return minioFileUpload(content, file.name)
case `txCOS`: case `txCOS`:

View File

@ -2,8 +2,8 @@ import juice from 'juice'
import { format } from 'prettier/standalone' import { format } from 'prettier/standalone'
import * as prettierPluginMarkdown from 'prettier/plugins/markdown' import * as prettierPluginMarkdown from 'prettier/plugins/markdown'
import * as prettierPluginBabel from 'prettier/plugins/babel'; import * as prettierPluginBabel from 'prettier/plugins/babel'
import * as prettierPluginEstree from 'prettier/plugins/estree'; import * as prettierPluginEstree from 'prettier/plugins/estree'
import * as prettierPluginCss from 'prettier/plugins/postcss' import * as prettierPluginCss from 'prettier/plugins/postcss'
import { prefix } from '@/config' import { prefix } from '@/config'

View File

@ -163,7 +163,6 @@ function uploadImage(file, cb) {
toBase64(file) toBase64(file)
.then(base64Content => fileApi.fileUpload(base64Content, file)) .then(base64Content => fileApi.fileUpload(base64Content, file))
.then((url) => { .then((url) => {
console.log(url)
if (cb) { if (cb) {
cb(url) cb(url)
} }
@ -298,7 +297,6 @@ function mdLocalToRemote() {
.replace(`](${item.matchStr})`, `](${item.url})`) .replace(`](${item.matchStr})`, `](${item.url})`)
}) })
editor.value.setValue(md.str) editor.value.setValue(md.str)
console.log(`resList`, resList, md.str)
} }
dom.ondragover = evt => evt.preventDefault() dom.ondragover = evt => evt.preventDefault()