-
Notifications
You must be signed in to change notification settings - Fork 17
/
Engin.ts
198 lines (185 loc) · 5.77 KB
/
Engin.ts
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { AllDrivers } from "./oneindexconf";
export interface DriveItems {
name: string;
id: string;
size: number;
type: string;
data?: any;
thumbnail?: string;
}
export interface Driverer {
List(id?: string): Promise<DriveItems[]>; // id is optional
Search(name: string): Promise<DriveItems[]>;
GetFile(id: string, request: any): Promise<Response>;
FetchAccessToken(
client_id,
client_secret,
redirect_uri,
token_url,
refreshToken
): Promise<string>;
}
interface config {
ClientID: string;
ClientSecret: string;
redirect_url?: string;
RefreshToken: string;
}
export interface DriverConfig {
name: string;
path: string;
type: "onedrive" | "googledrive";
config: config;
}
// get driver by path
export async function GetDriver(path: string): Promise<Driverer> {
const driver = AllDrivers.find((d) => {
return d.path === path;
});
if (!driver) {
throw new Error("Driver not found");
}
switch (driver.type) {
case "onedrive":
const od = new OneDrive();
await od.FetchAccessToken(
driver.config.ClientID,
driver.config.ClientSecret,
driver.config.redirect_url,
"https://login.microsoftonline.com/common/oauth2/v2.0/token",
driver.config.RefreshToken
);
return od;
default:
throw new Error("Driver not found");
}
}
class OneDrive implements Driverer {
private accessToken: string;
public async FetchAccessToken(
client_id: string,
client_secret: string,
redirect_uri: string,
token_url: string,
refreshToken: string
): Promise<string> {
try {
const req = await fetch(token_url, {
method: "POST",
body: `client_id=${client_id}&scope=offline_access%20Files.ReadWrite.All&refresh_token=${refreshToken}&redirect_uri=${redirect_uri}&grant_type=refresh_token&client_secret=${client_secret}`,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
});
if (!req.ok) {
throw new Error(`Request failed with status ${req.status}`);
}
const data = await req.json();
this.accessToken = data.access_token;
console.log("access token", data);
return data.access_token;
} catch (error) {
console.error("Error fetching access token:", error);
throw error;
}
}
async List(id?: string): Promise<DriveItems[]> {
let url = "";
if (id) {
url = `https://graph.microsoft.com/v1.0/me/drive/items/${id}/children?$top=5000&$expand=thumbnails($select=medium)&$select=id,name,size,lastModifiedDateTime,content.downloadUrl,file,parentReference`;
} else {
url =
"https://graph.microsoft.com/v1.0/me/drive/root/children?$top=5000&$expand=thumbnails($select=medium)&$select=id,name,size,lastModifiedDateTime,content.downloadUrl,file,parentReference";
}
const request = await fetch(url, {
headers: {
Authorization: `Bearer ${this.accessToken}`,
},
});
const data = await request.json();
// return data.value.map((item: any) => {
// return {
// name: item.name,
// size: item.size,
// id: item.id,
// type: item.file ? item.file.mimeType : "folder",
// thumbnail:
// item.thumbnails && item.thumbnails.length > 0
// ? item.thumbnails[0].medium.url
// : null,
// date: item.lastModifiedDateTime,
// };
// });
// sort by new date
const sorted = data.value.map((item: any) => {
return {
name: item.name,
size: item.size,
id: item.id,
type: item.file ? item.file.mimeType : "folder",
thumbnail:
item.thumbnails && item.thumbnails.length > 0
? item.thumbnails[0].medium.url
: null,
data: item,
};
});
sorted.sort((a, b) => {
return (
+new Date(b.data.lastModifiedDateTime) -
+new Date(a.data.lastModifiedDateTime)
);
});
return sorted;
}
async GetFile(id: string, request: any): Promise<Response> {
const url = `https://graph.microsoft.com/v1.0/me/drive/items/${id}/content`;
const myheaders = {
Authorization: `Bearer ${this.accessToken}`,
};
try {
if (request.headers.get("Range" || "range")) {
console.log("got partial content request");
myheaders["Range"] = request.headers.get("Range" || "range");
const response = await fetch(url, { headers: myheaders });
const contentRange = response.headers.get("Content-Range");
const contentLength = response.headers.get("Content-Length");
const disposition = response.headers.get("content-disposition");
const filename = decodeURIComponent(
disposition
.substring(disposition.indexOf("utf-8") + 7)
.replace(/['"]/g, "")
.replace(";", "")
.replace("filename=", "")
);
return new Response(response.body, {
status: 206, // Partial Content
headers: {
"Content-Type": "application/octet-stream",
"Content-Disposition": `attachment; filename="${filename}"`,
"Content-Range": contentRange,
"Content-Length": contentLength,
"Accept-Ranges": "bytes",
},
});
} else {
const Head = new Headers();
Head.set("Authorization", `Bearer ${this.accessToken}`);
try {
return await fetch(url, { headers: Head });
} catch (error) {
return new Response("api limited, cant handle request ", {
status: 500,
});
}
}
} catch (error) {
return new Response("api limited, cant handle request ", {
status: 500,
});
}
}
Search(name: string): Promise<DriveItems[]> {
return Promise.resolve([]);
}
}