From 3714621173c606c4c58439ed8941100ce9ddea14 Mon Sep 17 00:00:00 2001
From: Codex Assistant <codex@example.com>
Date: 星期三, 05 十一月 2025 15:10:49 +0800
Subject: [PATCH] bug
---
web/src/utils/cos.ts | 484 +++++++++++++++++++++++++++++++++++-----------------
1 files changed, 323 insertions(+), 161 deletions(-)
diff --git a/web/src/utils/cos.ts b/web/src/utils/cos.ts
index 2a61394..53ccd09 100644
--- a/web/src/utils/cos.ts
+++ b/web/src/utils/cos.ts
@@ -1,162 +1,324 @@
-import COS from 'cos-js-sdk-v5'
-import axios from 'axios'
-
-// 浠庡悗绔幏鍙栦复鏃跺瘑閽�
-const getCredentialsFromBackend = async () => {
- try {
- const response = await axios.get('http://localhost:8080/api/cos/credentials')
- return response.data
- } catch (error) {
- throw error
- }
-}
-
-// 鍒涘缓COS瀹炰緥
-const cos = new COS({
- getAuthorization: async function (options: any, callback: any) {
- try {
- console.log('姝e湪浠庡悗绔幏鍙朇OS涓存椂瀵嗛挜...')
- // 浠庡悗绔幏鍙栦复鏃跺瘑閽�
- const credentials = await getCredentialsFromBackend()
-
- console.log('鎴愬姛鑾峰彇涓存椂瀵嗛挜:', {
- TmpSecretId: credentials.TmpSecretId?.substring(0, 10) + '...',
- bucket: credentials.config?.bucket,
- region: credentials.config?.region
- })
-
- callback({
- TmpSecretId: credentials.TmpSecretId,
- TmpSecretKey: credentials.TmpSecretKey,
- SecurityToken: credentials.SecurityToken,
- StartTime: credentials.StartTime,
- ExpiredTime: credentials.ExpiredTime,
- })
- } catch (error) {
- console.error('鑾峰彇涓存椂瀵嗛挜澶辫触:', error)
- callback(error)
- }
- }
-})
-
-// 鑾峰彇COS閰嶇疆淇℃伅
-export const getCOSConfig = async () => {
- try {
- const response = await axios.get('http://localhost:8080/api/cos/config')
- return response.data
- } catch (error) {
- console.error('鑾峰彇COS閰嶇疆澶辫触:', error)
- throw error
- }
-}
-
-/**
- * 涓婁紶鏂囦欢鍒拌吘璁簯COS
- * @param file 瑕佷笂浼犵殑鏂囦欢
- * @param folder 瀛樺偍鏂囦欢澶硅矾寰勶紝濡� 'avatars/', 'documents/'
- * @returns Promise<string> 杩斿洖鏂囦欢鐨勮闂甎RL
- */
-export const uploadToCOS = async (file: File, folder: string = ''): Promise<string> => {
- try {
- // 鑾峰彇COS閰嶇疆
- const config = await getCOSConfig()
-
- // 鐢熸垚鍞竴鏂囦欢鍚�
- const timestamp = Date.now()
- const randomStr = Math.random().toString(36).substring(2, 8)
- const fileExt = file.name.split('.').pop()
- const fileName = `${folder}${timestamp}_${randomStr}.${fileExt}`
-
- console.log('寮�濮嬩笂浼犳枃浠�:', fileName, '鍒板瓨鍌ㄦ《:', config.bucket)
-
- return new Promise((resolve, reject) => {
- cos.uploadFile({
- Bucket: config.bucket,
- Region: config.region,
- Key: fileName,
- Body: file,
- SliceSize: 1024 * 1024 * 5, // 澶т簬5MB鐨勬枃浠朵娇鐢ㄥ垎鍧椾笂浼�
- onProgress: (progressData) => {
- console.log('涓婁紶杩涘害:', Math.round(progressData.percent * 100) + '%')
- }
- }, (err, data) => {
- if (err) {
- console.error('涓婁紶澶辫触:', err)
- reject(err)
- } else {
- console.log('涓婁紶鎴愬姛:', data)
- // 杩斿洖鏂囦欢鐨勮闂甎RL
- const fileUrl = `https://${data.Location}`
- resolve(fileUrl)
- }
- })
- })
- } catch (error) {
- console.error('涓婁紶鏂囦欢澶辫触:', error)
- throw error
- }
-}
-
-/**
- * 鍒犻櫎COS涓殑鏂囦欢
- * @param key 鏂囦欢鐨凨ey锛堣矾寰勶級
- * @returns Promise<boolean>
- */
-export const deleteFromCOS = async (key: string): Promise<boolean> => {
- try {
- const config = await getCOSConfig()
-
- return new Promise((resolve, reject) => {
- cos.deleteObject({
- Bucket: config.bucket,
- Region: config.region,
- Key: key
- }, (err, data) => {
- if (err) {
- console.error('鍒犻櫎澶辫触:', err)
- reject(err)
- } else {
- console.log('鍒犻櫎鎴愬姛:', data)
- resolve(true)
- }
- })
- })
- } catch (error) {
- console.error('鍒犻櫎鏂囦欢澶辫触:', error)
- throw error
- }
-}
-
-/**
- * 鑾峰彇鏂囦欢鐨勪复鏃惰闂甎RL锛堢敤浜庣鏈夎鍙栫殑鏂囦欢锛�
- * @param key 鏂囦欢鐨凨ey锛堣矾寰勶級
- * @param expires 杩囨湡鏃堕棿锛堢锛夛紝榛樿1灏忔椂
- * @returns Promise<string>
- */
-export const getObjectUrl = async (key: string, expires: number = 3600): Promise<string> => {
- try {
- const config = await getCOSConfig()
-
- return new Promise((resolve, reject) => {
- cos.getObjectUrl({
- Bucket: config.bucket,
- Region: config.region,
- Key: key,
- Expires: expires,
- Sign: true
- }, (err, data) => {
- if (err) {
- console.error('鑾峰彇URL澶辫触:', err)
- reject(err)
- } else {
- resolve(data.Url)
- }
- })
- })
- } catch (error) {
- console.error('鑾峰彇鏂囦欢URL澶辫触:', error)
- throw error
- }
-}
-
+import COS from 'cos-js-sdk-v5'
+
+import axios from 'axios'
+import { serverUrl } from './appConfig.js'
+
+
+
+// 浠庡悗绔幏鍙栦复鏃跺瘑閽�
+
+const getCredentialsFromBackend = async () => {
+
+ try {
+
+ const response = await axios.get(`${serverUrl}/api/cos/credentials`)
+
+ return response.data
+
+ } catch (error) {
+
+ throw error
+
+ }
+
+}
+
+
+
+// 鍒涘缓COS瀹炰緥
+
+const cos = new COS({
+
+ getAuthorization: async function (options: any, callback: any) {
+
+ try {
+
+ console.log('姝e湪浠庡悗绔幏鍙朇OS涓存椂瀵嗛挜...')
+
+ // 浠庡悗绔幏鍙栦复鏃跺瘑閽�
+
+ const credentials = await getCredentialsFromBackend()
+
+
+
+ console.log('鎴愬姛鑾峰彇涓存椂瀵嗛挜:', {
+
+ TmpSecretId: credentials.TmpSecretId?.substring(0, 10) + '...',
+
+ bucket: credentials.config?.bucket,
+
+ region: credentials.config?.region
+
+ })
+
+
+
+ callback({
+
+ TmpSecretId: credentials.TmpSecretId,
+
+ TmpSecretKey: credentials.TmpSecretKey,
+
+ SecurityToken: credentials.SecurityToken,
+
+ StartTime: credentials.StartTime,
+
+ ExpiredTime: credentials.ExpiredTime,
+
+ })
+
+ } catch (error) {
+
+ console.error('鑾峰彇涓存椂瀵嗛挜澶辫触:', error)
+
+ callback(error)
+
+ }
+
+ }
+
+})
+
+
+
+// 鑾峰彇COS閰嶇疆淇℃伅
+
+export const getCOSConfig = async () => {
+
+ try {
+
+ const response = await axios.get(`${serverUrl}/api/cos/config`)
+
+ return response.data
+
+ } catch (error) {
+
+ console.error('鑾峰彇COS閰嶇疆澶辫触:', error)
+
+ throw error
+
+ }
+
+}
+
+
+
+/**
+
+ * 涓婁紶鏂囦欢鍒拌吘璁簯COS
+
+ * @param file 瑕佷笂浼犵殑鏂囦欢
+
+ * @param folder 瀛樺偍鏂囦欢澶硅矾寰勶紝濡� 'avatars/', 'documents/'
+
+ * @returns Promise<string> 杩斿洖鏂囦欢鐨勮闂甎RL
+
+ */
+
+export const uploadToCOS = async (file: File, folder: string = ''): Promise<string> => {
+
+ try {
+
+ // 鑾峰彇COS閰嶇疆
+
+ const config = await getCOSConfig()
+
+
+
+ // 鐢熸垚鍞竴鏂囦欢鍚�
+
+ const timestamp = Date.now()
+
+ const randomStr = Math.random().toString(36).substring(2, 8)
+
+ const fileExt = file.name.split('.').pop()
+
+ const fileName = `${folder}${timestamp}_${randomStr}.${fileExt}`
+
+
+
+ console.log('寮�濮嬩笂浼犳枃浠�:', fileName, '鍒板瓨鍌ㄦ《:', config.bucket)
+
+
+
+ return new Promise((resolve, reject) => {
+
+ cos.uploadFile({
+
+ Bucket: config.bucket,
+
+ Region: config.region,
+
+ Key: fileName,
+
+ Body: file,
+
+ SliceSize: 1024 * 1024 * 5, // 澶т簬5MB鐨勬枃浠朵娇鐢ㄥ垎鍧椾笂浼�
+
+ onProgress: (progressData) => {
+
+ console.log('涓婁紶杩涘害:', Math.round(progressData.percent * 100) + '%')
+
+ }
+
+ }, (err, data) => {
+
+ if (err) {
+
+ console.error('涓婁紶澶辫触:', err)
+
+ reject(err)
+
+ } else {
+
+ console.log('涓婁紶鎴愬姛:', data)
+
+ // 杩斿洖鏂囦欢鐨勮闂甎RL
+
+ const fileUrl = `https://${data.Location}`
+
+ resolve(fileUrl)
+
+ }
+
+ })
+
+ })
+
+ } catch (error) {
+
+ console.error('涓婁紶鏂囦欢澶辫触:', error)
+
+ throw error
+
+ }
+
+}
+
+
+
+/**
+
+ * 鍒犻櫎COS涓殑鏂囦欢
+
+ * @param key 鏂囦欢鐨凨ey锛堣矾寰勶級
+
+ * @returns Promise<boolean>
+
+ */
+
+export const deleteFromCOS = async (key: string): Promise<boolean> => {
+
+ try {
+
+ const config = await getCOSConfig()
+
+
+
+ return new Promise((resolve, reject) => {
+
+ cos.deleteObject({
+
+ Bucket: config.bucket,
+
+ Region: config.region,
+
+ Key: key
+
+ }, (err, data) => {
+
+ if (err) {
+
+ console.error('鍒犻櫎澶辫触:', err)
+
+ reject(err)
+
+ } else {
+
+ console.log('鍒犻櫎鎴愬姛:', data)
+
+ resolve(true)
+
+ }
+
+ })
+
+ })
+
+ } catch (error) {
+
+ console.error('鍒犻櫎鏂囦欢澶辫触:', error)
+
+ throw error
+
+ }
+
+}
+
+
+
+/**
+
+ * 鑾峰彇鏂囦欢鐨勪复鏃惰闂甎RL锛堢敤浜庣鏈夎鍙栫殑鏂囦欢锛�
+
+ * @param key 鏂囦欢鐨凨ey锛堣矾寰勶級
+
+ * @param expires 杩囨湡鏃堕棿锛堢锛夛紝榛樿1灏忔椂
+
+ * @returns Promise<string>
+
+ */
+
+export const getObjectUrl = async (key: string, expires: number = 3600): Promise<string> => {
+
+ try {
+
+ const config = await getCOSConfig()
+
+
+
+ return new Promise((resolve, reject) => {
+
+ cos.getObjectUrl({
+
+ Bucket: config.bucket,
+
+ Region: config.region,
+
+ Key: key,
+
+ Expires: expires,
+
+ Sign: true
+
+ }, (err, data) => {
+
+ if (err) {
+
+ console.error('鑾峰彇URL澶辫触:', err)
+
+ reject(err)
+
+ } else {
+
+ resolve(data.Url)
+
+ }
+
+ })
+
+ })
+
+ } catch (error) {
+
+ console.error('鑾峰彇鏂囦欢URL澶辫触:', error)
+
+ throw error
+
+ }
+
+}
+
+
+
export default cos
\ No newline at end of file
--
Gitblit v1.8.0