<commit_before>package service

import util.Directory._
import util.ControlUtil._
import SystemSettingsService._
import javax.servlet.http.HttpServletRequest

trait SystemSettingsService {

  def baseUrl(implicit request: HttpServletRequest): String = loadSystemSettings().baseUrl.getOrElse {
    defining(request.getRequestURL.toString){ url =>
      url.substring(0, url.length - (request.getRequestURI.length - request.getContextPath.length))
    }
  }.replaceFirst("/$", "")

  def saveSystemSettings(settings: SystemSettings): Unit = {
    defining(new java.util.Properties()){ props =>
      settings.baseUrl.foreach(props.setProperty(BaseURL, _))
      props.setProperty(AllowAccountRegistration, settings.allowAccountRegistration.toString)
      props.setProperty(Gravatar, settings.gravatar.toString)
      props.setProperty(Notification, settings.notification.toString)
      props.setProperty(Ssh, settings.ssh.toString)
      settings.sshPort.foreach(x => props.setProperty(SshPort, x.toString))
      if(settings.notification) {
        settings.smtp.foreach { smtp =>
          props.setProperty(SmtpHost, smtp.host)
          smtp.port.foreach(x => props.setProperty(SmtpPort, x.toString))
          smtp.user.foreach(props.setProperty(SmtpUser, _))
          smtp.password.foreach(props.setProperty(SmtpPassword, _))
          smtp.ssl.foreach(x => props.setProperty(SmtpSsl, x.toString))
          smtp.fromAddress.foreach(props.setProperty(SmtpFromAddress, _))
          smtp.fromName.foreach(props.setProperty(SmtpFromName, _))
        }
      }
      props.setProperty(LdapAuthentication, settings.ldapAuthentication.toString)
      if(settings.ldapAuthentication){
        settings.ldap.map { ldap =>
          props.setProperty(LdapHost, ldap.host)
          ldap.port.foreach(x => props.setProperty(LdapPort, x.toString))
          ldap.bindDN.foreach(x => props.setProperty(LdapBindDN, x))
          ldap.bindPassword.foreach(x => props.setProperty(LdapBindPassword, x))
          props.setProperty(LdapBaseDN, ldap.baseDN)
          props.setProperty(LdapUserNameAttribute, ldap.userNameAttribute)
          ldap.fullNameAttribute.foreach(x => props.setProperty(LdapFullNameAttribute, x))
          props.setProperty(LdapMailAddressAttribute, ldap.mailAttribute)
          ldap.tls.foreach(x => props.setProperty(LdapTls, x.toString))
          ldap.keystore.foreach(x => props.setProperty(LdapKeystore, x))
        }
      }
      props.store(new java.io.FileOutputStream(GitBucketConf), null)
    }
  }


  def loadSystemSettings(): SystemSettings = {
    defining(new java.util.Properties()){ props =>
      if(GitBucketConf.exists){
        props.load(new java.io.FileInputStream(GitBucketConf))
      }
      SystemSettings(
        getOptionValue(props, BaseURL, None),
        getValue(props, AllowAccountRegistration, false),
        getValue(props, Gravatar, true),
        getValue(props, Notification, false),
        getValue(props, Ssh, false),
        getOptionValue(props, SshPort, Some(DefaultSshPort)),
        if(getValue(props, Notification, false)){
          Some(Smtp(
            getValue(props, SmtpHost, ""),
            getOptionValue(props, SmtpPort, Some(DefaultSmtpPort)),
            getOptionValue(props, SmtpUser, None),
            getOptionValue(props, SmtpPassword, None),
            getOptionValue[Boolean](props, SmtpSsl, None),
            getOptionValue(props, SmtpFromAddress, None),
            getOptionValue(props, SmtpFromName, None)))
        } else {
          None
        },<commit_msg>Remove last '/' in base url<commit_after>package service

import util.Directory._
import util.ControlUtil._
import SystemSettingsService._
import javax.servlet.http.HttpServletRequest

trait SystemSettingsService {

  def baseUrl(implicit request: HttpServletRequest): String = loadSystemSettings().baseUrl.getOrElse {
    defining(request.getRequestURL.toString){ url =>
      url.substring(0, url.length - (request.getRequestURI.length - request.getContextPath.length))
    }
  }.replaceFirst("/$", "")

  def saveSystemSettings(settings: SystemSettings): Unit = {
    defining(new java.util.Properties()){ props =>
      settings.baseUrl.foreach(x => props.setProperty(BaseURL, x.replaceFirst("/\\Z", "")))
      props.setProperty(AllowAccountRegistration, settings.allowAccountRegistration.toString)
      props.setProperty(Gravatar, settings.gravatar.toString)
      props.setProperty(Notification, settings.notification.toString)
      props.setProperty(Ssh, settings.ssh.toString)
      settings.sshPort.foreach(x => props.setProperty(SshPort, x.toString))
      if(settings.notification) {
        settings.smtp.foreach { smtp =>
          props.setProperty(SmtpHost, smtp.host)
          smtp.port.foreach(x => props.setProperty(SmtpPort, x.toString))
          smtp.user.foreach(props.setProperty(SmtpUser, _))
          smtp.password.foreach(props.setProperty(SmtpPassword, _))
          smtp.ssl.foreach(x => props.setProperty(SmtpSsl, x.toString))
          smtp.fromAddress.foreach(props.setProperty(SmtpFromAddress, _))
          smtp.fromName.foreach(props.setProperty(SmtpFromName, _))
        }
      }
      props.setProperty(LdapAuthentication, settings.ldapAuthentication.toString)
      if(settings.ldapAuthentication){
        settings.ldap.map { ldap =>
          props.setProperty(LdapHost, ldap.host)
          ldap.port.foreach(x => props.setProperty(LdapPort, x.toString))
          ldap.bindDN.foreach(x => props.setProperty(LdapBindDN, x))
          ldap.bindPassword.foreach(x => props.setProperty(LdapBindPassword, x))
          props.setProperty(LdapBaseDN, ldap.baseDN)
          props.setProperty(LdapUserNameAttribute, ldap.userNameAttribute)
          ldap.fullNameAttribute.foreach(x => props.setProperty(LdapFullNameAttribute, x))
          props.setProperty(LdapMailAddressAttribute, ldap.mailAttribute)
          ldap.tls.foreach(x => props.setProperty(LdapTls, x.toString))
          ldap.keystore.foreach(x => props.setProperty(LdapKeystore, x))
        }
      }
      props.store(new java.io.FileOutputStream(GitBucketConf), null)
    }
  }


  def loadSystemSettings(): SystemSettings = {
    defining(new java.util.Properties()){ props =>
      if(GitBucketConf.exists){
        props.load(new java.io.FileInputStream(GitBucketConf))
      }
      SystemSettings(
        getOptionValue[String](props, BaseURL, None).map(x => x.replaceFirst("/\\Z", "")),
        getValue(props, AllowAccountRegistration, false),
        getValue(props, Gravatar, true),
        getValue(props, Notification, false),
        getValue(props, Ssh, false),
        getOptionValue(props, SshPort, Some(DefaultSshPort)),
        if(getValue(props, Notification, false)){
          Some(Smtp(
            getValue(props, SmtpHost, ""),
            getOptionValue(props, SmtpPort, Some(DefaultSmtpPort)),
            getOptionValue(props, SmtpUser, None),
            getOptionValue(props, SmtpPassword, None),
            getOptionValue[Boolean](props, SmtpSsl, None),
            getOptionValue(props, SmtpFromAddress, None),
            getOptionValue(props, SmtpFromName, None)))
        } else {
          None
        },
{'fraction_non_alphanumeric': 0.11707587737550285, 'fraction_numerical': 0.00027743098904147595, 'mean_word_length': 3.3433734939759034, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, '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': '2553687', 'n_tokens_mistral': 2130, 'n_tokens_neox': 2102, 'n_words': 301}