mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-14 06:07:23 +08:00
feat(ocr): add filtering by registration status to provider list
Add optional query parameter to filter OCR providers by registration status Prevent modification and deletion of built-in OCR providers
This commit is contained in:
parent
7ea7e7134d
commit
389dfc08f6
@ -360,6 +360,7 @@ export interface ApiSchemas {
|
||||
|
||||
'/ocr/providers': {
|
||||
GET: {
|
||||
query: { registered?: boolean }
|
||||
response: ListOcrProvidersResponse
|
||||
}
|
||||
POST: {
|
||||
|
||||
@ -212,8 +212,8 @@ export const apiHandlers: ApiImplementation = {
|
||||
},
|
||||
|
||||
'/ocr/providers': {
|
||||
GET: async () => {
|
||||
return ocrService.listProviders()
|
||||
GET: async ({ query }) => {
|
||||
return ocrService.listProviders(query.registered)
|
||||
},
|
||||
POST: async ({ body }) => {
|
||||
return ocrService.createProvider(body)
|
||||
|
||||
@ -13,7 +13,7 @@ import type {
|
||||
PutOcrProviderResponse,
|
||||
SupportedOcrFile
|
||||
} from '@types'
|
||||
import { BuiltinOcrProviderIdMap } from '@types'
|
||||
import { BuiltinOcrProviderIdMap, BuiltinOcrProviderIds } from '@types'
|
||||
import { eq } from 'drizzle-orm'
|
||||
import { merge } from 'lodash'
|
||||
|
||||
@ -56,17 +56,17 @@ export class OcrService {
|
||||
this.registry.delete(providerId)
|
||||
}
|
||||
|
||||
public async listProviders(): Promise<ListOcrProvidersResponse> {
|
||||
const registeredKeys = Array.from(this.registry.keys())
|
||||
public async listProviders(registered?: boolean): Promise<ListOcrProvidersResponse> {
|
||||
const providers = await dbService.getDb().select().from(ocrProviderTable)
|
||||
|
||||
return { data: providers.filter((p) => registeredKeys.includes(p.id)) }
|
||||
if (registered) {
|
||||
const registeredKeys = Array.from(this.registry.keys())
|
||||
return { data: providers.filter((p) => registeredKeys.includes(p.id)) }
|
||||
} else {
|
||||
return { data: providers }
|
||||
}
|
||||
}
|
||||
|
||||
public async getProvider(providerId: string) {
|
||||
if (!this.registry.has(providerId)) {
|
||||
throw new Error(`OCR provider ${providerId} is not registered`)
|
||||
}
|
||||
const providers = await dbService
|
||||
.getDb()
|
||||
.select()
|
||||
@ -121,6 +121,9 @@ export class OcrService {
|
||||
}
|
||||
|
||||
public async putProvider(update: PutOcrProviderRequest): Promise<PutOcrProviderResponse> {
|
||||
if (BuiltinOcrProviderIds.some((pid) => pid === update.id)) {
|
||||
throw new Error('Builtin OCR providers cannot be modified with PUT method.')
|
||||
}
|
||||
const providers = await dbService
|
||||
.getDb()
|
||||
.select()
|
||||
@ -144,8 +147,8 @@ export class OcrService {
|
||||
}
|
||||
|
||||
public async deleteProvider(providerId: string): Promise<void> {
|
||||
if (!this.registry.has(providerId)) {
|
||||
throw new Error(`OCR provider ${providerId} is not registered`)
|
||||
if (BuiltinOcrProviderIds.some((pid) => pid === providerId)) {
|
||||
throw new Error('Builtin OCR providers cannot be deleted.')
|
||||
}
|
||||
const providers = await dbService
|
||||
.getDb()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user