{"size": 1785, "ext": "js", "max_stars_repo_path": "src/components/ImportFromCognitoS3Dialog/get-annotation-from-aws.js", "max_stars_repo_name": "anaplian/universal-data-tool", "max_stars_repo_stars_event_min_datetime": "2020-07-29T06:31:57.000Z", "max_stars_repo_stars_event_max_datetime": "2020-10-19T08:07:19.000Z", "max_issues_repo_path": "src/components/ImportFromCognitoS3Dialog/get-annotation-from-aws.js", "max_issues_repo_name": "anaplian/universal-data-tool", "max_issues_repo_issues_event_min_datetime": "2020-04-15T16:47:44.000Z", "max_issues_repo_issues_event_max_datetime": "2020-05-22T19:09:40.000Z", "max_forks_repo_path": "src/components/ImportFromCognitoS3Dialog/get-annotation-from-aws.js", "max_forks_repo_name": "anaplian/universal-data-tool", "max_forks_repo_forks_event_min_datetime": "2021-02-07T14:20:37.000Z", "max_forks_repo_forks_event_max_datetime": "2021-03-28T15:10:41.000Z"}
{"max_stars_count": 3, "max_issues_count": 82, "max_forks_count": 2, "avg_line_length": 30.775862069, "max_line_length": 80, "alphanum_fraction": 0.649859944}
import isEmpty from "lodash/isEmpty"
import Amplify, { Storage } from "aws-amplify"
import * as datasetHelper from "../../utils//dataset-helper"
function CheckIfAnnotationExist(result, folderToFetch) {
  return result.find(
    (element) => element.key === `${folderToFetch}/annotations/annotations.json`
  )
}

function GetSampleFromAnnotation(json, samples) {
  if (isEmpty(json.content.samples)) return
  var newSamples = []
  for (var i = 0; i < json.content.samples.length; i++) {
    var sampleName = datasetHelper.getSampleName(json.content.samples[i])
    var annotation = json.content.samples[i].annotation
    var sampleFound = datasetHelper.getSampleWithThisSampleName(
      sampleName,
      samples
    )
    var url
    if (sampleFound === null) {
      url = datasetHelper.getSampleUrl(samples)
    } else {
      url = datasetHelper.getSampleUrl(sampleFound)
    }
    newSamples.push(
      datasetHelper.createOneNewSample(sampleName, url, annotation)
    )
  }
  json.content.samples = newSamples
}

export default async (result, samples, folderToFetch, authConfig) => {
  Amplify.configure(authConfig)

  var json = null
  if (CheckIfAnnotationExist(result, folderToFetch)) {
    await Storage.get(`${folderToFetch}/annotations/annotations.json`, {
      expires: 24 * 60 * 60 * 2000,
      level: "private",
    })
      .then(async (result) => {
        await fetch(result).then(async (data) => {
          return await data.json().then(async (result) => {
            if (typeof result.content === "undefined") return
            json = result
            GetSampleFromAnnotation(json, samples)
          })
        })
      })
      .catch((err) => {
        console.log("error getting link for s3 image", err)
        return null
      })
  }
  return json
}
