<commit_before>package com.github.kei10in

import scala.util._

import java.io.{ByteArrayOutputStream, File}
import javax.imageio.ImageIO
import java.awt.image.BufferedImage

import org.scalatra._
import org.scalatra.servlet.FileItem

import org.apache.commons.validator.routines.EmailValidator

import model._


class ScalavatarServlet extends ScalavatarStack with UrlGeneratorSupport {

  lazy val app = new Application(new File(servletContext.getRealPath("/")))

  get("/") {
    contentType = "text/html"
    jade("/index")
  }

  val avatarUrl = get("/avatar/:avatarRequest") {
    val avatarKey = parseAvatarRequest(params("avatarRequest"))
    val s = params.get("s") map { v =>
      Try(Integer.parseInt(v)).getOrElse(1)  // 1 for gravatar compatible
    }

    app.findImageByHash(avatarKey) match {
      case Some(avatar) =>
        val bytes = app.avatarImageBytesWithSize(avatar, s)
        contentType = "image/png"
        response.setHeader("Content-Disposition", "inline; filename=\"" + avatarKey + ".png\"")
        Ok (bytes)
      case None =>
        NotFound ("file not found")
    }
  }

  def parseAvatarRequest(avatarRequest: String) = {
    if (avatarRequest.contains('.'))
      avatarRequest.take(avatarRequest.indexOf('.'))
    else
      avatarRequest
  }
<commit_msg>refactoring. extract method<commit_after>package com.github.kei10in

import scala.util._

import java.io.{ByteArrayOutputStream, File}
import javax.imageio.ImageIO
import java.awt.image.BufferedImage

import org.scalatra._
import org.scalatra.servlet.FileItem

import org.apache.commons.validator.routines.EmailValidator

import model._


class ScalavatarServlet extends ScalavatarStack with UrlGeneratorSupport {

  lazy val app = new Application(new File(servletContext.getRealPath("/")))

  get("/") {
    contentType = "text/html"
    jade("/index")
  }

  val avatarUrl = get("/avatar/:avatarRequest") {
    val avatarKey = parseAvatarRequest(params("avatarRequest"))
    val s = parseAvatarSize(params.get("s"))

    app.findImageByHash(avatarKey) match {
      case Some(avatar) =>
        val bytes = app.avatarImageBytesWithSize(avatar, s)
        contentType = "image/png"
        response.setHeader("Content-Disposition", "inline; filename=\"" + avatarKey + ".png\"")
        Ok (bytes)
      case None =>
        NotFound ("file not found")
    }
  }

  def parseAvatarRequest(avatarRequest: String) = {
    if (avatarRequest.contains('.'))
      avatarRequest.take(avatarRequest.indexOf('.'))
    else
      avatarRequest
  }

  def parseAvatarSize(s: Option[String]) = s map { v =>
      Try(Integer.parseInt(v)).getOrElse(1)  // 1 for gravatar compatible
    }

{'fraction_non_alphanumeric': 0.12448598130841121, 'fraction_numerical': 0.0029906542056074765, 'mean_word_length': 3.5665529010238908, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '3514872', 'n_tokens_mistral': 852, 'n_tokens_neox': 830, 'n_words': 170}