async readFromJsonFile()

in source/s3event/lib/index.js [226:280]


  async readFromJsonFile(bucket, key) {
    const response = await CommonUtils.download(bucket, key, false);

    let json = JSON.parse(response.Body);
    json = json.legacyArchiveObject || json;

    /* find the most possible video file from the Json */
    const videos = json.files.filter((x) => {
      const type = CommonUtils.parseMimeType(CommonUtils.getMime(x.location || x.name));
      return type === 'video' || type === 'mxf';
    });

    /* find largest file */
    let video = videos.reduce((prev, cur) => {
      if (Number.parseInt(cur.sizeBytes || 0, 10) > Number.parseInt(prev.sizeBytes || 0, 10)) {
        return cur;
      }
      return prev;
    }, {});

    video = video || videos.shift();
    if (!video) {
      throw new Error(`failed to find video from Json file, ${key}`);
    }

    if (!video.uuid || !video.location || !video.name) {
      throw new Error(`missing uuid, location, or name from json file, ${key}`);
    }

    /* extract md5 from Json file */
    const md5 = ((video.checksums || []).filter(x =>
      x.type === 'MD5').shift() || {}).value;

    /* make sure md5 is valid */
    if (md5 && !md5.match(/([0-9a-fA-F]{32})/)) {
      throw new Error(`not a valid md5 format, ${md5}, ${video.location || video.name}`);
    }

    let ingestDate = (json.ingestDate || json.archiveDate);
    ingestDate = (ingestDate && new Date(ingestDate).getTime()) || undefined;

    return CommonUtils.neat({
      uuid: video.uuid,
      key: video.location || video.name,
      md5,
      attributes: {
        collectionUuid: json.collectionUuid || json.legacyArchiveObjectUuid,
        ingestDate: (json.ingestDate || json.archiveDate),
        category: json.categoryName,
        comments: json.comments,
        description: json.collectionDescription || json.legacyArchiveName,
        name: json.collectionName || json.objectName,
      },
    });
  }