<commit_before>package controllers

import play.api.mvc.{Action, Controller}
import fly.play.s3._
import models.{Destination, FlowData}
import play.api.mvc.BodyParsers.parse.Multipart.PartHandler
import play.api.mvc.BodyParsers.parse.Multipart.handleFilePart
import play.api.mvc.BodyParsers.parse.Multipart.FileInfo
import play.api.mvc.BodyParsers.parse.multipartFormData
import play.api.mvc.MultipartFormData.FilePart
import play.api.libs.iteratee.Iteratee
import java.io.ByteArrayOutputStream
import play.api.mvc.MultipartFormData
import play.api.libs.MimeTypes
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext
import ExecutionContext.Implicits.global
import scala.concurrent.Await
import scala.concurrent.Awaitable
import scala.concurrent.Future
import scala.concurrent.duration.Duration
import play.api.mvc.BodyParsers.parse.Multipart.FileInfo
import fly.play.s3.BucketFilePartUploadTicket
import fly.play.s3.BucketFilePart
import play.api.mvc.MultipartFormData.FilePart
import fly.play.s3.BucketFile
import java.net.URLEncoder

object Files extends Controller {

  var partUploadTickets: Seq[BucketFilePartUploadTicket] = Seq()

  def upload = Action(multipartFormDataAsBytes) { request =>
    System.out.println("here")
    // Turns Map(String, Seq[String]) into Map(String, String)
    val body = request.body.dataParts.map { case (k,Seq(v)) => (k,v) }

    // Map the incoming request to a FlowData object
    val flowData = FlowData(body.get("flowChunkNumber").get.toLong,
      body.get("flowChunkSize").get.toLong,
      body.get("flowCurrentChunkSize").get.toLong,
      body.get("flowTotalSize").get.toLong,
      body.get("flowTotalChunks").get.toLong,
      body.get("flowIdentifier").get,
      body.get("flowFilename").get)

    // Get the s3 bucket
    val bucket = S3("pigion")

    // Now we need to figure out if this is the beginning of an upload, an in progress upload, or the end of an upload
    val fileName = flowData.flowFileName
    val fileContentType = MimeTypes.forFileName(flowData.flowFileName).getOrElse(".txt")
    val bucketFile = BucketFile(URLEncoder.encode(fileName, "UTF-8"), fileContentType)

    val uploadTicket = await(bucket.initiateMultipartUpload(bucketFile))

    // For each of the file parts, send them to the S3
    request.body.files foreach {
      case FilePart(key, filename, contentType, bytes) => {
        // Upload the data
        val filePart = BucketFilePart(flowData.flowChunkNumber.toInt, bytes)
        val partUploadTicket = await(bucket.uploadPart(uploadTicket, filePart))
        partUploadTickets = Seq(partUploadTicket)
      }
    }

    val uploadResult = await(bucket completeMultipartUpload (uploadTicket, partUploadTickets))

    // We only want to create a URL from the last one
    if(flowData.flowChunkNumber == flowData.flowTotalChunks) {
      val url = bucket.url(URLEncoder.encode(flowData.flowFileName, "UTF-8"))
      Destination.create(url)
      Ok(Destination.create(url))
    }
    Ok("")<commit_msg>uploading response while uploading<commit_after>package controllers

import play.api.mvc.{Action, Controller}
import fly.play.s3._
import models.{Destination, FlowData}
import play.api.mvc.BodyParsers.parse.Multipart.PartHandler
import play.api.mvc.BodyParsers.parse.Multipart.handleFilePart
import play.api.mvc.BodyParsers.parse.Multipart.FileInfo
import play.api.mvc.BodyParsers.parse.multipartFormData
import play.api.mvc.MultipartFormData.FilePart
import play.api.libs.iteratee.Iteratee
import java.io.ByteArrayOutputStream
import play.api.mvc.MultipartFormData
import play.api.libs.MimeTypes
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext
import ExecutionContext.Implicits.global
import scala.concurrent.Await
import scala.concurrent.Awaitable
import scala.concurrent.Future
import scala.concurrent.duration.Duration
import play.api.mvc.BodyParsers.parse.Multipart.FileInfo
import fly.play.s3.BucketFilePartUploadTicket
import fly.play.s3.BucketFilePart
import play.api.mvc.MultipartFormData.FilePart
import fly.play.s3.BucketFile
import java.net.URLEncoder

object Files extends Controller {

  var partUploadTickets: Seq[BucketFilePartUploadTicket] = Seq()

  def upload = Action(multipartFormDataAsBytes) { request =>
    System.out.println("here")
    // Turns Map(String, Seq[String]) into Map(String, String)
    val body = request.body.dataParts.map { case (k,Seq(v)) => (k,v) }

    // Map the incoming request to a FlowData object
    val flowData = FlowData(body.get("flowChunkNumber").get.toLong,
      body.get("flowChunkSize").get.toLong,
      body.get("flowCurrentChunkSize").get.toLong,
      body.get("flowTotalSize").get.toLong,
      body.get("flowTotalChunks").get.toLong,
      body.get("flowIdentifier").get,
      body.get("flowFilename").get)

    // Get the s3 bucket
    val bucket = S3("pigion")

    // Now we need to figure out if this is the beginning of an upload, an in progress upload, or the end of an upload
    val fileName = flowData.flowFileName
    val fileContentType = MimeTypes.forFileName(flowData.flowFileName).getOrElse(".txt")
    val bucketFile = BucketFile(URLEncoder.encode(fileName, "UTF-8"), fileContentType)

    val uploadTicket = await(bucket.initiateMultipartUpload(bucketFile))

    // For each of the file parts, send them to the S3
    request.body.files foreach {
      case FilePart(key, filename, contentType, bytes) => {
        // Upload the data
        val filePart = BucketFilePart(flowData.flowChunkNumber.toInt, bytes)
        val partUploadTicket = await(bucket.uploadPart(uploadTicket, filePart))
        partUploadTickets = Seq(partUploadTicket)
      }
    }

    val uploadResult = await(bucket completeMultipartUpload (uploadTicket, partUploadTickets))

    // We only want to create a URL from the last one
    if(flowData.flowChunkNumber == flowData.flowTotalChunks) {
      val url = bucket.url(URLEncoder.encode(flowData.flowFileName, "UTF-8"))
      Destination.create(url)
      Ok(Destination.create(url))
    }
    Ok("uploading")
{'fraction_non_alphanumeric': 0.1022764764104256, 'fraction_numerical': 0.0029693170570768723, 'mean_word_length': 4.327768014059754, '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': '5495433', 'n_tokens_mistral': 1874, 'n_tokens_neox': 1852, 'n_words': 398}