-
Notifications
You must be signed in to change notification settings - Fork 0
/
Drive.js
69 lines (59 loc) · 2.05 KB
/
Drive.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import DriveRequestHandler from "./DriveRequestHandler.js";
export default class Drive {
#requestHandler = new DriveRequestHandler();
#fileId;
constructor(fileId, refreshToken){
this.setFileId(fileId);
this.setRefreshToken(refreshToken);
}
async initialize(authorizationCode){
await this.#requestHandler.fetchRefreshToken(authorizationCode);
let files = await this.#requestHandler.getFiles();
if(files.length == 0)
await this.#createEmptyPasswordListFile();
else
this.setFileId(files[0].id);
}
async #createEmptyPasswordListFile(){
let data = '{}';
let requestOptions = {
method: 'PUT',
headers: {'Content-Type':'text/plain','Content-Length': Buffer.byteLength(data)},
body: data,
};
let resumableUploadUrl = await this.#requestHandler.getUploadUrl();
let fileResource = await this.#requestHandler.requestDriveApi(resumableUploadUrl, requestOptions);
this.setFileId(fileResource.id);
}
setFileId(fileId){
this.#fileId = fileId;
}
getFileId(){
this.#checkFileId();
return this.#fileId;
}
setRefreshToken(refreshToken){
this.#requestHandler.setRefreshToken(refreshToken);
}
getRefreshToken(){
return this.#requestHandler.getRefreshToken();
}
async updatePasswordList(data){
this.#checkFileId();
let resumableUpdateUrl = await this.#requestHandler.getUpdateUrl(this.#fileId);
let requestOptions = {
method: 'PUT',
headers: {'Content-Type': 'text/plain','Content-Length': Buffer.byteLength(data)},
body: data
}
await this.#requestHandler.requestDriveApi(resumableUpdateUrl, requestOptions);
}
async getPasswordList(){
this.#checkFileId();
return await this.#requestHandler.readFile(this.#fileId);
}
#checkFileId(){
if(this.#fileId == undefined)
throw new Error("File Id not set");
}
}