<commit_before>/**
 * Copyright(c) 2015 <NAME>, all rights reserved.
 */
package controllers

import dman.FileWrapper
import play.api.mvc.{Controller, ResponseHeader, Result}

import scala.concurrent.{ExecutionContext, Future}

trait FileStreaming {
  self: Controller =>

  val CT_DISP_ATTACHMENT = "attachment"
  val CT_DISP_INLINE = "inline"

  /**
   * Serves a file by streaming the contents back as chunks to the client.
   *
   * @param file FileWrapper
   * @param ec ExecutionContext required due to using Futures
   * @return Result (Ok)
   */
  def serve(file: FileWrapper, dispositionMode: String = CT_DISP_ATTACHMENT)(implicit ec: ExecutionContext): Result =
    file.enumerate.map { fenum =>
      Result(
        // prepare the header
        header = ResponseHeader(OK, Map(
          CONTENT_LENGTH -> ("" + file.size),
          CONTENT_DISPOSITION -> (s"""$dispositionMode; filename="${file.filename}"; filename*=UTF-8''""" + java.net.URLEncoder.encode(file.filename, "UTF-8").replace("+", "%20")),
          CONTENT_TYPE -> file.contentType.getOrElse("application/octet-stream"))),
        // give Play this file enumerator
        body = fenum)
    }.getOrElse(NotFound)

  /**
   * Serves a file by streaming the contents back as chunks to the client.
   *
   * @param maybeFile Option[FileWrapper]
   * @param ec ExecutionContext required due to using Futures
   * @return Result (Ok or NotFound)
   */
  def serve(maybeFile: Option[FileWrapper])(implicit ec: ExecutionContext): Result =
    maybeFile.map(fw => serve(fw)).getOrElse(NotFound)

  /**
   * Serves a Future file by streaming the content back as chunks to the client.
   *
   * @param futureFile Future[FileWrapper]
   * @param ec ExecutionContext required due to using Futures
   * @return Future[Result] (Ok or NotFound)
   */
  def serve(futureFile: Future[FileWrapper])(implicit ec: ExecutionContext): Future[Result] =
    futureFile.map(fw => serve(fw)).recover {
      case _ => NotFound
    }

}
<commit_msg>Fixing a bug in FileStreaming which caused download to not complete<commit_after>/**
 * Copyright(c) 2015 <NAME>, all rights reserved.
 */
package controllers

import dman.FileWrapper
import play.api.mvc.{Controller, ResponseHeader, Result}

import scala.concurrent.{ExecutionContext, Future}

trait FileStreaming {
  self: Controller =>

  val CT_DISP_ATTACHMENT = "attachment"
  val CT_DISP_INLINE = "inline"

  /**
   * Serves a file by streaming the contents back as chunks to the client.
   *
   * @param file FileWrapper
   * @param ec ExecutionContext required due to using Futures
   * @return Result (Ok)
   */
  def serve(file: FileWrapper, dispositionMode: String = CT_DISP_ATTACHMENT)(implicit ec: ExecutionContext): Result =
    file.enumerate.map { fenum =>
      Ok.chunked(fenum).withHeaders(
        CONTENT_DISPOSITION -> (s"""$dispositionMode; filename="${file.filename}"; filename*=UTF-8''""" + java.net.URLEncoder.encode(file.filename, "UTF-8").replace("+", "%20"))
      )
    }.getOrElse(NotFound)

  /**
   * Serves a file by streaming the contents back as chunks to the client.
   *
   * @param maybeFile Option[FileWrapper]
   * @param ec ExecutionContext required due to using Futures
   * @return Result (Ok or NotFound)
   */
  def serve(maybeFile: Option[FileWrapper])(implicit ec: ExecutionContext): Result =
    maybeFile.map(fw => serve(fw)).getOrElse(NotFound)

  /**
   * Serves a Future file by streaming the content back as chunks to the client.
   *
   * @param futureFile Future[FileWrapper]
   * @param ec ExecutionContext required due to using Futures
   * @return Future[Result] (Ok or NotFound)
   */
  def serve(futureFile: Future[FileWrapper])(implicit ec: ExecutionContext): Future[Result] =
    futureFile.map(fw => serve(fw)).recover {
      case _ => NotFound
    }

}

{'fraction_non_alphanumeric': 0.11678449855983242, 'fraction_numerical': 0.004189578423671118, 'mean_word_length': 3.7160493827160495, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3379835', 'n_tokens_mistral': 1173, 'n_tokens_neox': 1141, 'n_words': 331}