aboutsummaryrefslogtreecommitdiff
path: root/packages/server/src/git/blob.ts
blob: 4dadc01ad58b78c23c639ff708bc1d07272afb40 (plain)
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
import { BlobError, createError } from "./error";
import { Tree } from "./tree";
import { BlobTreeEntry } from "./tree_entry";

/**
 * A representation of a blob
 */
export class Blob {
	private _tree_entry: BlobTreeEntry;

	public path;

	/**
	 * @param entry - A tree entry that's a blob
	 */
	constructor(entry: BlobTreeEntry) {
		this._tree_entry = entry;

		this.path = entry.path;
	}

	/**
	 * Returns the blob's content
	 */
	public async content(): Promise<string> {
		return (await this._tree_entry.ng_tree_entry.getBlob()).content.toString();
	}

	/**
	 * Returns a blob from a path in a tree
	 *
	 * @param tree - The tree to look in
	 * @param path - A path to a blob
	 * @returns An instance of a blob
	 */
	public static async fromPath(tree: Tree, path: string): Promise<Blob> {
		const entry = await tree.find(path);

		if(!(entry instanceof BlobTreeEntry)) {
			throw(createError(BlobError, 500, "Not a blob"));
		}

		return new Blob(entry);
	}
}