<commit_before>/*
 * # Copyright (c) 2016-2017 The Khronos Group Inc.
 * # Copyright (c) 2016 <NAME>
 * #
 * # Licensed under the Apache License, Version 2.0 (the "License");
 * # you may not use this file except in compliance with the License.
 * # You may obtain a copy of the License at
 * #
 * #     http://www.apache.org/licenses/LICENSE-2.0
 * #
 * # Unless required by applicable law or agreed to in writing, software
 * # distributed under the License is distributed on an "AS IS" BASIS,
 * # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * # See the License for the specific language governing permissions and
 * # limitations under the License.
 */

import 'dart:async';
import 'dart:convert';
import 'dart:html';
import 'dart:js';
import 'dart:math';

import 'dart:typed_data';
import 'package:gltf/gltf.dart';

const int CHUNK_SIZE = 1024 * 1024;
const JsonEncoder kJsonEncoder = const JsonEncoder.withIndent('    ');

final Element dropZone = querySelector('#dropZone');
final Element output = querySelector('#output');

void main() {
  dropZone.onDragOver.listen((e) {
    dropZone.classes.add('hover');
    e.preventDefault();
  });

  dropZone.onDragLeave.listen((e) {
    dropZone.classes.remove('hover');
    e.preventDefault();
  });

  dropZone.onDrop.listen((e) async {
    e.preventDefault();
    output.text = "";
    dropZone.classes
      ..remove('hover')
      ..add('drop');

    final files = e.dataTransfer.files;

    File gltfFile;
    GltfReader reader;

    final context = new Context();

    for (gltfFile in files) {
      final lowerCaseName = gltfFile.name.toLowerCase();
      if (lowerCaseName.endsWith('.gltf')) {
        reader = new GltfJsonReader(_getFileStream(gltfFile), context);
        break;
      }
      if (lowerCaseName.endsWith('.glb')) {
        reader = new GlbReader(_getFileStream(gltfFile), context);
        break;
      }
    }

    if (reader == null) {
      dropZone.classes.remove('drop');
      return;
    }

    final readerResult = await reader.read();

    if (readerResult?.gltf != null) {
      final resourcesLoader = new ResourcesLoader(context, readerResult.gltf,
          externalBytesFetch: (uri) {
        if (uri != null) {
          final file = _getFileByUri(files, uri);
          if (file != null) {
            return _getFile(file);
          }
          return null;
        } else {
          return readerResult.buffer;
        }
      }, externalStreamFetch: (uri) {
        if (uri != null) {
          final file = _getFileByUri(files, uri);
          if (file != null) {
            return _getFileStream(file);
          }
          return null;
        }
      });

      await resourcesLoader.load();
    }
    final validationResult =
        new ValidationResult(Uri.parse(gltfFile.name), context, readerResult);
    _writeMap(validationResult.toMap());
    dropZone.classes.remove('drop');
  });
}

File _getFileByUri(List<File> files, Uri uri) =>
    files.firstWhere((file) => file.name == uri.path, orElse: () => null);

Stream<List<int>> _getFileStream(File file) {
  var isCanceled = false;
  final controller = new StreamController<List<int>>(onCancel: () {
    isCanceled = true;
  });

  controller.onListen = () {
    var index = 0;
    final fileReader = new FileReader();
    fileReader.onLoadEnd.listen((_) {
      if (isCanceled) {
        return;
      }

      final result = fileReader.result;
      if (result is Uint8List) {
        controller.add(result);
      }<commit_msg>Move async block out of MouseEvent handler<commit_after>/*
 * # Copyright (c) 2016-2017 The Khronos Group Inc.
 * # Copyright (c) 2016 <NAME>
 * #
 * # Licensed under the Apache License, Version 2.0 (the "License");
 * # you may not use this file except in compliance with the License.
 * # You may obtain a copy of the License at
 * #
 * #     http://www.apache.org/licenses/LICENSE-2.0
 * #
 * # Unless required by applicable law or agreed to in writing, software
 * # distributed under the License is distributed on an "AS IS" BASIS,
 * # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * # See the License for the specific language governing permissions and
 * # limitations under the License.
 */

import 'dart:async';
import 'dart:convert';
import 'dart:html';
import 'dart:js';
import 'dart:math';

import 'dart:typed_data';
import 'package:gltf/gltf.dart';

const int CHUNK_SIZE = 1024 * 1024;
const JsonEncoder kJsonEncoder = const JsonEncoder.withIndent('    ');

final Element dropZone = querySelector('#dropZone');
final Element output = querySelector('#output');

void main() {
  dropZone.onDragOver.listen((e) {
    dropZone.classes.add('hover');
    e.preventDefault();
  });

  dropZone.onDragLeave.listen((e) {
    dropZone.classes.remove('hover');
    e.preventDefault();
  });

  dropZone.onDrop.listen((e) {
    e.preventDefault();
    output.text = "";
    dropZone.classes
      ..remove('hover')
      ..add('drop');

    _validate(e.dataTransfer.files).then((_) {
      dropZone.classes.remove('drop');
    });
  });
}

Future<Null> _validate(List<File> files) async {
  File gltfFile;
  GltfReader reader;

  final context = new Context();

  for (gltfFile in files) {
    final lowerCaseName = gltfFile.name.toLowerCase();
    if (lowerCaseName.endsWith('.gltf')) {
      reader = new GltfJsonReader(_getFileStream(gltfFile), context);
      break;
    }
    if (lowerCaseName.endsWith('.glb')) {
      reader = new GlbReader(_getFileStream(gltfFile), context);
      break;
    }
  }

  if (reader == null) {
    return;
  }

  final readerResult = await reader.read();

  if (readerResult?.gltf != null) {
    final resourcesLoader = new ResourcesLoader(context, readerResult.gltf,
        externalBytesFetch: (uri) {
      if (uri != null) {
        final file = _getFileByUri(files, uri);
        if (file != null) {
          return _getFile(file);
        }
        return null;
      } else {
        return readerResult.buffer;
      }
    }, externalStreamFetch: (uri) {
      if (uri != null) {
        final file = _getFileByUri(files, uri);
        if (file != null) {
          return _getFileStream(file);
        }
        return null;
      }
    });

    await resourcesLoader.load();
  }
  final validationResult =
      new ValidationResult(Uri.parse(gltfFile.name), context, readerResult);
  _writeMap(validationResult.toMap());
}

File _getFileByUri(List<File> files, Uri uri) =>
    files.firstWhere((file) => file.name == uri.path, orElse: () => null);

Stream<List<int>> _getFileStream(File file) {
  var isCanceled = false;
  final controller = new StreamController<List<int>>(onCancel: () {
    isCanceled = true;
  });

  controller.onListen = () {
    var index = 0;
    final fileReader = new FileReader();
    fileReader.onLoadEnd.listen((_) {
      if (isCanceled) {
        return;
      }

      final result = fileReader.result;
      if (result is Uint8List) {
        controller.add(result);
      }
{'fraction_non_alphanumeric': 0.13052781740370897, 'fraction_numerical': 0.007417974322396576, 'mean_word_length': 2.897165091717621, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5870625', 'n_tokens_mistral': 2173, 'n_tokens_neox': 2116, 'n_words': 552}