{"size": 686, "ext": "js", "max_stars_repo_path": "lib/http/analyzers/name.js", "max_stars_repo_name": "jdesboeufs/plunger", "max_stars_repo_stars_event_min_datetime": "2017-09-16T09:15:32.000Z", "max_stars_repo_stars_event_max_datetime": "2017-09-26T14:58:43.000Z", "max_issues_repo_path": "lib/http/analyzers/name.js", "max_issues_repo_name": "jdesboeufs/plunger", "max_issues_repo_issues_event_min_datetime": "2018-07-24T14:29:58.000Z", "max_issues_repo_issues_event_max_datetime": "2020-07-18T10:07:28.000Z", "max_forks_repo_path": "lib/http/analyzers/name.js", "max_forks_repo_name": "inspireteam/plunger", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 3, "max_issues_count": 104, "max_forks_count": null, "avg_line_length": 22.8666666667, "max_line_length": 64, "alphanum_fraction": 0.6836734694}
'use strict'

const url = require('url')
const contentDisposition = require('content-disposition')
const rfc2047 = require('rfc2047')

function analyzeName(token) {
  if (token.analyzed) {
    return false
  }

  const cdHeader = token.response.headers['content-disposition']
  if (cdHeader) {
    const cd = contentDisposition.parse(cdHeader)
    if (cd.parameters.filename) {
      token.fileName = rfc2047.decode(cd.parameters.filename)
    }
  }

  if (!token.fileName) {
    const {pathname} = url.parse(token.finalUrl)
    const fileName = pathname.split('/').pop()
    if (fileName) {
      token.fileName = decodeURIComponent(fileName)
    }
  }
}

module.exports = analyzeName
