<commit_before>import 'dart:async';
import 'dart:io';
import 'dart:convert';
import 'package:http/http.dart' show Client;
import 'package:googleapis_auth/auth_io.dart' as auth;
//import 'package:googleapis/common/common.dart' show Media, DownloadOptions;
import 'package:_discoveryapis_commons/_discoveryapis_commons.dart' show Media, DownloadOptions;
import 'package:googleapis/drive/v2.dart' as drive;
import 'base_storage_manager.dart';
import '../../errors.dart';

// Obtain the client email / secret from the Google Developers Console by
// creating new OAuth credentials of application type
// "Installed application / Other".
final identifier = new auth.ClientId(
    "<please fill in>.apps.googleusercontent.com",
    "<please fill in>");


final accountCredentials = new auth.ServiceAccountCredentials.fromJson(r'''
{
  "type": "service_account",
  "project_id": "mobdisttool-1200",
  "private_key_id": "99d613570100ea5f8064dad617f2670cf969a44f",
  "private_key": "-----<KEY>",
  "client_email": "<EMAIL>",
  "client_id": "104717954167324583811",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/mobdisttool%40mobdisttool-1200.iam.gserviceaccount.com"
}

''');

// This is the list of scopes this application will use.
// You need to enable the Drive API in the Google Developers Console.

class GoogleDriveStorageManager extends BaseStorageManager {
  String storageIdentifier = "gdrive";

  final scopes = [drive.DriveApi.DriveScope];
  auth.AutoRefreshingAuthClient authClient;
  var api;
  GoogleDriveStorageManager() {

  }

  Future initializeStorage(Map config)async {
    authClient = await auth.clientViaServiceAccount(accountCredentials, scopes);
    api = new drive.DriveApi(authClient);
  }

  Future<String> storeFile(File file,{String filename, String contentType}) async {
    var media = new Media(file.openRead(), file.lengthSync());
    var driveFile = new drive.File();
    if (filename != null){
      driveFile.title = filename;
    }

    if(contentType != null){
      driveFile.mimeType = contentType;
    }
    var result = await api.files.insert(driveFile, uploadMedia: media);
    if (result == null){
      throw new ArtifactError("Error on storaging file");
    }
    return generateStorageInfos(result.id);
  }

  Future<Stream> getStreamFromStoredFile(String storedInfos) async{
    String objectId= extractStorageId(storedInfos);
    drive.File file =  await api.files.get(objectId);
    var bytes = await authClient.readBytes(file.downloadUrl);
    print(new AsciiDecoder().convert(bytes));
    var tmpDirectory = await Directory.systemTemp.createTemp('mdt');
    var tmpFile = new File(objectId);
    await tmpFile.writeAsBytes(bytes);
    return tmpFile.openRead();<commit_msg>working in progress artefact for google drive<commit_after>import 'dart:async';
import 'dart:io';
import 'dart:convert';
import 'package:http/http.dart' show Client;
import 'package:googleapis_auth/auth_io.dart' as auth;
//import 'package:googleapis/common/common.dart' show Media, DownloadOptions;
import 'package:_discoveryapis_commons/_discoveryapis_commons.dart' show Media, DownloadOptions;
import 'package:googleapis/drive/v2.dart' as drive;
import 'base_storage_manager.dart';
import '../../errors.dart';

// Obtain the client email / secret from the Google Developers Console by
// creating new OAuth credentials of application type
// "Installed application / Other".
final identifier = new auth.ClientId(
    "<please fill in>.apps.googleusercontent.com",
    "<please fill in>");


final accountCredentials = new auth.ServiceAccountCredentials.fromJson(r'''
{
  "type": "service_account",
  "project_id": "mobdisttool-1200",
  "private_key_id": "99d613570100ea5f8064dad617f2670cf969a44f",
  "private_key": "-----<KEY>",
  "client_email": "<EMAIL>",
  "client_id": "104717954167324583811",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/mobdisttool%40mobdisttool-1200.iam.gserviceaccount.com"
}

''');

// This is the list of scopes this application will use.
// You need to enable the Drive API in the Google Developers Console.

class GoogleDriveStorageManager extends BaseStorageManager {
  String storageIdentifier = "gdrive";

  final scopes = [drive.DriveApi.DriveScope];
  auth.AutoRefreshingAuthClient authClient;
  var api;
  GoogleDriveStorageManager() {

  }

  Future initializeStorage(Map config)async {
    authClient = await auth.clientViaServiceAccount(accountCredentials, scopes);
    api = new drive.DriveApi(authClient);
  }

  Future<String> storeFile(File file,{String filename, String contentType}) async {
    var media = new Media(file.openRead(), file.lengthSync());
    var driveFile = new drive.File();
    if (filename != null){
      driveFile.title = filename;
    }

    if(contentType != null){
      driveFile.mimeType = contentType;
    }
    var result = await api.files.insert(driveFile, uploadMedia: media);
    if (result == null){
      throw new ArtifactError("Error on storaging file");
    }
    return generateStorageInfos(result.id);
  }

  Future<Stream> getStreamFromStoredFile(String storedInfos) async{
    String objectId= extractStorageId(storedInfos);
    drive.File file =  await api.files.get(objectId);
    var bytes = await authClient.readBytes(file.downloadUrl);
    print(new AsciiDecoder().convert(bytes));
    var tmpDirectory = await Directory.systemTemp.createTemp('mdt');
    var tmpFile = new File(objectId);
    await tmpFile.writeAsBytes(bytes,flush:true);
    return tmpFile.openRead();
{'fraction_non_alphanumeric': 0.12527361508671495, 'fraction_numerical': 0.02492002020542179, 'mean_word_length': 4.795121951219512, 'pattern_counts': {'":': 20, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 8, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7054820', 'n_tokens_mistral': 1952, 'n_tokens_neox': 1837, 'n_words': 415}