Skip to content

Instantly share code, notes, and snippets.

@emanuel-braz
Forked from damondouglas/convert.dart
Created May 1, 2020 12:48
Show Gist options
  • Save emanuel-braz/03d491fad17020ac98d84f571e87b9be to your computer and use it in GitHub Desktop.
Save emanuel-braz/03d491fad17020ac98d84f571e87b9be to your computer and use it in GitHub Desktop.
Convert MS Word file to Google Drive format.
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