-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.ts
206 lines (181 loc) · 4.77 KB
/
storage.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
199
200
201
202
203
204
205
206
// Copyright (C) 2020-2022 Russell Clarey. All rights reserved. MIT license.
import {
dirname,
join,
relative,
SEP,
} from "https://deno.land/[email protected]/path/mod.ts#^";
import { writeAll } from "https://deno.land/[email protected]/io/util.ts#^";
import type { InfoDict } from "./metainfo.ts";
import { BLOCK_SIZE } from "./piece.ts";
import { readN } from "./_bytes.ts";
/** A method of persisting the downloaded files, e.g. on the local filesystem */
export interface StorageMethod {
get(
path: string[],
offset: number,
length: number,
): Promise<Uint8Array | null>;
/** return value indicates whether the call was successful or not */
set(path: string[], offset: number, bytes: Uint8Array): Promise<boolean>;
/** returns whether or not the file already exists */
exists(path: string[]): Promise<boolean>;
}
const OPEN_OPTIONS = {
read: true,
write: true,
create: true,
};
export class Storage {
#method: StorageMethod;
#info: InfoDict;
#dirPath: string[];
#written: boolean[] = [];
constructor(method: StorageMethod, info: InfoDict, dirPath: string) {
this.#method = method;
this.#info = info;
this.#dirPath = relative(Deno.cwd(), dirPath).split(SEP);
if (this.#dirPath[0] === "") {
this.#dirPath = this.#dirPath.slice(1);
}
}
async get(offset: number, length: number): Promise<Uint8Array | null> {
const bytes = new Uint8Array(length);
const success = await this.findAndDo(
offset,
bytes,
async (path, fileOffset, slice) => {
const got = await this.#method.get(path, fileOffset, slice.length);
if (got) {
slice.set(got);
}
return !!got;
},
);
return success ? bytes : null;
}
async set(offset: number, bytes: Uint8Array): Promise<boolean> {
const index = offset / BLOCK_SIZE;
if (this.#written[index]) {
// TODO log error
// return true because it signals that this block is set successfully (since it already was set)
return true;
}
const success = await this.findAndDo(
offset,
bytes,
(path, fileOffset, slice) => this.#method.set(path, fileOffset, slice),
);
if (success) {
this.#written[index] = true;
}
return success;
}
private async findAndDo(
offset: number,
bytes: Uint8Array,
action: (
path: string[],
offset: number,
arr: Uint8Array,
) => Promise<boolean>,
): Promise<boolean> {
try {
if (!("files" in this.#info)) {
return await action([...this.#dirPath, this.#info.name], offset, bytes);
}
const length = bytes.length;
let i = 0;
let fileStart = 0;
for (const file of this.#info.files) {
const fileEnd = fileStart + file.length;
if (fileEnd >= offset) {
const nBytes = Math.min(fileEnd - offset - i, length - i);
const fileOffset = Math.max(0, offset - fileStart);
const success = await action(
[...this.#dirPath, ...file.path],
fileOffset,
bytes.subarray(i, i + nBytes),
);
if (!success) {
return false;
}
i += nBytes;
if (i === length) {
return true;
}
}
fileStart = fileEnd;
}
} catch {
// TODO log error
return false;
}
// TODO log error
return false;
}
}
async function mkdirAndOpen(path: string) {
try {
return await Deno.open(path, OPEN_OPTIONS);
} catch {
await Deno.mkdir(dirname(path), { recursive: true });
return Deno.open(path, OPEN_OPTIONS);
}
}
export const fsStorage: StorageMethod = {
async get(
path: string[],
offset: number,
length: number,
): Promise<Uint8Array | null> {
const resolvedPath = join(...path);
let f: Deno.FsFile | null = null;
try {
f = await Deno.open(resolvedPath, OPEN_OPTIONS);
f.seek(offset, Deno.SeekMode.Start);
const bytes = await readN(f, length);
f.close();
return bytes;
} catch {
// TODO log error properly
try {
f?.close();
} catch {
// do nothing
}
return null;
}
},
async set(
path: string[],
offset: number,
bytes: Uint8Array,
): Promise<boolean> {
const resolvedPath = join(...path);
let f: Deno.FsFile | null = null;
try {
f = await mkdirAndOpen(resolvedPath);
f.seek(offset, Deno.SeekMode.Start);
await writeAll(f, bytes);
f.close();
return true;
} catch {
// TODO log error
try {
f?.close();
} catch {
// do nothing
}
return false;
}
},
async exists(path: string[]): Promise<boolean> {
try {
await Deno.stat(join(...path));
return true;
} catch {
return false;
}
},
};