<commit_before>'use strict';

import focalStorage from './../external/focalStorage.js'

export default class Files {

  static  fetchChunks(fetchPromise, eachChunkCB, doneCB) {
    fetchPromise.then(function(response) {
        var reader = response.body.getReader();
        var decoder = new TextDecoder();
        var all = "";
        (function read() {
          reader.read().then(function(result) {
            var text = decoder.decode(result.value || new Uint8Array, {
              stream: !result.done
            });
            all += text
            if (eachChunkCB) eachChunkCB(text, result)
            if (result.done) {
              if (doneCB) doneCB(all, result)
            } else {
              read() // fetch next chunk
            }
          })
        })()
      })
  }

  static async loadFile(urlString) {
    var path = this.getGoogledrivePath(urlString);
    if(path) {
      var id = await this.googlePathToId(path);
      if(!id) {
        return Promise.reject('No file found');
      } else {
        return this.googleAPIFetch(`files/`+id)
          .then(r => r.json())
          .then(metaData => {
            var m =metaData.mimeType.match(/application\/vnd.google-apps\.(.*)/)
            if( m) {
          		// Need conversion for Google Document types

        			var type = m[1]
              if (type == "spreadsheet") {
          			return this.googleAPIFetch(`files/`+id + '/export?mimeType=text/csv')
          			  .then(r => r.text())            
              } else if (type == "drawing") {
                // #TODO svg+xml does not seem to work any more? 
                // we can only display this (easily) when this code moves into the service worker
          			return this.googleAPIFetch(`files/`+id + '/export?mimeType=application/pdf')
          			  .then(r => r.text())            
              } else {
                
          			return this.googleAPIFetch(`files/`+id + '/export?mimeType=text/html')
          			  .then(r => r.text())              
              } 
        		} else {
        			// download file
        			return this.googleAPIFetch(`files/`+id + '?alt=media').then(r => r.text())
        		}
        });
      }
    }

    urlString = "" + urlString
    if(urlString.match('https://lively4/googled/')) {
      return Promise.resolve('no content2');
      
    }
    var url = new URL(urlString);
    return fetch(url).then(function (response, err) {
      if (err) {
        console.log("Err: ",  err)
      }
      console.log("file " + url + " read.");
      return response.text();
    })
  }

  static async saveFile(urlString, data){
    var path = this.getGoogledrivePath(urlString);
    if(path) {
      var id = await this.googlePathToId(path);
      if(!id) {
        debugger
        return Promise.reject('No file found');
      } else {
        return this.googleAPIUpload(id, data);
      }
    }
    
    return new Promise((resolve, reject) => {
    	var url = new URL(urlString)
    	var tried = 0;
      (function trySave() {
        tried += 1;
        $.ajax({
    	    url: url,
    	    type: 'PUT',
    	    data: data,
    	    success: function(text) {
            console.log("file " + url + " written.");
            resolve(data);
          },
      		error: function(xhr, status, error) {
            // We expect the service worker to answer this, this might fail due to the brower
            // anserwring before the service worker is started. So we try again at least once.<commit_msg>upload of new files works in google drive<commit_after>'use strict';

import focalStorage from './../external/focalStorage.js'

import generateUuid from './uuid.js'

export default class Files {

  static  fetchChunks(fetchPromise, eachChunkCB, doneCB) {
    fetchPromise.then(function(response) {
        var reader = response.body.getReader();
        var decoder = new TextDecoder();
        var all = "";
        (function read() {
          reader.read().then(function(result) {
            var text = decoder.decode(result.value || new Uint8Array, {
              stream: !result.done
            });
            all += text
            if (eachChunkCB) eachChunkCB(text, result)
            if (result.done) {
              if (doneCB) doneCB(all, result)
            } else {
              read() // fetch next chunk
            }
          })
        })()
      })
  }

  static async loadFile(urlString) {
    var path = this.getGoogledrivePath(urlString);
    if(path) {
      var id = await this.googlePathToId(path);
      if(!id) {
        return Promise.reject('No file found');
      } else {
        return this.googleAPIFetch(`files/`+id)
          .then(r => r.json())
          .then(metaData => {
            var m =metaData.mimeType.match(/application\/vnd.google-apps\.(.*)/)
            if( m) {
          		// Need conversion for Google Document types

        			var type = m[1]
              if (type == "spreadsheet") {
          			return this.googleAPIFetch(`files/`+id + '/export?mimeType=text/csv')
          			  .then(r => r.text())            
              } else if (type == "drawing") {
                // #TODO svg+xml does not seem to work any more? 
                // we can only display this (easily) when this code moves into the service worker
          			return this.googleAPIFetch(`files/`+id + '/export?mimeType=application/pdf')
          			  .then(r => r.text())            
              } else {
                
          			return this.googleAPIFetch(`files/`+id + '/export?mimeType=text/html')
          			  .then(r => r.text())              
              } 
        		} else {
        			// download file
        			return this.googleAPIFetch(`files/`+id + '?alt=media').then(r => r.text())
        		}
        });
      }
    }

    urlString = "" + urlString
    if(urlString.match('https://lively4/googled/')) {
      return Promise.resolve('no content2');
      
    }
    var url = new URL(urlString);
    return fetch(url).then(function (response, err) {
      if (err) {
        console.log("Err: ",  err)
      }
      console.log("file " + url + " read.");
      return response.text();
    })
  }

  static async saveFile(urlString, data){
    var path = this.getGoogledrivePath(urlString);
    if(path) {
      var id = await this.googlePathToId(path);
      if(!id) {
        var folderPath = path.replace(/\/[^\/]*$/,"");
        var fileName = path.replace(/.*\//,"")
        var folderId = await this.googlePathToId(folderPath);
        if (!folderId) return Proise.reject(`Folder $(folderPath) does not exit`);
        return focalStorage.getItem("googledriveToken").then( async (token) => {
          var delim = generateUuid()
          var body = `--${delim}
Content-Type: application/json; charset=UTF-8

{
  title: "${fileName}",
  parents: [
    {kind: "drive#folder", id: "${folderId}"}
  ]
}

--${delim}
Content-Type: text/plain; charset=UTF-8

`+data+`

--${delim}--`
          return fetch('https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart', {
	          method: 'POST',
          	headers: new Headers({
          		"Content-Type": "multipart/related; boundary=" + delim,
          		Authorization: "Bearer " + token,
          		"Content-Length": body.length
          	}),
          	body: body
})
      })
        
        
        // return Promise.reject('No file found');
      } else {
        return this.googleAPIUpload(id, data);
      }
    }
    
    return new Promise((resolve, reject) => {
    	var url = new URL(urlString)
    	var tried = 0;
      (function trySave() {
        tried += 1;
        $.ajax({
    	    url: url,
    	    type: 'PUT',
    	    data: data,
    	    success: function(text) {
            console.log("file " + url + " written.");
            resolve(data);
          },
      		error: function(xhr, status, error) {
            // We expect the service worker to answer this, this might fail due to the brower
            // anserwring before the service worker is started. So we try again at least once.
{'fraction_non_alphanumeric': 0.1366059253749845, 'fraction_numerical': 0.001859427296392711, 'mean_word_length': 1.7229159635504556, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 18, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1262223', 'n_tokens_mistral': 2454, 'n_tokens_neox': 2378, 'n_words': 563}