Feature: add video,text file and markdown file preview

This commit is contained in:
萌萌哒赫萝
2023-02-21 18:42:50 +08:00
parent 893e33d591
commit b3ce9b9543
21 changed files with 967 additions and 23 deletions
+3 -1
View File
@@ -6,6 +6,7 @@ import SmmsApi from './smms'
import GithubApi from './github'
import ImgurApi from './imgur'
import S3plistApi from './s3plist'
import WebdavplistApi from './webdavplist'
export default {
TcyunApi,
@@ -15,5 +16,6 @@ export default {
SmmsApi,
GithubApi,
ImgurApi,
S3plistApi
S3plistApi,
WebdavplistApi
}
+2 -5
View File
@@ -18,7 +18,7 @@ import { getSignedUrl } from '@aws-sdk/s3-request-presigner'
import https from 'https'
import http from 'http'
import { ManageLogger } from '../utils/logger'
import { formatError, getAgent, getFileMimeType, gotDownload } from '../utils/common'
import { formatEndpoint, formatError, getAgent, getFileMimeType, gotDownload } from '../utils/common'
import { isImage } from '@/manage/utils/common'
import { HttpsProxyAgent, HttpProxyAgent } from 'hpagent'
import windowManager from 'apis/app/window/windowManager'
@@ -64,7 +64,7 @@ class S3plistApi {
accessKeyId,
secretAccessKey
},
endpoint: endpoint ? this.formatEndpoint(endpoint, sslEnabled) : undefined,
endpoint: endpoint ? formatEndpoint(endpoint, sslEnabled) : undefined,
sslEnabled,
s3ForcePathStyle,
httpOptions: {
@@ -75,9 +75,6 @@ class S3plistApi {
this.agent = this.setAgent(proxy, sslEnabled)
}
formatEndpoint = (endpoint: string, sslEnabled: boolean): string =>
!/^https?:\/\//.test(endpoint) ? `${sslEnabled ? 'https' : 'http'}://${endpoint}` : endpoint
setAgent (proxy: string | undefined, sslEnabled: boolean) : HttpProxyAgent | HttpsProxyAgent | undefined {
if (sslEnabled) {
const agent = getAgent(proxy, true).https
+130
View File
@@ -0,0 +1,130 @@
import ManageLogger from '../utils/logger'
import { createClient, WebDAVClient, FileStat } from 'webdav'
import { formatError, formatEndpoint, getInnerAgent } from '../utils/common'
import { isImage } from '@/manage/utils/common'
import http from 'http'
import https from 'https'
import windowManager from 'apis/app/window/windowManager'
import { IWindowList } from '#/types/enum'
import { ipcMain, IpcMainEvent } from 'electron'
class WebdavplistApi {
endpoint: string
username: string
password: string
sslEnabled: boolean
proxy: string | undefined
logger: ManageLogger
agent: https.Agent | http.Agent
ctx: WebDAVClient
constructor (endpoint: string, username: string, password: string, sslEnabled: boolean, proxy: string | undefined, logger: ManageLogger) {
this.endpoint = formatEndpoint(endpoint, sslEnabled)
this.username = username
this.password = password
this.sslEnabled = sslEnabled
this.proxy = proxy
this.logger = logger
this.agent = getInnerAgent(proxy, sslEnabled).agent
this.ctx = createClient(
this.endpoint,
{
username: this.username,
password: this.password,
maxBodyLength: 4 * 1024 * 1024 * 1024,
maxContentLength: 4 * 1024 * 1024 * 1024,
httpsAgent: sslEnabled ? this.agent : undefined,
httpAgent: !sslEnabled ? this.agent : undefined
}
)
}
logParam = (error:any, method: string) =>
this.logger.error(formatError(error, { class: 'WebdavplistApi', method }))
formatFolder (item: FileStat, urlPrefix: string) {
return {
...item,
key: item.filename.replace(/^\/+/, ''),
fileName: item.basename,
fileSize: 0,
Key: item.filename.replace(/^\/+/, ''),
formatedTime: '',
isDir: true,
checked: false,
isImage: false,
match: false,
url: `${urlPrefix}${item.filename}`
}
}
formatFile (item: FileStat, urlPrefix: string) {
return {
...item,
key: item.filename.replace(/^\/+/, ''),
fileName: item.basename,
fileSize: item.size,
Key: item.filename.replace(/^\/+/, ''),
formatedTime: new Date(item.lastmod).toLocaleString(),
isDir: false,
checked: false,
match: false,
isImage: isImage(item.basename),
url: `${urlPrefix}${item.filename}`
}
}
isRequestSuccess = (code: number) => code >= 200 && code < 300
async getBucketListBackstage (configMap: IStringKeyMap): Promise<any> {
const window = windowManager.get(IWindowList.SETTING_WINDOW)!
const { prefix, customUrl, cancelToken } = configMap
const urlPrefix = customUrl || this.endpoint
const cancelTask = [false]
ipcMain.on('cancelLoadingFileList', (_evt: IpcMainEvent, token: string) => {
if (token === cancelToken) {
cancelTask[0] = true
ipcMain.removeAllListeners('cancelLoadingFileList')
}
})
let res = {} as any
const result = {
fullList: <any>[],
success: false,
finished: false
}
try {
res = await this.ctx.getDirectoryContents(prefix, {
deep: false,
details: true
})
if (this.isRequestSuccess(res.status)) {
if (res.data && res.data.length) {
res.data.forEach((item: FileStat) => {
if (item.type === 'directory') {
result.fullList.push(this.formatFolder(item, urlPrefix))
} else {
result.fullList.push(this.formatFile(item, urlPrefix))
}
})
}
} else {
result.finished = true
window.webContents.send('refreshFileTransferList', result)
ipcMain.removeAllListeners('cancelLoadingFileList')
return
}
} catch (error) {
this.logParam(error, 'getBucketListBackstage')
result.finished = true
window.webContents.send('refreshFileTransferList', result)
ipcMain.removeAllListeners('cancelLoadingFileList')
}
result.success = true
result.finished = true
window.webContents.send('refreshFileTransferList', result)
ipcMain.removeAllListeners('cancelLoadingFileList')
}
}
export default WebdavplistApi
+9
View File
@@ -69,6 +69,8 @@ export class ManageApi extends EventEmitter implements ManageApiType {
return new API.ImgurApi(this.currentPicBedConfig.imgurUserName, this.currentPicBedConfig.accessToken, this.currentPicBedConfig.proxy, this.logger)
case 's3plist':
return new API.S3plistApi(this.currentPicBedConfig.accessKeyId, this.currentPicBedConfig.secretAccessKey, this.currentPicBedConfig.endpoint, this.currentPicBedConfig.sslEnabled, this.currentPicBedConfig.s3ForcePathStyle, this.currentPicBedConfig.proxy, this.logger)
case 'webdavplist':
return new API.WebdavplistApi(this.currentPicBedConfig.endpoint, this.currentPicBedConfig.username, this.currentPicBedConfig.password, this.currentPicBedConfig.sslEnabled, this.currentPicBedConfig.proxy, this.logger)
default:
return {} as any
}
@@ -172,6 +174,12 @@ export class ManageApi extends EventEmitter implements ManageApiType {
Location: 'smms',
CreationDate: new Date().toISOString()
}]
case 'webdavplist':
return [{
Name: 'webdav',
Location: 'webdav',
CreationDate: new Date().toISOString()
}]
default:
console.log(param)
return []
@@ -309,6 +317,7 @@ export class ManageApi extends EventEmitter implements ManageApiType {
case 'github':
case 'imgur':
case 's3plist':
case 'webdavplist':
try {
client = this.createClient() as any
return await client.getBucketListBackstage(param!)
+51 -1
View File
@@ -14,8 +14,10 @@ import UpDownTaskQueue,
downloadTaskSpecialStatus
} from '../datastore/upDownTaskQueue'
import { ManageLogger } from '../utils/logger'
import { formatHttpProxy } from '@/manage/utils/common'
import { formatHttpProxy, IHTTPProxy } from '@/manage/utils/common'
import { HttpsProxyAgent, HttpProxyAgent } from 'hpagent'
import http from 'http'
import https from 'https'
export const getFSFile = async (
filePath: string,
@@ -244,6 +246,47 @@ export const getAgent = (proxy:any, https: boolean = true) => {
}
}
export const getInnerAgent = (proxy: any, sslEnabled: boolean = true) => {
const formatProxy = formatHttpProxy(proxy, 'object') as IHTTPProxy
if (sslEnabled) {
return formatProxy
? {
agent: new https.Agent({
keepAlive: true,
keepAliveMsecs: 1000,
rejectUnauthorized: false,
scheduling: 'lifo' as 'lifo' | 'fifo' | undefined,
host: formatProxy.host,
port: formatProxy.port
})
}
: {
agent: new https.Agent({
rejectUnauthorized: false,
keepAlive: true
})
}
} else {
return formatProxy
? {
agent: new http.Agent({
keepAlive: true,
keepAliveMsecs: 1000,
scheduling: 'lifo' as 'lifo' | 'fifo' | undefined,
host: formatProxy.host,
port: formatProxy.port
})
}
: {
agent: new http.Agent({
keepAlive: true,
keepAliveMsecs: 1000,
scheduling: 'lifo' as 'lifo' | 'fifo' | undefined
})
}
}
}
export function getOptions (
method?: string,
headers?: IStringKeyMap,
@@ -270,3 +313,10 @@ export function getOptions (
})
return options
}
export const formatEndpoint = (endpoint: string, sslEnabled: boolean): string =>
!/^https?:\/\//.test(endpoint)
? `${sslEnabled ? 'https' : 'http'}://${endpoint}`
: sslEnabled
? endpoint.replace('http://', 'https://')
: endpoint.replace('https://', 'http://')