<commit_before>/**
 * Copyright(c) 2015 <NAME>, all rights reserved.
 */
package net.scalytica.symbiotic.models.dman

import japgolly.scalajs.react.Callback
import net.scalytica.symbiotic.core.http.{AjaxStatus, Failed, Finished}
import net.scalytica.symbiotic.logger._
import net.scalytica.symbiotic.models.FileId
import net.scalytica.symbiotic.routing.SymbioticRouter
import org.scalajs.dom.ext.Ajax
import org.scalajs.dom.raw.{Event, FormData, HTMLFormElement, XMLHttpRequest}
import upickle.default._

import scala.concurrent.Future
import scala.scalajs.concurrent.JSExecutionContext.Implicits.runNow

case class ManagedFile(
  id: String,
  filename: String = "",
  contentType: Option[String] = None,
  uploadDate: Option[String] = None,
  length: Option[Long] = None,
  metadata: FileMetadata) {

  def fileId = FileId(metadata.fid)

  def path = metadata.path.map(_.stripPrefix("/root"))

  def downloadLink = s"${SymbioticRouter.ServerBaseURI}/document/${metadata.fid}"

}

case class ManagedFolder(folder: Option[ManagedFile], content: Seq[ManagedFile])

object ManagedFile {

  def load(folder: Option[String]): Future[Either[Failed, ManagedFolder]] = {
    val path = folder.map(fp => s"?path=$fp").getOrElse("")
    for {
      xhr <- Ajax.get(
        url = s"${SymbioticRouter.ServerBaseURI}/document/folder$path",
        headers = Map(
          "Accept" -> "application/json",
          "Content-Type" -> "application/json"
        )
      )
    } yield {
      xhr.status match {
        case ok: Int if ok == 200 =>
          Right(ManagedFolder(None, read[Seq[ManagedFile]](xhr.responseText)))
        case nc: Int if nc == 204 =>
          Right(ManagedFolder(None, Seq.empty[ManagedFile]))
        case _ =>
          Left(Failed(xhr.responseText))
      }
    }
  }

  def load(folderId: FileId): Future[Either[Failed, ManagedFolder]] = {
    for {
      xhr <- Ajax.get(
        url = s"${SymbioticRouter.ServerBaseURI}/document/folder/${folderId.value}",
        headers = Map(
          "Accept" -> "application/json",
          "Content-Type" -> "application/json"
        )
      )
    } yield {
      xhr.status match {
        case ok: Int if ok == 200 =>
          Right(read[ManagedFolder](xhr.responseText))
        case _ =>
          Left(Failed(xhr.responseText))
      }
    }
  }

  def upload(folderId: Option[FileId], form: HTMLFormElement)(callback: Callback) = {
    val url = folderId.map(fid => s"${SymbioticRouter.ServerBaseURI}/document/folder/${fid.value}/upload")
      .getOrElse(s"${SymbioticRouter.ServerBaseURI}/document/upload?path=/root")
    val fd = new FormData(form)
    val xhr = new XMLHttpRequest
    xhr.onreadystatechange = (e: Event) => {
      if (xhr.readyState == XMLHttpRequest.DONE) {
        if (xhr.status == 200) {
          log.info(xhr.responseText)
          form.reset()
          callback.runNow()
        }
      }
    }
    xhr.open(method = "POST", url = url, async = true)
    xhr.send(fd)
  }

  def lock(fileId: FileId): Future[Either[Failed, Lock]] =
    for {
      xhr <- Ajax.put(
        url = s"${SymbioticRouter.ServerBaseURI}/document/${fileId.value}/lock",
        headers = Map("Accept" -> "application/json")
      )
    } yield {
      if (xhr.status >= 200 && xhr.status < 400) Right(read[Lock](xhr.responseText))
      else Left(Failed(xhr.responseText))
    }

  def unlock(fileId: FileId): Future[AjaxStatus] =
    for {
      xhr <- Ajax.put(
        url = s"${SymbioticRouter.ServerBaseURI}/document/${fileId.value}/unlock",
        headers = Map("Accept" -> "application/json")
      )
    } yield {
      if (xhr.status >= 200 && xhr.status < 400) Finished
      else Failed(xhr.responseText)
    }

  def addFolder(folderId: Option[FileId], name: String) = {
    val addFolderURL = folderId.map { fid =>
      s"${SymbioticRouter.ServerBaseURI}/document/folder/${fid.value}/$name"
    }.getOrElse(s"${SymbioticRouter.ServerBaseURI}/document/folder?fullPath=/root")

    for {
      xhr <- Ajax.post(
        url = addFolderURL,
        headers = Map("Accept" -> "application/json")
      )
    } yield {
      if (xhr.status >= 200 && xhr.status < 400) Finished
      else Failed(xhr.responseText)
    }
  }<commit_msg>Resolves #35. Name of last folder was not specified whenever the first folder was created on the root context. Causing all sorts of weird behaviour.<commit_after>/**
 * Copyright(c) 2015 <NAME>, all rights reserved.
 */
package net.scalytica.symbiotic.models.dman

import japgolly.scalajs.react.Callback
import net.scalytica.symbiotic.core.http.{AjaxStatus, Failed, Finished}
import net.scalytica.symbiotic.logger._
import net.scalytica.symbiotic.models.FileId
import net.scalytica.symbiotic.routing.SymbioticRouter
import org.scalajs.dom.ext.Ajax
import org.scalajs.dom.raw.{Event, FormData, HTMLFormElement, XMLHttpRequest}
import upickle.default._

import scala.concurrent.Future
import scala.scalajs.concurrent.JSExecutionContext.Implicits.runNow

case class ManagedFile(
  id: String,
  filename: String = "",
  contentType: Option[String] = None,
  uploadDate: Option[String] = None,
  length: Option[Long] = None,
  metadata: FileMetadata) {

  def fileId = FileId(metadata.fid)

  def path = metadata.path.map(_.stripPrefix("/root"))

  def downloadLink = s"${SymbioticRouter.ServerBaseURI}/document/${metadata.fid}"

}

case class ManagedFolder(folder: Option[ManagedFile], content: Seq[ManagedFile])

object ManagedFile {

  def load(folder: Option[String]): Future[Either[Failed, ManagedFolder]] = {
    val path = folder.map(fp => s"?path=$fp").getOrElse("")
    for {
      xhr <- Ajax.get(
        url = s"${SymbioticRouter.ServerBaseURI}/document/folder$path",
        headers = Map(
          "Accept" -> "application/json",
          "Content-Type" -> "application/json"
        )
      )
    } yield {
      xhr.status match {
        case ok: Int if ok == 200 =>
          Right(ManagedFolder(None, read[Seq[ManagedFile]](xhr.responseText)))
        case nc: Int if nc == 204 =>
          Right(ManagedFolder(None, Seq.empty[ManagedFile]))
        case _ =>
          Left(Failed(xhr.responseText))
      }
    }
  }

  def load(folderId: FileId): Future[Either[Failed, ManagedFolder]] = {
    for {
      xhr <- Ajax.get(
        url = s"${SymbioticRouter.ServerBaseURI}/document/folder/${folderId.value}",
        headers = Map(
          "Accept" -> "application/json",
          "Content-Type" -> "application/json"
        )
      )
    } yield {
      xhr.status match {
        case ok: Int if ok == 200 =>
          Right(read[ManagedFolder](xhr.responseText))
        case _ =>
          Left(Failed(xhr.responseText))
      }
    }
  }

  def upload(folderId: Option[FileId], form: HTMLFormElement)(callback: Callback) = {
    val url = folderId.map(fid => s"${SymbioticRouter.ServerBaseURI}/document/folder/${fid.value}/upload")
      .getOrElse(s"${SymbioticRouter.ServerBaseURI}/document/upload?path=/root")
    val fd = new FormData(form)
    val xhr = new XMLHttpRequest
    xhr.onreadystatechange = (e: Event) => {
      if (xhr.readyState == XMLHttpRequest.DONE) {
        if (xhr.status == 200) {
          log.info(xhr.responseText)
          form.reset()
          callback.runNow()
        }
      }
    }
    xhr.open(method = "POST", url = url, async = true)
    xhr.send(fd)
  }

  def lock(fileId: FileId): Future[Either[Failed, Lock]] =
    for {
      xhr <- Ajax.put(
        url = s"${SymbioticRouter.ServerBaseURI}/document/${fileId.value}/lock",
        headers = Map("Accept" -> "application/json")
      )
    } yield {
      if (xhr.status >= 200 && xhr.status < 400) Right(read[Lock](xhr.responseText))
      else Left(Failed(xhr.responseText))
    }

  def unlock(fileId: FileId): Future[AjaxStatus] =
    for {
      xhr <- Ajax.put(
        url = s"${SymbioticRouter.ServerBaseURI}/document/${fileId.value}/unlock",
        headers = Map("Accept" -> "application/json")
      )
    } yield {
      if (xhr.status >= 200 && xhr.status < 400) Finished
      else Failed(xhr.responseText)
    }

  def addFolder(folderId: Option[FileId], name: String) = {
    val addFolderURL = folderId.map { fid =>
      s"${SymbioticRouter.ServerBaseURI}/document/folder/${fid.value}/$name"
    }.getOrElse(s"${SymbioticRouter.ServerBaseURI}/document/folder?fullPath=/root/$name")

    for {
      xhr <- Ajax.post(
        url = addFolderURL,
        headers = Map("Accept" -> "application/json")
      )
    } yield {
      if (xhr.status >= 200 && xhr.status < 400) Finished
      else Failed(xhr.responseText)
    }
  }
{'fraction_non_alphanumeric': 0.142773960638174, 'fraction_numerical': 0.008151857458949575, 'mean_word_length': 2.8912550974173086, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4335215', 'n_tokens_mistral': 2908, 'n_tokens_neox': 2788, 'n_words': 518}