mirror of
https://gitee.com/lijingbo-2021/open-anylink-web.git
synced 2025-12-30 02:52:26 +00:00
文件上传逻辑清除冗余代码
This commit is contained in:
112
src/api/mts.js
112
src/api/mts.js
@@ -1,79 +1,71 @@
|
||||
import request from '@/js/utils/request'
|
||||
|
||||
export const mtsUploadServiceForImage = async (requestBody, { originFile, thumbFile }) => {
|
||||
try {
|
||||
const res = await request.postForm('/mts/getUploadUrl', requestBody)
|
||||
const scope = res.data.data.scope
|
||||
const objectId = res.data.data.objectId
|
||||
const originUrl = res.data.data.originUrl
|
||||
const thumbUrl = res.data.data.thumbUrl
|
||||
if (scope === 1 && originUrl && thumbUrl) {
|
||||
// 如果文件之前已经上传过,直接获取下载地址
|
||||
return res
|
||||
} else {
|
||||
const uploadOriginUrl = res.data.data.uploadOriginUrl
|
||||
const uploadThumbUrl = res.data.data.uploadThumbUrl
|
||||
// 2 上传原图
|
||||
const originResponse = await fetch(uploadOriginUrl, {
|
||||
const res = await request.postForm('/mts/getUploadUrl', requestBody)
|
||||
const scope = res.data.data.scope
|
||||
const objectId = res.data.data.objectId
|
||||
const originUrl = res.data.data.originUrl
|
||||
const thumbUrl = res.data.data.thumbUrl
|
||||
if (scope === 1 && originUrl && thumbUrl) {
|
||||
// 如果文件之前已经上传过,直接获取下载地址
|
||||
return res
|
||||
} else {
|
||||
const uploadOriginUrl = res.data.data.uploadOriginUrl
|
||||
const uploadThumbUrl = res.data.data.uploadThumbUrl
|
||||
// 2 上传原图
|
||||
const originResponse = await fetch(uploadOriginUrl, {
|
||||
method: 'PUT',
|
||||
body: originFile
|
||||
})
|
||||
|
||||
if (!originResponse.ok) {
|
||||
throw new Error('原图上传失败')
|
||||
}
|
||||
|
||||
// 3 上传缩略图,如果原图和缩略图一样,就不上传
|
||||
if (uploadThumbUrl !== uploadOriginUrl) {
|
||||
const thumbResponse = await fetch(uploadThumbUrl, {
|
||||
method: 'PUT',
|
||||
body: originFile
|
||||
body: thumbFile
|
||||
})
|
||||
|
||||
if (!originResponse.ok) {
|
||||
throw new Error('原图上传失败')
|
||||
if (!thumbResponse.ok) {
|
||||
throw new Error('缩略图上传失败')
|
||||
}
|
||||
|
||||
// 3 上传缩略图,如果原图和缩略图一样,就不上传
|
||||
if (uploadThumbUrl !== uploadOriginUrl) {
|
||||
const thumbResponse = await fetch(uploadThumbUrl, {
|
||||
method: 'PUT',
|
||||
body: thumbFile
|
||||
})
|
||||
|
||||
if (!thumbResponse.ok) {
|
||||
throw new Error('缩略图上传失败')
|
||||
}
|
||||
}
|
||||
|
||||
// 4 上报服务端上传成功,服务端返回预签名下载URL
|
||||
const reportResponse = await request.postForm('/mts/reportUploaded', { objectId })
|
||||
return reportResponse
|
||||
}
|
||||
} catch (error) {
|
||||
throw new Error('图片上传失败')
|
||||
|
||||
// 4 上报服务端上传成功,服务端返回预签名下载URL
|
||||
const reportResponse = await request.postForm('/mts/reportUploaded', { objectId })
|
||||
return reportResponse
|
||||
}
|
||||
|
||||
// TODO 上传期间查询上传进度
|
||||
}
|
||||
|
||||
export const mtsUploadService = async (requestBody, { originFile }) => {
|
||||
try {
|
||||
// 1 获取上传的预签名URL
|
||||
const res = await request.postForm('/mts/getUploadUrl', requestBody)
|
||||
const scope = res.data.data.scope
|
||||
const objectId = res.data.data.objectId
|
||||
const downloadUrl = res.data.data.downloadUrl
|
||||
if (scope === 1 && downloadUrl) {
|
||||
// 如果文件之前已经上传过,直接过的下载地址
|
||||
return res
|
||||
} else {
|
||||
const uploadUrl = res.data.data.uploadUrl
|
||||
// 2 上传文件
|
||||
const uploadResponse = await fetch(uploadUrl, {
|
||||
method: 'PUT',
|
||||
body: originFile
|
||||
})
|
||||
// 1 获取上传的预签名URL
|
||||
const res = await request.postForm('/mts/getUploadUrl', requestBody)
|
||||
const scope = res.data.data.scope
|
||||
const objectId = res.data.data.objectId
|
||||
const downloadUrl = res.data.data.downloadUrl
|
||||
if (scope === 1 && downloadUrl) {
|
||||
// 如果文件之前已经上传过,直接过的下载地址
|
||||
return res
|
||||
} else {
|
||||
const uploadUrl = res.data.data.uploadUrl
|
||||
// 2 上传文件
|
||||
const uploadResponse = await fetch(uploadUrl, {
|
||||
method: 'PUT',
|
||||
body: originFile
|
||||
})
|
||||
|
||||
if (!uploadResponse.ok) {
|
||||
throw new Error('文件上传失败')
|
||||
}
|
||||
|
||||
// 3 上报服务端上传成功,服务端返回预签名下载URL
|
||||
const reportResponse = await request.postForm('/mts/reportUploaded', { objectId })
|
||||
return reportResponse
|
||||
if (!uploadResponse.ok) {
|
||||
throw new Error('文件上传失败')
|
||||
}
|
||||
} catch (error) {
|
||||
throw new Error('文件上传失败')
|
||||
|
||||
// 3 上报服务端上传成功,服务端返回预签名下载URL
|
||||
const reportResponse = await request.postForm('/mts/reportUploaded', { objectId })
|
||||
return reportResponse
|
||||
}
|
||||
|
||||
// TODO 上传期间查询上传进度
|
||||
|
||||
@@ -42,7 +42,6 @@ export const generateThumb = async (blob, originalWidth = null, originalHeight =
|
||||
canvas.toBlob(
|
||||
async (blob) => {
|
||||
if (blob) {
|
||||
console.log(blob.size)
|
||||
if (blob.size <= THUMB_IMAGE_MAX) {
|
||||
const thumbFile = new File([blob], blob.name, {
|
||||
type: blob.type
|
||||
|
||||
@@ -92,7 +92,7 @@ const onSelectedFile = async (file) => {
|
||||
})
|
||||
.catch(() => {
|
||||
msg.uploadStatus = msgFileUploadStatus.UPLOAD_FAILED
|
||||
ElMessage.error('上传失败')
|
||||
ElMessage.error('文件上传失败')
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user