Commit c434b3e3 by 王立鹏

Merge branch '代码优化/11642-上传视频负载优化' into 'master'

【代码优化 11642】 feat: 完成负载上传优化

See merge request !30
parents 31150849 7d5763ce
...@@ -5,7 +5,26 @@ import { storeToRefs } from 'pinia' ...@@ -5,7 +5,26 @@ import { storeToRefs } from 'pinia'
import type { UploadFileResponseItem, ChunkCheckResponse } from './types' import type { UploadFileResponseItem, ChunkCheckResponse } from './types'
// 'https://oa.yswg.com.cn:8085' // 'https://oa.yswg.com.cn:8085'
const OA_UPLOAD_CHUNK_BASE_URL = 'https://nas1.yswg.top:8088' // const OA_UPLOAD_CHUNK_BASE_URL = 'https://nas1.yswg.top:8088'
// nas大文件上传的api地址负载
// 负载比
// 40% nas1
// 20% nas2
// 40% nas3
export const uploadBaseUrlPool = [
{ url: "https://nas1.yswg.top:8088", weight: 40 },
{ url: "https://nas2.yswg.top:8088", weight: 20 },
{ url: "https://nas3.yswg.top:8088", weight: 40 },
];
// 获取到nas上传地址
export const resolveUploadBaseUrl = () => {
const random = Math.random() * 100;
if (random < 40) return uploadBaseUrlPool[0]!.url;
if (random < 60) return uploadBaseUrlPool[1]!.url;
return uploadBaseUrlPool[2]!.url;
};
const OA_UPLOAD_COMMON_BASE_URL = 'http://47.112.96.71:8082' const OA_UPLOAD_COMMON_BASE_URL = 'http://47.112.96.71:8082'
const CHUNK_UPLOAD_THRESHOLD = 10 * 1024 * 1024 const CHUNK_UPLOAD_THRESHOLD = 10 * 1024 * 1024
const CHUNK_SIZE = 5 * 1024 * 1024 const CHUNK_SIZE = 5 * 1024 * 1024
...@@ -105,7 +124,7 @@ async function uploadFileByChunks( ...@@ -105,7 +124,7 @@ async function uploadFileByChunks(
const fileSuffix = file.name.includes('.') ? file.name.slice(file.name.lastIndexOf('.')) : '' const fileSuffix = file.name.includes('.') ? file.name.slice(file.name.lastIndexOf('.')) : ''
const checkResponse = await axios.post<{ data: ChunkCheckResponse }>( const checkResponse = await axios.post<{ data: ChunkCheckResponse }>(
`${OA_UPLOAD_CHUNK_BASE_URL}/mobiles/file-upload/check`, `${resolveUploadBaseUrl()}/mobiles/file-upload/check`,
{ {
hash: fileHash, hash: fileHash,
fileName: file.name, fileName: file.name,
...@@ -154,7 +173,7 @@ async function uploadFileByChunks( ...@@ -154,7 +173,7 @@ async function uploadFileByChunks(
formData.append('filePart', chunk, file.name) formData.append('filePart', chunk, file.name)
formData.append('chunkNumber', String(index)) formData.append('chunkNumber', String(index))
await axios.post(`${OA_UPLOAD_CHUNK_BASE_URL}/mobiles/file-upload/chunk`, formData, { await axios.post(`${resolveUploadBaseUrl()}/mobiles/file-upload/chunk`, formData, {
signal, signal,
headers: { headers: {
'Content-Type': 'multipart/form-data', 'Content-Type': 'multipart/form-data',
...@@ -194,7 +213,7 @@ async function uploadFileByChunks( ...@@ -194,7 +213,7 @@ async function uploadFileByChunks(
// 上传完成 通知后端文件上传完成 // 上传完成 通知后端文件上传完成
const finishResponse = await axios.post<{ data: { fileUrl: string } }>( const finishResponse = await axios.post<{ data: { fileUrl: string } }>(
`${OA_UPLOAD_CHUNK_BASE_URL}/mobiles/file-upload/finish`, `${resolveUploadBaseUrl()}/mobiles/file-upload/finish`,
finishFormData, finishFormData,
{ {
signal, signal,
...@@ -265,7 +284,7 @@ export const uploadFile = ( ...@@ -265,7 +284,7 @@ export const uploadFile = (
export const getTimestamp = async () => { export const getTimestamp = async () => {
const { const {
data: { data: timestamp }, data: { data: timestamp },
} = await axios.get(`${OA_UPLOAD_CHUNK_BASE_URL}/api/auth/getTime`) } = await axios.get(`${resolveUploadBaseUrl()}/api/auth/getTime`)
return timestamp return timestamp
} }
...@@ -275,7 +294,7 @@ export const getChunkApiAuthToken = async (weChatId: string) => { ...@@ -275,7 +294,7 @@ export const getChunkApiAuthToken = async (weChatId: string) => {
const rawStr = CHUNK_API_AUTH_SECRET + timestamp const rawStr = CHUNK_API_AUTH_SECRET + timestamp
const { const {
data: { data }, data: { data },
} = await axios.post(`${OA_UPLOAD_CHUNK_BASE_URL}/api/auth/getToken`, { } = await axios.post(`${resolveUploadBaseUrl()}/api/auth/getToken`, {
timestamp, timestamp,
weChatId, weChatId,
secret: CryptoJS.MD5(rawStr).toString(), secret: CryptoJS.MD5(rawStr).toString(),
......
...@@ -385,9 +385,9 @@ ...@@ -385,9 +385,9 @@
import UploadVideo from '@/components/common/UploadVideo/index.vue' import UploadVideo from '@/components/common/UploadVideo/index.vue'
import { useResetData } from '@/hooks' import { useResetData } from '@/hooks'
import { ArticleTypeEnum, ReleaseStatusTypeEnum, SendTypeEnum, BooleanFlag } from '@/constants' import { ArticleTypeEnum, ReleaseStatusTypeEnum, SendTypeEnum, BooleanFlag } from '@/constants'
import { addOrUpdateArticle, uploadFile, getArticleDetail } from '@/api' import { addOrUpdateArticle, uploadFile, getArticleDetail,resolveUploadBaseUrl } from '@/api'
import SelectTags from '@/components/common/SelectTags/index.vue' import SelectTags from '@/components/common/SelectTags/index.vue'
import type { TagItemDto, AddOrUpdateVideoDto } from '@/api' import type { TagItemDto, AddOrUpdateVideoDto} from '@/api'
import { useVideoStore, useUserStore } from '@/stores' import { useVideoStore, useUserStore } from '@/stores'
import { storeToRefs } from 'pinia' import { storeToRefs } from 'pinia'
import { selectDepOrUser } from '@/utils' import { selectDepOrUser } from '@/utils'
...@@ -650,7 +650,7 @@ const handleFileChange = async (e: Event) => { ...@@ -650,7 +650,7 @@ const handleFileChange = async (e: Event) => {
// 防止跨域 // 防止跨域
const parseUrl = (url: string) => { const parseUrl = (url: string) => {
return 'https://vikijin.site:8088' + '/oa/nfs' + new URL(url).pathname.replace('/database', '') return resolveUploadBaseUrl() + '/oa/nfs' + new URL(url).pathname.replace('/database', '')
} }
onDeactivated(() => { onDeactivated(() => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment