Last active
May 1, 2020 12:48
-
-
Save damondouglas/b4690db23b6527bb6d73 to your computer and use it in GitHub Desktop.
Convert MS Word file to Google Drive format.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'dart:io'; | |
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:googleapis/drive/v2.dart' as drive; | |
import 'package:path/path.dart' as path; | |
Future convertFile(drive.DriveApi api, | |
Client client, | |
String objectId) { | |
var completer = new Completer(); | |
api.files.get(objectId).then((drive.File file) { | |
var fileName = path.basenameWithoutExtension(file.title); | |
var parents = file.parents; | |
client.readBytes(file.downloadUrl).then((bytes) { | |
var driveFile = new drive.File() | |
..title = fileName | |
..mimeType = 'application/vnd.google-apps.document' | |
..parents = parents; | |
api.files.insert(driveFile) | |
.then((driveFile){ | |
var byteList = bytes.toList(); | |
var stream = new Stream.fromIterable([byteList]); | |
var media = new Media(stream, byteList.length); | |
api.files.update(new drive.File(), driveFile.id, uploadMedia: media) | |
.then((drive.File f){ | |
api.files.delete(objectId) | |
.then((_){ | |
completer.complete(true); | |
print("Converted ${f.id}"); | |
}); | |
}); | |
}); | |
}); | |
}); | |
return completer.future; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment