<commit_before>import { File } from 'megajs/dist/main.browser-es.js'

const location = window.location
const body = document.body
let url = null

function handleFallback () {
  if (location.pathname.indexOf('/view') !== -1) {
    showMessage("Viewing files isn't supported in this browser.")

    const paragraph = document.createElement('p')
    paragraph.appendChild(document.createTextNode('You can '))
    const anchor = document.createElement('a')
    anchor.href = location.href.replace('/view', '/')
    anchor.textContent = 'download it'
    paragraph.appendChild(anchor)
    paragraph.appendChild(document.createTextNode('.'))
    body.appendChild(paragraph)

    return
  }

  const identifier = (location.hash.length > 2 ? location.hash : location.search || '').substr(1)
  url = 'https://mega.nz/#' + identifier

  let file

  try {
    file = File.fromURL(url)
  } catch (error) {
    showError(error.message)
    return
  }

  file.loadAttributes(afterLoadAttributes)
}

function afterLoadAttributes (error, file) {
  if (error) {
    showError(error.message)
    return
  }

  showMessage('"' + file.name + '" opened')

  if (file.children) {
    showMessage("For now folders can't be downloaded. Select a file from the list below:")

    let fileList = document.createElement('ul')
    file.children.sort((left, right) => {
      return (left.name || '').localeCompare(right.name || '')
    }).forEach(file => {
      const listItem = document.createElement('li')
      const link = document.createElement('a')
      link.href = location.href + '!' + file.handle
      link.textContent = file.name
      listItem.appendChild(link)
      fileList.appendChild(listItem)
    })

    body.appendChild(fileList)
    return
  }

  const downloadStream = file.download((err, data) => {
    afterDownload(err, data, file)
  })
  handleProgress(downloadStream, file.size)
}

function handleProgress (stream, total) {
  const progressElement = document.createElement('p')
  progressElement.textContent = '0%'

  stream.on('progress', function (data) {
    progressElement.textContent = Math.floor(data.bytesLoaded * 100 / total) + '%'
  })

  stream.on('end', function () {
    progressElement.textContent = '100%'
  })<commit_msg>Fix folder listing in fallback.js<commit_after>import { File } from 'megajs/dist/main.browser-es.js'

const location = window.location
const body = document.body
let url = null

function handleFallback () {
  if (location.pathname.indexOf('/view') !== -1) {
    showMessage("Viewing files isn't supported in this browser.")

    const paragraph = document.createElement('p')
    paragraph.appendChild(document.createTextNode('You can '))
    const anchor = document.createElement('a')
    anchor.href = location.href.replace('/view', '/')
    anchor.textContent = 'download it'
    paragraph.appendChild(anchor)
    paragraph.appendChild(document.createTextNode('.'))
    body.appendChild(paragraph)

    return
  }

  const identifier = (location.hash.length > 2 ? location.hash : location.search || '').substr(1)
  url = 'https://mega.nz/#' + identifier

  let file

  try {
    file = File.fromURL(url)
  } catch (error) {
    showError(error.message)
    return
  }

  file.loadAttributes(afterLoadAttributes)
}

function afterLoadAttributes (error, file) {
  if (error) {
    showError(error.message)
    return
  }

  showMessage('"' + file.name + '" opened')

  if (file.children) {
    showMessage("For now folders can't be downloaded. Select a file from the list below:")

    const baseURL = location.origin + location.pathname + '?' + identifier.split('!').slice(0, 3).join('!')
    let fileList = document.createElement('ul')
    file.children.sort((left, right) => {
      return (left.name || '').localeCompare(right.name || '')
    }).forEach(file => {
      const listItem = document.createElement('li')
      const link = document.createElement('a')
      link.href = baseURL + '!' + file.downloadId[1]
      link.textContent = file.name
      listItem.appendChild(link)
      fileList.appendChild(listItem)
    })

    body.appendChild(fileList)
    return
  }

  const downloadStream = file.download((err, data) => {
    afterDownload(err, data, file)
  })
  handleProgress(downloadStream, file.size)
}

function handleProgress (stream, total) {
  const progressElement = document.createElement('p')
  progressElement.textContent = '0%'

  stream.on('progress', function (data) {
    progressElement.textContent = Math.floor(data.bytesLoaded * 100 / total) + '%'
  })

  stream.on('end', function () {
    progressElement.textContent = '100%'
  })
{'fraction_non_alphanumeric': 0.1427330002172496, 'fraction_numerical': 0.0049967412557028025, 'mean_word_length': 3.5994005994005995, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7009187', 'n_tokens_mistral': 1398, 'n_tokens_neox': 1340, 'n_words': 292}