Browse Source

文件路径保存不重复

黄琪勇 3 months ago
parent
commit
adf2d91be9

+ 2 - 2
public/pptworker/upload.js

@@ -36,7 +36,7 @@ function fileUpload({ host, token }, { fileName, file }) {
 }
 
 const getUploadSign = (fileName, host, token) => {
-  const fileUrl = "pptList/" + fileName
+  const fileUrl = `pptList/${filePath || "errPath/"}` + fileName
   return ajaxRequest(
     `${host}/edu-app/open/getUploadSign?pluginName=${ossType}`,
     "POST",
@@ -66,7 +66,7 @@ const onOnlyFileUpload = (signature, params) => {
   return cos.uploadFile({
     Bucket: tencentBucket /* 填写自己的 bucket,必须字段 */,
     Region: "ap-nanjing" /* 存储桶所在地域,必须字段 */,
-    Key: `pptList/${params.fileName}`,
+    Key: `pptList/${filePath || "errPath/"}${params.fileName}`,
     /* 存储在桶里的对象键(例如:1.jpg,a/b/test.txt,图片.jpg)支持中文,必须字段 */
     Body: params.file, // 上传文件对象
     SliceSize: 1024 * 1024 * 500 /* 触发分块上传的阈值,超过5MB使用分块上传,小于5MB使用简单上传。可自行设置,非必须 */,

+ 8 - 10
src/store/pptWork.ts

@@ -13,8 +13,9 @@ import { useSlidesStore } from "@/store"
 type pptWork = { id: string; coverImg: string; jsonUrl: string; isSave: boolean }
 const useStore = defineStore("pptWork", {
   state: (): pptWork => {
+    const route = useRoute()
     return {
-      id: "",
+      id: route.query.id as string,
       coverImg: "",
       jsonUrl: "",
       isSave: false // 当前是否保存,用来关闭页面时候判断 提不提示
@@ -23,15 +24,12 @@ const useStore = defineStore("pptWork", {
   actions: {
     /** 初始化 */
     async initPPTData() {
-      const route = useRoute()
-      const id = route.query.id as string | undefined
-      if (id) {
+      if (this.id) {
         LoadingBar.loading(true, "资源加载中,请稍等...")
-        const res = await httpAjaxErrMsg(getTeacherChapterKnowledgeMaterial, id)
+        const res = await httpAjaxErrMsg(getTeacherChapterKnowledgeMaterial, this.id)
         if (res.code === 200) {
-          const { id, dataJson, chapterLessonCoursewareName } = res.data || {}
+          const { dataJson, chapterLessonCoursewareName } = res.data || {}
           const { coverImg, jsonUrl } = JSON.parse(dataJson)
-          this.id = id
           this.coverImg = coverImg || ""
           const slidesStore = useSlidesStore()
           slidesStore.setTitle(chapterLessonCoursewareName)
@@ -47,8 +45,8 @@ const useStore = defineStore("pptWork", {
     },
     async updatePPT() {
       await this.updateCoverImg()
-      const { blob, title } = getJsonToBlob()
-      fileUpload(title, blob)
+      const { blob } = getJsonToBlob()
+      fileUpload(`${this.id}ppt`, blob, `${this.id}/`, false)
         .then(url => {
           LoadingBar.loading(true, "课件保存中,请稍等...")
           httpAjaxErrMsg(putTeacherChapterKnowledgeMaterial, {
@@ -84,7 +82,7 @@ const useStore = defineStore("pptWork", {
         if (coverImgDom) {
           const dataBlob = await toBlob(coverImgDom)
           if (dataBlob) {
-            const url = await fileUpload("coverImg", dataBlob)
+            const url = await fileUpload(`${this.id}coverImg`, dataBlob, `${this.id}/`, false)
             url && (this.coverImg = url)
           }
         }

+ 11 - 8
src/utils/oss-file-upload.ts

@@ -6,16 +6,19 @@ import loadingBar from "@/plugins/loadingBar"
 const tencentBucket = "daya-online-1303457149"
 const ossType = "tencent"
 
-export default async function fileUpload(fileName: string, file: Blob) {
+export default async function fileUpload(fileName: string, file: Blob, filePath: string, isAddTimestamp = true) {
   loadingBar.loading(true, "资源上传中")
   // 上传名称加上时间戳
-  fileName = addTimestampBeforeExtension(fileName)
-  const resUploadSign = await getUploadSign(fileName)
+  if (isAddTimestamp) {
+    fileName = addTimestampBeforeExtension(fileName)
+  }
+  const resUploadSign = await getUploadSign(fileName, filePath)
   if (resUploadSign.code === 200) {
     try {
       const resUpload = await onOnlyFileUpload(resUploadSign.data.signature, {
         fileName,
-        file
+        file,
+        filePath
       })
       loadingBar.loading(false)
       if (resUpload.statusCode === 200) {
@@ -33,8 +36,8 @@ export default async function fileUpload(fileName: string, file: Blob) {
   }
 }
 
-const getUploadSign = async (fileName: string) => {
-  const fileUrl = "pptList/" + fileName
+const getUploadSign = async (fileName: string, filePath: string) => {
+  const fileUrl = `pptList/${filePath || "errPath/"}` + fileName
   return httpAjax(
     getUploadSignApi,
     {
@@ -49,7 +52,7 @@ const getUploadSign = async (fileName: string) => {
   )
 }
 
-const onOnlyFileUpload = (signature: string, params: { fileName: string; file: Blob }) => {
+const onOnlyFileUpload = (signature: string, params: { fileName: string; file: Blob; filePath: string }) => {
   const cos = new COS({
     Domain: "https://oss.dayaedu.com",
     Protocol: "https",
@@ -60,7 +63,7 @@ const onOnlyFileUpload = (signature: string, params: { fileName: string; file: B
   return cos.uploadFile({
     Bucket: tencentBucket /* 填写自己的 bucket,必须字段 */,
     Region: "ap-nanjing" /* 存储桶所在地域,必须字段 */,
-    Key: `pptList/${params.fileName}`,
+    Key: `pptList/${params.filePath || "errPath/"}${params.fileName}`,
     /* 存储在桶里的对象键(例如:1.jpg,a/b/test.txt,图片.jpg)支持中文,必须字段 */
     Body: params.file, // 上传文件对象
     SliceSize: 1024 * 1024 * 500 /* 触发分块上传的阈值,超过5MB使用分块上传,小于5MB使用简单上传。可自行设置,非必须 */,

+ 3 - 1
src/views/Editor/CanvasTool/index.vue

@@ -293,7 +293,9 @@ import PopoverMenuItem from "@/components/PopoverMenuItem.vue"
 import { ElUpload, ElMessage, type UploadRequestOptions } from "element-plus"
 import cloudCoachList from "@/views/components/element/cloudCoachElement/cloudCoachList"
 import fileUpload from "@/utils/oss-file-upload"
+import usePptWork from "@/store/pptWork"
 
+const usePptWorkHook = usePptWork()
 const mainStore = useMainStore()
 const { creatingElement, creatingCustomShape, showSelectPanel, showSearchPanel, showNotesPanel } = storeToRefs(mainStore)
 const { canUndo, canRedo } = storeToRefs(useSnapshotStore())
@@ -341,7 +343,7 @@ const moreToolsVisible = ref(false)
 // 音视频
 function handleUpload(fileData: UploadRequestOptions) {
   const type = /\.(mp3|wav|m4a)$/i.test(fileData.file.name) ? "audio" : "video"
-  fileUpload(fileData.file.name, fileData.file)
+  fileUpload(fileData.file.name, fileData.file, `${usePptWorkHook.id}/`)
     .then(res => {
       if (type === "audio") {
         createAudioElement(res)

+ 4 - 1
src/worker/useCoursewaresWorker.ts

@@ -2,6 +2,7 @@ import myWorker from "./index"
 import axios from "axios"
 import { getToken } from "@/libs/auth"
 import { URL_API } from "@/config/index"
+import usePptWork from "@/store/pptWork"
 
 let worker: any = null
 let useOnCoursewaresMessage = (e: any) => {}
@@ -13,10 +14,12 @@ export const initWorker = () => {
   // eslint-disable-next-line no-async-promise-executor
   return new Promise(async (resolve, reject) => {
     if (!worker) {
+      const usePptWorkHook = usePptWork()
       const str1 = await axios.get("./pptworker/cos-js-sdk-v5.min.js")
       const str2 = await axios.get("./pptworker/upload.js")
       const str3 = await axios.get("./pptworker/pptJson.js")
-      worker = new myWorker([str1.data, str2.data, str3.data])
+      /*  这里根据id 加上一个文件夹 */
+      worker = new myWorker([str1.data, `\r\nconst filePath = "${usePptWorkHook.id}/"\r\n`, str2.data, str3.data])
       worker.ready
         .then(() => worker.createTaskController())
         .then(() => {