X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fassets%2Fplayer%2Fpeertube-chunk-store.ts;h=767e468219b1bf9221200af8984a62cce6c31168;hb=5363a766d95ed8654ad3e1b94538f085c2a3a101;hp=005e98a81203b36ea3113013fed54762c02600d0;hpb=efda99c30f2c04702bf57cc150cdfdd0acccc178;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/assets/player/peertube-chunk-store.ts b/client/src/assets/player/peertube-chunk-store.ts index 005e98a81..767e46821 100644 --- a/client/src/assets/player/peertube-chunk-store.ts +++ b/client/src/assets/player/peertube-chunk-store.ts @@ -118,7 +118,13 @@ export class PeertubeChunkStore extends EventEmitter { // IndexDB could be slow, use our memory index first const memoryChunk = this.memoryChunks[index] - if (memoryChunk === undefined) return cb(null, new Buffer(0)) + if (memoryChunk === undefined) { + const err = new Error('Chunk not found') + err['notFound'] = true + + return process.nextTick(() => cb(err)) + } + // Chunk in memory if (memoryChunk !== true) return cb(null, memoryChunk) @@ -155,17 +161,17 @@ export class PeertubeChunkStore extends EventEmitter { this.cleanerInterval = null } + if (this.db) { + await this.db.close() + + await this.dropDatabase(this.databaseName) + } + if (this.expirationDB) { await this.expirationDB.close() this.expirationDB = null } - if (this.db) { - console.log('Destroying IndexDB database %s.', this.databaseName) - await this.db.close() - await this.db.delete() - } - return cb() } catch (err) { console.error('Cannot destroy peertube chunk store.', err) @@ -181,9 +187,11 @@ export class PeertubeChunkStore extends EventEmitter { }, PeertubeChunkStore.CLEANER_INTERVAL_MS) } - private checkExpiration () { - this.expirationDB.transaction('rw', this.expirationDB.databases, async () => { - try { + private async checkExpiration () { + let databasesToDeleteInfo: { name: string }[] = [] + + try { + await this.expirationDB.transaction('rw', this.expirationDB.databases, async () => { // Update our database expiration since we are alive await this.expirationDB.databases.put({ name: this.databaseName, @@ -191,24 +199,30 @@ export class PeertubeChunkStore extends EventEmitter { }) const now = new Date().getTime() - const databasesToDeleteInfo = await this.expirationDB.databases.where('expiration').below(now).toArray() - - for (const databaseToDeleteInfo of databasesToDeleteInfo) { - await this.dropDatabase(databaseToDeleteInfo.name) + databasesToDeleteInfo = await this.expirationDB.databases.where('expiration').below(now).toArray() + }) + } catch (err) { + console.error('Cannot update expiration of fetch expired databases.', err) + } - await this.expirationDB.databases.where({ name: databaseToDeleteInfo.name }).delete() - } - } catch (err) { - console.error('Cannot check expiration.', err) - } - }) + for (const databaseToDeleteInfo of databasesToDeleteInfo) { + await this.dropDatabase(databaseToDeleteInfo.name) + } } - private dropDatabase (databaseName: string) { + private async dropDatabase (databaseName: string) { const dbToDelete = new ChunkDatabase(databaseName) + console.log('Destroying IndexDB database %s.', databaseName) + + try { + await dbToDelete.delete() - console.log('Deleting %s.', databaseName) - return dbToDelete.delete() + await this.expirationDB.transaction('rw', this.expirationDB.databases, () => { + return this.expirationDB.databases.where({ name: databaseName }).delete() + }) + } catch (err) { + console.error('Cannot delete %s.', databaseName, err) + } } private nextTick (cb, err, val?) {