mirror of
https://github.com/Kuingsmile/PicList.git
synced 2025-02-02 11:08:13 -05:00
🔨 Refactor: rewrite some apis
This commit is contained in:
parent
ecf42fab96
commit
76e588b2ef
@ -1,19 +1,37 @@
|
|||||||
|
// 腾讯云 COS SDK
|
||||||
import COS from 'cos-nodejs-sdk-v5'
|
import COS from 'cos-nodejs-sdk-v5'
|
||||||
|
|
||||||
|
// 文件系统库
|
||||||
import fs from 'fs-extra'
|
import fs from 'fs-extra'
|
||||||
|
|
||||||
|
// 路径处理库
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
|
|
||||||
|
// 是否为图片的判断函数
|
||||||
import { isImage } from '~/renderer/manage/utils/common'
|
import { isImage } from '~/renderer/manage/utils/common'
|
||||||
|
|
||||||
|
// URL 编码处理函数
|
||||||
import { handleUrlEncode } from '~/universal/utils/common'
|
import { handleUrlEncode } from '~/universal/utils/common'
|
||||||
|
|
||||||
|
// 窗口管理器
|
||||||
import windowManager from 'apis/app/window/windowManager'
|
import windowManager from 'apis/app/window/windowManager'
|
||||||
|
|
||||||
|
// 枚举类型声明
|
||||||
import { IWindowList } from '#/types/enum'
|
import { IWindowList } from '#/types/enum'
|
||||||
|
|
||||||
|
// Electron 相关
|
||||||
import { ipcMain, IpcMainEvent } from 'electron'
|
import { ipcMain, IpcMainEvent } from 'electron'
|
||||||
|
|
||||||
|
// 错误格式化函数、获取文件 MIME 类型
|
||||||
import { formatError, getFileMimeType } from '../utils/common'
|
import { formatError, getFileMimeType } from '../utils/common'
|
||||||
import UpDownTaskQueue,
|
|
||||||
{
|
// 上传下载任务队列
|
||||||
uploadTaskSpecialStatus,
|
import UpDownTaskQueue, { uploadTaskSpecialStatus, commonTaskStatus, downloadTaskSpecialStatus } from '../datastore/upDownTaskQueue'
|
||||||
commonTaskStatus,
|
|
||||||
downloadTaskSpecialStatus
|
// 日志记录器
|
||||||
} from '../datastore/upDownTaskQueue'
|
|
||||||
import { ManageLogger } from '../utils/logger'
|
import { ManageLogger } from '../utils/logger'
|
||||||
|
|
||||||
|
// 取消下载任务的加载文件列表、刷新下载文件传输列表
|
||||||
import { cancelDownloadLoadingFileList, refreshDownloadFileTransferList } from '@/manage/utils/static'
|
import { cancelDownloadLoadingFileList, refreshDownloadFileTransferList } from '@/manage/utils/static'
|
||||||
|
|
||||||
class TcyunApi {
|
class TcyunApi {
|
||||||
@ -62,7 +80,7 @@ class TcyunApi {
|
|||||||
*/
|
*/
|
||||||
async getBucketList (): Promise<any> {
|
async getBucketList (): Promise<any> {
|
||||||
const res = await this.ctx.getService({})
|
const res = await this.ctx.getService({})
|
||||||
return res && res.Buckets ? res.Buckets : []
|
return res?.Buckets || []
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -74,21 +92,8 @@ class TcyunApi {
|
|||||||
Bucket: bucketName,
|
Bucket: bucketName,
|
||||||
Region: region
|
Region: region
|
||||||
})
|
})
|
||||||
const result = [] as string[]
|
if (res?.statusCode !== 200 || !res?.DomainRule?.length) return []
|
||||||
if (res && res.statusCode === 200) {
|
return res.DomainRule.filter((item: any) => item.Status === 'ENABLED').map(item => item.Name)
|
||||||
if (res.DomainRule && res.DomainRule.length > 0) {
|
|
||||||
res.DomainRule.forEach((item: any) => {
|
|
||||||
if (item.Status === 'ENABLED') {
|
|
||||||
result.push(item.Name)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return result
|
|
||||||
} else {
|
|
||||||
return []
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return []
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -113,31 +118,29 @@ class TcyunApi {
|
|||||||
Bucket: configMap.BucketName,
|
Bucket: configMap.BucketName,
|
||||||
Region: configMap.region
|
Region: configMap.region
|
||||||
})
|
})
|
||||||
return res && res.statusCode === 200
|
return res?.statusCode === 200
|
||||||
}
|
}
|
||||||
|
|
||||||
async getBucketListRecursively (configMap: IStringKeyMap): Promise<any> {
|
async getBucketListRecursively (configMap: IStringKeyMap): Promise<any> {
|
||||||
const window = windowManager.get(IWindowList.SETTING_WINDOW)!
|
const window = windowManager.get(IWindowList.SETTING_WINDOW)!
|
||||||
const bucket = configMap.bucketName
|
const { bucketName: bucket, bucketConfig: { Location: region }, prefix, customUrl, cancelToken } = configMap
|
||||||
const region = configMap.bucketConfig.Location
|
|
||||||
const prefix = configMap.prefix as string
|
|
||||||
const slicedPrefix = prefix.slice(1, prefix.length)
|
const slicedPrefix = prefix.slice(1, prefix.length)
|
||||||
const urlPrefix = configMap.customUrl || `https://${bucket}.cos.${region}.myqcloud.com`
|
const urlPrefix = customUrl || `https://${bucket}.cos.${region}.myqcloud.com`
|
||||||
let marker
|
|
||||||
const cancelToken = configMap.cancelToken as string
|
|
||||||
const cancelTask = [false]
|
const cancelTask = [false]
|
||||||
|
let marker
|
||||||
|
|
||||||
ipcMain.on(cancelDownloadLoadingFileList, (_evt: IpcMainEvent, token: string) => {
|
ipcMain.on(cancelDownloadLoadingFileList, (_evt: IpcMainEvent, token: string) => {
|
||||||
if (token === cancelToken) {
|
if (token === cancelToken) {
|
||||||
cancelTask[0] = true
|
cancelTask[0] = true
|
||||||
ipcMain.removeAllListeners(cancelDownloadLoadingFileList)
|
ipcMain.removeAllListeners(cancelDownloadLoadingFileList)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
let res = {} as COS.GetBucketResult
|
|
||||||
const result = {
|
const result = {
|
||||||
fullList: <any>[],
|
fullList: <any>[],
|
||||||
success: false,
|
success: false,
|
||||||
finished: false
|
finished: false
|
||||||
}
|
}
|
||||||
|
let res = {} as COS.GetBucketResult
|
||||||
do {
|
do {
|
||||||
res = await this.ctx.getBucket({
|
res = await this.ctx.getBucket({
|
||||||
Bucket: bucket,
|
Bucket: bucket,
|
||||||
@ -145,9 +148,9 @@ class TcyunApi {
|
|||||||
Prefix: slicedPrefix === '' ? undefined : slicedPrefix,
|
Prefix: slicedPrefix === '' ? undefined : slicedPrefix,
|
||||||
Marker: marker
|
Marker: marker
|
||||||
})
|
})
|
||||||
if (res && res.statusCode === 200) {
|
if (res?.statusCode === 200) {
|
||||||
res.Contents.forEach((item: COS.CosObject) =>
|
result.fullList.push(...res.Contents.filter(item => parseInt(item.Size) !== 0)
|
||||||
parseInt(item.Size) !== 0 && result.fullList.push(this.formatFile(item, slicedPrefix, urlPrefix)))
|
.map(item => this.formatFile(item, slicedPrefix, urlPrefix)))
|
||||||
window.webContents.send(refreshDownloadFileTransferList, result)
|
window.webContents.send(refreshDownloadFileTransferList, result)
|
||||||
} else {
|
} else {
|
||||||
result.finished = true
|
result.finished = true
|
||||||
@ -165,14 +168,12 @@ class TcyunApi {
|
|||||||
|
|
||||||
async getBucketListBackstage (configMap: IStringKeyMap): Promise < any > {
|
async getBucketListBackstage (configMap: IStringKeyMap): Promise < any > {
|
||||||
const window = windowManager.get(IWindowList.SETTING_WINDOW)!
|
const window = windowManager.get(IWindowList.SETTING_WINDOW)!
|
||||||
const bucket = configMap.bucketName
|
const { bucketName: bucket, bucketConfig: { Location: region }, prefix, customUrl, cancelToken } = configMap
|
||||||
const region = configMap.bucketConfig.Location
|
|
||||||
const prefix = configMap.prefix as string
|
|
||||||
const slicedPrefix = prefix.slice(1, prefix.length)
|
const slicedPrefix = prefix.slice(1, prefix.length)
|
||||||
const urlPrefix = configMap.customUrl || `https://${bucket}.cos.${region}.myqcloud.com`
|
const urlPrefix = customUrl || `https://${bucket}.cos.${region}.myqcloud.com`
|
||||||
let marker
|
|
||||||
const cancelToken = configMap.cancelToken as string
|
|
||||||
const cancelTask = [false]
|
const cancelTask = [false]
|
||||||
|
let marker
|
||||||
|
|
||||||
ipcMain.on('cancelLoadingFileList', (_evt: IpcMainEvent, token: string) => {
|
ipcMain.on('cancelLoadingFileList', (_evt: IpcMainEvent, token: string) => {
|
||||||
if (token === cancelToken) {
|
if (token === cancelToken) {
|
||||||
cancelTask[0] = true
|
cancelTask[0] = true
|
||||||
@ -193,11 +194,12 @@ class TcyunApi {
|
|||||||
Delimiter: '/',
|
Delimiter: '/',
|
||||||
Marker: marker
|
Marker: marker
|
||||||
})
|
})
|
||||||
if (res && res.statusCode === 200) {
|
if (res?.statusCode === 200) {
|
||||||
res.CommonPrefixes.forEach((item: { Prefix: string}) =>
|
result.fullList.push(
|
||||||
result.fullList.push(this.formatFolder(item, slicedPrefix)))
|
...res.CommonPrefixes.map(item => this.formatFolder(item, slicedPrefix)),
|
||||||
res.Contents.forEach((item: COS.CosObject) =>
|
...res.Contents.filter(item => parseInt(item.Size) !== 0)
|
||||||
parseInt(item.Size) !== 0 && result.fullList.push(this.formatFile(item, slicedPrefix, urlPrefix)))
|
.map(item => this.formatFile(item, slicedPrefix, urlPrefix))
|
||||||
|
)
|
||||||
window.webContents.send('refreshFileTransferList', result)
|
window.webContents.send('refreshFileTransferList', result)
|
||||||
} else {
|
} else {
|
||||||
result.finished = true
|
result.finished = true
|
||||||
@ -229,36 +231,34 @@ class TcyunApi {
|
|||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
async getBucketFileList (configMap: IStringKeyMap): Promise<any> {
|
async getBucketFileList (configMap: IStringKeyMap): Promise<any> {
|
||||||
const bucket = configMap.bucketName
|
const { bucketName: bucket, bucketConfig: { Location: region }, prefix, customUrl, marker, itemsPerPage } = configMap
|
||||||
const region = configMap.bucketConfig.Location
|
|
||||||
const prefix = configMap.prefix as string
|
|
||||||
const slicedPrefix = prefix.slice(1)
|
const slicedPrefix = prefix.slice(1)
|
||||||
const urlPrefix = configMap.customUrl || `https://${bucket}.cos.${region}.myqcloud.com`
|
const urlPrefix = customUrl || `https://${bucket}.cos.${region}.myqcloud.com`
|
||||||
const marker = configMap.marker as string
|
const res = await this.ctx.getBucket({
|
||||||
const itemsPerPage = configMap.itemsPerPage as number
|
|
||||||
let res = {} as COS.GetBucketResult
|
|
||||||
const result = {
|
|
||||||
fullList: <any>[],
|
|
||||||
isTruncated: false,
|
|
||||||
nextMarker: '',
|
|
||||||
success: false
|
|
||||||
}
|
|
||||||
res = await this.ctx.getBucket({
|
|
||||||
Bucket: bucket,
|
Bucket: bucket,
|
||||||
Region: region,
|
Region: region,
|
||||||
Prefix: slicedPrefix === '' ? undefined : slicedPrefix,
|
Prefix: slicedPrefix === '' ? undefined : slicedPrefix,
|
||||||
Delimiter: '/',
|
Delimiter: '/',
|
||||||
Marker: marker,
|
Marker: marker,
|
||||||
MaxKeys: itemsPerPage
|
MaxKeys: itemsPerPage
|
||||||
})
|
}) as COS.GetBucketResult
|
||||||
if (res && res.statusCode === 200) {
|
if (res?.statusCode !== 200) {
|
||||||
res.CommonPrefixes.forEach((item: { Prefix: string}) =>
|
return {
|
||||||
result.fullList.push(this.formatFolder(item, slicedPrefix)))
|
fullList: [],
|
||||||
res.Contents.forEach((item: COS.CosObject) =>
|
isTruncated: false,
|
||||||
parseInt(item.Size) !== 0 && result.fullList.push(this.formatFile(item, slicedPrefix, urlPrefix)))
|
nextMarker: '',
|
||||||
result.isTruncated = res.IsTruncated === 'true'
|
success: false
|
||||||
result.nextMarker = res.NextMarker || ''
|
}
|
||||||
result.success = true
|
}
|
||||||
|
const result = {
|
||||||
|
fullList: [
|
||||||
|
...res.CommonPrefixes.map(item => this.formatFolder(item, slicedPrefix)),
|
||||||
|
...res.Contents.filter(item => parseInt(item.Size) !== 0)
|
||||||
|
.map(item => this.formatFile(item, slicedPrefix, urlPrefix))
|
||||||
|
],
|
||||||
|
isTruncated: res.IsTruncated === 'true',
|
||||||
|
nextMarker: res.NextMarker || '',
|
||||||
|
success: true
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
@ -275,22 +275,22 @@ class TcyunApi {
|
|||||||
*/
|
*/
|
||||||
async renameBucketFile (configMap: IStringKeyMap): Promise<boolean> {
|
async renameBucketFile (configMap: IStringKeyMap): Promise<boolean> {
|
||||||
const { bucketName, region, oldKey, newKey } = configMap
|
const { bucketName, region, oldKey, newKey } = configMap
|
||||||
const res = await this.ctx.putObjectCopy({
|
const copyRes = await this.ctx.putObjectCopy({
|
||||||
Bucket: bucketName,
|
Bucket: bucketName,
|
||||||
Region: region,
|
Region: region,
|
||||||
Key: newKey,
|
Key: newKey,
|
||||||
CopySource: handleUrlEncode(`${bucketName}.cos.${region}.myqcloud.com/${oldKey}`)
|
CopySource: handleUrlEncode(`${bucketName}.cos.${region}.myqcloud.com/${oldKey}`)
|
||||||
})
|
})
|
||||||
if (res && res.statusCode === 200) {
|
|
||||||
const res2 = await this.ctx.deleteObject({
|
if (copyRes?.statusCode !== 200) return false
|
||||||
|
|
||||||
|
const deleteRes = await this.ctx.deleteObject({
|
||||||
Bucket: bucketName,
|
Bucket: bucketName,
|
||||||
Region: region,
|
Region: region,
|
||||||
Key: oldKey
|
Key: oldKey
|
||||||
})
|
})
|
||||||
return res2 && res2.statusCode === 204
|
|
||||||
} else {
|
return deleteRes?.statusCode === 204
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -309,7 +309,7 @@ class TcyunApi {
|
|||||||
Region: region,
|
Region: region,
|
||||||
Key: key
|
Key: key
|
||||||
})
|
})
|
||||||
return res && res.statusCode === 204
|
return res?.statusCode === 204
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -319,72 +319,38 @@ class TcyunApi {
|
|||||||
async deleteBucketFolder (configMap: IStringKeyMap): Promise<boolean> {
|
async deleteBucketFolder (configMap: IStringKeyMap): Promise<boolean> {
|
||||||
const { bucketName, region, key } = configMap
|
const { bucketName, region, key } = configMap
|
||||||
let marker
|
let marker
|
||||||
let isTruncated
|
let res: any
|
||||||
const allFileList = {
|
const allFileList = {
|
||||||
CommonPrefixes: [] as any[],
|
CommonPrefixes: [] as any[],
|
||||||
Contents: [] as any[]
|
Contents: [] as any[]
|
||||||
}
|
}
|
||||||
let res = await this.ctx.getBucket({
|
do {
|
||||||
Bucket: bucketName,
|
|
||||||
Region: region,
|
|
||||||
Prefix: key,
|
|
||||||
Delimiter: '/',
|
|
||||||
MaxKeys: 1000
|
|
||||||
})
|
|
||||||
if (res && res.statusCode === 200) {
|
|
||||||
res.CommonPrefixes.length > 0 && allFileList.CommonPrefixes.push(...res.CommonPrefixes)
|
|
||||||
res.Contents.length > 0 && allFileList.Contents.push(...res.Contents)
|
|
||||||
isTruncated = res.IsTruncated
|
|
||||||
marker = res.NextMarker
|
|
||||||
while (isTruncated === 'true') {
|
|
||||||
res = await this.ctx.getBucket({
|
res = await this.ctx.getBucket({
|
||||||
Bucket: bucketName,
|
Bucket: bucketName,
|
||||||
Region: region,
|
Region: region,
|
||||||
Prefix: key,
|
Prefix: key,
|
||||||
Delimiter: '/',
|
Delimiter: '/',
|
||||||
Marker: marker,
|
MaxKeys: 1000,
|
||||||
MaxKeys: 1000
|
Marker: marker
|
||||||
}) as any
|
})
|
||||||
if (res && res.statusCode === 200) {
|
|
||||||
res.CommonPrefixes.length > 0 && allFileList.CommonPrefixes.push(...res.CommonPrefixes)
|
if (res?.statusCode !== 200) return false
|
||||||
res.Contents.length > 0 && allFileList.Contents.push(...res.Contents)
|
|
||||||
isTruncated = res.IsTruncated
|
allFileList.CommonPrefixes.push(...res.CommonPrefixes)
|
||||||
|
allFileList.Contents.push(...res.Contents)
|
||||||
marker = res.NextMarker
|
marker = res.NextMarker
|
||||||
} else {
|
} while (res.IsTruncated === 'true')
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if (allFileList.CommonPrefixes.length > 0) {
|
|
||||||
for (const item of allFileList.CommonPrefixes) {
|
for (const item of allFileList.CommonPrefixes) {
|
||||||
res = await this.deleteBucketFolder({
|
if (!(await this.deleteBucketFolder({ bucketName, region, key: item.Prefix }))) return false
|
||||||
bucketName,
|
|
||||||
region,
|
|
||||||
key: item.Prefix
|
|
||||||
}) as any
|
|
||||||
if (!res) {
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
}
|
const cycles = Math.ceil(allFileList.Contents.length / 1000)
|
||||||
}
|
for (let i = 0; i < cycles; i++) {
|
||||||
if (allFileList.Contents.length > 0) {
|
const res = await this.ctx.deleteMultipleObject({
|
||||||
const cycle = Math.ceil(allFileList.Contents.length / 1000)
|
|
||||||
for (let i = 0; i < cycle; i++) {
|
|
||||||
res = await this.ctx.deleteMultipleObject({
|
|
||||||
Bucket: bucketName,
|
Bucket: bucketName,
|
||||||
Region: region,
|
Region: region,
|
||||||
Objects: allFileList.Contents.slice(i * 1000, (i + 1) * 1000).map((item: any) => {
|
Objects: allFileList.Contents.slice(i * 1000, (i + 1) * 1000).map((item: any) => ({ Key: item.Key }))
|
||||||
return {
|
|
||||||
Key: item.Key
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}) as any
|
if (res?.statusCode !== 200) return false
|
||||||
if (!(res && res.statusCode === 200)) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -500,7 +466,7 @@ class TcyunApi {
|
|||||||
Key: key,
|
Key: key,
|
||||||
Body: ''
|
Body: ''
|
||||||
})
|
})
|
||||||
return res && res.statusCode === 200
|
return res?.statusCode === 200
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,19 +1,41 @@
|
|||||||
|
// 忽略 TypeScript 错误
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import Upyun from 'upyun'
|
import Upyun from 'upyun'
|
||||||
|
|
||||||
|
// 加密函数、获取文件 MIME 类型、新的下载器、got 上传函数、并发异步任务池、错误格式化函数
|
||||||
import { md5, hmacSha1Base64, getFileMimeType, NewDownloader, gotUpload, ConcurrencyPromisePool, formatError } from '../utils/common'
|
import { md5, hmacSha1Base64, getFileMimeType, NewDownloader, gotUpload, ConcurrencyPromisePool, formatError } from '../utils/common'
|
||||||
|
|
||||||
|
// 是否为图片的判断函数
|
||||||
import { isImage } from '~/renderer/manage/utils/common'
|
import { isImage } from '~/renderer/manage/utils/common'
|
||||||
|
|
||||||
|
// 窗口管理器
|
||||||
import windowManager from 'apis/app/window/windowManager'
|
import windowManager from 'apis/app/window/windowManager'
|
||||||
|
|
||||||
|
// 枚举类型声明
|
||||||
import { IWindowList } from '#/types/enum'
|
import { IWindowList } from '#/types/enum'
|
||||||
|
|
||||||
|
// Electron 相关
|
||||||
import { ipcMain, IpcMainEvent } from 'electron'
|
import { ipcMain, IpcMainEvent } from 'electron'
|
||||||
|
|
||||||
|
// Axios
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
|
|
||||||
|
// 表单数据库
|
||||||
import FormData from 'form-data'
|
import FormData from 'form-data'
|
||||||
|
|
||||||
|
// 文件系统库
|
||||||
import fs from 'fs-extra'
|
import fs from 'fs-extra'
|
||||||
|
|
||||||
|
// 路径处理库
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
import UpDownTaskQueue,
|
|
||||||
{
|
// 上传下载任务队列
|
||||||
commonTaskStatus
|
import UpDownTaskQueue, { commonTaskStatus } from '../datastore/upDownTaskQueue'
|
||||||
} from '../datastore/upDownTaskQueue'
|
|
||||||
|
// 日志记录器
|
||||||
import { ManageLogger } from '../utils/logger'
|
import { ManageLogger } from '../utils/logger'
|
||||||
|
|
||||||
|
// 取消下载任务的加载文件列表、刷新下载文件传输列表
|
||||||
import { cancelDownloadLoadingFileList, refreshDownloadFileTransferList } from '@/manage/utils/static'
|
import { cancelDownloadLoadingFileList, refreshDownloadFileTransferList } from '@/manage/utils/static'
|
||||||
|
|
||||||
class UpyunApi {
|
class UpyunApi {
|
||||||
@ -35,9 +57,10 @@ class UpyunApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
formatFolder (item: any, slicedPrefix: string) {
|
formatFolder (item: any, slicedPrefix: string) {
|
||||||
|
const key = `${slicedPrefix}${item.name}/`
|
||||||
return {
|
return {
|
||||||
...item,
|
...item,
|
||||||
key: `${slicedPrefix}${item.name}/`,
|
key,
|
||||||
fileSize: 0,
|
fileSize: 0,
|
||||||
formatedTime: '',
|
formatedTime: '',
|
||||||
fileName: item.name,
|
fileName: item.name,
|
||||||
@ -45,11 +68,12 @@ class UpyunApi {
|
|||||||
checked: false,
|
checked: false,
|
||||||
isImage: false,
|
isImage: false,
|
||||||
match: false,
|
match: false,
|
||||||
Key: `${slicedPrefix}${item.name}/`
|
Key: key
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
formatFile (item: any, slicedPrefix: string, urlPrefix: string) {
|
formatFile (item: any, slicedPrefix: string, urlPrefix: string) {
|
||||||
|
const key = `${slicedPrefix}${item.name}`
|
||||||
return {
|
return {
|
||||||
...item,
|
...item,
|
||||||
fileName: item.name,
|
fileName: item.name,
|
||||||
@ -59,8 +83,8 @@ class UpyunApi {
|
|||||||
checked: false,
|
checked: false,
|
||||||
match: false,
|
match: false,
|
||||||
isImage: isImage(item.name),
|
isImage: isImage(item.name),
|
||||||
url: `${urlPrefix}/${slicedPrefix}${item.name}`,
|
url: `${urlPrefix}/${key}`,
|
||||||
key: `${slicedPrefix}${item.name}`
|
key
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,18 +95,10 @@ class UpyunApi {
|
|||||||
operator: string,
|
operator: string,
|
||||||
password: string
|
password: string
|
||||||
) {
|
) {
|
||||||
const passwordMd5 = md5(password, 'hex')
|
return `UPYUN ${operator}:${hmacSha1Base64(
|
||||||
const date = new Date().toUTCString()
|
md5(password, 'hex'),
|
||||||
const upperMethod = method.toUpperCase()
|
`${method.toUpperCase()}&${encodeURI(uri)}&${new Date().toUTCString()}${contentMd5 ? `&${contentMd5}` : ''}`
|
||||||
let stringToSign = ''
|
)}`
|
||||||
const codedUri = encodeURI(uri)
|
|
||||||
if (contentMd5 === '') {
|
|
||||||
stringToSign = `${upperMethod}&${codedUri}&${date}`
|
|
||||||
} else {
|
|
||||||
stringToSign = `${upperMethod}&${codedUri}&${date}&${contentMd5}`
|
|
||||||
}
|
|
||||||
const signature = hmacSha1Base64(passwordMd5, stringToSign)
|
|
||||||
return `UPYUN ${operator}:${signature}`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -120,7 +136,7 @@ class UpyunApi {
|
|||||||
iter: marker
|
iter: marker
|
||||||
})
|
})
|
||||||
if (res) {
|
if (res) {
|
||||||
res.files && res.files.forEach((item: any) => {
|
res.files?.forEach((item: any) => {
|
||||||
item.type === 'F' && folderQueue.push(`${slicedPrefix}${item.name}/`)
|
item.type === 'F' && folderQueue.push(`${slicedPrefix}${item.name}/`)
|
||||||
item.type === 'N' && result.fullList.push(this.formatFile(item, folder, urlPrefix))
|
item.type === 'N' && result.fullList.push(this.formatFile(item, folder, urlPrefix))
|
||||||
})
|
})
|
||||||
@ -169,7 +185,7 @@ class UpyunApi {
|
|||||||
iter: marker
|
iter: marker
|
||||||
})
|
})
|
||||||
if (res) {
|
if (res) {
|
||||||
res.files && res.files.forEach((item: any) => {
|
res.files?.forEach((item: any) => {
|
||||||
item.type === 'N' && result.fullList.push(this.formatFile(item, slicedPrefix, urlPrefix))
|
item.type === 'N' && result.fullList.push(this.formatFile(item, slicedPrefix, urlPrefix))
|
||||||
item.type === 'F' && result.fullList.push(this.formatFolder(item, slicedPrefix))
|
item.type === 'F' && result.fullList.push(this.formatFolder(item, slicedPrefix))
|
||||||
})
|
})
|
||||||
@ -219,7 +235,7 @@ class UpyunApi {
|
|||||||
iter: marker || ''
|
iter: marker || ''
|
||||||
})
|
})
|
||||||
if (res) {
|
if (res) {
|
||||||
res.files && res.files.forEach((item: any) => {
|
res.files?.forEach((item: any) => {
|
||||||
item.type === 'N' && result.fullList.push(this.formatFile(item, slicedPrefix, urlPrefix))
|
item.type === 'N' && result.fullList.push(this.formatFile(item, slicedPrefix, urlPrefix))
|
||||||
item.type === 'F' && result.fullList.push(this.formatFolder(item, slicedPrefix))
|
item.type === 'F' && result.fullList.push(this.formatFolder(item, slicedPrefix))
|
||||||
})
|
})
|
||||||
|
@ -1,19 +1,38 @@
|
|||||||
|
// 日志记录器
|
||||||
import ManageLogger from '../utils/logger'
|
import ManageLogger from '../utils/logger'
|
||||||
|
|
||||||
|
// WebDAV 客户端库
|
||||||
import { createClient, WebDAVClient, FileStat, ProgressEvent } from 'webdav'
|
import { createClient, WebDAVClient, FileStat, ProgressEvent } from 'webdav'
|
||||||
|
|
||||||
|
// 错误格式化函数、端点地址格式化函数、获取内部代理、新的下载器、并发异步任务池
|
||||||
import { formatError, formatEndpoint, getInnerAgent, NewDownloader, ConcurrencyPromisePool } from '../utils/common'
|
import { formatError, formatEndpoint, getInnerAgent, NewDownloader, ConcurrencyPromisePool } from '../utils/common'
|
||||||
|
|
||||||
|
// HTTP 代理格式化函数、是否为图片的判断函数
|
||||||
import { formatHttpProxy, isImage } from '@/manage/utils/common'
|
import { formatHttpProxy, isImage } from '@/manage/utils/common'
|
||||||
|
|
||||||
|
// HTTP 和 HTTPS 模块
|
||||||
import http from 'http'
|
import http from 'http'
|
||||||
import https from 'https'
|
import https from 'https'
|
||||||
|
|
||||||
|
// 窗口管理器
|
||||||
import windowManager from 'apis/app/window/windowManager'
|
import windowManager from 'apis/app/window/windowManager'
|
||||||
|
|
||||||
|
// 枚举类型声明
|
||||||
import { IWindowList } from '#/types/enum'
|
import { IWindowList } from '#/types/enum'
|
||||||
|
|
||||||
|
// Electron 相关
|
||||||
import { ipcMain, IpcMainEvent } from 'electron'
|
import { ipcMain, IpcMainEvent } from 'electron'
|
||||||
import UpDownTaskQueue,
|
|
||||||
{
|
// 上传下载任务队列
|
||||||
uploadTaskSpecialStatus,
|
import UpDownTaskQueue, { uploadTaskSpecialStatus, commonTaskStatus } from '../datastore/upDownTaskQueue'
|
||||||
commonTaskStatus
|
|
||||||
} from '../datastore/upDownTaskQueue'
|
// 文件系统库
|
||||||
import fs from 'fs-extra'
|
import fs from 'fs-extra'
|
||||||
|
|
||||||
|
// 路径处理库
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
|
|
||||||
|
// 取消下载任务的加载文件列表、刷新下载文件传输列表
|
||||||
import { cancelDownloadLoadingFileList, refreshDownloadFileTransferList } from '@/manage/utils/static'
|
import { cancelDownloadLoadingFileList, refreshDownloadFileTransferList } from '@/manage/utils/static'
|
||||||
|
|
||||||
class WebdavplistApi {
|
class WebdavplistApi {
|
||||||
@ -53,12 +72,13 @@ class WebdavplistApi {
|
|||||||
this.logger.error(formatError(error, { class: 'WebdavplistApi', method }))
|
this.logger.error(formatError(error, { class: 'WebdavplistApi', method }))
|
||||||
|
|
||||||
formatFolder (item: FileStat, urlPrefix: string, isWebPath = false) {
|
formatFolder (item: FileStat, urlPrefix: string, isWebPath = false) {
|
||||||
|
const key = item.filename.replace(/^\/+/, '')
|
||||||
return {
|
return {
|
||||||
...item,
|
...item,
|
||||||
key: item.filename.replace(/^\/+/, ''),
|
key,
|
||||||
fileName: item.basename,
|
fileName: item.basename,
|
||||||
fileSize: 0,
|
fileSize: 0,
|
||||||
Key: item.filename.replace(/^\/+/, ''),
|
Key: key,
|
||||||
formatedTime: '',
|
formatedTime: '',
|
||||||
isDir: true,
|
isDir: true,
|
||||||
checked: false,
|
checked: false,
|
||||||
@ -69,12 +89,13 @@ class WebdavplistApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
formatFile (item: FileStat, urlPrefix: string, isWebPath = false) {
|
formatFile (item: FileStat, urlPrefix: string, isWebPath = false) {
|
||||||
|
const key = item.filename.replace(/^\/+/, '')
|
||||||
return {
|
return {
|
||||||
...item,
|
...item,
|
||||||
key: item.filename.replace(/^\/+/, ''),
|
key,
|
||||||
fileName: item.basename,
|
fileName: item.basename,
|
||||||
fileSize: item.size,
|
fileSize: item.size,
|
||||||
Key: item.filename.replace(/^\/+/, ''),
|
Key: key,
|
||||||
formatedTime: new Date(item.lastmod).toLocaleString(),
|
formatedTime: new Date(item.lastmod).toLocaleString(),
|
||||||
isDir: false,
|
isDir: false,
|
||||||
checked: false,
|
checked: false,
|
||||||
@ -109,7 +130,7 @@ class WebdavplistApi {
|
|||||||
details: true
|
details: true
|
||||||
})
|
})
|
||||||
if (this.isRequestSuccess(res.status)) {
|
if (this.isRequestSuccess(res.status)) {
|
||||||
if (res.data && res.data.length) {
|
if (res.data?.length) {
|
||||||
res.data.forEach((item: FileStat) => {
|
res.data.forEach((item: FileStat) => {
|
||||||
if (item.type !== 'directory') {
|
if (item.type !== 'directory') {
|
||||||
result.fullList.push(this.formatFile(item, urlPrefix))
|
result.fullList.push(this.formatFile(item, urlPrefix))
|
||||||
@ -142,7 +163,7 @@ class WebdavplistApi {
|
|||||||
urlPrefix = urlPrefix.replace(/\/+$/, '')
|
urlPrefix = urlPrefix.replace(/\/+$/, '')
|
||||||
let webPath = configMap.webPath || ''
|
let webPath = configMap.webPath || ''
|
||||||
if (webPath && customUrl && webPath !== '/') {
|
if (webPath && customUrl && webPath !== '/') {
|
||||||
webPath = webPath.replace(/^\/+/, '').replace(/\/+$/, '')
|
webPath = webPath.replace(/^\/+|\/+$/, '')
|
||||||
}
|
}
|
||||||
const cancelTask = [false]
|
const cancelTask = [false]
|
||||||
ipcMain.on('cancelLoadingFileList', (_evt: IpcMainEvent, token: string) => {
|
ipcMain.on('cancelLoadingFileList', (_evt: IpcMainEvent, token: string) => {
|
||||||
@ -163,7 +184,7 @@ class WebdavplistApi {
|
|||||||
details: true
|
details: true
|
||||||
})
|
})
|
||||||
if (this.isRequestSuccess(res.status)) {
|
if (this.isRequestSuccess(res.status)) {
|
||||||
if (res.data && res.data.length) {
|
if (res.data?.length) {
|
||||||
res.data.forEach((item: FileStat) => {
|
res.data.forEach((item: FileStat) => {
|
||||||
const relativePath = path.relative(baseDir, item.filename)
|
const relativePath = path.relative(baseDir, item.filename)
|
||||||
const relative = webPath && urlPrefix + `/${path.join(webPath, relativePath)}`.replace(/\\/g, '/').replace(/\/+/g, '/')
|
const relative = webPath && urlPrefix + `/${path.join(webPath, relativePath)}`.replace(/\\/g, '/').replace(/\/+/g, '/')
|
||||||
|
Loading…
Reference in New Issue
Block a user