Skip to content

Commit

Permalink
chore: reconstructure
Browse files Browse the repository at this point in the history
  • Loading branch information
a1mersnow committed Jan 20, 2025
1 parent 04d2833 commit 2f4a79a
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 21 deletions.
18 changes: 11 additions & 7 deletions src/providers/123.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import type { Provider, Resource } from '~/types'
import ButtonComponent from '~/components/Button123.vue'

function getExt(fileName: string) {
const i = fileName.lastIndexOf('.')
return i > -1 ? fileName.slice(i + 1) : ''
}
import { getExtFromName } from '~/utils/tools'

function getParentId() {
const u = new URLSearchParams(location.search)
Expand All @@ -29,7 +25,7 @@ async function getFileListOfCurrentDir(parentId = getParentId()) {
file_id: x.FileId,
name: x.FileName,
parent_file_id: x.ParentFileId,
file_extension: getExt(x.FileName),
file_extension: getExtFromName(x.FileName),
mime_type: 'whocare',
type: x.type === 1 ? 'folder' : 'file',
})))
Expand All @@ -41,7 +37,7 @@ async function getFileListOfCurrentDir(parentId = getParentId()) {

const headers: Record<string, string> = {}

export function setRequestHeader(key: string, value: string) {
function setRequestHeader(key: string, value: string) {
if (key.toLowerCase() === 'authorization')
headers.authorization = value
}
Expand Down Expand Up @@ -105,6 +101,14 @@ function getContainer() {
const provider: Provider = {
DRIVE_NAME: '123云盘',
HOSTS: ['www.123pan.com'],
getApiDelay(size) {
if (size > 100) {
return 3000
}
else {
return 200
}
},
ButtonComponent,
shouldShowEntry,
getContainer,
Expand Down
10 changes: 3 additions & 7 deletions src/providers/quark.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import type { Provider, Resource } from '~/types'
import ButtonComponent from '~/components/ButtonQuark.vue'

function getExt(fileName: string) {
const i = fileName.lastIndexOf('.')
return i > -1 ? fileName.slice(i + 1) : ''
}
import { getExtFromName } from '~/utils/tools'

function getParentId() {
const hash = location.hash.replace('#/list/all', '').replace(/^\//, '').replace(/\/$/, '')
Expand All @@ -28,7 +24,7 @@ async function getFileListOfCurrentDir(parentId = getParentId()) {
file_id: x.fid,
name: x.file_name,
parent_file_id: x.pdir_fid,
file_extension: getExt(x.file_name),
file_extension: getExtFromName(x.file_name),
mime_type: x.format_type,
type: x.file ? 'file' : 'folder',
})))
Expand All @@ -47,7 +43,7 @@ async function rename(driveId: string, fileId: string, newName: string) {

const headers: Record<string, string> = {}
// eslint-disable-next-line unused-imports/no-unused-vars
export function setRequestHeader(key: string, value: string) {}
function setRequestHeader(key: string, value: string) {}

function post(api: URL | string, payload: object) {
return fetch(api, {
Expand Down
6 changes: 4 additions & 2 deletions src/stores/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export const useMainStore = defineStore('main', () => {
}
}

const MaxConcurrent = 3
const MaxConcurrent = provider.getApiMaxConcurrent()
async function run() {
if (disabled.value || running.value)
return
Expand All @@ -147,6 +147,8 @@ export const useMainStore = defineStore('main', () => {

const queue = selectedList.value.slice()

const totalTodoSize = queue.length

while (queue.length) {
const subQueue: Resource[] = []
for (let i = 0; i < MaxConcurrent; i++) {
Expand All @@ -167,7 +169,7 @@ export const useMainStore = defineStore('main', () => {
})
processData.value.done++
}))
await new Promise(r => setTimeout(r, provider.getApiDelay()))
await new Promise(r => setTimeout(r, provider.getApiDelay(totalTodoSize)))
}

running.value = false
Expand Down
6 changes: 4 additions & 2 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ export interface ContainerInfo {
}

export interface Provider {
/** intervals between file list page requests */
API_DELAY?: number
/** max concurrent size */
MAX_CONCURRENT?: number
/** fetch timing */
FETCH_MODE?: FetchMode
/** hosts at which the provider will be enabled */
HOSTS: string[]
DRIVE_NAME: string
/** intervals between file list page requests */
getApiDelay?: (size: number) => number
/** whether the rename button should be showed */
shouldShowEntry: (url: string) => boolean
/** API for file list */
Expand Down
11 changes: 8 additions & 3 deletions src/utils/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ const providers: Record<string, { default: Provider }> = import.meta.glob('/src/
function resolveProvider() {
for (const path in providers) {
const provider = providers[path].default
if (provider.HOSTS.includes(location.host))
if (provider.HOSTS.some(h => h.includes(location.host)))
return provider
}
throw new Error('unimplemented provider')
}

export function getApiDelay() {
return resolveProvider().API_DELAY || 200
export function getApiDelay(size: number) {
const p = resolveProvider()
return p.getApiDelay ? p.getApiDelay(size) : 200
}

export function getApiMaxConcurrent() {
return resolveProvider().MAX_CONCURRENT ?? 3
}

export function getFetchMode(): FetchMode {
Expand Down
5 changes: 5 additions & 0 deletions src/utils/tools.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export function random(n: number) {
return Math.floor(Math.random() * n)
}

export function getExtFromName(fileName: string) {
const i = fileName.lastIndexOf('.')
return i > -1 ? fileName.slice(i + 1) : ''
}
1 change: 1 addition & 0 deletions uno.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default defineConfig({
},
shortcuts: [
['btn', 'px-1 py-1 rounded inline-block text-primary-600 hover:text-white hover:bg-primary-600 transition border border-current disabled:opacity-50'],
['z-app', 'z-100000'],
],
presets: [
presetUno(),
Expand Down

0 comments on commit 2f4a79a

Please sign in to comment.