<commit_before><?php

namespace BNETDocs\Controllers\Attachment;

use \BNETDocs\Libraries\Attachment;
use \BNETDocs\Libraries\Cache;
use \BNETDocs\Libraries\Common;
use \BNETDocs\Libraries\Controller;
use \BNETDocs\Libraries\Database;
use \BNETDocs\Libraries\DatabaseDriver;
use \BNETDocs\Libraries\Exceptions\AttachmentNotFoundException;
use \BNETDocs\Libraries\Exceptions\UnspecifiedViewException;
use \BNETDocs\Libraries\Router;
use \BNETDocs\Models\Attachment\Download as DownloadModel;
use \BNETDocs\Views\Attachment\DownloadRaw as DownloadRawView;

class Download extends Controller {

  public function run(Router &$router) {
    switch ($router->getRequestPathExtension()) {
      case "":
        $view = new DownloadRawView();
      break;
      default:
        throw new UnspecifiedViewException();
    }

    $data = $router->getRequestQueryArray();
    $id   = (isset($data["id"]) ? (int) $data["id"] : null);

    $model                = new DownloadModel();
    $model->attachment_id = $id;

    try {
      $model->attachment = new Attachment($id);
    } catch (AttachmentNotFoundException $e) {
      $model->attachment = null;
    }

    ob_start();
    $view->render($model);
    $router->setResponseCode(($model->attachment ? 200 : 404));
    $router->setResponseTTL(31536000); // 1 year
    $router->setResponseHeader("Content-Type", $view->getMimeType());
    if ($model->extra_headers) {
      foreach ($model->extra_headers as $name => $value) {
        $router->setResponseHeader($name, $value);
      }
    }
    $router->setResponseContent(ob_get_contents());
    ob_end_clean();
  }

}<commit_msg>Move TTL into a const value<commit_after><?php

namespace BNETDocs\Controllers\Attachment;

use \BNETDocs\Libraries\Attachment;
use \BNETDocs\Libraries\Cache;
use \BNETDocs\Libraries\Common;
use \BNETDocs\Libraries\Controller;
use \BNETDocs\Libraries\Database;
use \BNETDocs\Libraries\DatabaseDriver;
use \BNETDocs\Libraries\Exceptions\AttachmentNotFoundException;
use \BNETDocs\Libraries\Exceptions\UnspecifiedViewException;
use \BNETDocs\Libraries\Router;
use \BNETDocs\Models\Attachment\Download as DownloadModel;
use \BNETDocs\Views\Attachment\DownloadRaw as DownloadRawView;

class Download extends Controller {

  const TTL_ONE_YEAR = 31536000;

  public function run(Router &$router) {
    switch ($router->getRequestPathExtension()) {
      case "":
        $view = new DownloadRawView();
      break;
      default:
        throw new UnspecifiedViewException();
    }

    $data = $router->getRequestQueryArray();
    $id   = (isset($data["id"]) ? (int) $data["id"] : null);

    $model                = new DownloadModel();
    $model->attachment_id = $id;

    try {
      $model->attachment = new Attachment($id);
    } catch (AttachmentNotFoundException $e) {
      $model->attachment = null;
    }

    ob_start();
    $view->render($model);
    $router->setResponseCode(($model->attachment ? 200 : 404));
    $router->setResponseTTL(self::TTL_ONE_YEAR);
    $router->setResponseHeader("Content-Type", $view->getMimeType());
    if ($model->extra_headers) {
      foreach ($model->extra_headers as $name => $value) {
        $router->setResponseHeader($name, $value);
      }
    }
    $router->setResponseContent(ob_get_contents());
    ob_end_clean();
  }

}
{'fraction_non_alphanumeric': 0.14, 'fraction_numerical': 0.008787878787878787, 'mean_word_length': 3.770231213872832, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4809920', 'n_tokens_mistral': 1072, 'n_tokens_neox': 1045, 'n_words': 186}