2022-02-02 09:03:29 +00:00
|
|
|
//
|
|
|
|
// Copyright © 2022 Hardcore Engineering Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License. You may
|
|
|
|
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
//
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
2023-02-02 08:01:01 +00:00
|
|
|
import { concatLink } from '@hcengineering/core'
|
2022-09-21 08:08:25 +00:00
|
|
|
import { getMetadata, PlatformError, unknownError } from '@hcengineering/platform'
|
2022-02-02 09:03:29 +00:00
|
|
|
import plugin from './plugin'
|
|
|
|
import { ReconiDocument } from './types'
|
|
|
|
|
|
|
|
export { default } from './plugin'
|
|
|
|
export * from './types'
|
|
|
|
|
2022-02-10 09:04:18 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2023-03-15 16:36:50 +00:00
|
|
|
export async function recognizeDocument (token: string, file: File): Promise<ReconiDocument> {
|
2022-02-02 09:03:29 +00:00
|
|
|
const rekoniUrl = getMetadata(plugin.metadata.RekoniUrl)
|
|
|
|
if (rekoniUrl === undefined) {
|
|
|
|
// We could try use recognition service to find some document properties.
|
|
|
|
throw new PlatformError(unknownError('recognition framework is not configured'))
|
|
|
|
}
|
2023-03-15 16:36:50 +00:00
|
|
|
const data = new FormData()
|
|
|
|
data.append('file', file)
|
|
|
|
data.append('type', file.type)
|
|
|
|
data.append('name', file.name)
|
|
|
|
|
2022-04-29 05:27:17 +00:00
|
|
|
return (await (
|
2023-02-02 08:01:01 +00:00
|
|
|
await fetch(concatLink(rekoniUrl, '/recognize'), {
|
2022-04-29 05:27:17 +00:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
2023-03-15 16:36:50 +00:00
|
|
|
Authorization: 'Bearer ' + token
|
2022-04-29 05:27:17 +00:00
|
|
|
},
|
2023-03-15 16:36:50 +00:00
|
|
|
body: data
|
2022-02-02 09:03:29 +00:00
|
|
|
})
|
2022-04-29 05:27:17 +00:00
|
|
|
).json()) as ReconiDocument
|
2022-02-02 09:03:29 +00:00
|
|
|
}
|