|
| 1 | +export class StreamerFileDb { |
| 2 | + baseDirectory = "that-open-company-streaming"; |
| 3 | + |
| 4 | + maxDeadTime = 60000; |
| 5 | + |
| 6 | + private _memoryCleanTime = 10000; |
| 7 | + |
| 8 | + get memoryCleanTime() { |
| 9 | + return this._memoryCleanTime; |
| 10 | + } |
| 11 | + |
| 12 | + set memoryCleanTime(value: number) { |
| 13 | + this._memoryCleanTime = value; |
| 14 | + this.dispose(); |
| 15 | + this.setupMemoryCleanup(); |
| 16 | + } |
| 17 | + |
| 18 | + private _intervalID: number | null = null; |
| 19 | + |
| 20 | + private _isCleaningMemory = false; |
| 21 | + |
| 22 | + constructor() { |
| 23 | + this.setupMemoryCleanup(); |
| 24 | + } |
| 25 | + |
| 26 | + async get(name: string) { |
| 27 | + const encodedName = this.encodeName(name); |
| 28 | + const baseDir = await this.getDir(this.baseDirectory); |
| 29 | + try { |
| 30 | + const fileHandle = await baseDir.getFileHandle(encodedName); |
| 31 | + const file = await fileHandle.getFile(); |
| 32 | + this.updateLastAccessTime(encodedName); |
| 33 | + const buffer = await file.arrayBuffer(); |
| 34 | + return new Uint8Array(buffer); |
| 35 | + } catch (e) { |
| 36 | + return null; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + async add(name: string, buffer: Uint8Array) { |
| 41 | + const encodedName = this.encodeName(name); |
| 42 | + const baseDir = await this.getDir(this.baseDirectory); |
| 43 | + const fileHandle = await baseDir.getFileHandle(encodedName, { |
| 44 | + create: true, |
| 45 | + }); |
| 46 | + |
| 47 | + const writable = await fileHandle.createWritable(); |
| 48 | + await writable.write(buffer); |
| 49 | + await writable.close(); |
| 50 | + this.updateLastAccessTime(encodedName); |
| 51 | + } |
| 52 | + |
| 53 | + async clear() { |
| 54 | + const baseDir = await this.getDir(this.baseDirectory); |
| 55 | + // @ts-ignore |
| 56 | + for await (const [name] of baseDir.entries()) { |
| 57 | + await baseDir.removeEntry(name); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + dispose() { |
| 62 | + if (this._intervalID !== null) { |
| 63 | + window.clearInterval(this._intervalID); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private setupMemoryCleanup() { |
| 68 | + this._intervalID = window.setInterval( |
| 69 | + this.cleanMemory, |
| 70 | + this.memoryCleanTime, |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + private cleanMemory = async () => { |
| 75 | + if (this._isCleaningMemory) { |
| 76 | + return; |
| 77 | + } |
| 78 | + this._isCleaningMemory = true; |
| 79 | + |
| 80 | + const rootDir = await this.getDir(this.baseDirectory); |
| 81 | + |
| 82 | + const filesToDelete = new Set<string>(); |
| 83 | + |
| 84 | + const now = new Date().getTime(); |
| 85 | + |
| 86 | + // @ts-ignore |
| 87 | + for await (const entry of rootDir.values()) { |
| 88 | + const serializedLastAccessed = localStorage.getItem(entry.name) || "0"; |
| 89 | + const lastAccess = parseInt(serializedLastAccessed, 10); |
| 90 | + |
| 91 | + const deadTime = now - lastAccess; |
| 92 | + |
| 93 | + if (deadTime > this.maxDeadTime) { |
| 94 | + filesToDelete.add(entry.name); |
| 95 | + localStorage.removeItem(entry.name); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + for (const name of filesToDelete) { |
| 100 | + console.log(`deleted: ${name}`); |
| 101 | + rootDir.removeEntry(name); |
| 102 | + } |
| 103 | + |
| 104 | + this._isCleaningMemory = false; |
| 105 | + }; |
| 106 | + |
| 107 | + private async getDir(path: string) { |
| 108 | + const root = await navigator.storage.getDirectory(); |
| 109 | + return root.getDirectoryHandle(path, { |
| 110 | + create: true, |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + private encodeName(name: string) { |
| 115 | + const illegalChars = /[\\/:*?"<>|]/g; |
| 116 | + return name.replace(illegalChars, "ñ"); |
| 117 | + } |
| 118 | + |
| 119 | + private updateLastAccessTime(encodedName: string) { |
| 120 | + const now = new Date().getTime().toString(); |
| 121 | + localStorage.setItem(encodedName, now); |
| 122 | + } |
| 123 | +} |
0 commit comments