Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: provide components for uploading files #103

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions spx-gui/package-lock.json

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

1 change: 1 addition & 0 deletions spx-gui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"nanoid": "^5.0.5",
"pinia": "^2.1.7",
"pinia-plugin-persistedstate": "^3.2.1",
"qiniu-js": "^3.4.2",
"vicons": "^0.0.1",
"vue": "^3.3.11",
"vue-i18n": "^9.9.0",
Expand Down
2 changes: 1 addition & 1 deletion spx-gui/src/axios/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { createDiscreteApi } from 'naive-ui'
import axios, { type AxiosResponse } from 'axios'
import { useUserStore } from '@/store'

const baseURL = 'http://116.62.66.126:8080'
const baseURL = import.meta.env.VITE_API_BASE;

const service = axios.create({
baseURL: baseURL,
Expand Down
38 changes: 38 additions & 0 deletions spx-gui/src/util/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as qiniu from 'qiniu-js'
import { service } from '@/axios'

async function getUploadToken() {
try {
const response = await service.get('/project/upload-token')
return response.data.data
} catch (error) {
console.error(error)
return null
}
}

async function uploadFile(file: File) {
const token = await getUploadToken()
if (!token) {
throw new Error('Unable to get upload token')
}
return new Promise((resolve, reject) => {

const observable = qiniu.upload(file, null, token)
observable.subscribe({
next(res) {
console.log('upload progress: ', res.total.percent)
},
error(err) {
console.error('Upload failed:', err)
reject(err)
},
complete(res) {
const url = import.meta.env.VITE_KODO_ADDRESS + '/' + `${res.key}`
resolve(url)
}
})
})
}

export { uploadFile }
Loading