-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgithubBlobStorage.ts
More file actions
64 lines (48 loc) · 1.92 KB
/
githubBlobStorage.ts
File metadata and controls
64 lines (48 loc) · 1.92 KB
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
import { IBlobStorage } from "@paperbits/common/persistence/IBlobStorage";
import { IGithubClient } from "./IGithubClient";
import { IGithubTreeItem } from "./IGithubTreeItem";
export class GithubBlobStorage implements IBlobStorage {
private readonly changes: IGithubTreeItem[];
constructor(private readonly githubClient: IGithubClient) {
this.changes = [];
}
public getChanges(): IGithubTreeItem[] {
return this.changes;
}
public async uploadBlob(key: string, content: Uint8Array): Promise<void> {
key = key.replaceAll("\\", "/");
if (key.startsWith("/")) {
key = key.substr(1);
}
const response = await this.githubClient.createBlob(key, content);
const newTreeItem: IGithubTreeItem = {
path: key,
sha: response.sha
};
this.changes.push(newTreeItem);
}
private base64ToUnit8Array(base64: string): Uint8Array {
const rawData = atob(base64);
const rawDataLength = rawData.length;
const byteArray = new Uint8Array(new ArrayBuffer(rawDataLength));
for (let i = 0; i < rawDataLength; i++) {
byteArray[i] = rawData.charCodeAt(i);
}
return byteArray;
}
public async downloadBlob(path: string): Promise<Uint8Array> {
const githubFile = await this.githubClient.getFileContent(path);
return this.base64ToUnit8Array(githubFile.content);
}
public async getDownloadUrl(permalink: string): Promise<string> {
throw new Error("Not supported");
}
public deleteBlob(path: string): Promise<void> {
throw new Error("Not supported");
}
public async listBlobs(): Promise<string[]> {
const latestCommitTree = await this.githubClient.getLatestCommitTree();
const blobPaths = latestCommitTree.tree.filter(item => item.type === "blob").map(item => item.path);
return blobPaths;
}
}