<commit_before>import { ImageAdapter, Result, format } from './interfaces'
import ResizeDto from './resize.dto'
import sharp from 'sharp'
import { getLogger } from './logger'
import Keyv from 'keyv'
import crypto from 'crypto'

const DEFAULT_CROP_MAX_SIZE = 2000

export class Transformer {
  log = getLogger('transformer')
  cropMaxSize = DEFAULT_CROP_MAX_SIZE

  constructor(private imageAdapter: ImageAdapter, private cache = new Keyv()) {}

  getCropDimensions(maxSize: number, width: number, height?: number) {
    height = height || width
    if (width <= maxSize && height <= maxSize) return [width, height]
    const aspectRatio = width / height
    if (width > height) return [maxSize, Math.round(maxSize / aspectRatio)]
    return [maxSize * aspectRatio, maxSize]
  }

  buildCacheKey(id: string, options: ResizeDto): string {
    const hash = crypto
      .createHash('sha256')
      .update(JSON.stringify(options, Object.keys(options).sort()))
      .digest('hex')
      .slice(0, 10)
    return `transform:${id}:${hash}`<commit_msg>fix magic number<commit_after>import { ImageAdapter, Result, format } from './interfaces'
import ResizeDto from './resize.dto'
import sharp from 'sharp'
import { getLogger } from './logger'
import Keyv from 'keyv'
import crypto from 'crypto'

const DEFAULT_CROP_MAX_SIZE = 2000
const CACHE_KEY_HASH_LENGTH = 10

export class Transformer {
  log = getLogger('transformer')
  cropMaxSize = DEFAULT_CROP_MAX_SIZE

  constructor(private imageAdapter: ImageAdapter, private cache = new Keyv()) {}

  getCropDimensions(maxSize: number, width: number, height?: number) {
    height = height || width
    if (width <= maxSize && height <= maxSize) return [width, height]
    const aspectRatio = width / height
    if (width > height) return [maxSize, Math.round(maxSize / aspectRatio)]
    return [maxSize * aspectRatio, maxSize]
  }

  buildCacheKey(id: string, options: ResizeDto): string {
    const hash = crypto
      .createHash('sha256')
      .update(JSON.stringify(options, Object.keys(options).sort()))
      .digest('hex')
      .slice(0, CACHE_KEY_HASH_LENGTH)
    return `transform:${id}:${hash}`
{'fraction_non_alphanumeric': 0.1215649743828598, 'fraction_numerical': 0.009315323707498836, 'mean_word_length': 4.277641277641278, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4289445', 'n_tokens_mistral': 664, 'n_tokens_neox': 645, 'n_words': 186}