-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,61 @@ | ||
const fs = {}; | ||
import {GoogleAuth} from 'google-auth-library'; | ||
import {drive_v3 as drive} from '@googleapis/drive'; | ||
|
||
export default class ClefsGoogleDrive { | ||
constructor() { | ||
this.name = 'googledrive'; | ||
} | ||
|
||
writeFile(file, data) { | ||
return new Promise(resolve => { | ||
fs[file] = data; | ||
resolve(); | ||
this.authPromise = new GoogleAuth({ | ||
scopes: [ | ||
'https://www.googleapis.com/auth/drive', | ||
'https://www.googleapis.com/auth/drive.appdata', | ||
'https://www.googleapis.com/auth/drive.file', | ||
'https://www.googleapis.com/auth/drive.metadata', | ||
'https://www.googleapis.com/auth/drive.metadata.readonly', | ||
'https://www.googleapis.com/auth/drive.photos.readonly', | ||
'https://www.googleapis.com/auth/drive.readonly', | ||
], | ||
}).then(auth => { | ||
google.options({auth}); | ||
Check failure on line 19 in packages/clefs-googledrive/src/index.js GitHub Actions / build (18.x)
|
||
}); | ||
} | ||
|
||
readFile(file) { | ||
return new Promise((resolve, reject) => { | ||
if (fs[file]) { | ||
resolve(fs[file]); | ||
} else { | ||
reject(new Error(`file not found ${file}`)); | ||
} | ||
}); | ||
async writeFile(file, data) { | ||
await this.authPromise; | ||
|
||
return drive.files.create( | ||
{ | ||
name: file, | ||
media: { | ||
body: new ReadableStream(data), | ||
}, | ||
}, | ||
); | ||
} | ||
|
||
async readFile(file) { | ||
await this.authPromise; | ||
|
||
return drive.files | ||
.get({file, alt: 'media'}, {responseType: 'stream'}) | ||
.then(res => { | ||
Check failure on line 41 in packages/clefs-googledrive/src/index.js GitHub Actions / build (18.x)
Check failure on line 41 in packages/clefs-googledrive/src/index.js GitHub Actions / build (18.x)
Check failure on line 41 in packages/clefs-googledrive/src/index.js GitHub Actions / build (20.x)
|
||
return new Promise((resolve, reject) => { | ||
let buffer = ''; | ||
|
||
res.data | ||
.on('end', () => { | ||
Check failure on line 46 in packages/clefs-googledrive/src/index.js GitHub Actions / build (18.x)
|
||
resolve(buffer); | ||
Check failure on line 47 in packages/clefs-googledrive/src/index.js GitHub Actions / build (18.x)
|
||
}) | ||
Check failure on line 48 in packages/clefs-googledrive/src/index.js GitHub Actions / build (18.x)
|
||
.on('error', err => { | ||
Check failure on line 49 in packages/clefs-googledrive/src/index.js GitHub Actions / build (18.x)
Check failure on line 49 in packages/clefs-googledrive/src/index.js GitHub Actions / build (18.x)
Check failure on line 49 in packages/clefs-googledrive/src/index.js GitHub Actions / build (20.x)
|
||
reject(err); | ||
Check failure on line 50 in packages/clefs-googledrive/src/index.js GitHub Actions / build (18.x)
|
||
}) | ||
Check failure on line 51 in packages/clefs-googledrive/src/index.js GitHub Actions / build (18.x)
|
||
.on('data', d => { | ||
buffer += d; | ||
}) | ||
.catch(error => { | ||
reject(error); | ||
}); | ||
}); | ||
}); | ||
} | ||
} |